function updatecart(productid){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
		var divid = "cart" + productid;
			var ajaxDisplay = document.getElementById(divid);
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		//if zip form has a zip, calculate the shipping
var destzip = document.getElementById('destzipcode').value.length;
var shippingQ = document.getElementById('shippingQ').value;
if (destzip == 5 && shippingQ == "Yes") {
	calcshipping();
}



//recalc planting trees and cart total
		planttrees('no');
		carttotal();
		}
	}
	
	var quantityid = "qnt" + productid;
	var quantity = document.getElementById(quantityid).value;
	
	var queryString = "?quantity=" + quantity + "&productid=" + productid;
	ajaxRequest.open("GET", "scripts/updatecart.php" + queryString, true);
	ajaxRequest.send(null); 
}
function shippingtoggle(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('shippingtotaldiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;
		//if zip form has a zip, calculate the shipping
var destzip = document.getElementById('destzipcode').value.length;
var shippingQ = document.getElementById('shippingQ').value;
if (destzip == 5 && shippingQ == "Yes") {
	calcshipping();
}

//recalc cart total
		carttotal();
		}
	}
	
	var shippingQ = document.getElementById('shippingQ').value;
	
	var queryString = "?shippingQ=" + shippingQ;
	ajaxRequest.open("GET", "scripts/shippingq.php" + queryString, true);
	ajaxRequest.send(null); 
	
	//show zip code if yes
	var zipcodediv = document.getElementById('zipcodeform');
	var zipcalcdiv = document.getElementById('zipcalcdiv');
	var shippingnodiv = document.getElementById('shippingno');
	var preferedshipping = document.getElementById('preferedshippingdiv');
	var preferedpickup = document.getElementById('preferedpickupdiv');
if(shippingQ == "Yes"){
          zipcodediv.style.display = 'block';
		  zipcalcdiv.style.display = 'block';
		  shippingnodiv.style.display = 'none';
		  preferedshipping.style.display = 'block';
		  preferedpickup.style.display = 'none';
      } else {
          zipcodediv.style.display = 'none';
		  zipcalcdiv.style.display = 'none';
		  shippingnodiv.style.display = 'block';
		  preferedshipping.style.display = 'none';
		  preferedpickup.style.display = 'block';
    }//done with zip code toggle
}
function showselectdate() {
	var shipdatebox = document.getElementById('shipdatediv');
 if(shipdatebox.style.display == 'block'){
          shipdatebox.style.display = 'none';
      } else {
          shipdatebox.style.display = 'block';
    }

}
function planttrees(changeit){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('planttreestotal');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;

		carttotal();
		}
	}
	
	var plant = document.getElementById('plant').value;
	
	var queryString = "?plant=" + plant + "&change=" + changeit;
	ajaxRequest.open("GET", "scripts/planttrees.php" + queryString, true);
	ajaxRequest.send(null); 
}
function carttotal(){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var ajaxDisplay = document.getElementById('totaldiv');
			ajaxDisplay.innerHTML = ajaxRequest.responseText;

		}
	}
	
	ajaxRequest.open("GET", "scripts/carttotal.php", true);
	ajaxRequest.send(null); 
}
function validatecart() {
	var shippingQ = document.getElementById('shippingQ').value;
	if (shippingQ == "No") {
	var answer = confirm("Are you sure you do not want us to ship your trees?  You must pick them up in Encinitas.")
	if (answer){
		return true;
	} else {
	return false;
	}
	}
		var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
		
	//insert ship date info
	var shipdateselected = document.getElementById('shipdateselect').value;
	var shipdate = document.getElementById('shipdatevalue').value;
	if (shipdateselected == "ASAP" || shipdate == "") {
		var shipdate = "ASAP";
	}
	
	var queryString = "?shipdate=" + shipdate;
	ajaxRequest.open("GET", "scripts/addshipdate.php" + queryString, true);
	ajaxRequest.send(null); 
	
	
	//if shipping yes, is miles greater than zero?
	var shippingmiles = document.getElementById('shippingmiles').value;
if (shippingmiles == 1 && shippingQ == "Yes") { //This checks to make sure the field is not empty
   alert("You must calculate your shipping by entering a zip code and clicking the Calc Shipping button.");
   return false; //This prevents the form from being submitted
   } else {
   return true;
   }
   
   
   

}
