function navJump(offset) {
	$('offset').value=offset;
	$('frmSearchResults').submit();
}

function submitform() {
	$('frmSearchResults').submit();
}
function refinesearch() {
	$('offset').value=1;
	$('txtSearch').value=$('txtRefineSearch').value;
	$('txtSubSearch').value=$('txtRefineSubSearch').value;
	$('fldSalaryRange').value=$('fldRSCacheSalaryRange').value;
	$('fldCompany').value=$('fldRSCacheCompany').value;		
	$('ckbfulltime').value=$('ckbRSCachefulltime').value;
	$('ckbpartime').value=$('ckbRSCachepartime').value;
	$('ckbcontract').value=$('ckbRSCachecontract').value;
	$('ckbtemporary').value=$('ckbRSCachetemporary').value;
	$('ckbpermanent').value=$('ckbRSCachepermanent').value;
	$('fldFunction').value=$('fldRSCacheFunction').value;
	if ( $('fldRSCacheLocation') && $('fldLocation') ) {
		$('fldLocation').value=$('fldRSCacheLocation').value;
	}

	$('frmSearchResults').submit();	
}
function sortindex() {
	$('offset').value=1;
	$('frmSearchResults').submit();	
}
function whynottry(pWhyNotTry) {
	$('offset').value=1;
	$('txtSearch').value=pWhyNotTry;
	$('txtSubSearch').value='';
	
	$('frmSearchResults').submit();	
}




function edit(pType)
{
	if (pType == 'salary') {	
		$('fldRSSalaryRange').selectedIndex=$('fldRSCacheSalaryRange').value;		
		Modalbox.show($('pnlSalary'), {title: $('pnlSalary_title').innerHTML, width: 300});
		
	} else if (pType == 'company') {				
		if ( $('fldRSCacheCompany').length == 0 ) {
			$('fldRSCompany').selectedIndex = 0;
		} else {
			selectload($('fldRSCompany'),$('fldRSCacheCompany').value);
		}
		Modalbox.show($('pnlCompany'), {title: $('pnlCompany_title').innerHTML, width: 350});
	} else if (pType == 'jobtype') {					
		if ( $('ckbRSCachefulltime').value.length > 0 ) {
			$('ckbRSfulltime').checked=true			
		} else {
			$('ckbRSfulltime').checked=false
		}
		if ( $('ckbRSCachepartime').value.length > 0 ) {
			$('ckbRSpartime').checked=true			
		} else {
			$('ckbRSpartime').checked=false
		}
		if ( $('ckbRSCachecontract').value.length > 0 ) {
			$('ckbRScontract').checked=true			
		} else {
			$('ckbRScontract').checked=false
		}
		if ( $('ckbRSCachetemporary').value.length > 0 ) {
			$('ckbRStemporary').checked=true			
		} else {
			$('ckbRStemporary').checked=false
		}	
		if ( $('ckbRSCachepermanent').value.length > 0 ) {
			$('ckbRSpermanent').checked=true			
		} else {
			$('ckbRSpermanent').checked=false
		}	
		Modalbox.show($('pnlJobType'), {title: $('pnlJobType_title').innerHTML, width: 300});
	} else if (pType == 'function') {				
		if ( $('fldRSCacheFunction').value=='0' ) {
			$('fldRSFunction').selectedIndex = 0;
		} else {
			selectload($('fldRSFunction'),$('fldRSCacheFunction').value);
		}	
		Modalbox.show($('pnlFunction'), {title: $('pnlFunction_title').innerHTML, width: 350});
	} else if (pType == 'location') {		
		if ( $('fldRSCacheLocation').value.length == 0 ) {
			$('fldRSLocation').selectedIndex = 0;
		} else {
			selectload($('fldRSLocation'),$('fldRSCacheLocation').value);
		}				
		Modalbox.show($('pnlLocation'), {title: $('pnlLocation_title').innerHTML, width: 300});		
	} else if (pType == 'thankyou') {
		Modalbox.show($('pnlAjax_thankyou'), {title: $('pnlAjax_thankyou_title').innerHTML, width: 300, height: 220});
	} else if (pType == 'errorjobsavedsearch') {		
		Modalbox.show($('pnlAjax_errorGeneral'),  {title: $('pnlAjax_errorGeneral_title').innerHTML, width: 300});		
	} else if (pType == 'waitjobsavedsearch') {
		Modalbox.show('<div>Waiting<\/div>', {title: $('pnlAjax_SavingNotification_title').innerHTML, width: 300, height: 120, transitions: false});
	} else if (pType == 'erroralreadysavedjob') {
		Modalbox.show($('pnlAjax_errorAlreadySaved'),  {title: $('pnlAjax_errorAlreadySaved_title').innerHTML, width: 300, height: 140});	
	}	

}

