preloaderOn = false;

/*************************************************************************\
This AJAX object will make a POST request to a specified PHP Script and then
call the function we specify while it is processing and once it has completed.
\*************************************************************************/
function ajaxObject(url, callbackFunction, preLoadFunction, special_items) {
	var that=this;
	this.updating = false;
	this.abort = function() {
		if (that.updating) {
			that.updating=false;
			that.AJAX.abort();
			that.AJAX=null;
		}
	}

	this.update = function(passData,postMethod) {
		if (that.updating) { return false; }
		that.AJAX = null;
		if (window.XMLHttpRequest) {
			that.AJAX=new XMLHttpRequest();
		} else {
			that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
		}
    	
		if (that.AJAX==null) {
			return false;
		} else {
			that.AJAX.onreadystatechange = function() {
			if (that.AJAX.readyState == 4) {
				if(that.AJAX.status == 200) {
					//the request has completed. call the call back.
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;
					preloaderOn = false
				} else {
					//load the preloader
					that.preloader(that.AJAX.status, special_items);
				}
    	    } else {
				//load the preloader
				that.preloader(that.AJAX.readyState, special_items);
			}
		}

		//send the POST REQUEST.  Add date to the query string to avoid cache issues.
		that.updating = new Date();
		if (/post/i.test(postMethod)) {
			var uri=urlCall+'?'+that.updating.getTime();
			that.AJAX.open("POST", uri, true);
			that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			that.AJAX.setRequestHeader("Content-Length", passData.length);
			that.AJAX.send(passData);
			
		} else {
			var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());
			that.AJAX.open("GET", uri, true);
			that.AJAX.overrideMimeType('text/xml');
			that.AJAX.send(null);
		}
		return true;
    }
  }

  var urlCall = url;
  
  //the function to call when the request is complete.
  this.callback = callbackFunction || function () { };
  
  //the function to call to display a message to the screen while AJAX runs
  this.preloader = preLoadFunction || function () { };
}

/*************************************************************************\
This function will prepare the parameter string for the AJAX POST Request.
POST requests are more secure and can handle a much larger amount of data.
\*************************************************************************/
function getParams (values, getResp) {
	param_str = '';
	if(getResp) {
		values["xmlr"] = "1";
	}
	
	var i = 0;
	if(values != null) {
		for (var key in values) {
			param_str += (i > 0 ? "&":"") + key + "=" + encodeURIComponent(values[key]);
			i++;
		}
	}

	return param_str;
}

/*************************************************************************\
While AJAX is running, we need to show the user that data is being accessed,
transferred, etc.  This function will display a message on the screen and
notify the user that we are Loading/Saving multilanguage data.
\*************************************************************************/
function preloader(status, details) {
	if(!preloaderOn) {
		preloaderOn = true;
		loading = "Loading";		
		if(details["saving"] == "1")
			loading = "Saving";
		message = "<strong>" + loading + " data...</strong><br><br>"
		img = (details["flag3"] == "" ? details["flag"] : details["flag3"]);
		message += details["lang"] + "<br><br><img src=\"" + img + "\"><br>"
		//showlayer('waitlayer', message);
	}
}

/*************************************************************************\
Execute the AJAX Request and call the callback function
\*************************************************************************/
function execute (scriptToCall, callBack, details, values, sendPost) {
	var myRequest = new ajaxObject(getReq(scriptToCall), callBack, preloader, details);
	myRequest.update((sendPost !== true ? getParams(values, true) : values),'POST');
}

/*************************************************************************\
This function will determine which PHP script is called by the AJAX Request
\*************************************************************************/
function getReq(scriptToCall) {
	switch (scriptToCall) {
		case "1":
			return "/ajax/video_popup.php";
			break;
		case "2":
			return "/ajax/directions.php";
			break;
	}
}

/*************************************************************************\ 
Or if we have a form, we can just pass the form and get the value of every 
element in the form that way... 
\*************************************************************************/ 
function getParamsFromForm(form, getResp, pairs) 
{ 
 	try {
		var elements = form.elements; 
	 
	 	if(!pairs)
		{
		    var pairs = new Array(); 
	 	}
	 
	     if(getResp) { 
	          pairs["xmlr"] = "1"; 
	     } 
	 
	    for (var i = 0; i < elements.length; i++) 
		{ 
			if((elements[i].type == "checkbox") && (!elements[i].checked))
				continue;
			else if((elements[i].type == "radio") && (!elements[i].checked))
				continue;
	        if ((name = elements[i].name) && (value = elements[i].value)) 
	               pairs[name] = encodeURIComponent(value) 
	    } 
	    return pairs; 
	} catch(err) {
		return -1;
	}
}