/*
 * create the requst object 
 */
function createXMLObj() {
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return xmlHttp;
}
/*
 * apply coupon code entered into the add cart callout
 */
function accApplyCoupon() {

	var xmlHttp = createXMLObj();
	var code = document.getElementById('couponcode').value;
	var url = "/js/ajax/add-cart-callout.php";
	var params = "couponcode="+code+"&action=applyCoupon";

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.send(params);
}
/*
 * cancel coupon code entered into the add cart callout
 */
function accCancelCoupon() {
	//get the XMLHttpRequest object
	var xmlHttp = createXMLObj();
	//path to the listener on the server
	var url = "/js/ajax/add-cart-callout.php";
	//params to send
	var params = "cancelCoupon=1";
	//build the POST request
	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	// define a function to capture the server response
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
		}
	}
	//send the request
	xmlHttp.send(params);
}
/*
 * increase quantity of the selected cart item by one
 */
function accAddOne(element, productid)
{
	var xmlHttp = createXMLObj();
	var url = "/js/ajax/add-cart-callout.php";

	document.getElementById(element).value++;

	var quant = document.getElementById(element).value;
	var params="productid="+productid+"&quant="+quant+"&action=addOne";

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.send(params);

}
/*
 * decrease quantity of the selected cart item by one
 */
function accSubtractOne(element, productid)
{
	var xmlHttp = createXMLObj();
	var url = "/js/ajax/add-cart-callout.php";

	document.getElementById(element).value--;

	var quant = document.getElementById(element).value;
	var params="productid="+productid+"&quant="+quant+"&action=subtractOne";

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.send(params);
}
/*
 * update callout itmes on text input change
 */
function accUpdateChange(productid, value){
	var xmlHttp = createXMLObj();
	var url = "/js/ajax/add-cart-callout.php";

	var params="productid="+productid+"&quant="+value+"&action=updateChange";

	xmlHttp.open("POST", url, true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
			document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.send(params);

}
/*
 * delete item from callout
 */
function accDeleteItem(productid)
{
	if(confirm("Are you sure you want to remove this item from your cart?")) {
		var xmlHttp = createXMLObj();
		var url = "/js/ajax/add-cart-callout.php";
		var params="productid="+productid+"&action=deleteItem";

		xmlHttp.open("POST", url, true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.setRequestHeader("Connection", "close");

		xmlHttp.onreadystatechange = function() {
			if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
				document.getElementById('calloutStub').innerHTML = xmlHttp.responseText;
			}
		}
		xmlHttp.send(params);
	}

}
/**
 * show the callout
 */
function showCallout() {
	document.getElementById("calloutStub").style.display = 'block';
}

/**
 * hide the callout
 */
function hideCallout() {
	document.getElementById("calloutStub").style.display = 'none';
}


