jQuery(document).ready(function(){
    jQuery.validator.addMethod("pricerange", function (value, element, param) {
           //var min_value = jQuery('#min_price').val();
           if (value >= jQuery(param).val()) return !this.optional(element);
            },
            "Please enter max price more than min price"
);
    jQuery.validator.addMethod("qtynotzero", function (value, element) {
           //var min_value = jQuery('#min_price').val();
           if (value != 0) return !this.optional(element);
            },
            "Invalid quantity"
);
	
   
    }); 

function selectChanged(){
    jQuery('#search_error_span').html('');

    if(jQuery('#search_in').val()=='price'){
        jQuery('#priceSpan').show();
        jQuery('#keyword').hide();
        //jQuery('#max_price').focus();
        //jQuery('#min_price').focus();

        
    }
    else{
         //jQuery('#min_price').val()='';
         //jQuery('#max_price').val()='';
         //alert('here');
         jQuery('#priceSpan').hide();
         jQuery('#keyword').show();
    }
}



function checkSearchStore() {
    
    
    jQuery('#search_error_span').html('');

    
 var storeform =  jQuery("#search_store").validate({
          rules: {
              price_range :{
                  required:function(element) {
                    return jQuery("#search_in").val() =='price';}

              }

//             min_price: {
//             number: true
//            },
//            max_price: {
//             number: true,
//             pricerange:'#min_price'
//            }
          },
          messages:
                        {

                          price_range :{required:"Please select a price range"}
//                         min_price:{
//                               number:"Please enter a valid min price."
//                                },
//                         max_price:{
//                               number:"Please enter a valid max price."
//
//                                }
                        },
         errorPlacement:function(error, element) {
             error.appendTo('#search_error_span');
//                    switch(element.attr("name"))
//                        {
//                             case "min_price":
//                             case "max_price":
//
//                             break;
//                        }
           },
           submitHandler:function() {
               jQuery('#loading_store_search').show();
               jQuery('#search_btn').hide();
               switch (jQuery('#search_in').val()){
                    case 'price':
                       // var search_keyword = jQuery('#min_price').val()+'|'+jQuery('#max_price').val();
                       var search_keyword = jQuery('#price_range').val();
                    break;
                    default:
                        var search_keyword = encodeURIComponent(jQuery('#keyword').val());
                    break;
                }
               if(SERVER_MODE=='SERVER'){
                  //location.replace(baseUrlForJs+'/store/search/'+search_keyword);
	       }
	     else{
		//location.replace(baseUrlForJs+'/store/store/index/keywords/'+search_keyword+'/search_in/'+jQuery('#search_in').val());
		jQuery('#keywords').val(search_keyword);
		
		location.replace(baseUrlForJs+'/store/?keywords='+search_keyword+'&search_in='+jQuery('#search_in').val()+'&searched=true');
		//document.forms[0].submit();
	        }
           },
            
            debug: true

        });

}

function selectKeywordsText()
{
    jQuery('#keyword').select();
}

