<!--

//=====================================================
// Assign XMLHttpRequest based on browser
//=====================================================

var ResponseDoc = "";

if (window.XMLHttpRequest) { // Non-IE browsers
	var XML_Request = new XMLHttpRequest();
	var LoadPageHttp =new XMLHttpRequest();
	//var ajaxXMLDoc = document.implementation.createDocument('', '', null);
} else if (window.ActiveXObject) { // IE
	var XML_Request = new ActiveXObject("Msxml2.XMLHTTP.3.0");
	var LoadPageHttp = new ActiveXObject("Msxml2.XMLHTTP.3.0");
	//var ajaxXMLDoc = new  ActiveXObject("Msxml.DOMdocument");
}

//=====================================================
// Main function for Making XMLHTTPRequests
// Method - Post or get
// location - URL to call
// data - data to post to the request
// responsetype - return response as xml or text
//=====================================================
function XML_req_exec(method, location, data, responsetype){
	var responsetxt = "";
	try{
			//Main Send
			//Append date/time stamp to remove possible browser caching
			location = updateURLTimestamp(location);
			LoadPageHttp.open(method, location, false);
			LoadPageHttp.send(data);
			//=====================================================
			// Checks status of request.
			// Readystate :
			// 0 = uninitialized
			// 1 = loading
			// 2 = loaded
			// 3 = interactive
			// 4 = complete
			//=====================================================
			if (LoadPageHttp.readyState == 4){
					//Requested is OK - not a 404 or other 
					if(LoadPageHttp.status == 200){ 
						// Returns the response as a string
						if(	responsetype == "xml"){
							var response = LoadPageHttp.responseXML.documentElement;
							return response;
						}
						else {
							var response = LoadPageHttp.responseText;
							response = TrimString(response);
							return response;							
						}
					
					}
					else {
						// return -200 to identify error with loaded response
						return "E-200";
					}
			}
	}
	catch(error){
		// Return -1 to identify an XMLHTTPrequest execute error
		return "E-1";
	}
}

//=====================================================
// Update time stamp string for AJAX URL to eliminate possible caching
//=====================================================
function updateURLTimestamp(location){

	var xLocation = location.indexOf('?');
	var timeStamp = new Date().getTime();
	if(xLocation == -1) location += "?ts="+timeStamp;
	else location += "&ts="+timeStamp;
	return location;

}

//=====================================================
// Populate a Select Box from an XML doc
//=====================================================
function ajaxSelect(){
	var updateURL = "";
	var uniqueId = "";
	var updateInput = "";
	var useCache = false;
	var xmlNode = "";
	var ajaxXMLDoc = "";
	var emptyFirstValue = true;
}

ajaxSelect.prototype.getValues = function(){

	var actionURL = "";
	var respType = 'xml';
	if(this.uniqueId) actionURL = this.updateURL + this.uniqueId;
	else actionURL = this.updateURL;
	

	if(this.useCache == true) this.ajaxXMLDoc = dataCache(this.uniqueId, actionURL,respType );
	else this.ajaxXMLDoc = XML_req_exec("GET", actionURL, '', respType);
	
}
ajaxSelect.prototype.loadOptions = function(){
	
	if (check_request_results(this.ajaxXMLDoc) == true){
		//select County Nodes and add them to the select form item
		try {
			var dataNode = this.ajaxXMLDoc.getElementsByTagName(this.xmlNode);
			//if there are counties returned, reset the counties select object
			if (dataNode.length > 0 && this.updateInput){
				this.updateInput.options.length = 0;
				if (this.emptyFirstValue == true)  this.updateInput.options[this.updateInput.options.length] = new Option('','');
			}
			else if (dataNode.length == 0){
				this.updateInput.options.length = 0;
				this.updateInput.options[this.updateInput.options.length] = new Option('No options available','');
			}
			for(i=0;i<dataNode.length;i++){
				dataItem = dataNode.item(i);
				optionName = dataItem.getAttribute("Name");
				optionValue = dataItem.getAttribute("Value");
				this.updateInput.options[this.updateInput.options.length] = new Option(optionName,optionValue);
			}

		} catch (e) {}
	}	
	
}

//=====================================================
// Checks results of request - if results dont equal an error code then retrun true
// else return false 
//=====================================================
function check_request_results(x){
	if (x != null){
		switch (x){
			case "E-1":	
				return false;
				break;
			case "E-200":
				return false;
				break;
			default:
				return true;
		}
	}
	else return false;
}

function TrimString(InString) {
  InString = InString.replace( /^\s+/g, "" ); // strip leading
  return InString.replace( /\s+$/g, "" ); // strip trailing
}




var Cache = new Array();
var ajaxCache = new objCache();


