var inputfieldName = new Array()
inputfieldName["kw"] = "ajax_keywords";
inputfieldName["loc"] = "ajax_location";
inputfieldName["email"] = "ajax_email";
inputfieldName["location"] = "candidate_reg_location";
inputfieldName["ftloc"] = "search_location_freetext";

HTTPObjectArray = new Array();

function genericSuggestBoxHide(prefix) {
	if (getElementById(prefix+'SuggestBox') != null) {
		getElementById(prefix+'SuggestBox').style.visibility = 'hidden';
		getElementById(inputfieldName[prefix]).className = prefix+'SuggestBoxHidden';
	}
}

function kwSuggestBoxHide() {
	if (getElementById('kwSuggestBox') != null) {
		getElementById('kwSuggestBox').style.visibility = 'hidden';
		getElementById('ajax_keywords').className = 'kwSuggestBoxHidden';
	}
}

function locSuggestBoxHide() {
	if (getElementById('locSuggestBox') != null) {
		getElementById('locSuggestBox').style.visibility = 'hidden';
		getElementById('ajax_location').className = 'locSuggestBoxHidden';
	}
}

function emailSuggestBoxHide() {
	if (getElementById('emailSuggestBox') != null) {
		getElementById('emailSuggestBox').style.visibility = 'hidden';
		getElementById('ajax_email').className = 'emailSuggestBoxHidden';
	}
}

function locationBoxHide() {
	if (getElementById('locationBox') != null) {
		getElementById('locationBox').style.height = '1px';
		getElementById('locationBox').style.visibility = 'hidden';
		getElementById('candidate_reg_location').className = 'locationBoxHidden';
	}
}

// Get the HTTP Object
function getHTTPObject(){
	if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest) return new XMLHttpRequest();
	else {
		//alert("Your browser does not support AJAX.");
		return null;
	}
}

//new generic functions
function ftlocSetOutput() {
		genericSetOutput('ftloc');
}

function genericSetOutput(prefix){
	if(HTTPObjectArray[prefix].readyState == 4){
		//document.getElementById('outputText').value = kwHTTPObject.responseText;
		getElementById(prefix+'SuggestBox').innerHTML = "";
		var myArray = eval(HTTPObjectArray[prefix].responseText);
		var arrayLen = myArray.length;
		var divHeights = 0;
		var suggestBoxHeightLimit = 150;
		var suggestBoxHeightMax = 0;
		var txt = "";
		for (var temp = 0; temp < arrayLen; temp++) {
			var range = 1;
			if (myArray.constructor == Array) {
				var newText = myArray[temp][0];
				if (prefix == 'ftloc') {
					range = myArray[temp][1];
				}
			} else {
				var newText = myArray[temp];
			}
			var originalText = newText;
			var keyword = ltrim(document.getElementById(inputfieldName[prefix]).value);
			newText = newText.replace(eval("/("+keyword+")/gi"), "<span class=\""+prefix+"SuggestHilite\">$1</span>");
			getElementById(prefix+'SuggestBox').innerHTML += "<div id=\""+prefix+"SuggestBoxLine"+temp+"\" class=\""+prefix+"SuggestBoxLine\" onmouseover=\"this.className='"+prefix+"SuggestBoxLineOver'\" onmouseout=\"this.className='"+prefix+"SuggestBoxLine'\" onclick=\"genericSuggestLineSelect('"+originalText+"','"+prefix+"',"+range+")\">"+newText+"</div>";
			divHeights += parseInt(eval("getElementById('"+prefix+"SuggestBoxLine"+temp+"').offsetHeight"));
			if (divHeights < suggestBoxHeightLimit) {
				//alert(divHeights);
				suggestBoxHeightMax = divHeights;
			}
		}
		if (arrayLen == 0) {
			genericSuggestBoxHide(prefix);
		} else {
			genericSuggestionMove('ftloc');
			getElementById(inputfieldName[prefix]).className = prefix+'SuggestBoxVisible';
			getElementById(prefix+'SuggestBox').style.height = (suggestBoxHeightMax+2)+"px";
			getElementById(prefix+'SuggestBox').style.visibility = 'visible';
		}
	}
}

