var rootURL;
rootURL="/";

	
	
	
function popUpShippingDetails(baseURL) {

	var URL;
	URL = baseURL + '?st=' + document.getElementById('ddlShipping').options[document.getElementById('ddlShipping').options.selectedIndex].value;

	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=700,height=400');");

}


function loadShippingDDL(){


	//remember last selected value;
	//var ddlShipping;
	//ddlShipping = document.getElementById('ddlShipping');
	//ddlShipping.length=0;

	//st = new Option("yoyoyoyo","111");
	//ddlShipping.add(st);
	
	//reset to last selected value
	
}



	
	
/***************************SUB TOTAL FUNCTIONS *******************************************************/

function UpdateProductQuantity(TempCartID,ProductID,Quantity) {

		var lblProgress;
		lblProgress = document.getElementById('lblProgress');
		lblProgress.style.display="inline";
		
		var url;
		url = rootURL + "ajax/UpdateCartProductQuantities.aspx?c=" + TempCartID + "&p=" + ProductID + "&q=" + Quantity;
		
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                        http_request.overrideMimeType('text/xml');
                        // See note below about this line
                }
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                }
        }

        if (!http_request) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        http_request.onreadystatechange = function() { checkResultOfProductQuantityUpdate(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);
        
		
       
}
 
function checkResultOfProductQuantityUpdate(http_request) {
        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request.readyState == 4) && (http_request.status == 200)) {
              if (http_request.responseText == "true") {
				updatePricesPerProductInGrid();
				
              }                  
        }
}







function updatePricesPerProductInGrid(){
		
	var lblProducts;
	lblProducts = document.getElementById('lblAllProductsInCart');
	
	var aryProducts=lblProducts.innerHTML.split(",");
	
	
	
	

	var intProduct=0;
	while (intProduct < aryProducts.length)
	{		
		//update prod total
		var strSpanNamePrice;
		strSpanNamePrice = 'lblUnitPrice' + aryProducts[intProduct];
		var UnitPrice;
		UnitPrice = document.getElementById(strSpanNamePrice).innerHTML;
		UnitPrice = UnitPrice.replace("$","");
		UnitPrice = UnitPrice.replace(",","");
		
		
		var strElementNameQuantity;
		strElementNameQuantity = 'txtQuantity' + aryProducts[intProduct];
		var Quantity;
		Quantity = document.getElementById(strElementNameQuantity).value;
		Quantity = Quantity.replace("$","");
				
		var strSpanName;
		strSpanName = 'lblProdTotal' + aryProducts[intProduct];
		document.getElementById(strSpanName).innerHTML = formatCurrency(UnitPrice * parseInt(Quantity));
		
		
		intProduct+=1;
	}
	
	
	showTotals();

}


//////////****************************COUPONS************************************//////////////////

function CheckCoupon(CouponCode,ShipType) {

	var lblProgress;
	lblProgress = document.getElementById('lblProgress');
	lblProgress.style.display="inline";
	
		

	if (CouponCode != ""){

		var url;
		url = rootURL + "ajax/checkCouponCode.aspx?c=" + CouponCode + "&s=" + ShipType;
		
        var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                        http_request.overrideMimeType('text/xml');
                        // See note below about this line
                }
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                }
        }

        if (!http_request) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        http_request.onreadystatechange = function() { setCouponSpans(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);
        
        }//if coupon not is ""

}
 
function setCouponSpans(http_request) {
        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request.readyState == 4) && (http_request.status == 200)) {
            //alert(http_request.responseText);               
            
            var aryParts=http_request.responseText.split("|");

			document.getElementById("lblCouponDescription").innerHTML = aryParts[0];
			document.getElementById("lblDiscount").innerHTML = formatCurrency(aryParts[1]);
			
			var lblProgress;
			lblProgress = document.getElementById('lblProgress');
			lblProgress.style.display="none";
	
			      
                    
        }
}







//////////****************************SHIPPING************************************//////////////////

function setShippingAmount(ShipTypeID){

	var url
	url = rootURL + "getShippingCost.aspx?st=" + ShipTypeID;
	
	var lblProgress;
	lblProgress = document.getElementById('lblProgress');
	lblProgress.style.display="inline";
	

	var http_request = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request = new XMLHttpRequest();
                if (http_request.overrideMimeType) {
                        http_request.overrideMimeType('text/xml');
                        // See note below about this line
                }
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (e) {
                        try {
                                http_request = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (e) {}
                }
        }

        if (!http_request) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        http_request.onreadystatechange = function() { getShippingAmountResponse(http_request); };
        http_request.open('GET', url, true);
        http_request.send(null);
        		
}


function getShippingAmountResponse(http_request) {
        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request.readyState == 4) && (http_request.status == 200)) {
			showTotals();     			            
        }
        	   
}






//////////****************************TOTALS************************************//////////////////

function showTotals(){
 
		loadShippingDDL();
   
		CheckCoupon(document.getElementById('txtCouponCode').value,document.getElementById('ddlShipping').options[document.getElementById('ddlShipping').options.selectedIndex].value)
		 
        var urlTotal;
		urlTotal = rootURL + "ajax/getAllCartTotals.aspx?s=" + document.getElementById('ddlShipping').options[document.getElementById('ddlShipping').options.selectedIndex].value;
				
        var http_request_total = false;

        if (window.XMLHttpRequest) { // Mozilla, Safari,...
                http_request_total = new XMLHttpRequest();
                if (http_request_total.overrideMimeType) {
                        http_request_total.overrideMimeType('text/xml');                        
                }
        } else if (window.ActiveXObject) { // IE
                try {
                        http_request_total = new ActiveXObject("Msxml2.XMLHTTP");
                } catch (eTotal) {
                        try {
                                http_request_total = new ActiveXObject("Microsoft.XMLHTTP");
                        } catch (eTotal) {}
                }
        }

        if (!http_request_total) {
                alert('Giving up :( Cannot create an XMLHTTP instance');
                return false;
        }

        http_request_total.onreadystatechange = function() { printTotals(http_request_total); };
        http_request_total.open('GET', urlTotal, true);
        http_request_total.send(null);
        


}


function printTotals(http_request_total) {

        //alert('Ready State: '+http_request.readyState+'\nHTTP Status: '+http_request.status);
        if ((http_request_total.readyState == 4) && (http_request_total.status == 200)) {
      
        
			//alert(http_request_total.responseText);
        
			var aryParts=http_request_total.responseText.split("|");
			
			//subtotal|discount|shipping|taxes|total
			var decSubtotal;
			var decDiscount;
			var decShipping;
			var decTaxes;
			var decTaxRate;
			var decTotal;
			
			decSubtotal = aryParts[0];
			decDiscount = aryParts[1];
			decShipping = aryParts[2];
			decTaxes = aryParts[3];
			decTaxRate = aryParts[4];
			decTotal = aryParts[5];
			
			document.getElementById('lblSubTotal').innerHTML = formatCurrency(decSubtotal);
			document.getElementById('lblDiscount').innerHTML = formatCurrency(decDiscount);
			document.getElementById('lblShipping').innerHTML = formatCurrency(decShipping);
			document.getElementById('lblTaxes').innerHTML = formatCurrency(decTaxes);
			document.getElementById('lblTaxRate').innerHTML = '(' + decTaxRate + '%)';
			document.getElementById('lblTotalNoTax').innerHTML = formatCurrency(decTotal);			
			 
			 
			var lblProgress;
			lblProgress = document.getElementById('lblProgress');
			lblProgress.style.display="none";
			
	     
        }
}






/*******************FORMATTING **************************/

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}