﻿// JScript File

var xmlHttp;

function initAjaxRequest()
{
	try
	{  // Firefox, Opera 8.0+, Safari  

		xmlHttp=new XMLHttpRequest();  
		
	}
	catch (e)
	{  // Internet Explorer  
		try
		{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  			  
		}
		catch (e)
		{    
			try
			{      
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");   
			}
			catch (e)
			{         
			}    
		}  
	} 
} 

function executeRequest(url)
{
	if (xmlHttp)
	{
		xmlHttp.open("GET", url, true);
		xmlHttp.send(null);
	}
}

function changeDropDown(url, id, parameterName, parameterValue, loadingText)
{
	/*
		Parameters
		
		url = the basic url of the request, minus any parameters
		id = the id of the dropdown to be populated
		parameterName = the name of the parameter to append to the url
		parameterValue = the value of the parameter to append to the url
		otherDropDownID = the id of the dropdown that this dropdown depends on
	*/
	initAjaxRequest();
	
	if (xmlHttp)
	{
		xmlHttp.onreadystatechange = function() 
		{
			populateDropDown(id); // populate the drop down when the request is complete
		};
		
		var fullUrl = url + "?" + parameterName + "=" + parameterValue
		var clearDropDown = false;
		var dropDown = getElement(id);
		var dropDownValue = "0";
		
		if (dropDown)
		{
			dropDownValue = dropDown.options[dropDown.selectedIndex].value
			if (dropDownValue != "0")
				fullUrl = fullUrl + "&SelectedOtherValue=" + dropDownValue;
				
			if (dropDownValue == "0") // we haven't selected a different value in the other drop down
			{			
				dropDown.options.length = 0; // clear current elements
				
				var option = document.createElement("option"); // create new option element
					
				option.text = loadingText; // loading text element
				option.value = "0"; // value element
				
				try
				{
					dropDown.add(option, null);
				}
				catch (ex)
				{
					dropDown.add(option);
				}
			}
		}
		
		setTimeout("executeRequest('" + fullUrl + "')", 100); // allow slight delay to allow the loading text to be displayed before the request is executed
	}
}

function populateDropDown(dropDownID)
{
	/*
		The xml returned by the script must be in the format
		
		<?xml version="1.0" encoding="utf-8" ?>
		<documentElement>
			<option>
				<text>Option 1</text>
				<value>1</value>
				<selected>false</selected>
			</option>
			<option>
				<text>Option 2</text>
				<value>2</value>
				<selected>true</selected>
			</option>
			..
		</documentElement>
		
		The name of the documentElement can be anything
	*/
	if (xmlHttp.readyState == 4)
	{
		var dropDown = getElement(dropDownID);
		if (dropDown)
		{
			var xmlDoc = xmlHttp.responseXML.documentElement;	// xml doc root element				
			var options = xmlDoc.getElementsByTagName("option"); // get the options
			dropDown.options.length = 0; // clear current elements
					
			var alreadySelected = false;
			
			
			for (i = 0; i < options.length; i++)
			{
				var option = document.createElement("option"); // create new option element
				
				var text = options[i].getElementsByTagName("text");
				var value = options[i].getElementsByTagName("value");
				var selected = options[i].getElementsByTagName("selected");
				
				/*if (i < 5)
				{
					alert(text.item(0).text);
					alert(value.item[0].text);
					alert(selected.item[0].text);
				}*/
				
				if (text.item(0).textContent)
					option.text = text.item(0).textContent; // text element
				else
					option.text = text.item(0).text;
					
				if (value.item(0).textContent)
					option.value = value.item(0).textContent; // value element
				else
					option.value = value.item(0).text;
				
				// determine if the element is to be selected, and that one has not already been selected
				var selectedText;
				if (selected.item(0).textContent)
					selectedText = selected.item(0).textContent;
				else
					selectedText = selected.item(0).text;
				
				if (selectedText == "true" && !alreadySelected)
				{
					option.selected = true;
					alreadySelected = true;
				}
				else
					option.selected = false;
				
				try
				{
					dropDown.add(option, null);
				}
				catch (ex)
				{
					dropDown.add(option);
				}
			}
		}
	}
}

function loadCategoryHelp(helpTopicID, idShort, idFull) {
    initAjaxRequest();

    var url = "AjaxScripts/HelpTopics.aspx?Operation=GetFullHelpContentForHelpTopicID&HelpTopicID=" + helpTopicID;


    xmlHttp.onreadystatechange = function() {
        setCategoryHelp(idShort, idFull); 
    };
    
    executeRequest(url);
} 

function setCategoryHelp(idShort, idFull) {
    if (xmlHttp.readyState == 4) {
        var divShort = document.getElementById(idShort);
        var divFull = document.getElementById(idFull);

        divFull.innerHTML = xmlHttp.responseText;
        divShort.style.visibility = 'hidden';
        divShort.style.display = 'none';
    }
}