// Change the value of the outputText field
function kwSetOutput(){
	if(kwHTTPObject.readyState == 4){
		//document.getElementById('outputText').value = kwHTTPObject.responseText;
		getElementById('kwSuggestBox').innerHTML = "";
		var myArray = eval(kwHTTPObject.responseText);
		var arrayLen = myArray.length;
		var divHeights = 0;
		var kwSuggestBoxHeightLimit = 150;
		var kwSuggestBoxHeightMax = 0;
		var txt = "";
		for (var temp = 0; temp < arrayLen; temp++) {
			var newText = myArray[temp];
			var originalText = newText;
			var keyword = ltrim(document.getElementById('ajax_keywords').value);
			newText = newText.replace(eval("/("+keyword+")/gi"), "<span class=\"kwSuggestHilite\">$1</span>");
			getElementById('kwSuggestBox').innerHTML += "<div id=\"kwSuggestBoxLine"+temp+"\" class=\"kwSuggestBoxLine\" onmouseover=\"this.className='kwSuggestBoxLineOver'\" onmouseout=\"this.className='kwSuggestBoxLine'\" onclick=\"kwSuggestLineSelect('"+originalText+"')\">"+newText+"</div>";
			divHeights += parseInt(eval("getElementById('kwSuggestBoxLine"+temp+"').offsetHeight"));
			if (divHeights < kwSuggestBoxHeightLimit) {
				//alert(divHeights);
				kwSuggestBoxHeightMax = divHeights;
			}
		}
		if (arrayLen == 0) {
			kwSuggestBoxHide();
		} else {
			getElementById('ajax_keywords').className = 'kwSuggestBoxVisible';
			getElementById('kwSuggestBox').style.height = (kwSuggestBoxHeightMax+2)+"px";
			getElementById('kwSuggestBox').style.visibility = 'visible';
		}
	}
}

function locSetOutput(){
	if(locHTTPObject.readyState == 4){
		//document.getElementById('outputText').value = kwHTTPObject.responseText;
		getElementById('locSuggestBox').innerHTML = "";
		var myArray = eval(locHTTPObject.responseText);
		var arrayLen = myArray.length;
		var divHeights = 0;
		var locSuggestBoxHeightLimit = 150;
		var locSuggestBoxHeightMax = 0;
		var txt = "";
		for (var temp = 0; temp < arrayLen; temp++) {
			var newText = myArray[temp];
			var originalText = newText;
			var location = ltrim(document.getElementById('ajax_location').value);
			newText = newText.replace(eval("/("+location+")/gi"), "<span class=\"locSuggestHilite\">$1</span>");
			getElementById('locSuggestBox').innerHTML += "<div id=\"locSuggestBoxLine"+temp+"\" class=\"locSuggestBoxLine\" onmouseover=\"this.className='locSuggestBoxLineOver'\" onmouseout=\"this.className='locSuggestBoxLine'\" onclick=\"locSuggestLineSelect('"+originalText+"')\">"+newText+"</div>";
			divHeights += parseInt(eval("getElementById('locSuggestBoxLine"+temp+"').offsetHeight"));
			if (divHeights < locSuggestBoxHeightLimit) {
				//alert(divHeights);
				locSuggestBoxHeightMax = divHeights;
			}
		}
		if (arrayLen == 0) {
			locSuggestBoxHide();
		} else {
			getElementById('ajax_location').className = 'locSuggestBoxVisible';
			getElementById('locSuggestBox').style.height = (locSuggestBoxHeightMax+2)+"px";
			getElementById('locSuggestBox').style.visibility = 'visible';
		}
	}
}

function emailSetOutput(){
	if(emailHTTPObject.readyState == 4){
		//document.getElementById('outputText').value = kwHTTPObject.responseText;
		getElementById('emailSuggestBox').innerHTML = "";
		var myArray = eval(emailHTTPObject.responseText);
		var arrayLen = myArray.length;
		var divHeights = 0;
		var emailSuggestBoxHeightLimit = 150;
		var emailSuggestBoxHeightMax = 0;
		var txt = "";
		for (var temp = 0; temp < arrayLen; temp++) {
			var newText = myArray[temp];
			var originalText = newText;
			var email = ltrim(document.getElementById('ajax_email').value);
			newText = newText.replace(eval("/("+email+")/gi"), "<span class=\"emailSuggestHilite\">$1</span>");
			getElementById('emailSuggestBox').innerHTML += "<div id=\"emailSuggestBoxLine"+temp+"\" class=\"emailSuggestBoxLine\" onmouseover=\"this.className='emailSuggestBoxLineOver'\" onmouseout=\"this.className='emailSuggestBoxLine'\" onclick=\"emailSuggestLineSelect('"+originalText+"')\">"+newText+"</div>";
			divHeights += parseInt(eval("getElementById('emailSuggestBoxLine"+temp+"').offsetHeight"));
			if (divHeights < emailSuggestBoxHeightLimit) {
				//alert(divHeights);
				emailSuggestBoxHeightMax = divHeights;
			}
		}
		if (arrayLen == 0) {
			emailSuggestBoxHide();
		} else {
			getElementById('ajax_email').className = 'emailSuggestBoxVisible';
			getElementById('emailSuggestBox').style.height = (emailSuggestBoxHeightMax+2)+"px";
			getElementById('emailSuggestBox').style.visibility = 'visible';
		}
	}
}

