//var baseurl = "http://220.225.200.139/jalenskyV2/code/";

/* function pageRedirection(param){
   //REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
  $.validationEngine.closePrompt('.formError',true);//to close the validation error messages while moving one page to other
  $.ajax({
            type: "GET",
            url: param.getAttribute("href1"),
            success: function(msg){	
                $("#include").html(msg);
                //REDIPS.dialog.hide('undefined');
                }
            });
 }*/

 function register(){
if (!($("form#registerForm").validationEngine({returnIsValid:true}))){
		    		///return;
}
else{
    REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
             type     : "POST",
             url      : "b_registerCust.php",
             data     : $("form#registerForm").serialize(),
             dataType : "json",
             success: function(data){           
                 if(data.status == "failure")
                   {
                       registerFn();
                   }

                 if (data.status == "success")
                  {                    
                        logReg(data.message,data.loginlogout);

                  }
                }
                    });
  }
           
}

function login(){    
if (!($("form#loginpage").validationEngine({returnIsValid:true}))){
		    		return;
		    		}

else {
   // REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
    $.ajax({
    	         type     : "POST",
    	         url      : "validateLogin.php",
                 cache    : false,
    	         data     : $("form#loginpage").serialize(),
                 dataType : "json",
    	         success: function(data){
                          
                           if(data.status == "failure")
                           {
                               loginFn();
                           }

                         if (data.status == "success")
                          {                                
                                logReg(data.message,data.loginlogout);

                          }
    		}
    	});
    
}

}


function logout(){
   // REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
    $.ajax({
    	         type     : "POST",
    	         url      : "b_logout.php",
                 cache    : false,   	        
                 dataType : "json",
    	         success: function(data){
                         if (data.status == "success")
                          {
                                logoutAccount(data.message,data.loginlogout);

                          }
    		}
    	});



}

function logoutAccount(message,loginlogout)
{
$.history.load("home.php/home");
$("#loginInfo").html(message);
 $("#header_id04").html(loginlogout);
    $.ajax({
    	         type     : "POST",
    	         url      : "home.php",
                 cache    : false,
    	         success: function(data){
                        REDIPS.dialog.hide('undefined');
                        $("#include").html(data);
    			 }
    	});
}
//page will redirect to the shopping cart page after login and register functionality with user details
function logReg(message,loginlogout)
{
 $.history.load("b_shoppingCart.php/home");
 $("#loginInfo").html(message);
 $("#header_id04").html(loginlogout);
    $.ajax({
    	         type     : "POST",
    	         url      : "b_shoppingCart.php",
                 cache    : false,
    	         success: function(data){
                        REDIPS.dialog.hide('undefined');
                        $("#include").html(data);
    			 }
    	});
}

function loginFn()
{
    $.ajax({
    	         type     : "POST",
    	         url      : "b_customerLogin.php",
                 cache    : false,
    	         success: function(data){
                        REDIPS.dialog.hide('undefined');
                        $("#include").html(data);
    			 }
    	});
}
function registerFn()
{
    $.ajax({
    	         type     : "POST",
    	         url      : "b_register.php",
                 cache    : false,
    	         success: function(data){
                        REDIPS.dialog.hide('undefined');
                        $("#include").html(data);
    			 }
    	});
}

function contactRegistration(){//to do the reigister functionality from contact info page

if (!($("form#registerForm").validationEngine({returnIsValid:true}))){
		    		return;
}
else{
    REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
                             type     : "POST",
                             url      : "b_addContactInfo.php",
                             cache    : false,
                             data     : $("form#registerForm").serialize(),
                             dataType : "json",
                             success: function(data){

                                 if(data.status == "failure")
                                   {
                                       callContactInfoPage();
                                   }

                                 if (data.status == "success")
                                  {
                                        callShippingWUserDetails(data.message,data.loginlogout);

                                  }
                                }
                    });
  }

}

function callShippingWUserDetails(message,loginlogout)//to call the shipping page with login details
{
 $("#loginInfo").html(message);
 $("#header_id04").html(loginlogout);
 callShippingPage();
}

function contactinfo(){

if (!($("form#contactInfo").validationEngine({returnIsValid:true}))){
    return;
}
else{
    REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
                 type     : "POST",
                 url      : "b_addContactInfo.php",
                 data     : $("form#contactInfo").serialize(),
                 dataType : "json",
                 success: function(data){
                   if (data.status == "success"){
                            callShippingPage();
                   }
                   else
                   {
                           callContactInfoPage();
                   }
                 }
        });
    }
  }
