var Subtotal = 0;
var ShippingTax = 0;
var CurrentPreviewProductID = 0;

Number.prototype.formatMoney = function(c, d, t){
	var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;
	return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
};

function Convert2Decimal(num){
    return num.formatMoney(2, ".", ",");
}

function GetCartInfo(){
    var url = "AjaxFunctions.aspx";
    var data = 'Method=GetCartInfo';
    new Ajax.Request(url, {postBody: data,onComplete: function(transport) {UpdateCartInfo(transport);}}); 
}

function GetCart(NoDetails){
    PupateDetails = typeof(NoDetails)=="undefined"?1:NoDetails;
    var url = "AjaxFunctions.aspx";
    var data = 'Method=GetCart';
    if(PupateDetails){
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {GetCart_CallBack(transport);}}); 
    }else{
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {ShowCartInfo(transport);}}); 
    }
}

function ShowCartInfo(transport){
    if(transport.status==200){		
		if(transport.responseText.isJSON()){
		    response = transport.responseText.evalJSON();
		    var TotalPrice = 0;
		    var TotalItems = 0;
		    var TotalQuantity = 0;
		    response.each(function(CartItem){
		        TotalPrice += CartItem.Total;
		        TotalItems++;
		        TotalQuantity += CartItem.Quantity;
		    });
		    /*=========== Shipping Charge Calculation =========*/
		    var shipping = 0;
		    if(TotalQuantity>0){
		        shipping += 6;
		    }
		    if(TotalQuantity>1){
		        shipping += 1.5 * (TotalQuantity-1);
		    }
		    TotalPrice = TotalPrice + shipping;
		    /*=================================================*/
		    $('cart_info_total_items').update(TotalItems);
            $('cart_info_total_price').update("$"+Convert2Decimal(TotalPrice + ShippingTax));
                        
            Subtotal = TotalPrice;
        }
    }
}

function GetCart_CallBack(transport){
    if(transport.status==200){		
		if(transport.responseText.isJSON()){
		    response = transport.responseText.evalJSON();
		    var TotalPrice = 0;
		    var TotalItems = 0;
		    var TotalQuantity = 0;
		    response.each(function(CartItem){
                AddCartRow(CartItem.TempCartID, CartItem.ProductName, CartItem.Quantity, CartItem.ColorCombinationShort, CartItem.Price, CartItem.Total);
		        TotalPrice += CartItem.Total;
		        TotalItems++;
		        TotalQuantity += CartItem.Quantity;
		    });
		    $('div_subtotal').update('$'+Convert2Decimal(TotalPrice));
		    /*=========== Shipping Charge Calculation =========*/
		    var shipping = 0;
		    if(TotalQuantity>0){
		        shipping += 6;
		    }
		    if(TotalQuantity>1){
		        shipping += 1.5 * (TotalQuantity-1);
		    }
		    TotalPrice = TotalPrice + shipping;
		    /*=================================================*/
		    $('div_shipping').update('$'+Convert2Decimal(shipping));
		    $('div_total').update('$'+Convert2Decimal(TotalPrice + ShippingTax));
		    $('cart_info_total_items').update(TotalItems);
            $('cart_info_total_price').update("$"+Convert2Decimal(TotalPrice + ShippingTax));
            
            Subtotal = TotalPrice;
        }
    }
}

function GetCheckoutCart(){
    var url = "AjaxFunctions.aspx";
    var data = 'Method=GetCart';
    new Ajax.Request(url, {postBody: data,onComplete: function(transport) {GetCheckoutCart_CallBack(transport);}});
}

