/*Query an ajax page and return the result with reload*/


//var targetElement = "";
var returnValue = null;
var callformward1 = null;
// creates an XMLHttpRequest instance
function getXMLHTTPRequest() 
{
	//store the reference to the XMLHttpRequest object
	var req;
	
	// try to instantiate the native XMLHttpRequest object
	try 
	{
		// create an XMLHttpRequest object
		req = new XMLHttpRequest();
	} 
	catch (err1) 
	{
		try 
		{
			req = ActiveXObject("Maxm12.XMLHttp");
		} 
		catch (err2) 
		{
			// assume IE6 or older
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHttp");
			} 
			catch (err3) 
			{
				req = false;
			}
		}
	}
	
	if (!req)
	{
    	alert("Error creating the XMLHttpRequest object.");
	}
	else
	{
		return req;
	}
}

//post method
function callAjax(simpleValue,callerId,target,callback,callforward,ajaxUrl)
{
	var targetElement = target;
	var sourceElement = callerId;
	var url = ajaxUrl;
	returnValue = null;
	callforward1 = callforward;
	params = "";
	
	//
	
	var http = getXMLHTTPRequest();
	
	if(document.getElementById(target+'_indicator'))
	{
		document.getElementById(target+'_indicator').style.display = '';
	}
		
	//	filter_countries_indicator
	
	//Determine if passed in a simple value or the ID of a form element and process as appropriate
	if(simpleValue == null || simpleValue == '')
	{	
		//document.getElementById('test').value=simpleValue;
		if(callerId != null)
		{
			var type = document.getElementById(callerId).type;//get the type of element that is calling this function
			
			//Determine the type of form element passing in values and read as appropriate
			if(type == "select-multiple")
			{
				//Loop through all selected items in multiselect list
				var first = 1;
						
				for (i=0; i < document.getElementById(callerId).options.length; i++)
				{
					if (document.getElementById(callerId).options[ i ].selected) 
					{
						if(first == 1)
						{
							params = "selected=";
						}
						else
						{
							params += ",";
						}
						params += document.getElementById(callerId).options[ i ].value;
						first = 0;
					}
				}
			}
			else if(type == "checkbox")
			{
				var checkLen = document.getElementsByName(callerId).length;
				var first = 1;
				
				if(checkLen > 0)
				{
					for (i = 0; i < checkLen; i++) 
					{
						if (document.getElementsByName(callerId)[i].checked) 
						{
							if(first == 1)
							{
								params = "selected=";
							}
							else
							{
								params += ",";
							}
							params += document.getElementsByName(callerId)[i].value;
							first = 0;
						}
					}
					if(first == 1)
					{
						params = "selected=0";
					}
				}
				else
				{
					if (document.getElementsByName(callerId).checked) 
					{
						params += document.getElementById(callerId).value;
					}
					else
					{
						params = "selected=0";
					}
				}
				
			}
			else
			{
				//document.getElementById("test").value = type;
				params = "selected="+document.getElementById(callerId).value;
				//document.getElementById('test').value = document.getElementById(callerId).value;
			}
		}
		else
		{
			alert("Error: You must provide a simple value or a valid id for a form element");	
		}
	}
	else
	{
		
		if(isArray(simpleValue) != false)
		{
			params = "selected=" + simpleValue;
		}
		else
		{
			for ( var i=0, len=simpleValue.length; i<len; i++ )
			{
				if(i ==0)
				{
					params = "selected" +i+"=" +simpleValue[i];
				}
				else
				{
					params = params + "&selected" +i+"=" +simpleValue[i];
				}
			}
		}
		//document.getElementById('test').value = simpleValue;
	}
	
	
	http.open("POST", url, true);	
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	var first = 0;
	http.onreadystatechange = function()
		{//Call a function when the state changes.
			if(http.readyState == 4 && http.status == 200) 
			{
 				removeWhitespace(http.responseXML);//required for IE/FF compatibility
				if(callback != null && callback != '')
				{
					returnValue = callback(http,sourceElement,targetElement);
				}
				if(first == 0 && callforward != null && callforward != '')
				{
					callforward();
					first = 1;
				}
				if(document.getElementById(target+'_indicator'))
				{
					document.getElementById(target+'_indicator').style.display = 'none';
				}
			}
		}
	
	http.send(params);//send parameters
}
		