function edit_onConfirm(pType)
{	
	if (pType == 'salary') {		
		$('fldRSCacheSalaryRange').value=$('fldRSSalaryRange').options[$('fldRSSalaryRange').selectedIndex].value;
		
		if ( $('fldRSSalaryRange').options[$('fldRSSalaryRange').selectedIndex].value == '0' ) {
			$('editsalarytext').innerHTML = 'All';			
		} else {
			$('editsalarytext').innerHTML = $('fldRSSalaryRange').options[$('fldRSSalaryRange').selectedIndex].text;
		}										
	} else if (pType == 'company') {
		var strCompanies,strCompaniesText, aryResults;
				
		

		aryResults=selectgetJOBS($('fldRSCompany'),$('txtSearchType').value);
		strCompanies=aryResults[0];
		strCompaniesText=aryResults[1];		
		
		if ( strCompanies.length > 0 ) {				
			$('editcompanytext').innerHTML=strCompaniesText;		
			$('fldRSCacheCompany').value=strCompanies;			
		} else {
			$('editcompanytext').innerHTML="All";
			$('fldRSCacheCompany').value="";
		}
		
	} else if (pType == 'jobtype') {		
		var boolAllJobTypes=true;
		var strOutput='';

		

		if ( $('ckbRSfulltime').checked ) {
			$('ckbRSCachefulltime').value='1';
			strOutput=strOutput+'Full Time<br/>';
		} else {
			$('ckbRSCachefulltime').value='';
			boolAllJobTypes=false;		
		}
		if ( $('ckbRSpartime').checked ) {
			$('ckbRSCachepartime').value='1';
			strOutput=strOutput+'Part Time<br/>';
		} else {
			$('ckbRSCachepartime').value='';
			boolAllJobTypes=false;		
		}
		if ( $('ckbRScontract').checked ) {
			strOutput=strOutput+'Contract<br/>';
			$('ckbRSCachecontract').value='1';
		} else {
			$('ckbRSCachecontract').value='';
			boolAllJobTypes=false;		
		}
		if ( $('ckbRStemporary').checked ) {
			$('ckbRSCachetemporary').value='1';
			strOutput=strOutput+'Temporary<br/>';	
		} else {
			$('ckbRSCachetemporary').value='';
			boolAllJobTypes=false;		
		}
		if ( $('ckbRSpermanent').checked ) {
			$('ckbRSCachepermanent').value='1';
			strOutput=strOutput+'Permanent<br/>';	
		} else {
			$('ckbRSCachepermanent').value='';
			boolAllJobTypes=false;		
		}
		
		if (boolAllJobTypes) {
			$('editjobtypetext').innerHTML='All';
		} else {
			$('editjobtypetext').innerHTML=strOutput;
		}
	} else if (pType == 'function') {
		var strFunctions,strFunctionsText, aryResults;
	
		

		aryResults=selectgetJOBS($('fldRSFunction'),$('txtSearchType').value);
		strFunctions=aryResults[0];
		strFunctionsText=aryResults[1];		
		
		if ( strFunctions.length > 0 ) {				
			$('editfunctiontext').innerHTML=strFunctionsText;		
			$('fldRSCacheFunction').value=strFunctions;
		} else {
			$('editfunctiontext').innerHTML="All";
			$('fldRSCacheFunction').value="";
		}		
	} else if (pType == 'location') {
		var strLocations,strLocationsText, aryResults;
				
		

		aryResults=selectgetJOBS($('fldRSLocation'),$('txtSearchType').value);
	
		strLocations=aryResults[0];
		strLocationsText=aryResults[1];		
		
		if ( strLocations.length > 0 ) {				
			$('editlocationtext').innerHTML=strLocationsText;		
			$('fldRSCacheLocation').value=strLocations;			
		} else {
			$('editlocationtext').innerHTML="All";
			$('fldRSCacheLocation').value="";
		}				
	}
	
	highlightSearchButtons();
	changeSuggestionText('To view your refined search please click on "refine search" button.');
	
	close();
}

function close()
{	
	Modalbox.hide();
}

function onChange_SubSearch() {
	
	highlightSearchButtons();
}
function onChange_Search() {
	
	highlightSearchButtons();
}

function highlightSearchButtons() {
	Effect.Pulsate('btn_refine_search');

	
	/*if ( $('arearefineone').className.indexOf('hightlightchange') < 1 ) {
			$('arearefineone').className= $('arearefineone').className+' hightlightchange';
			$('arearefinetwo').className= $('arearefinetwo').className+' hightlightchange';
	}*/	
}

function jobsearch_validateForm() {
	var errorMessage = "";

	if ($('txtSearchType').value == 'uk' ) {
		validateLocation($('fldLocation'));
	}
									
	if ($('ckbfulltime').checked == false && 
		$('ckbpartime').checked == false && 
		$('ckbcontract').checked == false && 
		$('ckbtemporary').checked == false &&
		$('ckbpermanent').checked == false ) {
		errorMessage = String('\n\rYou must select a job type.');
	}
	
	if ( !($('fldLocation') == null) ) 
	{
		if ($('fldLocation').selectedIndex < 0 )
		errorMessage = errorMessage + ' \n\rYou must select a location. ';	
	}
		
	if ( $('fldSortBy').value == '0' ) {
		errorMessage = errorMessage + ' \n\rYou must select a sort by order. ';
	}
	
	if (errorMessage.length > 0 ) {
		alert('ERROR '+errorMessage);		
		return false;
	}
	return true;
}