function GetCheckoutCart_CallBack(transport){
    if(transport.status==200){			
		if(transport.responseText.isJSON()){
		
		    response = transport.responseText.evalJSON();
		    var TotalPrice = 0;
		    var TotalItems = 0;
		    var TotalQuantity = 0;
		    var itemExists = false;
		    
		    var rows = $('tbl_cart').rows.length;
		    
		    for(var i=1; i<rows; i++){
		        Element.extend($('tbl_cart').rows[1]).remove();
		    }
		    
		    response.each(function(CartItem){
		        /*=========== Add a new row into the cart =============*/
		        var tr = $('tbl_cart').insertRow($('tbl_cart').rows.length);
		        var td;
		        var tmpStr = "";
		        
		        tr = Element.extend(tr);
		        
		        tr.id = 'cart_row_'+CartItem.TempCartID;
		        
		        td = tr.insertCell(0);
		        td = Element.extend(td);
		        td.align = 'center';
		        td.className = "checkout_productimg"
		        td.style.width = "159px";
		        td.style.height = "103px";
		        tmpStr = '<img id="product_img_'+CartItem.TempCartID+'" src="'+CartItem.ProductImage+'" alt="'+CartItem.ProductName+'" title="'+CartItem.ProductName+'" /><div class="productname" style="text-align:center"><h2>'+CartItem.ProductName+'</h2></div>';
		        td.update(tmpStr);
		        
		        td = tr.insertCell(1);
		        td = Element.extend(td);
		        var cmb = $('ctl00_ContentPlaceHolder1_cmb_ColorComination').cloneNode(true);
		        for(var i=0; i<cmb.options.length; i++){
		            if(cmb.options[i].value==CartItem.ColorCombinationCode){
		                cmb.selectedIndex = i;
		                break;
		            }
		        } 
		        cmb.className = 'checkout_dropdownbox';
		        cmb.onchange = function(){update_color_combination(this)};
		        td.align = 'center';
		        td.appendChild(cmb);
		        
		        td = tr.insertCell(2);
		        td = Element.extend(td);
		        td.className = 'orderdetail_grid';
		        td.align = 'center';
		        var txtbox = new Element('input', {'type':'text', 'style':'width: 30px', 'maxlength':'2'});
		        txtbox.onchange = function(){update_product_quantity(this)};
		        txtbox.value = CartItem.Quantity;
		        td.appendChild(txtbox);
		        
		        var hdnquantity = new Element('input', {'type':'hidden'});
		        hdnquantity.value = CartItem.Quantity;
		        td.appendChild(hdnquantity);
		        
		        td = tr.insertCell(3);
		        td = Element.extend(td);
		        td.className = 'orderdetail_price';
		        td.align = 'center';
		        td.update('$'+Convert2Decimal(CartItem.Price));
		        
		        td = tr.insertCell(4);
		        td = Element.extend(td);
		        td.className = 'orderdetail_price';
		        td.align = 'center';
		        td.update('$'+Convert2Decimal(CartItem.Total));
		        
		        td = tr.insertCell(5);
		        td = Element.extend(td);
		        td.align = 'center';
		        var img = new Element('img', {'src':'images/delete_butt.gif', 'style':'cursor:pointer'});
		        img.onclick = function(){delete_from_cart(this)};
		        td.appendChild(img);
		        /*=======================================================*/
		        
		        TotalPrice += CartItem.Total;
		        TotalItems++;
		        TotalQuantity += CartItem.Quantity;
		        itemExists = true;
		    });
		    $('cell_subtotal').update(Convert2Decimal(TotalPrice));
		    /*=========== Shipping Charge Calculation =========*/
		    var shipping = 0;
		    if(TotalQuantity>0){
		        shipping += 6;
		    }
		    if(TotalQuantity>1){
		        shipping += 1.5 * (TotalQuantity-1);
		    }
		    TotalPrice = TotalPrice + shipping;
		    /*=================================================*/
		    $('cell_shipping').update(Convert2Decimal(shipping));
		    $('cell_total').update(Convert2Decimal(TotalPrice + ShippingTax));
		    $('cart_info_total_items').update(Convert2Decimal(TotalItems));
            $('cart_info_total_price').update("$"+Convert2Decimal(TotalPrice + ShippingTax));
            Subtotal = TotalPrice;
            if(!itemExists){
                var tr = $('tbl_cart').insertRow($('tbl_cart').rows.length);
		        var td = tr.insertCell(0);
		        td.colSpan = 6;
		        td.height = 40;
		        td.align = 'center';
		        td.innerHTML = "<font color='red' font-face='verdana' size=3>There is no item in your cart</font>";
            }
            UpdateShippingTax($('ctl00_ContentPlaceHolder1_cmb_Shipping_State').options[$('ctl00_ContentPlaceHolder1_cmb_Shipping_State').selectedIndex].value);
        }
    }
}