function callContactInfoPage()
{
  REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
  $.history.load("b_contactInfo.php/home");
  $.validationEngine.closePrompt('.formError',true);
     $.ajax({
                             type     : "POST",
                             url      : "b_contactInfo.php",
                             success: function(data){
                                REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                          }
                    });
}

function callCustomerHome()
{
   // REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
    $.history.load("b_customerHome.php/home");
    $.ajax({
                             type     : "POST",
                             url      : "b_customerHome.php",
                             success: function(data){
                                REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                          }
                    });
}
function callShoppingCartPage()//to call shoppingcart page
{
  REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
  $.history.load("b_shoppingCart.php/home");
  $.validationEngine.closePrompt('.formError',true);

   $.ajax({
                             type     : "POST",
                             url      : "b_shoppingCart.php",
                             success: function(data){
                                REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                          }
                    });
}

function callShippingPage(){
 REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
$.history.load("b_shippingInfo.php/home");
$.validationEngine.closePrompt('.formError',true);
     $.ajax({
                             type     : "POST",
                             url      : "b_shippingInfo.php",
                             success: function(data){
                               REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                          }
                    });
}



function shippinginfo(){
if (!($("form#frmShippingInfo").validationEngine({returnIsValid:true}))){
    return;
 }
 else{
    REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
             type     : "POST",
             url      : "b_addShippingInfo.php",
             data     : $("form#frmShippingInfo").serialize(),
             datatype : "php",
             success: function(data){
               if (data == "success"){                   
                            callBillingInfoPage();
                }
                    else
                {
                        callShoppingCartPage();
                 }
             }
        });
    }

}


function billinginfo(){

if (!($("form#frmBillingInfo").validationEngine({returnIsValid:true}))){
    return;
 }

 else{
     REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
             type     : "POST",
             url      : "b_processBillingInfo.php",
             data     : $("form#frmBillingInfo").serialize(),
             dataType : "php",
             success: function(data){
                
                  if(data == "success"){
                         
                         callPaymentConfirmationPage();
                }
                    else
                {
                    //alert(data);
                        REDIPS.dialog.hide('undefined');
                        $("#displayErrorMessages").html(data);
                       // callBillingInfoPage();

                        // NEED TO HANDLE ERROR
                        // IF THERE IS ERROR IN THE CREDIT CARD CARD PROCESSING
                 }
             }
        });
    }
 }


function callBillingInfoPage(){
 $.history.load("b_billingInfo.php/home");
$.validationEngine.closePrompt('.formError',true);
    $.ajax({
                 type     : "POST",
                 url      : "b_billingInfo.php",
                 data     : $("form#frmShippingInfo").serialize(),
                 datatype : "php",
                 success: function(data){
                   REDIPS.dialog.hide('undefined');
                   $("#include").html(data);
                 }
        });

    }

function callPaymentConfirmationPage(){
 $.history.load("b_paymentConfirmation.php/home");
 $("#amt").html('$0');
 $("#item").html('0 items');
    $.ajax({
                 type     : "POST",
                 url      : "b_paymentConfirmation.php",
                // data     : $("form#frmShippingInfo").serialize(),
                 dataType : "php",
                 success: function(data){
                   REDIPS.dialog.hide('undefined');
                   $("#include").html(data);
                 }
        });

    }
function copyDataFrmContact(){

   if (document.getElementById('subscChk').checked == true )
    {
        document.getElementById('ordDeliveryName').value        = document.getElementById('hdeCustomerName').value;
        document.getElementById('ordDeliveryAddrLine1').value   = document.getElementById('hdeCustomerAddrLine1').value;
        document.getElementById('ordDeliveryAddrLine2').value   = document.getElementById('hdeCustomerAddrLine2').value;
        document.getElementById('ordDeliveryCity').value        = document.getElementById('hdeCustomerCity').value;
        //document.getElementById('ordDeliveryState').value       = document.getElementById('hdeCustomerStateCode').value;
        document.getElementById('ordDeliveryPostCode').value    = document.getElementById('hdeCustomerPostCode').value;

// Select the option from the dropdown

        for ( var i = 0; i < document.getElementById('ordDeliveryState').options.length; i++ ) {
        if ( document.getElementById('ordDeliveryState').options[i].value == document.getElementById('hdeCustomerStateCode').value ) {
            document.getElementById('ordDeliveryState').options[i].selected = true;
        }
    }
    }
}



