var xmlhttp;
var xmlhttpTerm;
var xmlhttpSearch;
var xmlhttpExport;
var elementId;

// Changes the cursor to an hourglass
function cursor_wait() {
	document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
	document.body.style.cursor = 'default';
}


function GetXmlHttpObject() {
	if (window.XMLHttpRequest) {
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

function updateSearchChoices(selectBox, searchCount) {
	// if the selectbox is AOE, REGION, CITIZENSHIP, or LANGUAGE we need to fill the dropdown
	var selectBoxValue = selectBox.value;
	currentSearchCount = searchCount;
	document.getElementById('searchTerm' + searchCount).style.display = 'inline';
	document.getElementById('searchAoECategory' + searchCount).style.display = 'none';
	document.getElementById('searchAoE' + searchCount).style.display = 'none';
	document.getElementById('searchRegion' + searchCount).style.display = 'none';
	document.getElementById('searchLanguage' + searchCount).style.display = 'none';
	document.getElementById('searchCitizenship' + searchCount).style.display = 'none';
	document.getElementById('searchProficiency' + searchCount).style.display = 'none';
	document.getElementById('searchAvailability' + searchCount).style.display = 'none';
	
	switch(selectBoxValue.toUpperCase()) {
	case "AOE":
		document.getElementById('searchTerm' + searchCount).style.display = 'none';
		document.getElementById('searchAoECategory' + searchCount).style.display = 'inline';
		document.getElementById('searchAoE' + searchCount).style.display = 'inline';
		break;
	case "REGION":
		document.getElementById('searchTerm' + searchCount).style.display = 'none';
		document.getElementById('searchRegion' + searchCount).style.display = 'inline';
		break;
	case "CITIZENSHIP":
		document.getElementById('searchTerm' + searchCount).style.display = 'none';
		document.getElementById('searchCitizenship' + searchCount).style.display = 'inline';
		break;
	case "LANGUAGE":
		document.getElementById('searchTerm' + searchCount).style.display = 'none';
		document.getElementById('searchLanguage' + searchCount).style.display = 'inline';
		document.getElementById('searchProficiency' + searchCount).style.display = 'inline';
		break;
	case "AVAILABILITY":
		document.getElementById('searchTerm' + searchCount).style.display = 'none';
		document.getElementById('searchAvailability' + searchCount).style.display = 'inline';
		break;
	}
}

function retrieveSearchAoEs(searchCount) {
	var currentAoECategoryId = document.getElementById('searchAoECategory' + searchCount).value;
	currentSearchCount = searchCount;
	
	// instantiant the http object which supports the ajax call
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	// create the url to the functional page
	var url = "/includes/ajax/searchAOEChoices.asp";
	url=url+"?AoECategoryId="+currentAoECategoryId;

	// set the method that will be called after the request is complete
	xmlhttp.onreadystatechange=responseSearchAoEs;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function responseSearchAoEs() {
	if (xmlhttp.readyState==4)
	{
		var aoeResponse = xmlhttp.responseText;
		var aoeReponseArray = aoeResponse.split("||");
		
		// split the string and loop through to add all the option items to the dropdown
		var aoeSelect = document.getElementById('searchAoE' + currentSearchCount);
		// remove all the select options
		removeAllOptions(aoeSelect); // exists in global.js
		
		var newOptionBlank = document.createElement('option');
		newOptionBlank.text = "---";
		newOptionBlank.value = "-9999";
		try
		{
			aoeSelect.add(newOptionBlank, null); // standards compliant
		}
		catch(ex)
		{
			aoeSelect.add(newOptionBlank); // IE only
		}
		
		var count = 0;
		while (count < aoeReponseArray.length) {
			if (aoeReponseArray[count].length > 0) {
				var aoeReponseItemArray = aoeReponseArray[count].split("%%");
				// create the new select option element and add value and text
				var newOption = document.createElement('option');
				newOption.text = aoeReponseItemArray[1];
				newOption.value = aoeReponseItemArray[0];
				// add the new option
				try
				{
					aoeSelect.add(newOption, null); // standards compliant
				}
				catch(ex)
				{
					aoeSelect.add(newOption); // IE only
				}
			}
			count+=1;
		}
	}	
}

function addLanguageRow(currentLanguageCount, language, proficiency) {
	// if this is the first row, then make the column headers visible
	if (document.getElementById('languageRows').innerHTML == "") {
		document.getElementById('languageColumnHeaders').style.visibility='visible';
	}

	// update the current language count
	languageCount++;
	
	// instantiant the http object which supports the ajax call
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	// create the url to the functional page
	var url = "/includes/ajax/languageentry.asp";
	url=url+"?languageCount="+languageCount;
	if (language != undefined && proficiency != undefined) {
		// language was an input parameter include the language and proficiency in the url
		// this is for updating
		url=url+"?language="+language;
		url=url+"?proficiency="+proficiency;
	}

	// set the method that will be called after the request is complete
	xmlhttp.onreadystatechange=responseAddLanguageRow;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}




function addContactRow(currentLanguageCount, language, proficiency) {
	// if this is the first row, then make the column headers visible
	if (document.getElementById('contactRows').innerHTML == "") {
		document.getElementById('contactColumnHeaders').style.visibility='visible';
	}

	// update the current language count
	contactCount++;
	
	// instantiant the http object which supports the ajax call
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	// create the url to the functional page
	var url = "/includes/ajax/contacteentry.asp";
	url=url+"?contactCount="+contactCount;
	if (contact != undefined && proficiency != undefined) {
		// language was an input parameter include the language and proficiency in the url
		// this is for updating
		url=url+"?language="+language;
		url=url+"?proficiency="+proficiency;
	}

	// set the method that will be called after the request is complete
	xmlhttp.onreadystatechange=responseAddLanguageRow;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}



function addHireRow(currentLanguageCount, language, proficiency) {
	// if this is the first row, then make the column headers visible
	if (document.getElementById('languageRows').innerHTML == "") {
		document.getElementById('languageColumnHeaders').style.visibility='visible';
	}

	// update the current language count
	languageCount++;
	
	// instantiant the http object which supports the ajax call
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	// create the url to the functional page
	var url = "/includes/ajax/languageentry.asp";
	url=url+"?languageCount="+languageCount;
	if (language != undefined && proficiency != undefined) {
		// language was an input parameter include the language and proficiency in the url
		// this is for updating
		url=url+"?language="+language;
		url=url+"?proficiency="+proficiency;
	}

	// set the method that will be called after the request is complete
	xmlhttp.onreadystatechange=responseAddLanguageRow;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}







function responseAddLanguageRow () {
	if (xmlhttp.readyState==4)
	{
		// create the div for the html
		var divTag = document.createElement("div");
		divTag.id = "languageTable" + languageCount;
		divTag.className ="gridBodyTable languageTable";
		divTag.innerHTML = xmlhttp.responseText;
		document.getElementById('languageRows').appendChild(divTag);
	}	
}

function addPositionRow(currentPositionCount) {
	// if this is the first row, then make the column headers visible
	if (document.getElementById('positionRows').innerHTML == "") {
		document.getElementById('positionColumnHeaders').style.visibility='visible';
	}
	
	// update the current language count
	positionCount++;
	
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	var url = "/includes/ajax/positionentry.asp";
	url=url+"?positionCount="+positionCount;
	
	xmlhttp.onreadystatechange=responseAddPositionRow;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function responseAddPositionRow () {
	if (xmlhttp.readyState==4)
	{
		document.getElementById('positionRows').innerHTML = document.getElementById('positionRows').innerHTML + xmlhttp.responseText;
	}	
}

function addSearchRow() {
	// set cursor to wait
	cursor_wait();
	
	// update the current language count
	searchCount++;
	
	xmlhttp = GetXmlHttpObject();
	if (xmlhttp == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	var url = "/includes/ajax/searchentry.asp";
	url=url+"?searchCount="+searchCount;
	
	xmlhttp.onreadystatechange=responseAddSearchRow;
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function responseAddSearchRow () {
	if (xmlhttp.readyState==4)
	{
		document.getElementById('searchRows').innerHTML = document.getElementById('searchRows').innerHTML + xmlhttp.responseText;
		
		// set cursor to default
		cursor_clear();
	}
}

function redoSearchFromReturn(searchParameters) {
	if (searchParameters.length > 0) {
		// clear the current search term display
		document.getElementById('searchTermDisplayDB').innerHTML = '';
		xmlhttpTerm = GetXmlHttpObject();
		if (xmlhttpTerm == null) {
			// browser does not support ajax TODO: handle this error
		}
		
		// set the hidden variable of the param string to the params, this is used to return search results after viewing an individual profile
		document.getElementById('hdnSearchParams').value = searchParameters;
		document.getElementById('hdnEmailSearchParams').value = searchParameters;
		
		var url = "/includes/ajax/searchTermDisplayDB.asp";
		var params = searchParameters;
		xmlhttpTerm.open("POST", url, true);
	
		//Send the proper header information along with the request
		xmlhttpTerm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttpTerm.setRequestHeader("Content-length", params.length);
		xmlhttpTerm.setRequestHeader("Connection", "close");
	
		xmlhttpTerm.onreadystatechange = responseFieldSearchTermDisplay;
		xmlhttpTerm.send(params);
		
		// second do the search itself
		// clear the current search results
		document.getElementById('ResultsRowsDB').innerHTML = '';
		xmlhttpSearch = GetXmlHttpObject();
		var url = "/includes/ajax/searchresultsDB.asp";
		xmlhttpSearch.open("POST", url, true);
	
		//Send the proper header information along with the request
		xmlhttpSearch.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlhttpSearch.setRequestHeader("Content-length", params.length);
		xmlhttpSearch.setRequestHeader("Connection", "close");
	
		xmlhttpSearch.onreadystatechange = responseSearchResults;
		xmlhttpSearch.send(params);
	}
}

function requestFieldSearch() {
	// set cursor to wait
	cursor_wait();
	
	// first do the search term display
	// clear the current search term display
	document.getElementById('searchTermDisplayDB').innerHTML = '';
	xmlhttpTerm = GetXmlHttpObject();
	if (xmlhttpTerm == null) {
		// browser does not support ajax TODO: handle this error
	}
	
	var url = "/includes/ajax/searchTermDisplayDB.asp";
	var params = "";
	params = createParamsString('searchFormDB');
	xmlhttpTerm.open("POST", url, true);

	//Send the proper header information along with the request
	xmlhttpTerm.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpTerm.setRequestHeader("Content-length", params.length);
	xmlhttpTerm.setRequestHeader("Connection", "close");

	xmlhttpTerm.onreadystatechange = responseFieldSearchTermDisplay;
	xmlhttpTerm.send(params);
	
	// set the hidden variable of the param string to the params, this is used to return search results after viewing an individual profile
	document.getElementById('hdnSearchParams').value = params;
	document.getElementById('hdnEmailSearchParams').value = params;	
	
	// second do the search itself
	// clear the current search results
	document.getElementById('ResultsRowsDB').innerHTML = '';
	xmlhttpSearch = GetXmlHttpObject();
	var url = "/includes/ajax/searchresultsDB.asp";
	xmlhttpSearch.open("POST", url, true);

	//Send the proper header information along with the request
	xmlhttpSearch.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpSearch.setRequestHeader("Content-length", params.length);
	xmlhttpSearch.setRequestHeader("Connection", "close");

	xmlhttpSearch.onreadystatechange = responseSearchResults;
	xmlhttpSearch.send(params);
	
	
	// third do the search to create the export table
	document.getElementById('exportHtml').value = '';
	xmlhttpExport = GetXmlHttpObject();
	var url = "/includes/ajax/searchresultsExport.asp";
	xmlhttpExport.open("POST", url, true);

	//Send the proper header information along with the request
	xmlhttpExport.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlhttpExport.setRequestHeader("Content-length", params.length);
	xmlhttpExport.setRequestHeader("Connection", "close");

	xmlhttpExport.onreadystatechange = responseExportResults;
	xmlhttpExport.send(params);
}

function responseFieldSearchTermDisplay() {
	if (xmlhttpTerm.readyState==4)
	{
		document.getElementById('searchResultsDB').className = 'resultsDisplay';
		document.getElementById('searchTermDisplayDB').innerHTML = xmlhttpTerm.responseText;
	}
}

function responseSearchResults() {
	if (xmlhttpSearch.readyState==4)
	{
		document.getElementById('searchResultsDB').className = 'resultsDisplay';
		document.getElementById('ResultsRowsDB').innerHTML = xmlhttpSearch.responseText;
		
		// set cursor to default
		cursor_clear();
	}
}

function responseExportResults() {
	if (xmlhttpExport.readyState==4)
	{
		document.getElementById('exportHtml').value = xmlhttpExport.responseText;
	}
}

function createParamsString(formId) {
	// clear the hdnSearchParams and hdnEmailSearchParams
	document.getElementById('hdnSearchParams').value = "";
	document.getElementById('hdnEmailSearchParams').value = "";	
	
	// retrieve all the params from the form and add to a querystring formatted string
	var strParam = "";
	strParam = "?param1=";
	
	var frm = document.forms[formId];
	for (var i=0;i<frm.length;i++) {
		strParam = strParam + '&' + frm.elements[i].id + '=' + frm.elements[i].value;
	}
	
	return strParam;
}
