var url = '../includesdotNET/XmlData.aspx';
var isWorking = false;
var http = getHTTPObject();
var geocoder = null;
var googleMapKey = "";

ajax_init();

function ajax_init()
{
}

function includeJS(jsURL)
{
  document.write('<script language="Javascript" src="' + jsURL + '"></script>'); 
}

function ajax_getValue(xmlTag, parameters, xmlURL, isAsynchronous) {
	var retVal
	if (xmlURL.length == 0){xmlURL = url;}
	if (isAsynchronous.length == 0){isAsynchronous = true;}
	if (!isWorking && http) {
		http.open("GET", xmlURL + parameters, isAsynchronous);
		http.onreadystatechange = function(){retVal = ajax_loadValue(xmlTag);};
		isWorking = true;
		http.send(null);
	}
	return retVal
}

function ajax_initTextBox(textbox, xmlTag, parameters, xmlURL, isAsynchronous) {
	if (xmlURL.length == 0){xmlURL = url;}
	if (isAsynchronous.length == 0){isAsynchronous = true;}
	if (!isWorking && http) {
		http.open("GET", xmlURL + parameters, isAsynchronous);
		http.onreadystatechange = function(){ajax_loadTextBox(textbox, xmlTag);};
		isWorking = true;
		http.send(null);
	}
}

function ajax_initDropDown(dropdown, id_tag, name_tag, parameters, xmlURL, isAsynchronous ) 
{
	if (xmlURL.length == 0){xmlURL = url;}
	if (isAsynchronous.length == 0){isAsynchronous = true;}
	if (!isWorking && http) 
	{
		http.open("GET", xmlURL + parameters, isAsynchronous);
		http.onreadystatechange = function(){ajax_loadDropDown(dropdown, id_tag, name_tag);};
		isWorking = true;
		
		http.send(null);
	}
}

function ajax_loadValue(xmlTag) {
	var retVal;
	if(http.readyState == 4){
		if(http.status == 200) {
			var xmlDoc = http.responseXML;
			if(xmlDoc.documentElement != null) {
				var root = xmlDoc.getElementsByTagName(xmlDoc.documentElement.nodeName)[0];
				if(root != null) {
					if(root.childNodes.length > 0){
						if(root.getElementsByTagName(xmlTag)[0].firstChild.nodeValue.length > 0){
							retVal = root.getElementsByTagName(xmlTag)[0].firstChild.nodeValue;
						}else{ //if the root is a collection
							retVal = root.childNodes[0].getElementsByTagName(xmlTag)[0].firstChild.nodeValue;
						}
					}
					isWorking = false;
				}
			}
		}
	}
	return retVal
}

function ajax_loadTextBox(textbox, xmlTag) {
	textbox.length = 0;
	textbox.value = " loading... ";
	if(http.readyState == 4){
		textbox.length = 0;
		if(http.status == 200) {
			var xmlDoc = http.responseXML;
			if(xmlDoc.documentElement != null) {
				var root = xmlDoc.getElementsByTagName(xmlDoc.documentElement.nodeName)[0];
				if(root != null) {
					var text = root.childNodes[0].getElementsByTagName(xmlTag)[0].firstChild.nodeValue;
					textbox.value = text;
					isWorking = false;
				}
			}
		}
	}
}

function ajax_loadDropDown(dropdown, id_tag, name_tag) 
{
	dropdown.length = 0;
	dropdown.options[0] = new Option(' loading... ', '');

	if(http.readyState == 4)
	{
		dropdown.length = 0;
		dropdown.options[0] = new Option('Please Select','');
		dropdown.selectedIndex = 0;		
		if(http.status == 200) 
		{
			var xmlDoc = http.responseXML;
			if(xmlDoc.documentElement != null) 
			{
				var root = xmlDoc.getElementsByTagName(xmlDoc.documentElement.nodeName)[0];
				if(root != null) 
				{
					for(var i = 0; i < root.childNodes.length; i++)
					{
						var id = root.childNodes[i].getElementsByTagName(id_tag)[0].firstChild.nodeValue;
						var name = '';
						var tag = '';
						
						for(var a = 0; a <= name_tag.length - 1; a++)
						{
							tag = root.childNodes[i].getElementsByTagName(name_tag[a])[0].firstChild;
							if(tag != null)
							{
								if(a > 0 && a < name_tag.length)
								{
									name += ' - ';
								}
								name += tag.nodeValue;
							}
						}	
						
						dropdown.options[i+1] = new Option(name,id);
					} // for i 
						
					//isWorking = false;
				} // root 
			} // xmlDoc.documentElement
			
			 
		} // http.status
		// BB moving this out here... once ready state reaches 4, we're not working any more... 
		// before isWorking was not being set to false if no models were returned for a make and subsequent calls would short circuit.... (MannyCode hehehehehe) 
		isWorking = false;
	} // http.readyState
} // function 

function ajax_resetDropDown(dropdown){
	dropdown.length = 0;
	dropdown.options[0] = new Option('','');
	dropdown.selectedIndex = 0;
}

function getHTTPObject() {
  var xmlhttp;
  /*@cc_on
  @if (@_jscript_version >= 5)
    try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        xmlhttp = false;
      }
    }
  @else
  xmlhttp = false;
  @end @*/
  if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    try {
      xmlhttp = new XMLHttpRequest();
    } catch (e) {
      xmlhttp = false;
    }
  }
  return xmlhttp;
}