function jobsearch_onSubmit() {
	if ( jobsearch_validateForm() ) {
		$('frmJobSearch').submit();
	}						
}

function changeSuggestionText(pText) {
	
	$('alert_text').innerHTML=pText;

}

/* Stock Code */
function selectload(objSelect, strListOfSelected) {
	var optionsPOS, selectedPOS;
	
	aryItems = strListOfSelected.split(',');
			
	for ( optionsPOS = 0; optionsPOS < objSelect.options.length; optionsPOS++)  {
		for (selectedPOS=0; selectedPOS < aryItems.length ; selectedPOS++ ) {
			if ( isNaN(objSelect.options[optionsPOS].value) == false ) {
				if ( Number(objSelect.options[optionsPOS].value) == Number(aryItems[selectedPOS]) ) {
					objSelect.options[optionsPOS].selected=true;
					break;
				} else {
					objSelect.options[optionsPOS].selected=false;
				}
			}
			else {
				objSelect.options[optionsPOS].selected=false;
			}
		}
	}
}
	
function selectgetJOBS(objSelect,strSearchType) {
	var aryReturn = new Array(2); 
	var aryAbbrevations,strName;

	if (strSearchType == 'uk' && objSelect.id.indexOf('Location') > 0 ) {
		validateLocation(objSelect);
		aryAbbrevations=$('fldLocationAbbrv').value.split(',');
	}


	for ( optionsPOS = 0; optionsPOS < objSelect.options.length; optionsPOS++)  {
		var strName =objSelect.options[optionsPOS].text;
			
		if ( strName.indexOf('(') > 0 ) {
			strName = strName.slice(0,strName.indexOf('('));
		}			
		if (strSearchType == 'uk' && objSelect.id.indexOf('Location') > 0  ) {
			strName = aryAbbrevations[optionsPOS] + strName.slice(1,(strName.length));
		}
		
		if (objSelect.options[optionsPOS].selected && objSelect.options[optionsPOS].value != 0  ) {
			if (aryReturn[0] == undefined ) {
				aryReturn[0]=objSelect.options[optionsPOS].value;
				aryReturn[1]=strName;
			} else {
				aryReturn[0]=aryReturn[0]+","+objSelect.options[optionsPOS].value;
				aryReturn[1]=aryReturn[1]+"<BR/>"+strName;
			}				
		}
	}
	
	if (aryReturn[0] == undefined ) {
		aryReturn[0]='';
		aryReturn[1]='';
	}
	
	return aryReturn;
}

function validateLocation(objLocations) {
	var optionsPOS, objLocations;
	
	for ( optionsPOS = 0; optionsPOS < objLocations.options.length; optionsPOS++) {
		if ( isNaN(objLocations.options[optionsPOS].value) && objLocations.options[optionsPOS].selected ) {
			var SubPos;
					
			for (SubPos=optionsPOS+1; SubPos < objLocations.options.length;SubPos++ ) {
				if (isNaN(objLocations.options[SubPos].value) ) {
					SubPos--;
					break;
				}	
				if ( objLocations.options[SubPos].value < 1 ) {
					SubPos--;
					break;							
				}
						
				objLocations.options[SubPos].selected=true;
			}
			objLocations.options[optionsPOS].selected=false;
			optionsPOS=SubPos;
		}
	}			
}

/* 
 * Search Result Page
 */
Event.observe(window, 'load', function() {
									   

	if ($('search_toolbox')) {$('search_toolbox').appear( {duration:3.0});}
	
									   
	if ( !($('fldSavedSearch') == null )) { 

		$('fldSavedSearch').observe('focus', function(e){ clearSearchField('fldSavedSearch','email address') });
		$('fldSavedSearch').observe('blur', function(e){ unClearSearchField('fldSavedSearch','email address') });
		
	}	
	
	
});

function validateSavedSearch(){	
	var email = $('fldSavedSearch').value;
	
	if ( validateInput('email',email)==false ) {
		alert('You must enter a valid email address');		
	} else if ( email.length > 200 ) {
		alert('Your email address is to large');		
	} else {
		ajax_jobsavesearch(email);
	}
}


/*
 * Ajax Calls
 */
function ajax_jobsavesearch(tEmail) {
	edit('waitjobsavedsearch');
	var pars = 'ACT=jobsavedsearch&email=' + tEmail;
	var myAjax = new Ajax.Request(
		 url,
		 {  method: 'get',
		  parameters: pars,
		  onComplete: return_ajax_jobsavesearch
		});		
}	

function return_ajax_jobsavesearch(returnvalue) {
	var json=checkAJAXResult(returnvalue.responseText);
	
	if ( json == false ) {
	   edit('errorjobsavedsearch');				
	} else if (json.success == 2) {		
		edit('erroralreadysavedjob');		
	} else if (json.success  != 1) {
		edit('errorjobsavedsearch');
	} else {		
		edit('thankyou');
	}	
}

function checkAJAXResult(text) {
	return Try.these(
		function() {return text.evalJSON(true);}								 
	) || false;
}

var url='http://'+hostName+'/code/classes/ajax/reader/searchresults.asp';