//Browser Support Code
function update_cart_display(){
	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){
			alert(ajaxRequest.responseText);
		}
	}
	ajaxRequest.open("GET", "/store/ajax/update-cart-display.php", true);
	ajaxRequest.send(null); 
}



//Browser Support Code
function set_int_shipping(val,state){
	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){
			//alert(ajaxRequest.responseText);
		}
	}
	ajaxRequest.open("GET", "/store/ajax/get_shipping.php?val="+val+"&state="+state, true);
	ajaxRequest.send(null); 
}



function get_new_total(){
	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){
			document.getElementById("total_amount").value=ajaxRequest.responseText;
			//alert(document.getElementById("total_amount").value);
		}
	}
	ajaxRequest.open("GET", "/store/ajax/get-cart-total.php", true);
	ajaxRequest.send(null); 
}


function addAjax()
{
	var myajax9=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax9.readyState == 4)
	{ //if request of file completed
		if (myajax9.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			if (myfiletype=="txt")
			{
				document.getElementById("cart_count").innerHTML = myajax9.responseText;
			
					///javascript:location.reload(true);
					
			}else{
				document.getElementById("cart_count").innerHTML = myajax9.responseXML;
			}
		}
	}
}
function processGetPost()
{
	var myajax1=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax1.readyState == 4)
	{ //if request of file completed
		if (myajax1.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			if (myfiletype=="txt")
			{
				document.getElementById("cart_count").innerHTML = myajax1.responseText;
			
					///javascript:location.reload(true);
					
			}else{
				document.getElementById("cart_count").innerHTML = myajax1.responseXML;
			}
		}
	}
}
function dupeProcessGetPost()
{
	var myajax3=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax3.readyState == 4)
	{ //if request of file completed
		if (myajax3.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			if (myfiletype=="txt")
			{
				document.getElementById("cart_count").innerHTML = myajax3.responseText;
			
					///javascript:location.reload(true);
					
			}else{
				document.getElementById("cart_count").innerHTML = myajax3.responseXML;
			}
		}
	}
}
function removeThruAjax()
{
	var myajax2=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax2.readyState == 4)
	{ //if request of file completed
		if (myajax2.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			if (myfiletype=="txt")
			{
				document.getElementById("cart_count").innerHTML = myajax2.responseText;
			
					javascript:location.reload(true);
					
			}else{
				document.getElementById("cart_count").innerHTML = myajax2.responseXML;
			}
		}
	}
}
function cart_prices()
{
	var myajax5=ajaxpack.ajaxobj
	var myfiletype=ajaxpack.filetype
	if (myajax5.readyState == 4)
	{ //if request of file completed
		if (myajax5.status==200 || window.location.href.indexOf("http")==-1)
		{ 
			if (myfiletype=="txt")
			{
				document.getElementById("total_amount").value=myajax5.responseText;
				alert(document.getElementById("total_amount").value);
				
			}else{
				alert(myajax5.responseText);
			}
		}
	}
}
function add(q,id)
{
	ajaxpack.getAjaxRequest("/store/ajax/add-to-cart.php", "q="+q+"&P_ID="+id, addAjax, "txt");
}
function addclothing(q,id)
{
	var size=document.getElementById(id+"_size").value;
	ajaxpack.getAjaxRequest("/store/ajax/add-to-cart.php", "q="+q+"&P_ID="+id+"&size="+size, addAjax, "txt");
}
function remove(id,j_ids)
{
	alert(id);
	ajaxpack.getAjaxRequest("/store/ajax/remove-cart-item.php", "q=1&P_ID="+id, removeThruAjax, "txt");
}
function init_cart()
{
	ajaxpack.getAjaxRequest("/store/ajax/check-cart-qty.php", "", dupeProcessGetPost, "txt");

}
function calc_hand(state)
{
	ajaxpack.getAjaxRequest("/store/ajax/cali-hand.php", "state="+state, processGetPost, "txt");

}
function adjust_size_value(id)
{
	var selected_size=document.getElementById("select_"+id).value;
	document.getElementById(id+"_size").value = selected_size;
}
/*function update_cart_display()
{
	ajaxpack.getAjaxRequest("/store/ajax/update-cart-display.php", "q=1", cart_prices, "txt");

}*/
var called =false;
function checkstatetax()
{
		if(document.getElementById("s_state").selectedIndex == 5 && document.getElementById("s_country_code").selectedIndex == 0)
		{ called =true;
			alert("An additional 8.25% tax will be added for all shipments to California State... Sorry, its the law." );
			calc_hand("CA");
			update_cart_display();
			get_new_total();
			
		}else{
			
			calc_hand("AnyOtherState");
			if(called){update_cart_display();}
			called =false;
			get_new_total();
		}
		
		
}
var int_shipping=false;
function check_for_international(){
	if(document.getElementById("s_state").selectedIndex == 5)
	{
		var state="cali";
	}else{
		var state="other";
	}
	if(document.getElementById("s_country_code").selectedIndex !== 0)
	{
		alert("An additional $6.99 will be added for international shipping");// tell the user were about to add 7 bux
		set_int_shipping("international",state);//set $cart->ship to 6.99
		int_shipping = true;
		get_new_total(); // set total value in hidden HTML field
		update_cart_display();//tell the user what just happned 
	}else{
		set_int_shipping("domestic",state);
		int_shipping = false;
		get_new_total();
		update_cart_display();
	}
}