/*
dataCache >> Allows caching of ajax calls where the data is not expected to change between calls. Minimizes the amount of servers calls
	ID >> the variable you want to pass in. FOr example, if you are making a ajax call to update a county list, the  ID would be state and the data saved would be the county xml list to be parsed
	actionURL >> ajax url to call
	respType >> text or xml response
*/

function dataCache(ID, actionURL, respType){
		//check cache for data 
		if(!ajaxCache.get(ID)){
			xmlDOC = XML_req_exec("GET", actionURL, '', respType);
			ajaxCache.set(ID, xmlDOC);
		} else {
			xmlDOC = ajaxCache.get(ID);
		}
		return xmlDOC;
}


function objCache(){
    
	this.get = objCacheGet;
	this.set = objCacheSet;
	this.getMulti = objCacheGetMulti;
	this.remove = objCacheRemove;
	this.flush = objCacheFlush;
	this.set = objCacheSet;
}


function objCacheGet(n){

            if(Cache[n]) return Cache[n];

}
function objCacheSet(n,v){

			if(Cache[n]) delete(Cache[n]);
			Cache[n]=v;
			return (Cache[n])?1:0;
}
function objCacheGetMulti(x){

 			var a = new Array();
            for (var k in x) a.push(this.get(x[k]));
            return a;
}
function objCacheRemove(x){

 			var a = new Array();
            for (var k in x) a.push(this.get(x[k]));
            return a;
}
function objCacheFlush(){
	
            for(var k in Cache) delete(Cache[k]);
            return 1;
}

function callOut(url, waitString, methodToInvoke) { 

   		//time_object.StartTiming();

   		showLoaderBar('#006600', waitString);
   		
   		// tack on date to prevent caching
   		url = url + "&ts=" + new Date();
   		//alert('going to make ajax call...using uRL' + url);   		
   		new Ajax.Request(url, {  
   			method:'get',    
   			asynchronous:true, 
   			onSuccess: function(transport){
				//alert('going to invoke..' + methodToInvoke);
				var response = transport.responseText;
				methodToInvoke(response);
				
   			},  
    		onComplete: function(){
	 			// clean up processes if needed
	 			//alert('completed');
	 			hideLoaderBar();
	 			
    		}, 
    		onFailure: function(transport){ 
    			alert('Problem with call ' + url); 
    		}

 }); 
}
function callOutPOST(url, parameters, methodToInvoke) { 
   		// tack on date to prevent caching
   		//url = url + "&ts=" + new Date() ;
   		//alert("Going to call POST" + url + ' and params are ' + parameters);
   		var http = new Ajax.Request(url,  {  
   			method:'POST',   
   			asynchronous:true, 
   			onSuccess: function(transport){
				//alert('going to invoke..' );
				var response = transport.responseText;
				//alert('Resp is ' + respone);
				methodToInvoke(response);
				
   			},  
    		onComplete: function(){
	 			// clean up processes if needed
	 			
    		}, 
    		onFailure: function(transport){ 
    			alert('Problem with callOutPOST ' + url); 
    		}

 		}); 
 	 
 	  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      	  http.setRequestHeader("Content-length", parameters.length);
      	  http.setRequestHeader("Connection", "close");
      	  http.send(parameters);
}

function showLoaderBar(borderStyle, loaderText){
	//Since this doesn't seem to be doing it's job, I'll comment for now
	//if (document.getElementById("notificationDiv") != null) {
	if (false) {
	
		//document.getElementById("notificationDiv").style.display = "block";
		//document.getElementById("notificationDiv").style.position = "absolute";
		//document.getElementById("notificationDiv").style.margin = "0 auto";
		//document.getElementById("notificationDiv").style.top = "300";
		//document.getElementById("notificationDiv").style.left = "100";
	
		document.getElementById("notificationDiv").className = 'loading';
		
		if(borderStyle) { 
			document.getElementById("notificationDiv").style.border = "5px solid " + borderStyle;
		}
		document.getElementById("notificationDiv").innerHTML = 
		"<table width=\"100%\"><tr><td><img src=\"images\/shim.gif\" width=\"1\" height=\"1\" \/><br></td></tr><tr><td align=\"center\" bgcolor=\"blue\"><img src=\"images\/ajax-loader.gif\" valign=\"absmiddle\" width=\"50\" height=\"50\" \/><\/td> <\/tr><tr><td align=\"center\" bgcolor=\"blue\">" + loaderText + "<\/td><\/tr><\/table>";

	}   
}
function hideLoaderBar(){ 

	//document.getElementById("notificationFrame").style.display = "none";
	document.getElementById("notificationDiv").style.display = "none";
	//document.getElementById("notificationDiv").className = "";
	document.getElementById("notificationDiv").innerHTML = "";
}


//-->
