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 populateSearchSelect(type) {
	if (type) {
		professionBox = document.getElementById("jobSearch").professionID;
		gradeBox = document.getElementById("jobSearch").gradeID;
		specialityBox = document.getElementById("jobSearch").specialityID;
		countryBox = document.getElementById("jobSearch").countryID;
		regionBox = document.getElementById("jobSearch").regionID;
		locationBox = document.getElementById("jobSearch").locationID;
		professionID = professionBox.value;
		gradeID = gradeBox.value;
		specialityID = specialityBox.value;
		countryID = countryBox.value;
		regionID = regionBox.value;
		locationID = locationBox.value;
		targetBox = "";
		targetText = "";
		targetBox2 = "";
		targetText2 = "";
		requestURL = "/ajax/populateSelect2.php";
		requestVar = "type=" + type;
		requestVar += "&professionID=" + professionID;
		requestVar += "&gradeID=" + gradeID;
		requestVar += "&specialityID=" + specialityID;
		requestVar += "&countryID=" + countryID;
		requestVar += "&regionID=" + regionID;
		requestVar += "&withVacancies=true";
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
		myRequest.update(requestVar,"POST");
	}
}

function populateSelect(type) {
	requestURL = "";
	requestVar = "";
	switch (type) {
		case "location":
			if (document.getElementById("RegionID")) {
				regionID = document.getElementById("RegionID").value;
				locationID = document.getElementById("LocationID").value;
				requestURL = "/ajax/populateSelect.php";
				requestVar = "regionID=" + regionID;
				requestVar += "&locationID=" + locationID;
				requestVar += "&targetBox=LocationID";
			}
		break;
		case "gradeSpeciality":
			if (document.getElementById("ProfessionID")) {
				professionID = document.getElementById("ProfessionID").value;
				gradeID = document.getElementById("GradeID").value;
				specialityID = document.getElementById("SpecialityID").value;
				requestURL = "/ajax/populateSelect.php";
				requestVar = "professionID=" + professionID;
				requestVar += "&gradeID=" + gradeID;
				requestVar += "&targetBox=GradeID";
				requestVar += "&specialityID=" + specialityID;
				requestVar += "&targetBox2=SpecialityID";
			}
		break;
	}
	if (requestURL) {
		myRequest = new ajaxObject(requestURL);
		myRequest.callback = function(responseText,responseStatus) { processResult(responseText,responseStatus);}
		myRequest.update(requestVar,"POST");
	}
}
