﻿
function GetHttpObject() {var xmlhttp;if(window.ActiveXObject) {if(_XML_ActiveX) {xmlhttp = new ActiveXObject(_XML_ActiveX);} else {var versions = ["MSXML2.XMLHTTP", "Microsoft.XMLHTTP", "Msxml2.XMLHTTP.7.0", "Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.5.0", "Msxml2.XMLHTTP.4.0", "MSXML2.XMLHTTP.3.0"]; for (var i = 0; i < versions.length; i++) {try {xmlhttp = new ActiveXObject(versions[i]);if(xmlhttp) {var _XML_ActiveX = versions[i];break;}}catch (e) {}};}}if(!xmlhttp && typeof XMLHttpRequest != 'undefined') {try {xmlhttp = new XMLHttpRequest();} catch (e) {xmlhttp = false;}}return xmlhttp;}

function AsyncRequest(params, useInputs) {
	var xmlhttp = GetHttpObject();

	xmlhttp.open("POST", sitePath + "xmlHttp.aspx", true)
	xmlhttp.onreadystatechange = function() {
		ShowResults(xmlhttp)
	}

	var sendString = params + getQueryStrings();

	if(useInputs)
	{	
		var inputs = GatherInputs();
		if(document.forms[0].elements.length > 1){sendString += "&" + inputs.join('&');}
	}

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(sendString);
}

function ShowResults(xmlhttp){
	if(xmlhttp.readyState == 4) {
		if(xmlhttp.status == 200) {
			eval(xmlhttp.responseText);
		}
	}
}

function AsyncRequestMarker(params, marker) {
	var xmlhttp = GetHttpObject();

	xmlhttp.open("POST", sitePath + "xmlHttp.aspx", true)
	xmlhttp.onreadystatechange = function() {
		if(xmlhttp.readyState == 4) {
			if(xmlhttp.status == 200) {
				marker.openInfoWindowHtml(xmlhttp.responseText);
			}
		}
	}

	var sendString = params + getQueryStrings();

	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(sendString);
}

function getQueryStrings() {
	if(window.location != null && window.location.search.length > 1) {
		var urlParms = window.location.search.substring(1);
		if(urlParms != "")
			return "&" + urlParms;
		else
			return "";
	}
	else
		return "";
}

function GatherInputs()
{
	var inputs = new Array();

	for (var x = 1; x < eval("document.forms[0].elements.length"); x++){
		if(document.forms[0].elements[x].id.indexOf("__") < 0)
		{
			if(document.forms[0].elements[x].id != "__VIEWSTATE" && 
				document.forms[0].elements[x].type != "button")
			{
				var elValue = "";
				if(document.forms[0].elements[x].id != "")
				{
					if(document.forms[0].elements[x].type == "select-multiple")
					{
						var tempSelectedValues = new Array();
						for (var k=0; k<=document.forms[0].elements[x].options.length-1; k++)
						{
							if ( document.forms[0].elements[x].options[k].selected )
							{
								eval("tempSelectedValues.push(document.forms[0].elements[x].options[k].value)");
							}
						}
						eval("inputs.push(document.forms[0].elements[x].id+'='+escape(tempSelectedValues.join(',')))");
					}
					else if(document.forms[0].elements[x].type == "checkbox" ||
							document.forms[0].elements[x].type == "radio")
					{
						if(document.forms[0].elements[x].checked)
							eval("inputs.push(document.forms[0].elements[x].id+'=1')");
						else
							eval("inputs.push(document.forms[0].elements[x].id+'=0')");
					}
					else
					{
						elValue = escape(document.forms[0].elements[x].value);
						elValue = replaceAll(elValue, '&', '%26'); elValue = replaceAll(elValue, '+', '%2b');
						eval("inputs.push(document.forms[0].elements[x].id + '=' + elValue)");
					}
				}
			}
		}
	}

	return inputs;
}

function replaceAll(repl, what, withVal)
{
	var indx = repl.indexOf(what);
	while (indx > -1) {repl = repl.replace(what, withVal); indx = repl.indexOf(what);}
	return repl;
}
