// mini cart functions


function addToCart (part) {
	

	if(part){
		// do something to let the user know we're adding to the cart	
		addToCartInit();

		// see if there's a qty
		var qty = 1; 
		var qtyID = 'qty' + part;
		var qtyText = document.getElementById(qtyID); 
		if(qtyText) qty = qtyText.value; 
		encodedPart = escape(part);
		url = '/exec/shoppingCart.tsb?insertLine=' + encodedPart;
		if(qty) url = url + '&qty=' + qty; 
	}
	else {
		// if part isn't defined then we just want to do a refresh of the mini cart
		// we would do this on the main cart page when a qty is changed for example
		url = '/exec/shoppingCart.tsb?';

	}

	// add a variable to the url so it knows to return the minicart html
	url = url + '&mode=minicart';
	while( url.indexOf('+') > 0)
        {
                url  = url.substring(0,url.indexOf('+')) + '%2B'+ url.substring(url.indexOf('+') + 1);
        }


	var myHtmlReq;	
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
  		{
		myHtmlReq = new XMLHttpRequest();
		}
	// code for IE
	else if (window.ActiveXObject)
  		{
 	myHtmlReq = new ActiveXObject("Microsoft.XMLHTTP");
 	 }
	if (myHtmlReq==null)
  	{
 	alert("Your browser does not support XMLHTTP.")
  	}

	
	myHtmlReq.onreadystatechange = function(){
		if(myHtmlReq.readyState == 4){
			if(myHtmlReq.status == 200){
				document.getElementById('miniCartDiv').innerHTML = myHtmlReq.responseText;
				updateCartCookies();
				if(part) {
					// only do this if we added a part
					addToCartFinished();	
				}
			}
			else {
				// there was a problem
				addToCartServerProblem();
			}
		}
	}
	myHtmlReq.open("GET", url, true);
	myHtmlReq.send(null);


}

function updateCartCookies() {

	var myXMLReq;
	// code for Mozilla, etc.
	if (window.XMLHttpRequest)
  		{
		myXMLReq = new XMLHttpRequest();
		}
	// code for IE
	else if (window.ActiveXObject)
  		{
 	myXMLReq = new ActiveXObject("Microsoft.XMLHTTP");
 	 }
	if (myXMLReq==null)
  	{
 	alert("Your browser does not support XMLHTTP.")
  	}

	var url = "/exec/shoppingCart.tsb?updateCookie=1";
	
	myXMLReq.onreadystatechange = function(){
		if(myXMLReq.readyState == 4){
			if(myXMLReq.status == 200){
				eval(myXMLReq.responseText);
			}
		}
	}
	myXMLReq.open("GET", url, true);
	myXMLReq.send(null);


}

var cartState = "closed";
function addToCartInit(){
	/* function to let the user know we're adding to the cart */
	document.getElementById("cartContent").style.display = "none";
	document.getElementById("cartError").style.display = "none";
	document.getElementById("cartWait").style.display = "block";
	hideSelect();
	cartState = "open";
}

function addToCartFinished(){
	/* function to let the user know we're done adding */
	document.getElementById("cartContent").style.display = "block";
	document.getElementById("cartError").style.display = "none";
	document.getElementById("cartWait").style.display = "none";
	hideSelect();
	cartState = "open";
}

function addToCartServerProblem() {
	/* the server returned a status other than ok
	 do something to tell the user */
	document.getElementById("cartContent").style.display = "none";
	document.getElementById("cartError").style.display = "block";
	document.getElementById("cartWait").style.display = "none";
	hideSelect()
	cartState = "open";
}

function closeMiniCart(){
	document.getElementById("cartContent").style.display = "none";
	document.getElementById("cartError").style.display = "none";
	document.getElementById("cartWait").style.display = "none";
 	showSelect()
	cartState = "closed";
}

function openMiniCart(){
	document.getElementById("cartContent").style.display = "block";
	document.getElementById("cartError").style.display = "none";
	document.getElementById("cartWait").style.display = "none";
	hideSelect()
	cartState = "open";
}


function cartControl(){
	if(cartState == "open"){
		closeMiniCart();
	}
	else if(cartState == "closed"){
		openMiniCart();
	}
}

function hideSelect(){
	if(document.getElementById("sortByProduct")){
	document.getElementById("sortByProduct").style.visibility = "hidden";
	}
	if(document.getElementById("sortByContent")){
	document.getElementById("sortByContent").style.visibility = "hidden";
	}
}

function showSelect(){
	if(document.getElementById("sortByProduct")){
	document.getElementById("sortByProduct").style.visibility = "visible";
	}
	if(document.getElementById("sortByContent")){
	document.getElementById("sortByContent").style.visibility = "visible";
	}
}