function checkInventory(){
   //alert('here');
   //console.log(jQuery('.ajax_loader'));
   jQuery('#add_cart').hide();
   jQuery('#continue_shopping').hide();
   jQuery('#proceed_carts').hide();
   jQuery('#cart_success').hide();
   
 
    jQuery('#store_error_span').html('');

    var queryString = jQuery('#attribute_set').formSerialize();
    jQuery.ajax({
                url :  baseUrlForJs+'/store/store/checkinventory',
                type : 'POST',
                data :  queryString,
                beforeSend: function(XMLHttpRequest) {
                    jQuery('.ajax_loader').show();
                    //jQuery('#check_inventory').hide();

                },
                complete: function (XMLHttpRequest) {
                    jQuery('.ajax_loader').hide();
                    //jQuery('#check_inventory').show();
                },
                success : function (msg) {
					jQuery('.ajax_loader').hide();
                    //jQuery('#check_inventory').show();
                    var chunks = msg.split('|');
                    //alert(chunks[1]);
                    if(chunks[1] == 'Success'){
                        //console.log(chunks);
                        jQuery('#products_quantity').show();
                        jQuery('#add_cart').show();
                       
                        
                        if(parseFloat(chunks[3]) > 0) {
							//alert(parseFloat(chunks[3]));
                            jQuery('#products_price').html('<sup>$</sup><strike>'+chunks[2]+'</strike>');
							jQuery('#products_price').addClass('strike');
                            //var str = '&nbsp;<span id="products_discount_price">$'+chunks[3]+'</span>';
                            //jQuery('#products_price').after(str);
                            jQuery('#products_discount_price').html('<sup>$</sup>'+chunks[3]);
                            jQuery('#products_discount_price').show();
                        }
                        else {
                            jQuery('#products_price').html('<sup>$</sup>'+chunks[2]);
                            jQuery('#products_discount_price').html('');
                            jQuery('#products_discount_price').hide();
							jQuery('#products_price').removeClass('strike');
                        }
                        
                        jQuery('#products_inventory_id').val(chunks[4]);
                        jQuery('#inventory_price_div').show('slow');
						//jQuery('#cart_success').show();
                    }
                    else{
                        jQuery('#products_price').html('$0.00');
                        jQuery('#products_discount_price').html('$0.00');
                        jQuery('#products_inventory_id').val('');
                        jQuery('#inventory_price_div').hide('slow');
                        jQuery('#products_quantity').hide();
                        jQuery('#add_cart').hide();
                        jQuery('#store_error_span').html('<div class="error">Your desired combination is not available</div>');
                        //jQuery('#check_inventory').show();
                        //jQuery('#add_cart').hide();
                    }
               }

    });
        
   
}

function updateSelectedAttr(val,key){
    jQuery('#selected_attr_'+key).val(key+':'+val+'##');
    jQuery('#add_cart').hide();
    jQuery('#continue_shopping').hide();
    jQuery('#proceed_carts').hide();
    //jQuery('#check_inventory').show();
    //jQuery('#loader').show();
    checkInventory();
}


function addToCart(){
    jQuery('#store_error_span').html('');
    jQuery('#continue_shopping').hide();
    jQuery('#proceed_carts').hide();
    jQuery('#add_cart').hide();
    jQuery('#cart_success').hide();
	jQuery('#preorder_tip').hide();
//    var selectedAttr = '';
//    jQuery('.attr').each(function () {
//        //console.log(selectedAttr);
//        selectedAttr += jQuery(this).val()+'';
//    });
//    jQuery('#attribute_selected').val(selectedAttr);


    var queryString = 'qty='+jQuery('#qty').val()
                        +'&products_inventory_id='+jQuery('#products_inventory_id').val()
                        +'&products_id='+jQuery('#products_id').val();
    //console.log(queryString);
            jQuery.ajax({
                        beforeSend: function(XMLHttpRequest) {
                        jQuery('.ajax_loader').show();
                        },
                        complete: function (XMLHttpRequest) {
                        jQuery('.ajax_loader').hide();
                        },
                        url :  baseUrlForJs+'/store/store/addtocart',
                        type : 'POST',
                        data :  queryString,
                        success : function (msg) {
                            var chunks = msg.split('|');
                            //alert(chunks[1]);
                            jQuery('#add_cart').show();
                            if(chunks[1] == 'Success'){
                                jQuery('#continue_shopping').show();
                                
                                jQuery('#proceed_carts').val('PROCEED TO '+chunks[2]);
                                jQuery('#proceed_carts').show();
				jQuery('#cart_success').show();
                                //jQuery('#header_div').load(baseUrlForJs+'/store/store/returnfile');
                                showReviewLink(chunks[3]); 
                            }
                            else{
                                jQuery('#store_error_span').html('<div class="error">'+chunks[2]+'</div>');

                            }
                       }

            });
            
}
function continueShopping(){
    if(SERVER_MODE=='SERVER'){

    }
    else{
        location.replace(baseUrlForJs+'/store/store');
    }
}