function copyContactInfo(){

            if ( document.getElementById('chkCopyShippingInfo') != null )
            {
                if( document.getElementById('chkCopyShippingInfo').checked )
                    document.getElementById('chkCopyShippingInfo').checked = false;
                
            }


            if ( document.getElementById('chkCopyContactInfo').checked == true )
            {

                populateFieldWithValue('contact_name','txtName');
                populateFieldWithValue('contact_addr1','txtAddr1');
                populateFieldWithValue('contact_addr2','txtAddr2');
                populateFieldWithValue('contact_city','txtCity');
                populateFieldWithValue('contact_state','txtState');
                populateFieldWithValue('contact_postal_code','txtZip');


            }

        }


function copyShippingInfo(){


     if ( document.getElementById('chkCopyContactInfo') != null )
            {
                if( document.getElementById('chkCopyContactInfo').checked )
                    document.getElementById('chkCopyContactInfo').checked = false;
            }

            if ( document.getElementById('chkCopyShippingInfo').checked == true )
            {
                document.getElementById('txtName').value        = document.getElementById('shipping_name').value;
                document.getElementById('txtAddr1').value       = document.getElementById('shipping_addr1').value;
                document.getElementById('txtAddr2').value       = document.getElementById('shipping_addr2').value;
                document.getElementById('txtCity').value        = document.getElementById('shipping_city').value;
                document.getElementById('txtState').value       = document.getElementById('shipping_state').value;
                document.getElementById('txtZip').value         = document.getElementById('shipping_postal_code').value;
            }

}

function validateAddToCart(id,productListingNumber){
var formName="form#"+id;
if (!($(formName).validationEngine({returnIsValid:true}))){
    return;
 }
else{
    REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
     document.forms['m_submit'].productID.value         = id;
     document.forms['m_submit'].salePrice.value         = document.getElementById('salePrice_'+productListingNumber).value;
     document.forms['m_submit'].retailPrice.value       = document.getElementById('retailPrice_'+productListingNumber).value;
     document.forms['m_submit'].productWeight.value     = document.getElementById('productWeight_'+productListingNumber).value;
     document.forms['m_submit'].taxClassID.value        = document.getElementById('taxClassID_'+productListingNumber).value;
     document.forms['m_submit'].qty.value               = document.getElementById('qty_'+productListingNumber).value;
        
       $.ajax({
                             type     : "POST",
                             url      : "b_addToCart.php",
                             cache    : false,
                             data     : $("form#m_submit").serialize(),
                            dataType : "json",
                             success: function(data){ 
                                 addToCartRedirection(data.cartTotalAmount,data.cartItemQty);
                             }
                    });

 }

}

function addToCartRedirection(cartTotalAmount,cartItemQty){

 $.history.load("b_shoppingCart.php/home");
 $.validationEngine.closePrompt(".formError",true);
 $("#amt").html(cartTotalAmount);
 $("#item").html(cartItemQty);
     $.ajax({
    	         type     : "POST",
    	         url      : "b_shoppingCart.php",             
                 dataType : "php",
    	         success: function(data){
                          REDIPS.dialog.hide('undefined');
                          $("#include").html(data);
    			 }
    	});
}


function m_removeProduct(productId,productListingNumber){//remove product from shopping cart page
     REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
     $.validationEngine.closePrompt(".formError",true);
     document.forms['m_submit'].qty.value               = document.getElementById('txtQty_'+productListingNumber).value;
     document.forms['m_submit'].salePrice.value         = document.getElementById('finalPrice_'+productListingNumber).value;
     document.forms['m_submit'].productID.value         = productId;
    
      $.ajax({
                             type     : "POST",
                             url      : "removeItemFromCart.php",
                             cache    : false,
                             data     : $("form#m_submit").serialize(),
                             dataType : "json",
                             success: function(data){                             
                                removeCartRedirection(data.cartTotalAmount,data.cartItemQty,data.checkout);
                             }
                    });
}
function removeCartRedirection(cartTotalAmount,cartItemQty,checkout){//to show the header shopping informations after removing the product form add to cart
    $("#amt").html(cartTotalAmount);
    $("#item").html(cartItemQty);
    $("#checkout").html(checkout);
    $.ajax({
    	         type     : "POST",
    	         url      : "b_shoppingCart.php",
                 //cache    : false,
                 dataType : "php",
    	         success: function(data){
                          REDIPS.dialog.hide('undefined');
                          $("#include").html(data);
    			 }
    	});
}
function couponCode(){/*update coupon code*/
    if (!($("form#processCoupon").validationEngine({returnIsValid:true}))){
    return;
    }
    else{
      REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
        $.ajax({
                             type     : "POST",
                             url      : "b_processCoupon.php",
                             cache    : false,
                             data     : $("form#processCoupon").serialize(),
                             dataType : "json",
                             success: function(data){
                             if(data.status == "success"){
                                     couponCodeUpdate(data.cartTotalAmount);
                            }
                                else
                            {
                                    couponCodeUpdateFailure();
                             }
                         }
                    });
    }
}
function couponCodeUpdate(cartTotalAmount){
    $("#amt").html(cartTotalAmount);
    $.ajax({
    	         type     : "POST",
    	         url      : "b_shoppingCart.php",
                 //cache    : false,
                 dataType : "php",
    	         success: function(data){
                          REDIPS.dialog.hide('undefined');
                          $("#include").html(data);
    			 }
    	});
}
function couponCodeUpdateFailure(){
    $.ajax({
    	         type     : "POST",
    	         url      : "b_shoppingCart.php",
                 //cache    : false,
                 dataType : "php",
    	         success: function(data){
                            REDIPS.dialog.hide('undefined');
                          $("#include").html(data);
    			 }
    	});
}
 // Call the ajax function  for change Password in MyAccount
