function cart_update(prod_id,prod_label_comb_id,qty,label,orig_qty){
  
    document.cart.prod_id.value = prod_id;
    var prod_var = "prod_variation_"+qty;
    if(document.getElementById(prod_var)){
        document.cart.prod_label_comb_id.value = document.getElementById(prod_var).value;
    }
    prodqty = "qty_"+qty;
    wish_qty = "buy_wish_qty_"+qty;
    notify_str = "notify_friend_check"+qty;  
    buy_str = "buying_for_check"+qty;          
    if (document.getElementById(notify_str) && document.getElementById(notify_str).checked == true && parseInt(document.cart[prodqty].value) < parseInt(document.cart[wish_qty].value)) {
        alert("Total quantity cannot be less than the bought quantity.");
        document.cart[prodqty].value = orig_qty; 
    } else if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == true && parseInt(document.cart[prodqty].value) < parseInt(document.cart[wish_qty].value)) {  
        alert("Total quantity cannot be less than the bought quantity.");
        document.cart[prodqty].value = orig_qty;
    } else {   
        document.cart.prod_qty.value = document.cart[prodqty].value;
        document.cart.updtcart.value = 1;
        document.cart.prodlabel.value = label;
        document.cart.shop_id.value = document.cart.str_shop_id.value;
        document.cart.submit();
    }
}

function wish_cart_update(prod_id,prod_label_comb_id,qty,label,prod_id,orig_qty){
    prodqty = "qty_"+qty;
    wish_qty = "buy_wish_qty_"+qty;
    buy_str = "buying_for_check"+qty;
    anonym_str = "anonymous_check"+qty;
    anonym_div_str =  "anonymous"+qty;
    
    if (parseInt(document.cart[prodqty].value) < parseInt(document.cart[wish_qty].value)) {
        alert("Total quantity cannot be less than the bought quantity.");
        document.cart[wish_qty].value = orig_qty;
    } else if (document.cart[wish_qty].value == 0) {
        if (document.getElementById(anonym_str) && document.getElementById(anonym_str).checked == true) {
            document.getElementById(anonym_str).checked = false; 
        }
        if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == true) {
            document.getElementById(buy_str).checked = false;
        }
        if (document.getElementById(anonym_div_str)) { 
           document.getElementById(anonym_div_str).style.display = "none"; 
        }
        cart_update1(prod_id,prod_label_comb_id,qty,label,prod_id); 
    } else {
        if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == false) {
            document.getElementById(buy_str).checked = true; 
            if (document.getElementById(anonym_div_str)) { 
               document.getElementById(anonym_div_str).style.display = ""; 
            } 
        }
        cart_update1(prod_id,prod_label_comb_id,qty,label,prod_id);
    }
    
    
}
function handleHttpResponse(transport) {     
    var results = transport.responseText;
    if(transport.responseText != ""){
        res_divText = results.split("|~|");
        if (res_divText[0] != '') {
            if ($("#error")) {
               $("#error").html(res_divText[0]); 
            }
        }
        wish_qty = "buy_wish_qty_"+res_divText[1];   
        anonym_str = "anonymous_check"+res_divText[1];
        buy_str = "buying_for_check"+res_divText[1];
        anonym_str =  "anonymous"+res_divText[1];
        if ($("#"+buy_str)) {
            if (res_divText[2] == 'Y') {    
                 document.getElementById(buy_str).checked = true; 
                 if ($("#"+anonym_str)) {  
                    document.getElementById(anonym_str).style.display = "";
                 }
                 if (res_divText[4] == 'Y' && document.getElementById(anonym_str)) {
                    document.getElementById(anonym_str).checked = true;   
                 } else if (res_divText[4] == 'N' && document.getElementById(anonym_str)) { 
                    document.getElementById(anonym_str).checked = false; 
                 }
            } else if(res_divText[2] == 'N') {
                document.getElementById(buy_str).checked = false; 
                if ($("#"+anonym_str)) {  
                    document.getElementById(anonym_str).style.display = "none";
                }
            }
        }                   
        if ($("#"+wish_qty) && res_divText[3] != '') {  
            $("#"+wish_qty).val(res_divText[3]);
        } 
    } 
}
function cart_update1(prod_id,prod_label_comb_id,qty,label,orig_qty){
    document.cart.prod_id.value = prod_id;
    var prod_var = "prod_variation_"+qty;
    if(document.getElementById(prod_var)){
        document.cart.prod_label_comb_id.value = document.getElementById(prod_var).value;
    }
    prodqty = "qty_"+qty;
    wish_qty = "buy_wish_qty_"+qty;
    notify_str = "notify_friend_check"+qty;   
    anonym_str = "anonymous_check"+qty;
    buy_str = "buying_for_check"+qty;
    if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == true) {
        buying_for_frnd = "Y";
    } else if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == false) {
        buying_for_frnd = "N";
    }
    if (document.getElementById(anonym_str) && document.getElementById(anonym_str).checked == true) {
        let_my_frnd_know = "N";
    } else if (document.getElementById(anonym_str) && document.getElementById(anonym_str).checked == false) {
        let_my_frnd_know = "Y";
    }
    if (document.getElementById(notify_str) && document.getElementById(notify_str).checked == true && parseInt(document.cart[prodqty].value) < parseInt(document.cart[wish_qty].value)) {
        alert("Total quantity cannot be less than the bought quantity.");
        document.cart[prodqty].value = orig_qty;    
    } else if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == true && parseInt(document.cart[prodqty].value) < parseInt(document.cart[wish_qty].value)) {
        alert("Total quantity cannot be less than the bought quantity.");
        document.cart[prodqty].value = orig_qty;
    } else {   
        document.cart.prod_qty.value = document.cart[prodqty].value;    
        document.cart.prodlabel.value = label;
        document.cart.shop_id.value = document.cart.str_shop_id.value;       
        new Ajax.Request('ajax_mybag.php',
      {
        method: 'post',
        parameters: {buying_for_frnd: buying_for_frnd, let_my_frnd_know: let_my_frnd_know, cart_id: qty, updtcart: 1, prod_id: prod_id, prod_label_comb_id: document.cart.prod_label_comb_id.value, prod_qty: document.cart[prodqty].value, wish_qty: document.cart[wish_qty].value},
        onSuccess: handleHttpResponse        
      });
    }
}