function update_color_combination(cntrl){
    var tr = Element.extend(cntrl).up(1);
    var tmparr = tr.id.split("_");
    var TempCartID = tmparr[2];
    var ColorCombinationCode = cntrl.options[cntrl.selectedIndex].value;
    var url = "AjaxFunctions.aspx";
    var data = 'Method=UpdateColorCombination&TempCartID='+TempCartID+'&ColorCombinationCode='+ColorCombinationCode;
    new Ajax.Request(url, {postBody: data,onComplete: function(transport) {
        if(transport.status==200){
            if(transport.responseText.isJSON()){
		        response = transport.responseText.evalJSON();
		        if(response.Merged == "1"){
		            GetCheckoutCart();
		        }else{
                    $('product_img_'+TempCartID).src = response.NewImage;
                }
            }else{
                alert("There was an error updating your cart");
            }
        }else{
            alert("There was an error updating your cart");
        }
    }});
}

function update_product_quantity(cntrl){
    var isOK = true;
    cntrl = Element.extend(cntrl);
    var childs = cntrl.up().childElements();
    var hdn_quantity = childs[1];
    if(cntrl.value.match(/^[0-9]+$/)==null){
        alert("Invalid quantity");
        cntrl.value = hdn_quantity.value;
        isOK = false;
    }
    if(cntrl.value<1){
        alert("Invalid quantity");
        cntrl.value = hdn_quantity.value;
        isOK = false;
    }
    if(isOK){
        hdn_quantity.value = cntrl.value;
        var tr = Element.extend(cntrl).up(1);
        var tmparr = tr.id.split("_");
        var TempCartID = tmparr[2];
        var url = "AjaxFunctions.aspx";
        var data = 'Method=UpdateQuantity&TempCartID='+TempCartID+'&Quantity='+cntrl.value;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {GetCheckoutCart_CallBack(transport);}});
    }
}


function delete_from_cart(img){
    if(confirm("Do you really wan to remove this item?")){
        img = Element.extend(img);
        var tr = img.up(1);
        var tmparr = img.up(1).id.split("_");
        var TempCartID = tmparr[2];
        var url = "AjaxFunctions.aspx";
        var data = 'Method=DeleteCartItem&TempCartID='+TempCartID;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {delete_from_cart_CallBack(transport);}});
    }
}

function delete_from_cart_CallBack(transport){
    if(transport.status==200){			
		if(transport.responseText.isJSON()){
		    response = transport.responseText.evalJSON();
		    if(response.Status=="1"){
		        var tr = $('cart_row_'+response.TempCartID);
		        if(tr!=null){
		            tr.remove();
		        }
		        $('cart_info_total_items').update(response.Cart.TotalItems);
                $('cart_info_total_price').update("$"+(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax)));
                
                $('cell_subtotal').update(Convert2Decimal(response.Cart.TotalPrice));
                $('cell_shipping').update(Convert2Decimal(response.Cart.Shipping));
                $('cell_total').update(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax));
                
                Subtotal = response.Cart.TotalPrice + response.Cart.Shipping;
                
                if($('tbl_cart').rows.length<2){
                    var tr = $('tbl_cart').insertRow($('tbl_cart').rows.length);
		            var td = tr.insertCell(0);
		            td.colSpan = 6;
		            td.height = 40;
		            td.align = 'center';
		            td.innerHTML = "<font color='red' font-face='verdana' size=3>There is no item in your cart</font>";
                }
		    }else{
		        alert("Could not delete them Item from the cart");
		    }
        }
    }
}