function proceedCart(){
    if(SERVER_MODE=='SERVER'){

    }
    else{
        location.replace(baseUrlForJs+'/store/store/review');
    }
}
function proceedToCheckOut(){
     location.replace(baseUrlForJsHttps+'/store/checkout');
}


function getChechbox(val){
    if(val){
		//console.log(val);
	/*jQuery('#ship_submit_btn').hide();
    jQuery('#ship_btn').show();
	jQuery('#ship_submit_btn').hide();
	jQuery('#same_as_bill').val('true');
	jQuery('#ship_address_type').val('old');
	jQuery('#ship_form').hide();*/
	jQuery("#shipping_detail").load(baseUrlForJs+'/store/store/sameasshipping');
	jQuery('#ship_form').show();
	jQuery('#ship_submit_btn').show();
    jQuery('#ship_btn').hide();
	//showCheckout('step3');

	jQuery('#ship_address_type').val('old');
    jQuery('#checkout_3').removeClass('disabled');
    jQuery('#checkout_3').addClass('active');
	
    }
    else{
		jQuery("#shipping_detail").load(baseUrlForJs+'/store/store/differentasshipping');
	//var shipvalidator = jQuery("#checkout_ship_form").validate();
	//jQuery("#checkout_ship_form").resetForm();	
	jQuery('#same_as_bill').val('false');
	jQuery('#ship_address_type').val('new');
	jQuery('#ship_form').show();
	jQuery('#ship_submit_btn').show();
    jQuery('#ship_btn').hide();
    }
}
function shipAddress(){
    var shipOption = jQuery('#ship_address').val();
    jQuery('#ship_as_bill').removeAttr('checked');
    if(shipOption == 'new'){
            jQuery('#ship_form').show();
	    jQuery('#ship_submit_btn').show();
            jQuery('#ship_btn').hide();
            jQuery('#ship_address_type').val('new');
         }
    else{
        jQuery('#ship_form').hide();
	jQuery('#ship_submit_btn').hide();
        jQuery('#ship_btn').show();
        jQuery('#ship_address_type').val('old');
    }
}	

function uncheckBill(){
	 jQuery('#ship_as_bill').removeAttr('checked');
	}

