var PHP_URL= "functions.php";

/*****************************************************************************\
* getAJAXHeader
\*****************************************************************************/
function getAJAXHeader()
{
	try
	{	//Firefox, Opera 8.0+, Safari
		xmlHTTP=new XMLHttpRequest();
	}
	catch(e)
	{
		try
		{	//Internet Explorer
			xmlHTTP=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)
		{
			try
			{
				xmlHTTP=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)
			{
				return null;
			}
		}
	}

	//We now have a XML HTTP Request Object
	return xmlHTTP;
}
/*****************************************************************************\
* ajaxRequest
\*****************************************************************************/
function ajaxRequest(sURL, onReadyStateChanged)
{
	xmlHTTP=getAJAXHeader();

	if(xmlHTTP != null)
	{          
		xmlHTTP.onreadystatechange=onReadyStateChanged;

		// Open and send the request to the server
		xmlHTTP.open("GET", sURL, true);
		xmlHTTP.send(null);
	}
	else
		alert("Your browser does not support AJAX!");
}
/*****************************************************************************\
* dataToArray
*
* Takes a string 'data1=value1,data2=value2' and puts it in to an array
\*****************************************************************************/
function dataToArray(sData)
{
	aValues=sData.split(",");	//Strtok equiv. Returns an array of strings;

	aVars = new Array();

	for(i=0; i<aValues.length; i++)
	{
		aTmp=aValues[i].split("=", 2);
		aVars[aTmp[0]]=aTmp[1];
	}

	return aVars;
}
/*****************************************************************************\
* isNumeric
\*****************************************************************************/
function isNumeric(sText)
{
	return isValidString(sText, "1234567890");
}
/*****************************************************************************\
* checkLength
\*****************************************************************************/
function checkLength(o, len)
{
	if(o.value.length > len)
		o.value=o.value.substr(0,len);
}
/*****************************************************************************\
* isValidString
\*****************************************************************************/
function isValidString(sText, sAllowedChars)
{
   var ValidChars = "0123456789";
   var IsValid=true;
   var Char;

   for (i = 0; i < sText.length && IsValid == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (sAllowedChars.indexOf(Char) == -1) 
	      IsValid = false;
   }
   return IsValid;
}

/*****************************************************************************\
* setProductFromType
\*****************************************************************************/
function setProductFromType()
{
	var type = document.getElementById("type");
	var typeVal = type.options[type.selectedIndex].value;

	if(typeVal == 0)
		return;

  //Set the selected text
  var setText = document.getElementById("typerange");
  setText.innerHTML = "<b style='color:#000033;'> Selecting from : &nbsp;"+type.options[type.selectedIndex].text+"</b>";
  
  //Send off the AJAX request
	sURL=PHP_URL+"?command=getProduct&type="+typeVal;
	ajaxRequest(sURL, getOS);
}

/*****************************************************************************\
* setProductFromRange
\*****************************************************************************/
function setProductFromRange()
{
	var type = document.getElementById("range");
	var typeVal = type.options[type.selectedIndex].value;

	if(typeVal == 0)
		return;

  //Set the selected text
  var setText = document.getElementById("typerange");
  setText.innerHTML = "<b style='color:#000033;'> Selecting from : &nbsp; "+type.options[type.selectedIndex].text+" range</b>";
  
  //Send off the AJAX request
	sURL=PHP_URL+"?command=getProduct&range="+typeVal;
	ajaxRequest(sURL, getProduct);
}

/*****************************************************************************\
* getProduct
\*****************************************************************************/
function getProduct()
{
	if(xmlHTTP.readyState == 4)
	{
		if(xmlHTTP.responseText.length <= 0)
			return;

		var o=document.getElementById("product");
		//alert(xmlHTTP.responseText);
		o.innerHTML=xmlHTTP.responseText;
	}
}

/*****************************************************************************\
* setProduct
\*****************************************************************************/
function setProduct()
{
	var product = document.getElementById("selectedproduct");
	var productVal = product.options[product.selectedIndex].value;

	if(productVal == 0)
		return;

  //Set the selected text
  var setText = document.getElementById("product");
  setText.innerHTML = "<b style='color:#000033;'> Selected product : &nbsp; "+product.options[product.selectedIndex].text+"</b>";
  
  //Send off the AJAX request
	sURL=PHP_URL+"?command=getOS&product="+productVal;
	ajaxRequest(sURL, getOS);
}

/*****************************************************************************\
* getOS
\*****************************************************************************/
function getOS()
{
	if(xmlHTTP.readyState == 4)
	{
		if(xmlHTTP.responseText.length <= 0)
			return;

		var o=document.getElementById("os");
		//alert(xmlHTTP.responseText);
		o.innerHTML=xmlHTTP.responseText;
	}
}

/*****************************************************************************\
* setOS
\*****************************************************************************/
function setOS()
{
	var os = document.getElementById("selectedos");
	var osVal = os.options[os.selectedIndex].value;

	if(osVal == 0)
		return;

  //Set the selected text
  var setText = document.getElementById("os");
  setText.innerHTML = "<b style='color:#000033;'> Selected OS : &nbsp; "+os.options[os.selectedIndex].text+"</b>";
  
  //Send off the AJAX request
	sURL=PHP_URL+"?command=getDownload&os="+osVal;
	ajaxRequest(sURL, getDownload);
}

/*****************************************************************************\
* getDownload
\*****************************************************************************/
function getDownload()
{
	if(xmlHTTP.readyState == 4)
	{

		if(xmlHTTP.responseText.length <= 0)
			return;

		var o=document.getElementById("download");
		//alert(xmlHTTP.responseText);
		o.innerHTML=xmlHTTP.responseText;
	}
}