function UpdateCartRow(tr, Quantity, Total){    
    td = Element.extend(tr.cells[1]);
    var QuantityBox = td.down();
    QuantityBox.value = response.Item.Quantity;
        
    td = Element.extend(tr.cells[4]);
    td.update("$"+Convert2Decimal(response.Item.Total));
}

function AddCartRow(TempCartID, ProductName, Quantity, ColorCombination, Price, Total){
    var tr = $('tblQuickCart').insertRow($('tblQuickCart').rows.length);
    tr.id = 'CartRow_'+TempCartID;
    tr.className = "grid_content";
    td = Element.extend(tr.insertCell(0));
    td.style.width = "50px";
    td.style.height = "30px";
    td.update(ProductName);
    
    td = Element.extend(tr.insertCell(1));
    td.style.width = "50px";
    var QuantityBox = new Element('input', {'type':'text', 'style':'width: 30px', 'maxlength':'2'});
    QuantityBox.value = Quantity;
    QuantityBox.onchange = function(){UpdateQuickCheckoutQuantity(this);};
    td.appendChild(QuantityBox);
    
    var QuantityBoxHdn = new Element('input', {'type':'hidden'});
    QuantityBoxHdn.value = Quantity;
    td.appendChild(QuantityBoxHdn);
    
    td = Element.extend(tr.insertCell(2));
    td.style.width = "100px";
    td.update(ColorCombination);
    
    td = Element.extend(tr.insertCell(3));
    td.style.width = "50px";
    td.update("$"+Convert2Decimal(Price));
    
    td = Element.extend(tr.insertCell(4));
    td.style.width = "50px";
    td.update("$"+Convert2Decimal(Total));
    
    td = Element.extend(tr.insertCell(5));
    td.style.width = "50px";
    td.align = "center";
    var img = new Element('img', {'src':'images/delete_butt.gif', 'alt':'delete', 'title':'delete', 'style':'cursor:pointer'});
    img.onclick = function(){DeleteCartItem(this)};
    td.update(img);
}

function DeleteCartItem(img){
    if(confirm("Do you really want to delete this item from cart?")){
        img = Element.extend(img);
        var tmparr = img.up(1).id.split("_");
        var TempCartID = tmparr[1];
        var url = "AjaxFunctions.aspx";
        var data = 'Method=DeleteCartItem&TempCartID='+TempCartID;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {DeleteCartItem_CallBack(transport);}}); 
    }
}

function DeleteCartItem_CallBack(transport){
    if(transport.status==200){			
		if(transport.responseText.isJSON()){
		    response = transport.responseText.evalJSON();
		    if(response.Status=="1"){
		        var tr = $('CartRow_'+response.TempCartID);
		        if(tr!=null){
		            tr.remove();
		        }
		        $('cart_info_total_items').update(response.Cart.TotalItems);
                $('cart_info_total_price').update("$"+(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax)));
                
                $('div_subtotal').update('$'+Convert2Decimal(response.Cart.TotalPrice));
                $('div_shipping').update('$'+Convert2Decimal(response.Cart.Shipping));
                $('div_total').update("$"+(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax)));
                
                Subtotal = response.Cart.TotalPrice + response.Cart.Shipping;
		    }else{
		        alert("Could not delete them Item from the cart");
		    }
        }
    }
}

function UpdateCartInfo(transport){
    if(transport.status==200){							
		if(transport.responseText.isJSON()){
		    response = transport.responseText.evalJSON();
		    $('cart_info_total_items').update(response.TotalItems);
            $('cart_info_total_price').update("$"+(Convert2Decimal(response.TotalPrice+response.Shipping)));
		}
	}
}