function validateshipform(){
    var shipOption = jQuery('#ship_address_type').val();
	jQuery('#shipping_error_span').html('');
	jQuery('#shipping_error_span').hide();
    if(shipOption == 'new'){
        //alert(billOption);
        var billform =  jQuery("#checkout_ship_form").validate({
            rules: {
              ship_first_name:{
                                required:true,
                                special:true,
                                minlength:3
                                },
             ship_last_name:{
                                required:true,
                                special:true,
                                minlength:3
                                },
             ship_add_1:{
                                required:true

                                },
             /*ship_add_2:{
                                required:true
                                },*/

             

            ship_phone :  {required:true,phone: true},
            ship_city :{ required:true,special : true},
            ship_country:{required:true},
            ship_nonus_state:{ required:function(element) {
                    return jQuery('#ship_country').val() != 'US' ;},
                    special : true},
            ship_us_state:{ required:function(element) {
                    return jQuery('#ship_country').val() == 'US' ;}
                    },
            ship_postalcode:{required:true,postal:true}

        },

        messages:
                  {
                    ship_first_name :{
                        required:"Fill your first name",
                        special:"First name contains iligal character",
                        minlength:jQuery.format("Enter at least {0} characters")
                    },
                    ship_last_name :{
                        required:"Fill your last name",
                        special:"Last name contains iligal character",
                        minlength:jQuery.format("Enter at least {0} characters")
                    },
                   ship_add_1 :{
                         required:"Fill your address line 1"},
                   /*ship_add_2 :{
                        required:"Fill your address line 2"},*/
                  
                  ship_phone :{required:"Fill your Phone No"},
                  ship_city :{required:"Fill your City",special : "City contains iligal character"},

                  ship_country:{required:"Select your Country"},
                  ship_us_state:{required:"Select your State"},
                  ship_nonus_state:{required:"Fill your State",special : "State contains iligal character"},
                  ship_postalcode :{required:"Fill your Zip Code"}
                 },

           errorPlacement:function(error, element) {
              error.appendTo('#'+element.attr("name")+'_error');

             },
           submitHandler:function() {
               var country = jQuery('#ship_country').val();
               if(country == 'US'){
                   var state = jQuery('#ship_us_state').val();
               }
               else{
                   var state = jQuery('#ship_nonus_state').val();
               }

         var queryString = jQuery('#checkout_ship_form').formSerialize()+'&ship_state='+state;
                        jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/shipdetail',
                                    type : 'POST',
                                    data :  queryString,
                                    beforeSend: function(XMLHttpRequest) {
                                         jQuery('#ship_submit_btn').hide();
                                         jQuery('#loading_ship').show();
                                   },
                                   complete: function (XMLHttpRequest) {
                                         jQuery('#ship_submit_btn').show();
                                         jQuery('#loading_ship').hide();
                                  },
                                    success : function (msg) {
                                        var chunks = msg.split('|');
                                        //alert(chunks[1]);
                                        if(chunks[1] == 'Success'){
                                            showCheckout('step4');
                                            jQuery('#step3').attr('onclick', 'javascript:showCheckout(this.id)');
                                            jQuery('#checkout_3').removeClass('active');
                                            jQuery('#checkout_3').addClass('processed');
                                            jQuery('#checkout_4').removeClass('disabled');
                                            jQuery('#checkout_4').addClass('active');
                                            
                                            jQuery("#shipping_method").load(baseUrlForJs+'/store/store/shippingmethod');
                                        }
                                        else{
                                            jQuery('#shipping_error_span').html(chunks[2]);
											jQuery('#shipping_error_span').show();
                                        }
                                        jQuery('#loading_progress').show();
                                        jQuery("#checkout_progress").load(baseUrlForJs+'/store/store/checkoutprogress');
                                   }

                        });
            },

            debug: true

        });

    }
    else if(jQuery('#same_as_bill').val() == 'true'){
	var queryString = 'same_as_bill='+jQuery('#same_as_bill').val();
       //alert(queryString);
       jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/shipdetail',
                                    type : 'POST',
                                    data : queryString,
                                    beforeSend: function(XMLHttpRequest) {
                                          jQuery('#ship_btn').hide();
                                         jQuery('#loading_ship').show();
                                    },
                                    complete: function (XMLHttpRequest) {
                                         jQuery('#ship_btn').show();
                                         jQuery('#loading_ship').hide();
                                   },
                                    success : function (msg) {
                                        var chunks = msg.split('|');

                                        if(chunks[1] == 'Success'){
                                           showCheckout('step4');
                                           jQuery('#step3').attr('onclick', 'javascript:showCheckout(this.id)');
                                            jQuery('#checkout_3').removeClass('active');
                                            jQuery('#checkout_3').addClass('processed');
                                            jQuery('#checkout_4').removeClass('disabled');
                                            jQuery('#checkout_4').addClass('active');
                                           
                                           jQuery("#shipping_method").load(baseUrlForJs+'/store/store/shippingmethod');
                                        }
                                        else{
                                             jQuery('#shipping_error_span').html(chunks[2]);
											 jQuery('#shipping_error_span').show();
                                        }
                                        jQuery('#loading_progress').show();
                                        jQuery("#checkout_progress").load(baseUrlForJs+'/store/store/checkoutprogress');
                                   }
//
                       });
    }
    else{


       var queryString = 'ship_address='+jQuery('#ship_address').val();
       //alert(queryString);
       jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/shipdetail',
                                    type : 'POST',
                                    data : queryString,
                                    beforeSend: function(XMLHttpRequest) {
                                          jQuery('#ship_btn').hide();
                                         jQuery('#loading_ship').show();
                                    },
                                    complete: function (XMLHttpRequest) {
                                         jQuery('#ship_btn').show();
                                         jQuery('#loading_ship').hide();
                                   },
                                    success : function (msg) {
                                        var chunks = msg.split('|');

                                        if(chunks[1] == 'Success'){
                                           showCheckout('step4');
                                           jQuery('#step3').attr('onclick', 'javascript:showCheckout(this.id)');
                                            jQuery('#checkout_3').removeClass('active');
                                            jQuery('#checkout_3').addClass('processed');
                                            jQuery('#checkout_4').removeClass('disabled');
                                            jQuery('#checkout_4').addClass('active');
                                           
                                           jQuery("#shipping_method").load(baseUrlForJs+'/store/store/shippingmethod');
                                        }
                                        else{
											 //alert(chunks[2]);
                                             jQuery('#shipping_error_span').html(chunks[2]);
											 jQuery('#shipping_error_span').show();
                                        }
                                        jQuery('#loading_progress').show();
                                        jQuery("#checkout_progress").load(baseUrlForJs+'/store/store/checkoutprogress');
                                   }
//
                       });
    }
}

