// JavaScript Document
function increase_divheight(conditions){
	var browserName=navigator.appName;
	if (browserName!="Microsoft Internet Explorer"){
		if(conditions==1){
			 document.getElementById("Layer2").style.height= document.getElementById("Layer2").clientHeight+262;	
		}else{
			document.getElementById("Layer2").style.height= document.getElementById("Layer2").clientHeight+39;	
		}
	}
}
function hideselections(){
	totalnos=document.getElementById("totalcounts").value;
	for(ii=0;ii<=totalnos;ii++){
		if(ii==0){ 
			tempvar2='prices';
		}
		else{ 
			tempvar2='prices'+ii;
		}
		document.getElementById(tempvar2).style.display='none';
	}
}
function hidecart(){
	document.getElementById('Layer2').style.visibility = 'hidden';
	document.getElementById('Layer2').innerHTML = '';
	document.getElementById('Layer2').style.height = 100;
	totalnos=document.getElementById("totalcounts").value;
	for(ii=0;ii<=totalnos;ii++){
		if(ii==0){ 
			tempvar2='prices';
		}
		else{ 
			tempvar2='prices'+ii;
		}
		document.getElementById(tempvar2).style.display='block';
	}
}
function showcart(){
	document.getElementById("Layer2").innerHTML="<table width='100%' padding='3' cellspacing='3' bgcolor='#2c487f' class='redtext1'><tr><td bgcolor='#e7eef6'><br><BR><br><center><img src='../../images/green_rot.gif'><br><BR>Please wait! Loading cart......</center><br><BR><br></TD></TR></TABLE>" ;
	document.getElementById('Layer2').style.visibility = 'visible';
	getvalues();
	hideselections();
}
function showcartadd(){
	window.scroll(0,150);
	document.getElementById('Layer2').style.visibility = 'visible';
	addvalues();
	hideselections();
}
   var request = null;

   function createRequest() {
     try {
       request = new XMLHttpRequest();
     } catch (trymicrosoft) {
       try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (othermicrosoft) {
         try {
           request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = null;
         }
       }
     }

     if (request == null)
       alert("Error creating request object!");
   }

   function getvalues(){
	 createRequest();
     var url = "../dynamic_cart.php";
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
  }

  function updatePage(){
    if (request.readyState == 4) {
		 var cashEl = document.getElementById("cash");
		 var cash = request.responseText;
		document.getElementById("Layer2").innerHTML=cash ;
		increase_divheight(1);
	}
  }
	function addvalues(){
		
		document.getElementById("Layer2").innerHTML="<table width='100%' padding='3' cellspacing='3' bgcolor='#2c487f' class='redtext1'><tr><td bgcolor='#e7eef6' align='center'><br><BR><BR><img src='../../images/green_rot.gif'><br><BR><center>Please Wait! Adding Items......</center><br><BR><br></TD></TR></TABLE>" ;
		totalnos=document.getElementById("totalcounts").value;
		sentvars="totalnos="+totalnos;
		
		for(ii=0;ii<=totalnos;ii++){
			if(ii==0){ 
				tempvar="qty";
				tempvar2='prices';
				tempvar3='prodname';
				tempvar4='category';
			}
			else{ 
				tempvar="qty"+ii; 
				tempvar2='prices'+ii;
				tempvar3='prodname'+ii;
				tempvar4='category'+ii;
			}
			sentvars=sentvars+'&'+tempvar+'='+document.getElementById(tempvar).value+'&'+tempvar2+'='+document.getElementById(tempvar2).value+'&'+tempvar3+'='+document.getElementById(tempvar3).value+'&'+tempvar4+'='+document.getElementById(tempvar4).value;
			document.getElementById(tempvar).value=0;
		}
		
		createRequest();
		var url = "../add_shoppingcart.php?"+sentvars;
		request.open("GET", url, true);
		request.onreadystatechange = updatePage2;
		request.send(null);
	
  	}
	function updatePage2(){
    if (request.readyState == 4) {
		var cashEl = document.getElementById("cash");
		var cash = request.responseText;
		document.getElementById("Layer2").innerHTML=cash ;
		document.getElementById("discartstatus").innerHTML="";
		increase_divheight(1);
		//select1.style.visibility="hidden";
    }
	}
	function showcartupdate(){
		document.getElementById("discartstatus").innerHTML="<center><img src='../../images/green_rot.gif'><BR>Updating Cart......</center></div>";
		totalnos=document.getElementById("viewtotcounts").value;
		sentvars="totalnos="+totalnos;
		for(ii=1;ii<totalnos;ii++){
				tempvar="viewqty"+ii; 
				tempvar2='viewprices'+ii;
				sentvars=sentvars+'&'+tempvar+'='+document.getElementById(tempvar).value+'&'+tempvar2+'='+document.getElementById(tempvar2).value;
		}
		createRequest();
		var url = "../update_shoppingcart.php?"+sentvars;
		request.open("GET", url, true);
		request.onreadystatechange = updatePage2;
		request.send(null);
  	}
	
	///ESTIMATE SHIPPING
	function estimateShipping(){
		var zipcode = document.getElementById("zipcode").value;
  		var country = document.getElementById("country").value;
  		var addType = document.getElementById("add_type").value;
		if(validate(zipcode)){
			if(zipcode != ""){
				//alert(country);
				increase_divheight(2);
				document.getElementById("ship_type_domestic").innerHTML="<BR><img src='../../images/green_rot.gif'><BR><font color='red'>Calculating Shipping Estimates......</font>";
				url = "../estimateshipping.php?zipcode=" + zipcode + "&addType=" + addType  +"&country=" + country;
				createRequest();
				request.open("GET", url, true);
				request.onreadystatechange = updatePage3;
				request.send(null);
			}
			else{
				alert("Please enter zip code");
			}
		}
	}
	function updatePage3(){
		if (request.readyState == 4) {
			  var cashEl = document.getElementById("cash");
			  var cash = request.responseText;
			  document.getElementById("ship_type_domestic").innerHTML=cash;
		}
	}
	
	///Send content to check out page
	function submitPage()
		{
		    var zipcode = document.getElementById("zipcode").value;
			var country = document.getElementById("country").value;
			if(zipcode == "")
			{
				alert("Please, enter zipcode!");
				document.getElementById("zipcode").focus();
				document.getElementById("zipcode").value="";
			}
			else
			{
				if(validate(zipcode))
				{
					var addType = "";
					if(zipcode != "")
					{
						addType = document.getElementById("add_type").value;
					}
					
					if ((document.getElementById("us_priority").checked==true)||(document.getElementById("us_priority2").checked==true)){
						var url = 'https://www.bestbottles.com/checkout.php?zipcode=' + zipcode + '&country=' + country +'&addType=' + addType + '&us_priority=Yes';
						//var url = '../../checkout.php?zipcode=' + zipcode + '&country=' + country +'&addType=' + addType + '&us_priority=Yes';
					}
					else{
						var url = 'https://www.bestbottles.com/checkout.php?zipcode=' + zipcode + '&country=' + country + '&addType=' + addType + '&us_priority=No';
						//var url = '../../checkout.php?zipcode=' + zipcode + '&country=' + country + '&addType=' + addType + '&us_priority=No';
					}
					window.location.href = url;
				}
			}	
		}


		function validate(zipcode) {

			var error_message = "" ;

			var form_okay =true ;

			var bad_zip = false ;

			var count = 0 ;

			if (zipcode != "" && !is_zip(zipcode))
			{
			  bad_zip = true ;
			  count++ ;
			}

			form_okay = (!bad_zip);



			if (bad_zip)
			{
				error_message = "Invalid Zip Code" ;
			}


			if (!form_okay) {

				window.alert(error_message) ;

			}

			return form_okay ;

		}



		function is_zip(zip) {

			len = zip.length ;
			if((document.getElementById("country").value=="United States")||(document.getElementById("country").value=="United States Minor Outlying Islands")){
				if (len < 5 || len > 5) { return false ; }
				for (i=0; i<len; i++) {
	
					if ((i == 5) && (zip.charAt(i) != '-')) { return false ; }
	
					else if ((i == 5) && (zip.charAt(i) == '-')) ;
	
					else if ((zip.charAt(i) < '0') || (zip.charAt(i) > '9')) { return false ; }
	
				}
			}
			return true ;

		}