function UpdateQuickCheckoutQuantity(cntrl){
    var isOK = true;
    cntrl = Element.extend(cntrl);
    var childs = cntrl.up().childElements();
    var hdn_quantity = childs[1];
    if(cntrl.value.match(/^[0-9]+$/)==null){
        alert("Invalid quantity");
        cntrl.value = hdn_quantity.value;
        isOK = false;
    }
    if(cntrl.value<1){
        alert("Invalid quantity");
        cntrl.value = hdn_quantity.value;
        isOK = false;
    }
    if(isOK){
        hdn_quantity.value = cntrl.value;
        var tr = Element.extend(cntrl).up(1);
        var tmparr = tr.id.split("_");
        var TempCartID = tmparr[1];
        var url = "AjaxFunctions.aspx";
        var data = 'Method=UpdateQuickCheckoutQuantity&TempCartID='+TempCartID+'&Quantity='+cntrl.value;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {AddQuickCheckoutRow(transport);}});
    }
}


function AddProductToCart(ProductID, cmbCombination, cmbQuantity, SessionID){   
    var cntrlCombination = $(cmbCombination);
    var cntrlQuantity = $(cmbQuantity);
    var isOK = true;
    if(cntrlQuantity.value==""){
        alert("Please specify the value of quantity");
        isOK = false;
    }
    if(isOK){
        if(cntrlQuantity.value.match(/^[0-9]+$/)==null){
            alert("Only numeric value is permitted in Quantity");
            isOK = false;
        }
        if(isOK){
            var url = "AjaxFunctions.aspx";
            var data = 'Method=AddToCart&ProductID='+ProductID+'&CombinationCode='+cntrlCombination.options[cntrlCombination.selectedIndex].value+'&Quantity='+cntrlQuantity.value;
            new Ajax.Request(url, {postBody: data,onComplete: function(transport) {AddQuickCheckoutRow(transport);}}); 
        }
    }
}


function AddQuickCheckoutRow(transport){
    var isOK = false;
    var update = false;
    if(transport.status==200){						
		if(transport.responseText.isJSON()){			
			response = transport.responseText.evalJSON();
			var tr;
			var td;
			if(response.Status=="1"){
			    tr = $('CartRow_'+response.Item.TempCartID);
			    if(tr==null){
			        AddCartRow(response.Item.TempCartID, response.Item.ProductName, response.Item.Quantity, response.Item.ColorCombinationShort, response.Item.Price, response.Item.Total);
                }else{
                    update = true;
                    tr = Element.extend(tr);
                    UpdateCartRow(tr, response.Item.Quantity, response.Item.Total);                 
                }
                $('div_subtotal').update('$'+Convert2Decimal(response.Cart.TotalPrice));
                $('div_shipping').update('$'+Convert2Decimal(response.Cart.Shipping));
                $('div_total').update("$"+(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax)));
                $('cart_info_total_items').update(response.Cart.TotalItems);
                $('cart_info_total_price').update("$"+(Convert2Decimal(response.Cart.TotalPrice + response.Cart.Shipping + ShippingTax)));
                Subtotal = response.Cart.TotalPrice + response.Cart.Shipping;
			    isOK=true;
			}
		}
	}
	if(!isOK){
	    if(update){
	        alert("Could not update product quantity");
	    }else{
	        alert("Could not add the product to cart. Please try later on");
	    }
	}else{
	    if(update){
	        alert("Product quantity has been updated");
	    }else{
	        alert("The product has been added into your cart");
	    }
	}
}

function updateImage(ProductID, cmb, img){
    var url = "AjaxFunctions.aspx";
    var data = 'Method=GetProductCombinationImage&ProductID='+ProductID+'&ColorCombinationCode='+cmb.options[cmb.selectedIndex].value;
    new Ajax.Request(url, {postBody: data,onComplete: function(transport) {
        if(transport.status==200){	
            $(img).src = transport.responseText;
        }
    }}); 
}

function getProductColorVariations(ProductID){
    if(CurrentPreviewProductID != ProductID){
        CurrentPreviewProductID = ProductID;
        var url = "AjaxFunctions.aspx";
        var data = 'Method=GetVariations&ProductID='+ProductID;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {getProductColorVariations_CallBack(transport);}}); 
    }
}

function ChangeAllColors(cntrl){
    var form = $('aspnetForm');
    var elements = form.getElements();
    elements.each(function(element){
        if(element.name.indexOf('cmbColorCombination')>=0){
            element.selectedIndex = cntrl.selectedIndex;
            element.onchange();
        }
    });
}