function update_variation(prod_id,qty) {
    prodqty = "qty_"+qty;
    prod_var = "prod_variation_"+qty;
    document.cart.prod_qty.value = document.cart[prodqty].value;
    document.cart.prod_id.value = prod_id;
    if(document.getElementById(prod_var)){
        document.cart.prod_label_comb_id.value = document.getElementById(prod_var).value;
    }
    document.cart.variation_update.value = 1;
    document.cart.cart_id.value = qty;
    
    wishqty = "buy_wish_qty_"+qty;
    anostr = "anonymous_check"+qty;
    buy_str = "buying_for_check"+qty;   
    document.cart.submit();
}

function update_notify(prod_id,qty) { 
    prodqty = "qty_"+qty;
    prod_var = "prod_variation_"+qty;
    wishqty = "buy_wish_qty_"+qty;
    anostr = "anonymous_check"+qty;
    document.cart.prod_id.value = prod_id;
    
    if (document.getElementById(anostr) && document.getElementById(anostr).checked == true) {
        document.cart.let_my_frnd_know.value = "N";
    } else if (document.getElementById(anostr) && document.getElementById(anostr).checked == false) {
        document.cart.let_my_frnd_know.value = "Y";
    }
    if(document.getElementById(prod_var)){
        document.cart.prod_label_comb_id.value = document.getElementById(prod_var).value;
    }
    document.cart.prod_qty.value = document.cart[prodqty].value;
    document.cart.cart_id.value = qty;
    document.cart.wish_qty.value = document.cart[wishqty].value;   
    new Ajax.Request('ajax_mybag.php',
      {
        method: 'post',
        parameters: {cart_id: qty, let_my_frnd_know: document.cart.let_my_frnd_know.value, prod_id: prod_id, prod_label_comb_id: document.cart.prod_label_comb_id.value, prod_qty: document.cart.prod_qty.value, wish_qty: document.cart.wish_qty.value, notify:'save'},
        onSuccess: handleHttpResponse        
      });
}

function update_buy(prod_id,qty) {
    prodqty = "qty_"+qty;
    prod_var = "prod_variation_"+qty;
    wishqty = "buy_wish_qty_"+qty;
    buy_str = "buying_for_check"+qty;
    anonym_str =  "anonymous"+qty;
    anonymous_str = 'anonymous_check'+qty;   
    
    document.cart.prod_id.value = prod_id;
    if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == true) {
        document.cart.buying_for_frnd.value = "Y";
        if (document.getElementById(anonym_str)) {
            document.getElementById(anonym_str).style.display = "";
        }
    } else if (document.getElementById(buy_str) && document.getElementById(buy_str).checked == false) {
        document.cart.buying_for_frnd.value = "N";     
        if (document.getElementById(anonym_str)) {
            document.getElementById(anonym_str).style.display = "none";
        }
        if (document.getElementById(anonymous_str)) {
            document.getElementById(anonymous_str).checked = false;
        } 
        $("#"+wishqty).val(0);
    }
    if(document.getElementById(prod_var)){
        document.cart.prod_label_comb_id.value = document.getElementById(prod_var).value;
        
    }
    document.cart.prod_qty.value = document.cart[prodqty].value;
    document.cart.cart_id.value = qty;
    document.cart.wish_qty.value = document.cart[wishqty].value;
                                                                           
    if (parseInt(document.cart[prodqty].value) < parseInt(document.cart[wishqty].value)) {
        alert("Total quantity cannot be less than the bought quantity.");
    } else {  
        new Ajax.Request('ajax_mybag.php',
      {
        method: 'post',
        parameters: {cart_id: qty, buying_for_frnd: document.cart.buying_for_frnd.value, prod_id: prod_id, prod_label_comb_id: document.cart.prod_label_comb_id.value, prod_qty: document.cart[prodqty].value, wish_qty: document.cart[wishqty].value, buy:'save'},
        onSuccess: handleHttpResponse        
      });
    }
}

