function ValidateForm(strForm)
{
	var x;
	var alertmsg;
	x=0;
	alertmsg = "The following fields are required: \n";
	if(strForm=="contactform")
	{
		if (document.getElementById("ename").value==""){alertmsg = alertmsg + "\nName";x = x + 1;}
		if (document.getElementById("eemail").value==""){alertmsg = alertmsg + "\nEmail";x = x + 1;}
		if (document.getElementById("esubject").value==""){alertmsg = alertmsg + "\nSubject";x = x + 1;}
		if (document.getElementById("ebody").value==""){alertmsg = alertmsg + "\nBody";x = x + 1;}
	}
	if(x!=0){alert(alertmsg);return false;}
}

function AddToCart(strZip)
{
	if(!IsInteger(document.getElementById("quantity").value))
	{
		alert("Quantity must be numeric");
	}
	else
	{
		if(strZip=="")
		{
			alert("You must enter your shipping zip code");
		}
		else
		{
			if(parseFloat(document.getElementById("amount").value) > 0)
			{
				if(document.getElementById("freeshipping").value != "1")
				{
					if(parseFloat(document.getElementById("weight").value) > 0)
					{
						if(parseFloat(document.getElementById("weight").value) < 150)
						{
							GetShipping(strZip);
						}
						else
						{
							alert("Shipping Weight must be less than 150 lbs.");
						}
					}
					else
					{
						alert("Shipping Weight must be greater than 0 lbs.");
					}
				}
				else
				{
					document.getElementById("shipping").value = "0";
					SubmitForm("paypalform");
				}
			}
			else
			{
				alert("Price must be greater than 0.");
			}
		}
	}
}

function GetShipping(strZip)
{
	var date_now = new Date()
	var strWeight = document.getElementById("weight").value;
	var strQty = document.getElementById("quantity").value;
	var xmlUrl = "include/ups.asp?weight="+strWeight+"&zip="+strZip+"&qty="+strQty+"&dtnow="+date_now;
	createXMLHttpRequest();
	xmlHttp.onreadystatechange = HandleStateChange;
	
	xmlHttp.open("GET",xmlUrl,true);
	xmlHttp.send(null);
}

function HandleStateChange()
{
	if(xmlHttp.readyState==4)
	{
		if(xmlHttp.status==200)
		{
			GetRate();
		}
	}
}

function GetRate()
{
	var results = xmlHttp.responseXML;
	var iItemValue = "";
	var iItems = results.getElementsByTagName("rate");
	iItemValue = iItems[0].getElementsByTagName("value")[0].firstChild.nodeValue;
	//alert(iItemValue);
	document.getElementById("shipping").value = iItemValue;
	if(document.getElementById("shipping").value != "")
	{
		SubmitForm("paypalform");
	}
	else
	{
		alert("Empty!");
	}	
}

function SubmitForm(strForm)
{
	document.getElementById(strForm).submit();
}

function createXMLHttpRequest()
{
	if(window.ActiveXObject){xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}else if (window.XMLHttpRequest){xmlHttp = new XMLHttpRequest();}
}

function IsInteger(strString)
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

function IsNumber(strString)
{
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

function CalculateCustom()
{
	var cLength = document.getElementById("length").value;
	var cWidth = document.getElementById("width").value;
	var cHeight = document.getElementById("height").value;
	var cPrice = document.getElementById("orgamount").value;
	var cWeight = document.getElementById("orgweight").value;
	
	if(!IsNumber(cLength) || !IsNumber(cWidth) || !IsNumber(cHeight))
	{
		alert("Dimensions must be numeric.");
	}
	else
	{
		if(cLength > 108 || cWidth > 108 || cHeight > 108)
		{
			if(cLength > 108)
			{
				alert("We cannot ship items longer than 108 inches");
				document.getElementById("length").value = 0;
				document.getElementById("length").focus();			
			}
			if(cWidth > 108) 
			{
				alert("We cannot ship items wider than 108 inches");
				document.getElementById("width").value = 0;
				document.getElementById("width").focus();
				
			}
			if(cHeight > 108) 
			{
				alert("We cannot ship items taller than 108 inches");
				document.getElementById("height").value = 0;
				document.getElementById("height").focus();
				
			}
		}
		else
		{
			var brdFoot = ((cLength * cWidth * cHeight)/144);
			var amount = brdFoot * cPrice;
			var weight = brdFoot * cWeight;
			if(weight < 1)
			{
				weight = 1;
			}
			//alert(weight.toFixed(2));
			document.getElementById("weight").value =  weight.toFixed(2);
			document.getElementById("amount").value = amount.toFixed(2);
			document.getElementById("total").value = amount.toFixed(2);
			document.getElementById("os0").value = cLength + " x " + cWidth + " x " + cHeight;
		}
	}	
}