function getProductColorVariations_CallBack(transport){
    if(transport.status==200){						
		if(transport.responseText.isJSON()){			
			response = transport.responseText.evalJSON();
			var strTmp = "";
			response.each(function(Variation){
                    strTmp += "<div class=\"image_right\"><img src=\"" + Variation.ImageFile + "\" alt=\"" + Variation.Color1 + " on " + Variation.Color2 + "\" /><div class=\"productname_rightside\"><h2>" + Variation.Color1 + " on " + Variation.Color2 + "</h2></div></div>";
			});
			$('product_variation_images').innerHTML = strTmp;
		}
	}
}

function toggleCheckoutBox(area){
    if(area == 'billing'){
        $('div_billing').show();
        $('div_shipping').hide();
        $('div_payment').hide();
    }else if(area == 'shipping'){
        $('div_billing').hide();
        $('div_shipping').show();
        $('div_payment').hide();
    }else if(area == 'payment'){
        $('div_billing').hide();
        $('div_shipping').hide();
        $('div_payment').show();
    }
}

function ValidateCheckoutPage(){
    frmvalidator = new Validator("aspnetForm");
    frmvalidator.EnableMsgsTogether();
    
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_FirstName","req","Shipping First Name is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_LastName","req","Shipping Last Name is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_Address1","req","Shipping Address is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_City","req","Specify the value of Shipping City");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_ZIP","req","Shipping ZIP can not be empty");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$cmb_Shipping_State","nosel=0","Please Select a Shipping State");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Shipping_Phone","req","Shipping Phone is required");    
    
    
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_FirstName","req","Billing First Name is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_LastName","req","Billing Last Name is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_Address1","req","Billing Address is required");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_City","req","Specify the value of Billing City");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_ZIP","req","Billing ZIP can not be empty");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$cmb_Billing_State","nosel=0","Please Select a Billing State");
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_Billing_Phone","req","Billing Phone is required");
    
    frmvalidator.addValidation("ctl00$ContentPlaceHolder1$txt_CardNumber","req","Please enter your Credit Card Number");
}

function ShippingValidation(){    
    var allok = true;
    var msg = "Please rectify the following things to proceed -\n\n";
    if($("ctl00_ContentPlaceHolder1_rdo_new_address").checked){
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_FirstName").value==""){
            msg += "Shipping First Name is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_LastName").value==""){
            msg += "Shipping Last Name is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_Address1").value == ""){
            msg += "Shipping Address is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_City").value == ""){
            msg += "Specify the value of Shipping City\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_ZIP").value == ""){
            msg += "Shipping ZIP can not be empty\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_cmb_Shipping_State").value == ""){
            msg += "Please Select a Shipping State\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Shipping_Phone").value == ""){
            msg += "Shipping Phone is required\n";
            allok = false;
        }
    }else{
        if($("ctl00_ContentPlaceHolder1_cmb_Addresses").selectedIndex == 0){
            msg += "Select a shipping address from the list of addresses\n";
            allok = false;
        }
    }
    if(!allok){
        alert(msg);
    }
    return allok;
}

function BillingValidation(){
    var allok = true;
    var msg = "Please rectify the following things to proceed -\n\n";
        if($("ctl00_ContentPlaceHolder1_rdo_NewAddressBilling").checked){
        if($("ctl00_ContentPlaceHolder1_txt_Billing_FirstName").value==""){
            msg += "Billing First Name is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Billing_LastName").value==""){
            msg += "Billing Last Name is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Billing_Address1").value == ""){
            msg += "Billing Address is required\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Billing_City").value == ""){
            msg += "Specify the value of Billing City\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Billing_ZIP").value == ""){
            msg += "Billing ZIP can not be empty\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_cmb_Billing_State").value == ""){
            msg += "Please Select a Billing State\n";
            allok = false;
        }
        if($("ctl00_ContentPlaceHolder1_txt_Billing_Phone").value == ""){
            msg += "Billing Phone is required\n";
            allok = false;
        }
    }else{
        if($("ctl00_ContentPlaceHolder1_cmb_BillingAddresses").selectedIndex == 0){
            msg += "Select a billing address from the list of addresses\n";
            allok = false;
        }
    }
    if(!allok){
        alert(msg);
    }
    return allok;
}