function showCheckout(stepId){
	
    switch (stepId){
         case 'step1':
           jQuery('#checkout_1').removeClass();
           jQuery('#checkout_1').addClass('main_box active');
           jQuery('#checkout_step2').hide();
           jQuery('#checkout_step1').show();
           jQuery('#checkout_2').removeClass();
           jQuery('#checkout_2').addClass('main_box disabled');
           jQuery('#checkout_step2').hide();
           jQuery('#step2').attr('onclick', '');
           jQuery('#checkout_3').removeClass();
           jQuery('#checkout_3').addClass('main_box disabled');
           jQuery('#checkout_step3').hide();
           jQuery('#step3').attr('onclick', '');
           jQuery('#checkout_4').removeClass();
           jQuery('#checkout_4').addClass('main_box disabled');
           jQuery('#checkout_step4').hide();
           jQuery('#step4').attr('onclick', '');
           jQuery('#checkout_5').removeClass();
           jQuery('#checkout_5').addClass('main_box disabled');
           jQuery('#checkout_step5').hide();
           jQuery('#step5').attr('onclick', '');
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box disabled');
           jQuery('#checkout_step6').hide();
           jQuery('#step6').attr('onclick', '');
        break;
        case 'step2':
            jQuery('#checkout_2').removeClass();
           jQuery('#checkout_2').addClass('main_box active');
           jQuery('#checkout_step2').show();
           jQuery('#checkout_step1').hide();
           jQuery('#checkout_3').removeClass();
           jQuery('#checkout_3').addClass('main_box disabled');
           jQuery('#checkout_step3').hide();
           jQuery('#step3').attr('onclick', '');
           jQuery('#checkout_4').removeClass();
           jQuery('#checkout_4').addClass('main_box disabled');
           jQuery('#checkout_step4').hide();
           jQuery('#step4').attr('onclick', '');
           jQuery('#checkout_5').removeClass();
           jQuery('#checkout_5').addClass('main_box disabled');
           jQuery('#checkout_step5').hide();
           jQuery('#step5').attr('onclick', '');
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box disabled');
           jQuery('#checkout_step6').hide();
           jQuery('#step6').attr('onclick', '');
        break;
        case 'step3':
		
		
           jQuery('#checkout_3').removeClass();
           jQuery('#checkout_3').addClass('main_box active');
           jQuery('#checkout_step3').show();
           jQuery('#checkout_step1').hide();
           jQuery('#checkout_step2').hide();
           jQuery('#checkout_4').removeClass();
           jQuery('#checkout_4').addClass('main_box disabled');
           jQuery('#checkout_step4').hide();
           jQuery('#step4').attr('onclick', '');
           jQuery('#checkout_5').removeClass();
           jQuery('#checkout_5').addClass('main_box disabled');
           jQuery('#checkout_step5').hide();
           jQuery('#step5').attr('onclick', '');
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box disabled');
           jQuery('#checkout_step6').hide();
           jQuery('#step6').attr('onclick', '');
        break;

        case 'step4':
           jQuery('#checkout_4').removeClass();
           jQuery('#checkout_4').addClass('main_box active');
           jQuery('#checkout_step4').show();
           jQuery('#checkout_step1').hide();
           jQuery('#checkout_step2').hide();
           jQuery('#checkout_step3').hide();
           jQuery('#checkout_5').removeClass();
           jQuery('#checkout_5').addClass('main_box disabled');
           jQuery('#checkout_step5').hide();
           jQuery('#step5').attr('onclick', '');
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box disabled');
           jQuery('#checkout_step6').hide();
           jQuery('#step6').attr('onclick', '');
        break;

        case 'step5':
           jQuery('#checkout_5').removeClass();
           jQuery('#checkout_5').addClass('main_box active');
           jQuery('#checkout_step5').show();
           jQuery('#checkout_step1').hide();
           jQuery('#checkout_step2').hide();
           jQuery('#checkout_step3').hide();
           jQuery('#checkout_step4').hide();
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box disabled');
           jQuery('#checkout_step6').hide();
           jQuery('#step6').attr('onclick', '');
        break;

        case 'step6':
           jQuery('#checkout_6').removeClass();
           jQuery('#checkout_6').addClass('main_box active');
           jQuery('#checkout_step6').show();
           jQuery('#checkout_step1').hide();
           jQuery('#checkout_step2').hide();
           jQuery('#checkout_step3').hide();
           jQuery('#checkout_step4').hide();
           jQuery('#checkout_step5').hide();
           
        break;
        
    }

}


