function getXMLHTTPRequest() {
try {
req = new XMLHttpRequest();
} catch(err1) {
  try {
  req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (err2) {
    try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (err3) {
      req = false;
    } 
  } 
}
return req;
}

var http = getXMLHTTPRequest();
  
function getServerText() 
{
	//The URL the code needs to be posted to
	var modurl="ajaxcode1.php";
	
	//If the url is static the page might reload from cache. To avoid that we load a dynamic value in the URL
	myRand = parseInt(Math.random()*999999999999999);
	http.open("POST", modurl, true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	//Following codee gets all the posted form variables
	var theForm = document.forms[0];
	//to decide if & should be put in the URL or not.
	var ampflag=0;
	//Final string to be sent
	var sendtxt="";
	sendtxt=sendtxt+"&price="+theForm.price.value+"&volume="+theForm.price.value+"&acc="+"&rand="+myRand+"&acc="+theForm.acc.value;
	//alert(sendtxt);
//Sending the parameters
	http.send(sendtxt);
	http.onreadystatechange = useHttpResponse;
	
	var w = theForm.price.selectedIndex;
	theForm.volume.value = theForm.price.options[w].value;

}

function useHttpResponse() 
{
   if (http.readyState == 4) 
   {
   		if(http.status == 200) 
		{ 
       		var mytext=http.responseText;
			posn=mytext.indexOf("-");
			var price=mytext.substring(posn+1);
			var size=mytext.substring(1, posn)
			
			document.getElementById('myPageElement').innerHTML="$"+price+" (Incl 10% GST)";
			document.theForm.hprice.value=price;
			document.theForm.volume.value=size;
			
    	}
  }
  else 
  {
		document.getElementById('myPageElement').innerHTML='<img src="loading.gif">';
  }

}


function IsNumeric1(strString)
   //  check for valid numeric strings	
{
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
	  }
   return blnResult;
}


function validate()
{
	if (document.theForm.quantity.value=="" || IsNumeric1(document.theForm.quantity.value)==false || document.theForm.quantity.value==0)
	{
		alert("Please enter a numerical value for quantity.");
		return false;
	}else if(document.theForm.hprice.value==0 || IsNumeric1(document.theForm.hprice.value)==false)
	{
		alert("Please select a can size.");
		return false;
	}else
	if(document.theForm.colour.value=="")
	{
		alert("Please enter a colour.");
		return false;
	}else
	if(document.theForm.manu.selectedIndex == 0)
	{
		if(document.theForm.colour.value.toLowerCase()!="white")
		{	alert("Please select a paint company");
			return false;
		}	
	}else
	if(document.theForm.colour.value=="" && theForm.code.value=="")
	{
		alert("Enter a colour code or a colour name.");
		return false;
	}
	return true;
}