function UpdateShippingTax(ShippingState){
    if(ShippingState=="NY"){
        ShippingTax = Subtotal * .0865;
    }else{
        ShippingTax = 0;
    }
    var CartTotal = Subtotal + ShippingTax;
    $('cell_tax').update(Convert2Decimal(ShippingTax));
    $('cell_total').update("$"+(Convert2Decimal(CartTotal)));
    $('cart_info_total_price').update("$"+(Convert2Decimal(CartTotal)));
    
}

function CheckCopy(cntrl){    
	if(cntrl.checked){
		ShipAddressFill();
	}else{
		ShipAddressClear();
	}	
}

function ShipAddressFill(){
	$("ctl00_ContentPlaceHolder1_txt_Billing_FirstName").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_FirstName").value;
	$("ctl00_ContentPlaceHolder1_txt_Billing_Address1").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_Address1").value;
	$("ctl00_ContentPlaceHolder1_txt_Billing_City").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_City").value;
	$("ctl00_ContentPlaceHolder1_txt_Billing_ZIP").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_ZIP").value;
	$("ctl00_ContentPlaceHolder1_txt_Billing_LastName").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_LastName").value;
	$("ctl00_ContentPlaceHolder1_txt_Billing_Address2").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_Address2").value;
	$("ctl00_ContentPlaceHolder1_cmb_Billing_State").selectedIndex = $("ctl00_ContentPlaceHolder1_cmb_Shipping_State").selectedIndex;
	$("ctl00_ContentPlaceHolder1_txt_Billing_Phone").value = $("ctl00_ContentPlaceHolder1_txt_Shipping_Phone").value;
		
}

function ShipAddressClear(){
	$("ctl00_ContentPlaceHolder1_txt_Billing_FirstName").value="";
	$("ctl00_ContentPlaceHolder1_txt_Billing_Address1").value="";
	$("ctl00_ContentPlaceHolder1_txt_Billing_City").value="";
	$("ctl00_ContentPlaceHolder1_txt_Billing_ZIP").value="";
	$("ctl00_ContentPlaceHolder1_txt_Billing_LastName").value="";
	$("ctl00_ContentPlaceHolder1_txt_Billing_Address2").value="";
	$("ctl00_ContentPlaceHolder1_cmb_Billing_State").selectedIndex=0;
	$("ctl00_ContentPlaceHolder1_txt_Billing_Phone").value="";	
}

function ToggleShipAddress(cntrl, type){
    if(cntrl.checked){
        if(type=="new"){
            $('div_new_address').show();
            $('div_existing_address').hide();
            $("ctl00_ContentPlaceHolder1_txt_Shipping_FirstName").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Shipping_Address1").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Shipping_City").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Shipping_ZIP").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Shipping_LastName").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Shipping_Address2").value = "";
            $("ctl00_ContentPlaceHolder1_cmb_Shipping_State").selectedIndex = 0;
            $("ctl00_ContentPlaceHolder1_txt_Shipping_Phone").value = "";
            $("ctl00_ContentPlaceHolder1_cmb_Addresses").selectedIndex=0;
        }else if(type=="existing"){
            $('div_new_address').hide();
            $('div_existing_address').show();
        }
    }
}

