<!--//
 	
 	//var url='https://www.yourdomain.com/cgi-bin/apluspro/scripts/apluspro.cgi?'
 	var url='https://www.eco-gear.ca/cgi-bin/apluspro/scripts/apluspro.cgi?'
 	
 	var currencySymbol = '$';
 	
 	// default tax state
 	default_local = 'ON';
 	// default tax country 
 	default_country = 'CA';
 	
 	// state selected
 	var v_localtax = '';
 	// country selected
 	var v_country = '';
 	
 	// flags
 	var localup = 0;
 	var countryup = 0;
 	
 	// Let the cgi script know about an update
 	var update_country = 0;
 	var update_localtax = 0;
 	
 	function CalculateShippingTotals(type) {
 	
	  GetNewCosts(type);
	 	
	}
 	
	function CalculateDiscount() {
 	
	  //alert("discount="+document.cartform1.discount_code.value);
	  GetDiscount(document.cartform1.discount_code.value);
	 	
	}
	
 	function CalculateTotals(type, value) {
 		
	 	//alert("CalculateTotals ..."); 	
	 	//alert("type="+type);
	 	
	 	//alert("localup="+localup);
	 	//alert("countryup="+countryup);
	 	
 		if (type == 1 && value == 'yes') {
	 		// state
	 		v_localtax = default_local;
	 		update_localtax = 1;
 		}
 		else if (type == 1 && value == 'no') {
	 		v_localtax = '';
	 		update_localtax = 1;
		}
 		else if (type == 2 && value == 'yes') {
	 		// state
	 		v_country = default_country;
	 		update_country = 1;
 		}
 		else if (type == 2 && value == 'no') {
	 		v_country = '';
	 		update_country = 1;
		}
 		else {
	 		// Uncomment for Web Pro
	 		//v_localtax = document.cartform2.STATE.value;
	 		//v_country = document.cartform2.COUNTRYCODE.value;
	 		v_localtax = '';
	 		v_country = '';
 		}
 				
	 	GetTax(v_localtax, v_country, update_localtax, update_country);
	 	
 	}

	function getHTTPObject() {
		  var xmlhttp;
		  /*@cc_on
		  @if (@_jscript_version >= 5)
		    try {
		      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		    } catch (e) {
		      try {
		        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		      } catch (E) {
		        xmlhttp = false;
		      }
		    }
		  @else
		  xmlhttp = false;
		  @end @*/
		  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		    try {
		      xmlhttp = new XMLHttpRequest();
		    } catch (e) {
		      xmlhttp = false;
		    }
		  }
		  return xmlhttp;
	}
	
	
	// We create the HTTP Object
	// Rates
	var http1 = getHTTPObject();
	var http2 = getHTTPObject();
	var http3 = getHTTPObject();
	
	function GetTax(state, countrycode, update_localtax, update_country) {
		urlstr = url + "action=44&STATE="+ escape(state)+"&COUNTRYCODE="+escape(countrycode)+"&update_localtax="+escape(update_localtax)+"&update_country="+escape(update_country);
		
		//alert( urlstr);
		http1.open("POST", urlstr, true);
		http1.onreadystatechange = handleHttpResponse1;
		http1.send(null);
	}
	
	function GetNewCosts(type) {
		urlstr = url + "action=46&shiptype="+ escape(type);
		
		//alert( urlstr);
		http2.open("POST", urlstr, true);
		http2.onreadystatechange = handleHttpResponse2;
		http2.send(null);
	}

	
	function GetDiscount(type) {
		urlstr = url + "action=47&coupon="+ escape(type);
		
		//alert( urlstr);
		http3.open("POST", urlstr, true);
		http3.onreadystatechange = handleHttpResponse3;
		http3.send(null);
	}
	
	
	function handleHttpResponse3() {
		var discountdesccol = document.getElementById("discountdesc");
		var discountcol = document.getElementById("discount");
		var gtcol = document.getElementById("gtotal");
 		
	  if (http3.readyState == 4) {
	    	// Split the comma delimited response into an array
	    	results = http3.responseText;
	    	
	    	// Parse out the shipping cost and the total cost and insert into the tds
	    	
	    	//alert("results="+results);
	    	
	    	if (results.indexOf('?') > 0) {
	    		options1 = results.split("?");
	    		if (options1[1] > 0) {
	    			discountdesccol.innerHTML=options1[0];
	    			discountcol.innerHTML=options1[1];
	    			gtcol.innerHTML=options1[2];
    			}
    			else {
	    			discountdesccol.innerHTML=options1[0];
	    			discountcol.innerHTML=options1[1];
	    			gtcol.innerHTML=options1[2];
    			}
    		}
	    	
	    	
    	}
	}	
	
	function handleHttpResponse2() {
		var shipcol = document.getElementById("shipping");
		var gtcol = document.getElementById("gtotal");
 		
	  if (http2.readyState == 4) {
	  	 	//alert("handleHttpResponse2 ...");

	  		var r_statetax = document.getElementsByName('statetax');	 	
	  		var r_countrytax = document.getElementsByName('countrytax');	 	

		  
	    	// Split the comma delimited response into an array
	    	results = http2.responseText;
	    	
	    	// Parse out the shipping cost and the total cost and insert into the tds
	    	
	    	//alert("results="+results);
	    	
	    	if (results.indexOf('?') > 0) {
	    		options1 = results.split("?");
	    		shipcol.innerHTML=options1[0];
	    		gtcol.innerHTML=options1[1];
    		}
	    	
	    	
    	}
	}

	
	function handleHttpResponse1() {
		var dyntable = document.getElementById("basket");
		
 		var putlocalup = 0;
 		var putcountryup = 0;
 		var putgrandtotalup = 0;
 		
	  if (http1.readyState == 4) {
	  	 	//alert("handleHttpResponse1 ...");

	  		var r_statetax = document.getElementsByName('statetax');	 	
	  		var r_countrytax = document.getElementsByName('countrytax');	 	

		  
	    	// Split the comma delimited response into an array
	    	results = http1.responseText;
	    	
	    	//alert("results="+results);
	    	var options1 =new Array();
	    	var options2 =new Array();
	    	var options3 =new Array();
	    	var grandtotal = 0.0;
	    	var yeschange = 0;
		   
	    	options1 = results.split("?");
		    	
		    	
		   options2 = options1[0].split("|"); 	
		   options3 = options1[1].split("|"); 	
		   grandtotal = options1[2]; 	
		   
		   
		  //alert("localup="+localup);
		  //alert("countryup="+countryup);
		   
		   if (countryup == 1 && localup == 1) {
			    delRow(dyntable, 3);
		   }
			else if (countryup == 1 || localup == 1) {
			    delRow(dyntable, 2);
		   }		   
		   else {
				delRow(dyntable, 1);
		   }
		   
		   
		   if (options2[1]>0) {
			   // Local Tax exists
			 	localup = 1;
			 	putlocalup = 1;
			 	// Uncomment for Web Pro
			 	//document.cartform2.STATE.value=default_local;
			 	r_statetax[1].checked=true;
	 		}
	 		else {
		 		//alert("a="+r_statetax[1].checked);
		 		//alert("b="+r_statetax[0].checked);

		 		if (localup == 1) {
			 		// initialize only if was set to yes
			 		// Uncomment for Web Pro
			 		// if (document.cartform2.STATE.value == default_local) {
			 		//	document.cartform2.STATE.value="";
		 			//}
			 		r_statetax[1].checked=false;
			 		r_statetax[0].checked=true;
		 		}
	 			putlocalup = 0;	
			 	localup = 0;		 		
		 	}

		   if (options3[1]>0) {
	 			countryup = 1;	 	
	 			putcountryup = 1;
	 			// Uncomment for Web Pro
			 	//document.cartform2.COUNTRYCODE.value=default_country;
			 	r_countrytax[1].checked=true;
	 		}
	 		else {
		 		if (countryup == 1) {
			 		// initialize only if was set to yes
			 		// Uncomment for Web Pro
			 		//if (document.cartform2.COUNTRYCODE.value == default_country) {
			 		//	document.cartform2.COUNTRYCODE.value="";
		 			//}
			 		r_countrytax[1].checked=false;
			 		r_countrytax[0].checked=true;
		 		}
			 	countryup = 0;
			 	putcountryup = 0;		 		
		 	}

	 		if (putlocalup == 1) {
	   		insRow(dyntable, options2[0], options2[1]);
   		}
	 		if (putcountryup == 1) {
	   		insRow(dyntable, options3[0], options3[1]);
   		}
	   	insGtotalRow(dyntable, "Total", grandtotal);
	  }
	}
			
	function delRow(table, no){
		var noOfRows = table.rows.length;
		
		
		if (no == 1) {
			table.deleteRow(noOfRows-1);
		}
		else if (no == 2) {
			table.deleteRow(noOfRows-1);
			noOfRows = table.rows.length;
			table.deleteRow(noOfRows-1);
		}
		else if (no == 3) {
			table.deleteRow(noOfRows-1);
			noOfRows = table.rows.length;
			table.deleteRow(noOfRows-1);
			noOfRows = table.rows.length;
			table.deleteRow(noOfRows-1);			
		}		
		
	}	
	
	// Insert row into table.
	function insRow(table, desc, price)
	{
	
		// noOfRpws in table.
		noOfRows = table.rows.length;
		// no of columns of last row.
		noOfCols = table.rows[noOfRows-1].cells.length;
	
		// insert row at last.
		var x=table.insertRow(noOfRows);
	
		// insert cells in row.
		
	
		newCell = x.insertCell(0);
		// make cell editable feature.
		
		newCell.setAttribute("align", "right");
		newCell.setAttribute("valign", "top");
		// IE
		newCell.setAttribute("className", "summary");
		// Firefox
		newCell.setAttribute("class", "summary");
		newCell.setAttribute("colSpan", "4");
		newCell.innerHTML=desc;
		
		newCell = x.insertCell(1);
		newCell.setAttribute("align", "right");
		newCell.setAttribute("valign", "top");
		// IE
		newCell.setAttribute("className", "summary");
		// Firefox
		newCell.setAttribute("class", "summary");
		// make cell editable feature.
		newCell.innerHTML=currencySymbol.concat(price);
	}
	
	// Insert row into table.
	function insGtotalRow(table, desc, price)
	{
	
		// noOfRpws in table.
		noOfRows = table.rows.length;
		// no of columns of last row.
		noOfCols = table.rows[noOfRows-1].cells.length;
	
		// insert row at last.
		var x=table.insertRow(noOfRows);
	
		// insert cells in row.
		
	
		newCell = x.insertCell(0);
		// make cell editable feature.
		
		newCell.setAttribute("align", "right");
		newCell.setAttribute("valign", "top");
		// IE
		newCell.setAttribute("className", "summary");
		// Firefox
		newCell.setAttribute("class", "summary");
		newCell.setAttribute("colSpan", "4");
		newCell.innerHTML=desc;
		
		newCell = x.insertCell(1);
		newCell.setAttribute("align", "right");
		newCell.setAttribute("valign", "top");
		// IE
		newCell.setAttribute("className", "summary");
		newCell.setAttribute("id", "gtotal");
		// Firefox
		newCell.setAttribute("class", "summary");
		// make cell editable feature.
		newCell.innerHTML=currencySymbol.concat(price);
	}
	
	function deleteTableRows(table){
		var noOfRows = table.rows.length;
		
		for (var j = 1; j < noOfRows; j++) {
			noOfRowsNew = table.rows.length;
			//alert("noOfRowsNew="+noOfRowsNew);
			table.deleteRow(noOfRowsNew-1);
		}
	}
	
	
	function InitializeVars() {
		localup = document.cartform1.localup.value;
		countryup = document.cartform1.countryup.value;
		
		if (localup == 1) {
			v_localtax = default_local;
			update_localtax = 1;
			// Uncomment for Web Pro
		 	//document.cartform2.STATE.value=default_local;

		}
		if (countryup == 1) {
			v_country = default_country;
			update_country = 1;
			// Uncomment for Web Pro
		 	//document.cartform2.COUNTRYCODE.value=default_country;
		}
		
		
	}
		
	// Disable enter key for submission
	function stopRKey(evt) {
	  var evt = (evt) ? evt : ((event) ? event : null);
	  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
	}
	
	document.onkeypress = stopRKey;		
 	//-->
 	
 	
