var XmlHttp6;

function CreateXmlHttp6()
{
	//Creating object of XMLHTTP in IE
	try
	{
		XmlHttp6 = new ActiveXObject("Msxml2.XMLHTTP");
	}
	catch(e)
	{
		try
		{
			XmlHttp6 = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(oc)
		{
			XmlHttp6 = null;
		}
	}
	//Creating object of XMLHTTP in Mozilla and Safari 
	if(!XmlHttp6 && typeof XMLHttpRequest != "undefined")
	{
		XmlHttp6 = new XMLHttpRequest();
	}
}

function SoftwareLookup()
{
	// URL to get states for a given client
	var requestURL = SoftwareLookupAjaxUrl;
	CreateXmlHttp6();
	// If browser supports XMLHTTPRequest object
	if(XmlHttp6)
	{
		//Setting the event handler for the response
		XmlHttp6.onreadystatechange = HandleSoftwareLookup;

		//Initializes the request object with POST (METHOD of posting),
		//Request URL and sets the request as asynchronous.
		XmlHttp6.open("POST", requestURL,  true);
			
		// send header for post
		XmlHttp6.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		
		//Sends the request to server
		XmlHttp6.send("");
	}
}

function HandleSoftwareLookup()
{
	// To make sure receiving response data from server is completed
	if(XmlHttp6.readyState == 4)
	{
		// To make sure valid response is received from the server, 200 means response received is OK
		if(XmlHttp6.status == 200)
		{
			if(XmlHttp6.responseXML != null){
				DisplaySoftwareTypes(XmlHttp6.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 = " + XmlHttp6.status + ")");
		}
	}
}

function DisplaySoftwareTypes(xmlString)
{
	// display radio buttons for returned Temperature Control Types
	var scDiv = document.getElementById('scDiv');
	scDiv.appendChild(document.createTextNode("Loading Software Methods..."));
	
	var softwareNodes = xmlString.getElementsByTagName("software");
	var rowcount = softwareNodes.length;
	var helpText = "";
	
	if (rowcount > 0)
	{
		scDiv.innerHTML = "";
		var tableSCs = document.createElement("table");
		tableSCs.className = "radioTable";
		var hasHelp = 0;
		helpText = "<br><b>Descriptions:</b><br>";
		
		for (i=0; i<rowcount; i++)
		{
			// insert new table
			var name = softwareNodes[i].getAttribute("softwareName");
			var id = softwareNodes[i].getAttribute("softwareID");
			var hlpTxt = softwareNodes[i].getAttribute("helpText");
			if (hlpTxt != "")
			{
				helpText += name + ": <i>" + hlpTxt + "</i><br><br>";
				hasHelp++;
			}
			
			try {
				var radioButton = document.createElement("<input name='rdoSoftwareTypes' id='rdoSoftwareTypes' type='radio' value='"+ id +"' />");
			} catch (e) {
				var radioButton = document.createElement('input');
				radioButton.setAttribute('type', 'radio');
				radioButton.setAttribute('name', 'rdoSoftwareTypes');
				radioButton.setAttribute('id', 'rdoSoftwareTypes');
				radioButton.setAttribute('value', id);
			}
			scDiv.appendChild(tableSCs);
			
			var newRow = tableSCs.insertRow(i);
			var cell1 = newRow.insertCell(0);
			var cell2 = newRow.insertCell(1);
			cell1.width="10px";
			cell1.appendChild(radioButton);
			cell2.innerHTML = name;
		}
		
		try {
			var radioButton = document.createElement("<input name='rdoSoftwareTypes' id='rdoSoftwareTypes' type='radio' value='0' />");
		} catch (e) {
			var radioButton = document.createElement('input');
			radioButton.setAttribute('type', 'radio');
			radioButton.setAttribute('name', 'rdoSoftwareTypes');
			radioButton.setAttribute('id', 'rdoSoftwareTypes');
			radioButton.setAttribute('value', '0');
		}
		scDiv.appendChild(tableSCs);
		
		var newRow = tableSCs.insertRow(i);
		var cell1 = newRow.insertCell(0);
		var cell2 = newRow.insertCell(1);
		cell1.width="10px";
		cell1.appendChild(radioButton);
		cell2.innerHTML = "No Software Required";
		
		/*if (hasHelp == 0)
		{
			helpText = "";
		}
		var newRow = tableSCs.insertRow(rowcount);
		var cell1 = newRow.insertCell(0);
		var cell2 = newRow.insertCell(0);
		cell1.innerHTML = helpText;
		cell2.innerHTML = "";*/
	}
}