/**
 * @author J. Noah Keitel
 */
var xmlHttp

function savedCartUpdate(requested_action, userID, product_id, sizes, colors, quantities){
   if(!sizes || !colors || !quantities){
   		if(!sizes){
			document.getElementById('size').style.backgroundColor = "#FFd2d2";
		}else{
			document.getElementById('size').style.backgroundColor = "#FFFFFF";
		}
		if(!colors){
			document.getElementById('color').style.backgroundColor = "#FFd2d2";
		}else{
			document.getElementById('color').style.backgroundColor = "#FFFFFF";
		}
		if(!quantities){
			document.getElementById('quantity').style.backgroundColor = "#FFd2d2";
		}else{
			document.getElementById('quantity').style.backgroundColor = "#FFFFFF";
		}
   }else{
  	 	document.getElementById('size').style.backgroundColor = "#FFFFFF";
		document.getElementById('color').style.backgroundColor = "#FFFFFF";
		document.getElementById('quantity').style.backgroundColor = "#FFFFFF";
		
   		xmlHttp = GetXmlHttpObject();
    
	    if (xmlHttp == null) {
	        alert("Browser does not support HTTP Request");
	        return;
	    }
	    
	    var url = "shoppingCart_update.php";
	    url = url + "?uID=" + userID + "&action=" + requested_action + "&product_id=" + product_id + "&sizes=" + sizes + "&colors=" + colors + "&quantities=" + quantities;
	    url = url + "&sid=" + Math.random();
	    xmlHttp.onreadystatechange = stateChanged;
	    xmlHttp.open("GET", url, true);
	    xmlHttp.send(null);
   }
}

function savedCartClear(requested_action, userID){
   
   $emptycart_confirmation = confirm("Are you sure you want to empty your entire shopping cart?");
   
   if($emptycart_confirmation==true){
   	xmlHttp = GetXmlHttpObject();
    
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    var url = "shoppingCart_update.php";
    url = url + "?uID=" + userID + "&action=" + requested_action;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
   }
   
}

function savedCartRemoveProduct(requested_action, userID, productID){
    xmlHttp = GetXmlHttpObject();
    
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    var url = "shoppingCart_update.php";
    url = url + "?uID=" + userID + "&action=" + requested_action + "&pID=" + productID;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function savedCartRemoveInstanceOfProduct(requested_action, uID, productID, sizeID, colorID){
    xmlHttp = GetXmlHttpObject();
    
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    var url = "shoppingCart_update.php";
    url = url + "?uID=" + uID + "&action=" + requested_action + "&pID=" + productID + "&sID=" + sizeID + "&cID=" + colorID;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function savedCartDisplay(userID, requested_action, product_id, display){
    xmlHttp = GetXmlHttpObject();
    
    if (xmlHttp == null) {
        alert("Browser does not support HTTP Request");
        return;
    }
    
    var url = "shoppingCart_update.php";
    url = url + "?uID=" + userID + "&action=" + requested_action + "&pID=" + product_id + "&display=" + display;
    url = url + "&sid=" + Math.random();
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);
}

function showAddress(address_type){
	if(address_type=='default_shipping'){
		document.getElementById('saved_shipping_address').style.height='130px';
		document.getElementById('saved_shipping_address').style.visibility='visible';
		
		document.getElementById('new_shipping_address').style.height='0px';
		document.getElementById('new_shipping_address').style.visibility='hidden';
	}else if(address_type=='new_shipping'){
		document.getElementById('new_shipping_address').style.height='130px';
		document.getElementById('new_shipping_address').style.visibility='visible';
		
		document.getElementById('saved_shipping_address').style.height='0px';
		document.getElementById('saved_shipping_address').style.visibility='hidden';
	}else if(address_type=='default_billing'){
		document.getElementById('saved_billing_address').style.height='130px';
		document.getElementById('saved_billing_address').style.visibility='visible';
		
		document.getElementById('new_billing_address').style.height='0px';
		document.getElementById('new_billing_address').style.visibility='hidden';
	}else if(address_type=='new_billing'){
		document.getElementById('new_billing_address').style.height='130px';
		document.getElementById('new_billing_address').style.visibility='visible';
		
		document.getElementById('saved_billing_address').style.height='0px';
		document.getElementById('saved_billing_address').style.visibility='hidden';
	}
}


function showPaymentMethod(payment_type){
	if(payment_type=='credit_card'){
		document.getElementById('credit_card').style.height='160px';
		document.getElementById('credit_card').style.visibility='visible';
		
		document.getElementById('purchase_order').style.height='0px';
		document.getElementById('purchase_order').style.visibility='hidden';
		document.getElementById('check').style.height='0px';
		document.getElementById('check').style.visibility='hidden';
	}else if(payment_type=='check'){
		document.getElementById('check').style.height='160px';
		document.getElementById('check').style.visibility='visible';
		
		document.getElementById('credit_card').style.height='0px';
		document.getElementById('credit_card').style.visibility='hidden';
		document.getElementById('purchase_order').style.height='0px';
		document.getElementById('purchase_order').style.visibility='hidden';
	}else if(payment_type=='purchase_order'){
		document.getElementById('purchase_order').style.height='160px';
		document.getElementById('purchase_order').style.visibility='visible';
		
		document.getElementById('credit_card').style.height='0px';
		document.getElementById('credit_card').style.visibility='hidden';
		document.getElementById('check').style.height='0px';
		document.getElementById('check').style.visibility='hidden';
	}
}


function showTaxExempt(field_id){
	if(field_id=='default'){
		document.getElementById('tax_exempt_number_default').style.visibility='visible';
		
		document.getElementById('tax_exempt_number_new').style.visibility='hidden';
	}else if(field_id=='new'){
		document.getElementById('tax_exempt_number_new').style.visibility='visible';
		
		document.getElementById('tax_exempt_number_default').style.visibility='hidden';
	}
}

















function stateChanged(){
    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("shoppingcart_product_view").innerHTML = xmlHttp.responseText;
    }
}



function GetXmlHttpObject(){
    var xmlHttp = null;
    try {
        // Firefox, Opera 8.0+, Safari
        xmlHttp = new XMLHttpRequest();
    } 
    catch (e) {
        //Internet Explorer
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
    return xmlHttp;
}