//White space is seen as valid nodes in firefox thus remove from xml to ensure cross browser consistency when parsing xml
function removeWhitespace(xml) 
{
	var loopIndex;
	for (loopIndex = 0; loopIndex < xml.childNodes.length; loopIndex++) 
	{
		var currentNode = xml.childNodes[loopIndex];
		if (currentNode.nodeType == 1) 
		{
			removeWhitespace(currentNode);
		}
		if (((/^\s+$/.test(currentNode.nodeValue))) && (currentNode.nodeType == 3)) 
		{
			xml.removeChild(xml.childNodes[loopIndex--]);
		}
	}
}

//returns true is it is an array
function isArray(obj) 
{
	if (obj == Array)
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isDefined( variable)
{
	if(typeof(window[variable]) == 'undefined')
		return false;
	else
		return true;
}


//Old ajaxSelect function using get method
/*function ajaxSelect1(callingListValue, targetList1, ajaxUrl)
{
	targetList = targetList1
	var url = ajaxUrl + "&selected=" + callingListValue;
	http.open("GET", url, true);	
	http.onreadystatechange = ajaxCreateSelect1;
	http.send(null);
}function ajaxCreateSelect1()*/

<!--*********************************AJAX SELECT functions****************************************-->

//Fill a select box with values from xml file
function ajaxSelect1(reqobj,sourceElement,targetElement)
{
	var http=reqobj;
	var type = document.getElementById(targetElement).type;//get the type of element that is calling this function
	var selectBox = document.getElementById(targetElement);
	var list_container = http.responseXML.getElementsByTagName("list_container");
	var childLen = list_container[0].childNodes.length;
	var selected = -1;
	var returned = -1;
	//selectBox.options.length = 0; //does not remove optgroups
	selectBox.innerHTML = "";
	
	for(i=0; i<childLen; i++)
	{
		var nodeType = list_container[0].childNodes[i].nodeName;
		var thisNode = list_container[0].childNodes[i];
		
		if(nodeType == "list_group")
		{
			returned = addOptgroup(selectBox,thisNode);
		}
		else if(nodeType == "list_option")
		{
			//var node_name = thisNode.getElementsByTagName("option_value")[0].nodeName;
			//var option_value = thisNode.getElementsByTagName("option_value")[0].firstChild.nodeValue;
			//var node_name = thisNode.getElementsByTagName("option_name")[0].nodeName;
			//var option_name = thisNode.getElementsByTagName("option_name")[0].firstChild.nodeValue;
			//addOption(selectBox,option_value,option_name)
			returned = addOption(selectBox,thisNode);
		}
		if(selected == -1 && returned >=0 && type != "select-multiple")
		{
			selected = returned;
		}
	}
	if(type != "select-multiple")
	{
		selectBox.options.selectedIndex = 0;
	}
	return selected;
}

//Add a single option to the list
/*function addOption(selectbox,value,text)
{
	var option = document.createElement("OPTION");
	option.value = value;
	option.text = text;
	option.innerText = text;//For IE you also need to set inner text
	selectbox.appendChild(option);
}*/

function addOption(parent,xml)
{
	var option = document.createElement("OPTION");
	option.value = xml.getElementsByTagName("option_value")[0].firstChild.nodeValue;
	option.text = xml.getElementsByTagName("option_name")[0].firstChild.nodeValue;
	option.innerText = xml.getElementsByTagName("option_name")[0].firstChild.nodeValue;//For IE you also need to set inner text
	parent.appendChild(option);
	
	return xml.getElementsByTagName("option_value")[0].firstChild.nodeValue;
}	

//Add an option group and all its associated options
function addOptgroup(parent,xml)
{
	var returnValue = null;
	var i = 0;
	
	if(parent && xml)
	{
		var optgroup = document.createElement('OPTGROUP');
		var childLen = xml.getElementsByTagName("list_option").length;
		var this_value = 0;
		
		optgroup.label = xml.getElementsByTagName("group_name")[0].firstChild.nodeValue;
		
		for(i=0; i<childLen; i++)
		{	
			var thisNode = xml.getElementsByTagName("list_option")[i];
			//var nodeType = thisNode.nodeName;
			this_value = addOption(optgroup,thisNode);
	
			//var node_name = thisNode.getElementsByTagName("option_value")[0].nodeName;
			//var this_value = thisNode.getElementsByTagName("option_value")[0].firstChild.nodeValue;
			//option.value = this_value;
			//option.text = thisNode.getElementsByTagName("option_name")[0].firstChild.nodeValue;
			//option.innerText = thisNode.getElementsByTagName("option_name")[0].firstChild.nodeValue;//For IE you also need to set inner text
			//optgroup.appendChild(option);
			
			if(returnValue < 0 && this_value >= 0)
			{
				returnValue = this_value;	
			}
		}
		
		parent.appendChild(optgroup);
	}
	return returnValue;
}

<!-- ********************************************AJAX TABLE functions ********************************-->

function ajaxTable1(reqobj,sourceElement,targetElement)
{
	var http=reqobj;
	
	//get the table to add rows into
	var tbl     = document.getElementById(targetElement);
	var tblBody = document.createElement("tbody");
	
	var xmlTable = http.responseXML.getElementsByTagName("xml_table");
	var rowCount = xmlTable[0].childNodes.length;
	
	//deletes all rows of a table IE compatible
	while (tbl.rows.length>0) 
	{
		tbl.deleteRow(0); //delete first row of contracting table until there are none left
	}
	
	// create all rows
	for (var i = 0; i < rowCount; i++) 
	{
		var nodeType = xmlTable[0].childNodes[i].nodeName;
		var thisNode = xmlTable[0].childNodes[i];
		addTableRow(tblBody,thisNode);
	}
		
	// put the <tbody> in the <table>
	tbl.appendChild(tblBody);
		
	// sets the border attribute of tbl to 2;
	//tbl.setAttribute("border", "2");
}

// creates a table row
function addTableRow(parent,xml)
{
	var row = document.createElement("tr");
	var cellCount = xml.childNodes.length;
	
	//add cells
	for (var i = 0; i < cellCount; i++) 
	{
		var nodeType = xml.childNodes[i].nodeName;
		var thisNode = xml.childNodes[i];
		
		
		if(nodeType == "table_heading")
		{
			addTableHeading(row,thisNode);
		}
		else if(nodeType == "table_cell")
		{
			addTableCell(row,thisNode);
		}
	}	

		parent.appendChild(row);
	
}

//create a table heading
function addTableHeading(parent,xml)
{
	var cell_text = "_";
	if(xml.getElementsByTagName("cell_text")[0].hasChildNodes())
	{
		cell_text = xml.getElementsByTagName("cell_text")[0].firstChild.nodeValue;
	}
	var heading = document.createElement("th");
	var headingText = document.createTextNode(cell_text);
	heading.appendChild(headingText);
	//heading.innerHTML = headingText;
	parent.appendChild(heading);
}

//create a table cell
function addTableCell(parent,xml)
{
	var cell_text = "_";
	if(xml.getElementsByTagName("cell_text")[0].hasChildNodes())
	{
		cell_text = xml.getElementsByTagName("cell_text")[0].firstChild.nodeValue;
	}
	var cell = document.createElement("td");
	var cellText = document.createTextNode(cell_text);
	
	cell.appendChild(cellText);		
	//cell.innerHTML = 'test';
	parent.appendChild(cell);
}

<!---*****************************************************AJAX Suggest Functions**************************************--->

function ajaxSuggest1(reqobj,sourceElement,targetElement)
{
	var http=reqobj;
	var target = document.getElementById(targetElement);
	var list_container = http.responseXML.getElementsByTagName("list_container");
	var childLen = list_container[0].childNodes.length;
	
	target.innerHTML = '';
	
	for(i=0; i<childLen; i++)
	{
		var nodeType = list_container[0].childNodes[i].nodeName;
		var thisNode = list_container[0].childNodes[i];
		
		if(nodeType == "list_option")
		{
			//Build our element string.  This is cleaner using the DOM, but			
			//IE doesn't support dynamically added attributes.	
			
			var option_value = thisNode.getElementsByTagName("option_value")[0].firstChild.nodeValue;
			
			var suggest = '<div onmouseover="javascript:suggestOver(this);" ';			
			suggest += 'onmouseout="javascript:suggestOut(this);" ';			
			suggest += 'onclick="javascript:setSearch(\''+sourceElement+'\',this.innerHTML,\''+targetElement+'\');" ';			
			suggest += 'class="suggest_link">' + option_value + '</div>';
			target.innerHTML += suggest;	
		}
	}
}

//Mouse over function
function suggestOver(div_value) 
{	
	div_value.className = 'suggest_link_over';
}
//Mouse out function
function suggestOut(div_value) 
{	
	div_value.className = 'suggest_link';
}
function setSearch(sourceElement,value,target) 
{	
	document.getElementById(sourceElement).value = value.replace(/\&amp;/g,'&');
	document.getElementById(target).innerHTML = '';
}