function ToggleBillAddress(cntrl, type){
    if(cntrl.checked){
        if(type=="new"){
            $('div_new_billing_address').show();
            $('div_existing_billing_address').hide();
            $("ctl00_ContentPlaceHolder1_txt_Billing_FirstName").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Billing_Address1").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Billing_City").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Billing_ZIP").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Billing_LastName").value = "";
            $("ctl00_ContentPlaceHolder1_txt_Billing_Address2").value = "";
            $("ctl00_ContentPlaceHolder1_cmb_Billing_State").selectedIndex = 0;
            $("ctl00_ContentPlaceHolder1_txt_Billing_Phone").value = "";
            $("ctl00_ContentPlaceHolder1_cmb_BillingAddresses").selectedIndex=0;
        }else if(type=="existing"){
            $('div_new_billing_address').hide();
            $('div_existing_billing_address').show();
        }
    }
}

function FetchShippingAddress(CustomerID, ShippingAddressID){
    if(ShippingAddressID!=0){
        var url = "AjaxFunctions.aspx";
        var data = 'Method=GetShippingAddress&CustomerID='+CustomerID+'&ShippingAddressID='+ShippingAddressID;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {
            if(transport.status==200){	
                if(transport.responseText.isJSON()){			
			        response = transport.responseText.evalJSON();
			        $("ctl00_ContentPlaceHolder1_txt_Shipping_FirstName").value = response.FirstName;
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_Address1").value = response.Address1;
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_City").value = response.City;
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_ZIP").value = response.ZIP;
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_LastName").value = response.LastName;
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_Address2").value = response.Address2;
	                for(i=0; i<$("ctl00_ContentPlaceHolder1_cmb_Shipping_State").options.length; i++){
	                    if($("ctl00_ContentPlaceHolder1_cmb_Shipping_State").options[i].text == response.State){
	                        $("ctl00_ContentPlaceHolder1_cmb_Shipping_State").selectedIndex = i;
	                        break;
	                    }
	                }
	                $("ctl00_ContentPlaceHolder1_txt_Shipping_Phone").value = response.Phone;
                }else{
                    alert("Could not get the details of the selected address from server");
                    $("ctl00_ContentPlaceHolder1_cmb_Addresses").selectedIndex = 0;
                }
            }else{
                alert("Could not get the details of the selected address from server");
                $("ctl00_ContentPlaceHolder1_cmb_Addresses").selectedIndex = 0;
            }
        }});
    }
}


function FetchBillingAddress(CustomerID, BillingAddressID){
    if(BillingAddressID!=0){
        var url = "AjaxFunctions.aspx";
        var data = 'Method=GetBillingAddress&CustomerID='+CustomerID+'&BillingAddressID='+BillingAddressID;
        new Ajax.Request(url, {postBody: data,onComplete: function(transport) {
            if(transport.status==200){	
                if(transport.responseText.isJSON()){			
			        response = transport.responseText.evalJSON();
			        $("ctl00_ContentPlaceHolder1_txt_Billing_FirstName").value = response.FirstName;
	                $("ctl00_ContentPlaceHolder1_txt_Billing_Address1").value = response.Address1;
	                $("ctl00_ContentPlaceHolder1_txt_Billing_City").value = response.City;
	                $("ctl00_ContentPlaceHolder1_txt_Billing_ZIP").value = response.ZIP;
	                $("ctl00_ContentPlaceHolder1_txt_Billing_LastName").value = response.LastName;
	                $("ctl00_ContentPlaceHolder1_txt_Billing_Address2").value = response.Address2;
	                for(i=0; i<$("ctl00_ContentPlaceHolder1_cmb_Billing_State").options.length; i++){
	                    if($("ctl00_ContentPlaceHolder1_cmb_Billing_State").options[i].text == response.State){
	                        $("ctl00_ContentPlaceHolder1_cmb_Billing_State").selectedIndex = i;
	                        break;
	                    }
	                }
	                $("ctl00_ContentPlaceHolder1_txt_Billing_Phone").value = response.Phone;
                }else{
                    alert("Could not get the details of the selected address from server");
                    $("ctl00_ContentPlaceHolder1_cmb_BillingAddresses").selectedIndex = 0;
                }
            }else{
                alert("Could not get the details of the selected address from server");
                $("ctl00_ContentPlaceHolder1_cmb_BillingAddresses").selectedIndex = 0;
            }
        }});
    }
}