function locationSet(){
	if(locHTTPObject.readyState == 4){
		//document.getElementById('outputText').value = kwHTTPObject.responseText;
		getElementById('locationBox').innerHTML = "";
		var myArray = eval(locHTTPObject.responseText);
		var arrayLen = myArray.length;
		var divHeights = 0;
		var locationBoxHeightLimit = 150;
		var locationBoxHeightMax = 0;
		var txt = "";
		for (var temp = 0; temp < arrayLen; temp++) {
			var newText = myArray[temp];
			var originalText = newText;
			var location = ltrim(document.getElementById('candidate_reg_location').value);
			newText = newText.replace(eval("/("+location+")/gi"), "<span class=\"locationHilite\">$1</span>");
			getElementById('locationBox').innerHTML += "<div id=\"locationBoxLine"+temp+"\" class=\"locationBoxLine\" onmouseover=\"this.className='locationBoxLineOver'\" onmouseout=\"this.className='locationBoxLine'\" onclick=\"locationLineSelect('"+originalText+"')\">"+newText+"</div>";
			divHeights += parseInt(eval("getElementById('locationBoxLine"+temp+"').offsetHeight"));
			if (divHeights < locationBoxHeightLimit) {
				//alert(divHeights);
				locationBoxHeightMax = divHeights;
			}
		}
		if (arrayLen == 0) {
			locationBoxHide();
		} else {
			getElementById('candidate_reg_location').className = 'locationBoxVisible';
			getElementById('locationBox').style.height = (locationBoxHeightMax+2)+"px";
			getElementById('locationBox').style.visibility = 'visible';
		}
	}
}


// Implement business logic
function doWork(elm){
	if (elm == 'kw') {
		kwHTTPObject = getHTTPObject();
		if (kwHTTPObject != null) {
			var keyword = ltrim(document.getElementById('ajax_keywords').value);
			if (keyword.length > 1) {
				kwHTTPObject.open("GET", "/allsites/common/ajax_server.php?keyword="
				+keyword, true);
				kwHTTPObject.send(null);
				kwHTTPObject.onreadystatechange = kwSetOutput;
			} else {
				kwSuggestBoxHide();
			}
		}
	} else if (elm == 'loc') {
		locHTTPObject = getHTTPObject();
		if (locHTTPObject != null) {
			var location = ltrim(document.getElementById('ajax_location').value);
			if (location.length > 1) {
				locHTTPObject.open("GET", "/allsites/common/ajax_server.php?location="
				+location, true);
				locHTTPObject.send(null);
				locHTTPObject.onreadystatechange = locSetOutput;
			} else {
				locSuggestBoxHide();
			}
		}
	} else if (elm == 'adminfc') {
		HTTPObject = getHTTPObject();
		if (HTTPObject != null) {
			var clientID = parseInt(document.getElementById('clientID').value);
			var childID = parseInt(document.getElementById('childID').value);
			var siteID = parseInt(document.getElementById('siteID').value);
			var comment = document.getElementById('comment').value;
			if (typeof(clientID) == 'number' && clientID > 0 && comment!="") {
				var url = "/allsites/common/ajax_server.php?siteID="+siteID+"&clientID="+clientID+"&comment="+comment;
				if (typeof(childID) == 'number' && childID > 0) {
					url += "&childID="+childID
				}
				HTTPObject.open("GET", url, true);
				HTTPObject.send(null);
				HTTPObject.onreadystatechange = addCompany;
			} else if(comment=="") {
				alert('You must enter a comment');
			} else {
				alert('You must enter a client ID');
			}
		}
	} else if (elm == 'email') {
		emailHTTPObject = getHTTPObject();
		if (emailHTTPObject != null) {
			var ajaxEmail = ltrim(document.getElementById('ajax_email').value);
			var clientID = parseInt(document.getElementById('clientID').value);
			if (typeof(clientID) == 'number' && clientID > 0) {
				var url = "/allsites/common/ajax_server.php?email="+ajaxEmail+"&clientID="+clientID;
				emailHTTPObject.open("GET", url, true);
				emailHTTPObject.send(null);
				emailHTTPObject.onreadystatechange = emailSetOutput;
			} 
		}
	} else if (elm == 'ftloc') {
		prefix = 'ftloc';
		HTTPObjectArray[prefix] = getHTTPObject();
		if (HTTPObjectArray[prefix] != null) {
			var query = ltrim(document.getElementById(inputfieldName[prefix]).value);
			if (query.length > 1) {
				HTTPObjectArray[prefix].open("GET", "/allsites/common/ajax_server.php?"+prefix+"="
				+query, true);
				HTTPObjectArray[prefix].send(null);
				HTTPObjectArray[prefix].onreadystatechange = ftlocSetOutput;
			} else {
				genericSuggestBoxHide(prefix);
			}
		}
	}
}