function setshippingservice(){
     var queryString = jQuery('#shipping_method_form').formSerialize()+'&grand_total='+jQuery('#shipping_result_total').html();
       //alert(queryString);
       jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/shippingservice',
                                    type : 'POST',
                                    data : queryString,
                                    beforeSend: function(XMLHttpRequest) {
                                          jQuery('#ship_method_btn').hide();
                                         jQuery('#loading_ship_method').show();
                                    },
                                    complete: function (XMLHttpRequest) {
                                         jQuery('#ship_method_btn').show();
                                         jQuery('#loading_ship_method').hide();
                                   },
                                    success : function (msg) {
                                        var chunks = msg.split('|');

                                        if(chunks[1] == 'Success'){
                                           showCheckout('step5');
                                           jQuery('#step5').attr('onclick', 'javascript:showCheckout(this.id)');
                                            jQuery('#checkout_4').removeClass('active');
                                            jQuery('#checkout_4').addClass('processed');
                                            jQuery('#checkout_5').removeClass('disabled');
                                            jQuery('#checkout_5').addClass('active');
                                           
                                           //jQuery("#shipping_method").load(baseUrlForJs+'/store/store/shippingmethod');
                                        }
                                        else{
                                             //alert(chunks[1]);
                                        }
                                        jQuery('#loading_progress').show();
                                           jQuery("#checkout_progress").load(baseUrlForJs+'/store/store/checkoutprogress');
                                   }
//
                       });
}




