var XmlHttp;

function CreateXmlHttp()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			XmlHttp = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp && typeof XMLHttpRequest != "undefined")
	{
		XmlHttp = new XMLHttpRequest();
	}
}

function WavelengthLookup()
{
	// URL to get states for a given client
	var requestURL = WavelengthLookupAjaxUrl;
	CreateXmlHttp();
	// If browser supports XMLHTTPRequest object
	if(XmlHttp)
	{
		//Setting the event handler for the response
		XmlHttp.onreadystatechange = HandleWavelengthLookup;

		//Initializes the request object with POST (METHOD of posting),
		//Request URL and sets the request as asynchronous.
		XmlHttp.open("POST", requestURL,  true);
			
		// send header for post
		XmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		
		//Sends the request to server
		XmlHttp.send("");
	}
}

function HandleWavelengthLookup()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp.status == 200)
		{
			if(XmlHttp.responseXML != null){
				DisplayWavelengthTypes(XmlHttp.responseXML.documentElement);
			}else{
				alert("There was a problem retrieving data from the server (responseXML == null)");
			}
		}
		else
		{
			alert("There was a problem retrieving data from the server (status = " + XmlHttp.status + ")");
		}
	}
}

function DisplayWavelengthTypes(xmlString)
{
	// display radio buttons for returned Temperature Control Types
	var wDiv = document.getElementById('wDiv');
	wDiv.appendChild(document.createTextNode("Loading Wavelength Ranges..."));
	
	var wavelengthNodes = xmlString.getElementsByTagName("wavelength");
	var rowcount = wavelengthNodes.length;
	var helpText = "";

	if (rowcount > 0)
	{
		wDiv.innerHTML = "";
		var tableWs = document.createElement("table");
		tableWs.className = "radioTable";
		var hasHelp = 0;
		helpText = "<br><b>Descriptions:</b><br>";
		
		for (i=0; i<rowcount; i++)
		{
			// insert new table
			var name = wavelengthNodes[i].getAttribute("range");
			var id = wavelengthNodes[i].getAttribute("wavelengthID");
			var hlpTxt = wavelengthNodes[i].getAttribute("helpText");
			if (hlpTxt != "")
			{
				helpText += name + ": <i>" + hlpTxt + "</i><br><br>";
				hasHelp++;
			}
			
			try {
				var radioButton = document.createElement("<input name='rdoWavelength' id='rdoWavelength' type='radio' value='"+ id +"' onClick='uncheckNextTabsRadios(1);' />");
			} catch (e) {
				var radioButton = document.createElement('input');
				radioButton.setAttribute('type', 'radio');
				radioButton.setAttribute('name', 'rdoWavelength');
				radioButton.setAttribute('id', 'rdoWavelength');
				radioButton.setAttribute('value', id);
				radioButton.setAttribute('onclick', "uncheckNextTabsRadios(1);");
			}
			wDiv.appendChild(tableWs);
			
			var newRow = tableWs.insertRow(i);
			var cell1 = newRow.insertCell(0);
			var cell2 = newRow.insertCell(1);
			cell1.width="10px";
			cell1.appendChild(radioButton);
			cell2.innerHTML = name;
		}
		if (hasHelp == 0)
		{
			helpText = "";
		}
		var newRow = tableWs.insertRow(rowcount);
		var cell1 = newRow.insertCell(0);
		var cell2 = newRow.insertCell(0);
		cell1.innerHTML = helpText;
		cell2.innerHTML = "";
	}
}