function ajaxObject(url, callbackFunction) {
	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) {
					that.updating=false;
					that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);
					that.AJAX=null;
				}
			}
			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+'&amp;timestamp='+(that.updating.getTime());
				that.AJAX.open("GET", uri, true);
				that.AJAX.send(null);
			}
			return true;
		}
	}
	var urlCall = url;
	this.callback = callbackFunction || function () { };
}

function processData(responseText, responseStatus) {
	if (responseStatus==200) {
		eval(responseText);
	} else {
		alert(responseStatus + " -- Error Processing Request");
	}
}

function processResult(responseText, responseStatus) {
	if (responseStatus==200) {
		eval(responseText);
	} else {
		alert(responseStatus + " -- Error Processing Request");
	}
}

function placeResponse(el,response) {
	el = document.getElementById(el);
	if (el) {
		el.innerHTML = response;
	}
}

function populateSelect(selectType) {
	switch (selectType) {
			case "adminState":
				regionID = document.getElementById("regionID").value;
				stateID = document.getElementById("stateID").value;
				requestURL = "/ajax/populateSelect.php";
				requestVar = "regionID=" + regionID;
				requestVar += "&stateID=" + stateID;
				requestVar += "&targetBox=stateID";
				requestVar += "&targetBox2=locationID";
			break;
			case "adminLocation":
				stateID = document.getElementById("stateID").value;
				locationID = document.getElementById("locationID").value;
				requestURL = "/ajax/populateSelect.php";
				requestVar = "stateID=" + stateID;
				requestVar += "&locationID=" + locationID;
				requestVar += "&targetBox=locationID";
			break;
			case "userSearch":
				countryID = document.getElementById("countryID").value;
				regionID = document.getElementById("regionID").value;
				stateID = document.getElementById("stateID").value;
				locationID = document.getElementById("locationID").value;
				hotelID = document.getElementById("hotelID").value;
				requestURL = "/ajax/populateSelect.php";
				requestVar = "countryID=" + countryID;
				requestVar += "&regionID=" + regionID;
				requestVar += "&stateID=" + stateID;
				requestVar += "&locationID=" + locationID;
				requestVar += "&hotelID=" + hotelID;
				requestVar += "&type=userSearch";
			break;
	}
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestVar,"POST");
}

function populateHotels(locationID,hotel1ID,hotel2ID,locationBox,index) {
	requestURL = "/ajax/populateSelect.php";
	requestVar = "locationID=" + locationID;
	requestVar += "&hotel1ID=" + hotel1ID;
	requestVar += "&hotel2ID=" + hotel2ID;
	requestVar += "&index=" + index;
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestVar,"POST");
}

function deleteImage(propertyID,imageID) {
	var confirm_text = 'Are you sure you want to delete this image?';
	if(confirm(confirm_text)){
		requestURL = "/ajax/deleteImage.php";
		requestVar = "imageID=" + imageID;
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
		myRequest.update(requestVar,"POST");
	}
}

function showInfo(response) {
	if (response) {
		placeResponse("PopContent",response);
		$("#PopInfo").reveal();
	} else {
		//alert("An error has occured");
	}
}

function ajaxPopOver(url) {
	destEl = document.getElementById("PopContent");
	if ((destEl) && (url)) {
		destEl.innerHTML = "";
		requestURL = url;
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { showInfo(responseText); }
		myRequest.update("","POST");
	}
}

function addToTrip(requestURL) {
	requestVar = "javascript=true";
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestVar);
}

function addHotelToTrip(ID) {
	requestVar = "javascript=true";
	requestVar += "&plannerType=hotel";
	myRequest = new ajaxObject(requestURL);
	myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
	myRequest.update(requestVar);
}

function removeTrip(plannerID) {
	var confirm_text = 'Are you sure you want to delete this trip item?';
	if(confirm(confirm_text)){
		requestURL = "/ajax/removeTripPlanner.php";
		requestVar = "plannerID=" + plannerID;
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
		myRequest.update(requestVar,"POST");
	}
}