function change_delivery_band(){
    var delivery_band = document.getElementById('delivery_band').value;
    var shop_id = document.cart.str_shop_id.value;
    if(delivery_band!=""){
        document.cart.shop_id.value = document.cart.str_shop_id.value;
        document.cart.updtcart.value = 1;
        document.cart.submit();
    }
    
}

function check_all() {
    var all_ok = true;
    str_cat = document.cart.str_cart_id.value;
    if (str_cat != '' && str_cat != ',') {
        var all_cat_arr = new Array();
        all_cat_arr = str_cat.split(',');
        for (var i = 0 ;i < all_cat_arr.length; i++) {
            var buying_for_str = 'buying_for_check'+all_cat_arr[i];
            if (document.getElementById(buying_for_str) && document.getElementById(buying_for_str).checked == true) {
                var notify_friend_str = 'notify_friend_check'+all_cat_arr[i];
                var anonymous_str = 'anonymous_check'+all_cat_arr[i];
                if (document.getElementById(notify_friend_str) && document.getElementById(notify_friend_str).checked == false && document.getElementById(anonymous_str) && document.getElementById(anonymous_str).checked == false) {  
                    alert("Please select atleast one checkbox,whether you want to notify your friend or want to buy anonymously.");
                    all_ok = false;
                    break;
                } else if (document.getElementById(notify_friend_str) && document.getElementById(notify_friend_str).checked == true && document.getElementById(anonymous_str) && document.getElementById(anonymous_str).checked == true) {
                    alert("You can't check both the options.");
                    all_ok = false;
                    break;
                }
            }
        }
    }    
    return all_ok;
}

function submit_cart() {  
    if (check_all() == true) {
        delivery_band = document.getElementById('delivery_band').value;
        if(delivery_band==""){
            alert("Please choose delivery location");
        }else{      
            document.cart.submit();
        }
    }
}

function make_visible(id) {
    var buying_for_check_str = 'buying_for_check'+id;
    if (document.getElementById(buying_for_check_str)) {
        var anonymous_str = 'anonymous'+id;
        if (document.getElementById(buying_for_check_str).checked == true) {
            if (document.getElementById(anonymous_str)) {
                document.getElementById(anonymous_str).style.display = "";
            }
        } else if (document.getElementById(buying_for_check_str).checked == false) {
            if (document.getElementById(anonymous_str)) {
                document.getElementById(anonymous_str).style.display = "none";
            }
        }
    }
}

function check_both(id,checkid) {
    var notify_friend_str = 'notify_friend_check'+id;
    var anonymous_str = 'anonymous_check'+id;
    var buy_qtr_str = 'buy_qty'+id;
    if (document.getElementById(notify_friend_str) && document.getElementById(anonymous_str)) {
        if (document.getElementById(notify_friend_str).checked == true &&  document.getElementById(anonymous_str).checked == true) {
            alert("You can't check both the options.");
            document.getElementById(anonymous_str).checked = false;
            document.getElementById(notify_friend_str).checked = false;   
            if (document.getElementById(buy_qtr_str)) {
                document.getElementById(buy_qtr_str).style.display = 'none';
            }
        } else if (document.getElementById(notify_friend_str).checked == true) {
            if (document.getElementById(buy_qtr_str)) {
                document.getElementById(buy_qtr_str).style.display = '';     
            }
        } else if (document.getElementById(anonymous_str).checked == true) {
            if (document.getElementById(buy_qtr_str)) {
                document.getElementById(buy_qtr_str).style.display = 'none';
            }
        } else if (document.getElementById(notify_friend_str).checked == false) {
            if (document.getElementById(buy_qtr_str)) {
                document.getElementById(buy_qtr_str).style.display = 'none';
            }
        } 
    }
    
}