function genericSuggestLineSelect(selected, prefix, range) {
	getElementById(inputfieldName[prefix]).value = selected;
	if (getElementById('search_radius')) {
		if (range == 0) {
			// disable range
			getElementById('search_radius').selectedIndex = 0;
			getElementById('search_radius').className = 'readonly';
			getElementById('search_radius').setAttribute('Disabled','disabled');
			getElementById('search_radius').setAttribute('OurDisabled',1);
		} else {
			getElementById('search_radius').className = '';
			getElementById('search_radius').removeAttribute('Disabled');
			getElementById('search_radius').setAttribute('OurDisabled',0);
		}
	}
}

function kwSuggestLineSelect(selected) {
	getElementById('ajax_keywords').value = selected;
}

function locSuggestLineSelect(selected) {
	getElementById('ajax_location').value = selected;
}

function emailSuggestLineSelect(selected) {
	getElementById('ajax_email').value = selected;
}

function locationLineSelect(selected) {
	getElementById('candidate_reg_location').value = selected;
}

function genericSuggestionMove(prefix) {
	ifN = inputfieldName[prefix];
	if (getElementById(prefix+'SuggestBox') != null) {
		var inputTop = getElementById(ifN).offsetTop;
		var inputLeft = getElementById(ifN).offsetLeft;
		var inputHeight = getElementById(ifN).offsetHeight;
		var suggestBoxTop = inputTop + inputHeight;
		getElementById(prefix+'SuggestBox').style.top = (suggestBoxTop-1)+"px";
		getElementById(prefix+'SuggestBox').style.left = inputLeft+"px";
	}
}

function kwSuggestionMove() {
	if (getElementById('kwSuggestBox') != null) {
		var inputTop = getElementById('ajax_keywords').offsetTop;
		var inputLeft = getElementById('ajax_keywords').offsetLeft;
		var inputHeight = getElementById('ajax_keywords').offsetHeight;
		var kwSuggestBoxTop = inputTop + inputHeight;
		getElementById('kwSuggestBox').style.top = (kwSuggestBoxTop-1)+"px";
		getElementById('kwSuggestBox').style.left = inputLeft+"px";
	}
}

function locSuggestionMove() {
	if (getElementById('locSuggestBox') != null) {
		var inputTop = getElementById('ajax_location').offsetTop;
		var inputLeft = getElementById('ajax_location').offsetLeft;
		var inputHeight = getElementById('ajax_location').offsetHeight;
		var locSuggestBoxTop = inputTop + inputHeight;
		getElementById('locSuggestBox').style.top = (locSuggestBoxTop-1)+"px";
		getElementById('locSuggestBox').style.left = inputLeft+"px";
	}
}

function emailSuggestionMove() {
	if (getElementById('emailSuggestBox') != null) {
		var inputTop = getElementById('ajax_email').offsetTop;
		var inputLeft = getElementById('ajax_email').offsetLeft;
		var inputHeight = getElementById('ajax_email').offsetHeight;
		var emailSuggestBoxTop = inputTop + inputHeight;
		getElementById('emailSuggestBox').style.top = (emailSuggestBoxTop-1)+"px";
		getElementById('emailSuggestBox').style.left = inputLeft+"px";
	}
}

function hideSuggestionBoxes() {
	setTimeout("kwSuggestBoxHide()", 500);
	setTimeout("locSuggestBoxHide()", 500);
	setTimeout("emailSuggestBoxHide()", 500);
	setTimeout("locationBoxHide()", 500);
	setTimeout("genericSuggestBoxHide('ftloc')", 500);
}


function onloadhandler() {
	document.getElementsByTagName("BODY")[0].onclick = hideSuggestionBoxes;
	kwSuggestionMove();
	locSuggestionMove();
	emailSuggestionMove();
	genericSuggestionMove('ftloc');
}

function getStyle(oElm, strCssRule){
  var strValue = "";
  if(document.defaultView && document.defaultView.getComputedStyle){
    strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
  }
  else if(oElm.currentStyle){
    strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
      return p1.toUpperCase();
    });
    strValue = oElm.currentStyle[strCssRule];
  }
  return strValue;
}

function clearRefine() {
	getElementById('ajax_refine').value = "";
}
var kwHTTPObject = null;