function finalcheckout(){

	//by vibhor - at this time, we will post the cc details part
	var payOption = jQuery('#paytype').val();
	var orderType = jQuery('#order_type').val();

    if(payOption == 'NEW' || orderType =='ORDER' ){
		var queryString = jQuery('#payment_form').formSerialize();
		//console.log(queryString);
	}
	else {
		var queryString = 'cim_profile_id='+jQuery('#order_cim_profile_id').val()+'&cc_no_id='+jQuery('#cc_no_id').val();
	}
	
     jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/placeorder',
                                    type : 'POST',
									data : queryString,
                                    beforeSend: function(XMLHttpRequest) {
                                          jQuery('#final_checkout').hide();
                                         jQuery('#loading_checkout').show();
										 jQuery('#final_checkout_error').hide();
                                    },
                                    /*complete: function (XMLHttpRequest) {
                                         jQuery('#final_checkout').show();
                                         jQuery('#loading_checkout').hide();
                                   },*/
                                    success : function (msg) {
                                        var chunks = msg.split('|');
										//jQuery('#final_checkout_error').html(msg); //comment when live
										//jQuery('#final_checkout_error').show();//comment when live
										
                                        if(chunks[1] == 'Success'){
										   jQuery('#loading_checkout').html('Order Successful, Redirecting Now...');
                                           location.replace(baseUrlForJs+'/store/store/orders');
										   
                                        }
                                        else{
                                             //alert(chunks);
											 jQuery('#final_checkout').show();
											 jQuery('#loading_checkout').hide();
											 jQuery('#final_checkout_error').html(chunks[3]);
											 /*if(chunks[2] == 'TESTING') {
											 	jQuery('#final_checkout_error').append(chunks[3]);
											 }*/
											 jQuery('#final_checkout_error').show();
											 
											 //by vibhor - if out of stock scene, redirect to review page
											 if(chunks[4] == 'final') {
												var url  = baseUrlForJs+'/store/store/review';
												setTimeout("location.replace('"+url+"')",2000); 
											 	//location.replace(baseUrlForJs+'/store/store/review');
											 }
											 
                                        }
                                   }
//
                       });
}


function payDisplay(val,text)
{
    if(val == "NEW"){
        jQuery('#payment_div').show();
        jQuery('#order_cim_profile_id').val('');
        jQuery('#cc_no_id').val('');
        jQuery('#payment_submit').show();
        jQuery('#payment_btn').hide();
    }
    else {
        jQuery('#payment_div').hide();
        jQuery('#order_cim_profile_id').val(val);
        jQuery('#cc_no_id').val(text);
        jQuery('#payment_submit').hide();
        jQuery('#payment_btn').show();
    }
    //console.log(jQuery('#order_cim_profile_id').val());
}

function checkCoupon(){
    jQuery('#coupon_result_error').html('');
    jQuery('#coupon_result').html('');
    
    


    var queryString = 'code='+jQuery('#coupon_code').val();
    jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/checkcoupon',
                                    type : 'POST',
                                    data : queryString,

                                    beforeSend: function(XMLHttpRequest) {
                                         jQuery('#check_coupon').hide();
                                         jQuery('#update_btn').hide();
                                         jQuery('#proceed_btn').hide();
                                         jQuery('#coupon_loader').show();
                                    },
                                    complete: function (XMLHttpRequest) {
                                         jQuery('#check_coupon').show();
                                         jQuery('#update_btn').show();
                                         jQuery('#proceed_btn').show();
                                         jQuery('#coupon_loader').hide();
                                   },
                                    success : function (msg) {
                                        var chunks = msg.split('|');
                                        
                                        if(chunks[1] == 'Success'){
                                            jQuery('#check_coupon').hide();
                                            
                                           /*jQuery('#cart_subtotal_td').html(chunks[2]); //insert subtotal value
                                           jQuery('#msg_after_discount').show('slow');
                                           jQuery('#cart_subtotal_td').show('slow');*/
					   window.location.replace(baseUrlForJs+'/store/store/review');
                                        }
                                        else{
                                             jQuery('#coupon_result_error').html('<div class="error">'+chunks[2]+'</div>');
                                        }
                                   }
//
                       });
}

