/* functions for validating shopping cart entries and updating the cart */
var request = null;
function checkout(form)
{
	if (isValid(form))
	{
		return true;
	}
	else
	{
		//alert("Please check for invalid entries.");
		return false;		
	} 		
}

function checkQuantity(id)
{
	//alert("validate id = " + id);
	var inputField = document.getElementById(id); 
	var value = inputField.value;
	if (value.length > 0 )
	{
		//not empty so check format
		if (validateRegEx( /^\d*$/,value,null,"Please enter a number (0-99)") == false)
		{
			alert("Please enter a number (0-99).");
			inputField.focus();
			return;
		}
	}
	else
	{
		value = 0;
	}
}

function validateQuantity(id,helpText)
{
	//alert("validate id = " + id);
	var inputField = document.getElementById(id); 
	var value = inputField.value;
	if (value.length > 0 )
	{
		//not empty so check format
		if (validateRegEx( /^\d*$/,value,helpText,"Please enter a number (0-99)") == false)
		{
			alert("Please enter a number (0-99). \n 0 will remove the item from the cart.");
			inputField.focus();
			return;
		}
	}
	else 
	{
		value = 0;
		updateQuantity(id);	
	}
}

function updateQuantity(id)
{
	var inputField = document.getElementById(id); 
	var quantity = inputField.value;
	//alert("updateQuantity = " + quantity);
	if (isNaN(quantity) == false)
	{
		if (quantity == 0 )
		{
			var form = document.forms["checkQuantity"+id];
			form.submit();
		}
		else 
		{
			createRequest();
			var url = "https://www.bedrechocolates.com/jsps/shoppingcart?updateQuantity&productID=" + id + "&quantity=" + quantity;
			request.open("POST",url,true);
			request.onreadystatechange = updatePage;
			request.send(null);	
		}
	}
	else
	  	alert("Please enter a number (0-99). \n 0 will remove the item from the cart.");
	
}

function validateRegEx(regex,inputStr, helpText, helpMessage)
{
	if (!regex.test(inputStr)) 
	{
		//the entry is invalid, so set the help message and return false
		if (helpText != null)
			helpText.innerHTML = helpMessage;
		return false;
	}
	else
	{
		//the entry is OK, so clear the help message and return true
		if(helpText != null)
			helpText.innerHTML = "";
		return true;
	}
}

function createRequest()
{
	if (window.XMLHttpRequest){
		try {
			request = new XMLHttpRequest();
		} catch(e) {
			request = null;
		}
	}
	//try IE version
	else if (window.ActiveXObject)
	{
		try {
			request = new ActiveXObject("Msxml12.XMLHTTP");
		} catch (e){
		//try older version of IE
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(e) {
				request = null;
			}
		}
	}
	
	if (request == null)
		alert("Error creating request");
}

function refresh()
{
	if ( request.readyState == 4 && request.status == 200 )
	{
		//alert("reload");
		document.location.reload();
	}
}

function addItem(id, cat, desc, price, units)
{
	alert("addItem " + id + ", " + cat + ", " + desc + ", " + price + ", " + units);
	createRequest();
	var quantityInput = document.getElementById(id);
	var quantity = quantityInput.value;
 	if (isNaN(quantity) == false)
 	{
		var url = "https://www.bedrechocolates.com/jsps/shoppingcart?addItem&productID=" + id
			+ "&categoryID=" + cat + "&description=" + desc + "&price=" + price + "&units=" + units + "&quantity= " + quantity;
		//alert("post to " + url);
		request.open("POST",url,true);
		request.onreadystatechange = function ()
		{
			if ( request.readyState == 4 && request.status == 200 )
			{
				var quantityID = "" + id;
				//alert(quantityID);
				//var resp = request.responseText;
				//quantity.value = resp;
			}
		}
	
		request.send(null);
	}	
}

function addToCart(id)
{	
	var form = document.getElementById("form" + id);
	var quantity = form.quantity.value; 
	//alert("addToCart " + quantity);
	if (isNaN(quantity) == false)
		form.submit();
	else
		alert("Please enter a number (0-99).");
	
}

function updatePage()
{
	if ( request.readyState == 4 && request.status == 200 )
	{
		//alert("Shopping cart updated");
		//document.location.reload();
		var resp = request.responseText;
		var cost = document.getElementById("cost");
		var numItems = document.getElementById("numItems");
		//var total = document.getElementById("total");
		//alert("Shopping cart updated: " + resp);
		var data = resp.split(",");
		var id = data[0];
		var subtotal = document.getElementById("subtotal" + id);
		//var handling = document.getElementById("handling");
		cost.innerHTML = data[1];
		numItems.innerHTML = data[2];
		//total.innerHTML = data[3];
		subtotal.innerHTML = data[4];
		//handling.innerHTML = data[5];
	}
	/*
	else 
	{
		alert("The update was not processed - please contact Bedre for assistance \n" +
				"State = " + request.readyState + ", Status = " + request.status);
	}
	*/
}

function checkSubtotal(subtotal)
{
	//alert("subtotal = " +subtotal);
	if (subtotal == 0)
	{
		alert("You must have items in the shopping cart before checking out");
		return false;
	}
	else
		return true;
}

function isValid(form)
{
	return true;
}