function passwordvalidate(){

 if (!($("form#passwordForm").validationEngine({returnIsValid:true}))){
		    		return;
}
else{
   // REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
                             type     : "POST",
                             url      : "b_changePassword.php",
                             cache    : false,
                             data     : $("form#passwordForm").serialize(),
                             dataType : "php",
                             success: function(data){
                               REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                             }
                    });
  }
 }

// Call the ajax function  for editProfile(Save) in MyAccount
function savecustomerinfo(){

 if (!($("form#contactForm").validationEngine({returnIsValid:true}))){
    return;
}
else{
    //REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
 $.ajax({
                             type     : "POST",
                             url      : "b_changeContactInfo.php",
                             cache    : false,
                             data     : $("form#contactForm").serialize(),
                             dataType : "php",
                             success: function(data){
                                 REDIPS.dialog.hide('undefined');
                               $("#include").html(data);
                             }
                    });
  }
 }


 function passwordsubmit(){
    
    if (!($("form#forgotPassword").validationEngine({returnIsValid:true}))){
        return;
    }
   else{
        $.ajax({
                             type     : "POST",
                             url      : "b_processForgotPassword.php",
                             cache    : false,
                             data     : $("form#forgotPassword").serialize(),
                             dataType : "php",
                             success: function(data){
                                if(data == "success"){
                                     disablePopup();
				}
                                else
				{
                                    $("#errorDiv").html(data);
				}
			}
                    });
    }

 }

//Search Functionality
 function searchByValue(searchFlg)
{
        REDIPS.dialog.init();
        REDIPS.dialog.op_high = 60;
        REDIPS.dialog.fade_speed = 18;
        REDIPS.dialog.show(10, 10, 'ajax-loader.gif');
       $.history.load("b_listProduct/"+searchFlg+"/"+document.getElementById('searchParam').value+"/home");
   // location.href= document.getElementsByTagName('base')[0].href + 'b_listProduct/'+searchFlg+'/'+document.getElementById('searchParam').value;
   $.ajax({
                type: "GET",
                url: 'b_listProduct/'+searchFlg+'/'+document.getElementById('searchParam').value,
                success: function(msg){
                        REDIPS.dialog.hide('undefined');
                        $("#include").html(msg);
                    }
                });
}

//billing Info
function populateFieldWithValue(sourceField,destField)
{
    document.getElementById(destField).value=document.getElementById(sourceField).value;
}

//SETTING UP POPUP FOR FORGOT PASSWORD
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
    //loads popup only if it is disabled
    if(popupStatus==0){
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup(){
    $.history.load("b_customerLogin.php/home");
    $.validationEngine.closePrompt('.formError',true);//to close the validation error messages while moving one page to other
    //disables popup only if it is enabled
    if(popupStatus==1){
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popcenterPopup()up
function ShowPopup(){
    
    $.validationEngine.closePrompt(".formError",true);
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    $("#errorDiv").html("");
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": "200px",
        "left": windowWidth/2-popupWidth/2
    });
    //only need force for IE6
    $("#backgroundPopup").css({
        "height": windowHeight
    });
        loadPopup();
}

    //$(document).keypress(function(e){
        //if(e.keyCode==27 && popupStatus==1){
            //disablePopup();
        //}
    //});