function accetpCoupon(){

var queryString = 'coupon_amt='+jQuery('#coupon_amt').val()+'&coupons_members='+jQuery('#coupons_members').val();
    jQuery.ajax({
                                    url :  baseUrlForJs+'/store/store/acceptcoupon',
                                    type : 'POST',
                                    data : queryString,

                                    beforeSend: function(XMLHttpRequest) {
                                         jQuery('#check_coupon').hide();
                                         jQuery('#update_btn').hide();
                                         jQuery('#proceed_btn').hide();
                                         jQuery('#coupon_accept').hide();
                                         jQuery('#coupon_loader').show();
                                    },
                                    complete: function (XMLHttpRequest) {
                                         jQuery('#coupons').hide();
                                         jQuery('#update_btn').show();
                                         jQuery('#proceed_btn').show();
                                         jQuery('#coupon_loader').hide();
                                   },
                                    success : function (msg) {
                                        var chunks = msg.split('|');

                                        if(chunks[1] == 'Success'){
                                           jQuery('#coupon_used').show();
					   
                                        }
                                        else{
                                             //alert(chunks[1]);
                                        }
                                   }
//
                       });
}

function redeemAmt(){
 var queryString = 'to_deduct='+jQuery('#to_deduct').val()+'&redeem_subtotal='+jQuery('#redeem_subtotal').val()+'&available_money='+jQuery('#available_money').val();
jQuery.ajax({
                url :  baseUrlForJs+'/store/store/redeem',
                type : 'POST',
                data : queryString,

                beforeSend: function(XMLHttpRequest) {
                     //jQuery('#check_coupon').hide();
                     //jQuery('#update_btn').hide();
                     //jQuery('#proceed_btn').hide();
                     //jQuery('#coupon_accept').hide();
                     //jQuery('#coupon_loader').show();
                },
                complete: function (XMLHttpRequest) {
                     //jQuery('#points_redeem').hide();
                     //jQuery('#update_btn').show();
                     //jQuery('#proceed_btn').show();
                     //jQuery('#coupon_loader').hide();
               },
                success : function (msg) {
                    var chunks = msg.split('|');

                    if(chunks[1] == 'Success'){
                        //jQuery('#redeem_used').show();
                        window.location.replace(baseUrlForJs+'/store/store/review');
                    }
                    else{
                         //alert(chunks[1]);
                    }
               }
//
   });
}

function unset(name) {
    window.location.replace(baseUrlForJs+'/store/store/unsetsession/to_unset/'+name);
}


//function to show the review cart link when product is added
function showReviewLink (qty) {
 
var storelink = baseUrlForJs+'/store/store/review/';
var checkoutlink = baseUrlForJs+'/store/checkout/';

    var str = '<a href="'+storelink+'"> My Cart('+qty+')</a> | <a href="'+checkoutlink+'">Checkout</a>';

    jQuery('#store_review').html(str);
    jQuery('#store_review').show('slow');
}

function requestForRestock(productsId){
	if(jQuery('#session').val() != 'set') { 
		function_name="restockRequest";
        jQuery.blockUI({ 
               message: jQuery('#login_popup'), 
               css: { width: '375px' }
                  
        });
    }
    else {
		restockRequest(productsId);
    } 
}

function restockRequest(productsId){
	jQuery.ajax({
                url :  baseUrlForJs+'/store/store/requesttorestock',
                type : 'POST',
                data : 'productsId='+productsId,

                beforeSend: function(XMLHttpRequest) {
                     jQuery('#products_restock').hide();
                     jQuery('#loader_restock').show();
                },
                complete: function (XMLHttpRequest) {
                    jQuery('#products_restock').hide();
                    jQuery('#loader_restock').hide();
               },
                success : function (msg) {
                    var chunks = msg.split('|');

                    if(chunks[1] == 'Success'){
                       jQuery('#success_restock').html(chunks[2]);
                    }
                    else{
                         jQuery('#success_restock').html(chunks[2]);
                    }
               }
//
   });	
}
