
function ButtonPostBack(oButton) {      
	oButton.disabled=true;
	Postback(oButton.id,'');
}
	
function Postback(sCommand, sArgument) {
	document.forms[0].command.value=sCommand;
	document.forms[0].argument.value=sArgument;
	document.forms[0].submit();
}


function SetFocus(sControlID) {
	var oControl=window.document.getElementById(sControlID);
	if (oControl != null){oControl.focus()};
}

function PostcodeFocus(oTextbox) {

	if (oTextbox.value=='e.g. CV2') {
		oTextbox.value='';
	}
		
}

function ClearPostcode() {
	
	var oTextbox=document.getElementById('txtPostcodeDistrict');
	oTextbox.value='';
}

function PostcodeBlur(oTextbox) {

	var sPostcode=oTextbox.value;
	if (sPostcode!='' && sPostcode!='e.g. CV2') {
		
		document.getElementById('ddlRegion').selectedIndex=0;	
	}
}


function PostcodeReturn(oEvent) {

	var iKeyPress=iif(oEvent.keyCode,oEvent.keyCode, oEvent.which);
	if (iKeyPress==13) {
		oEvent.cancelBubble = true;
		oEvent.returnValue = false;
		ValidateSearch();
		return false;
	}


}

function iif(bCondition, oTrue, oFalse) {
	return bCondition ? oTrue : oFalse;
}


function ValidateSearch() {

	var oButton=document.getElementById('btnSearch');

	//get the vlaues off the form
	var oJobSector=window.document.getElementById('ddlJobSector');
	var sJobSectorRole=oJobSector.options[oJobSector.selectedIndex].value;
	var sJobText=oJobSector.options[oJobSector.selectedIndex].text;
	var oDistance=window.document.getElementById('ddlDistance');
	var sDistance=oDistance.options[oDistance.selectedIndex].text;
	var oPostcodeDistrict=window.document.getElementById('txtPostcodeDistrict')
	var sPostcodeDistrict=oPostcodeDistrict.value;
	var oRegion=window.document.getElementById('ddlRegion');
	var iRegion=oRegion.options[oRegion.selectedIndex].value;
	
	//do the simple validation 
	var sWarn=''
	if (sJobSectorRole=='') {
		sWarn+='A Type of Work must be specified.\n'
		oJobSector.className='dropdown error';
	} else {
		oJobSector.className='dropdown';
	}
	
	if ((sPostcodeDistrict=='' || sPostcodeDistrict=='e.g. CV2') && iRegion=='') {
		sWarn+='A Postcode District or Preferred Region must be specified.';
		oPostcodeDistrict.className='textbox error'
		oRegion.className='dropdown error'
	} else {
		oPostcodeDistrict.className='textbox';
		oRegion.className='dropdown';
	}
	
	if (sWarn!='') {
		alert(sWarn);
	} else {
		Postback('btnSearch', sJobSectorRole+'#'+sDistance+'#'+sPostcodeDistrict+'#'+iRegion+'#'+sJobText);
	}
		
}

function InvalidPostcode() {

	var oPostcodeDistrict=window.document.getElementById('txtPostcodeDistrict')
	oPostcodeDistrict.className='dropdown error';
	alert('A valid Postcode District must be specified');

}

function ValidateSignUp(oButton) {
	
	//get the values off the form
	var oEmail=window.document.getElementById('txtEmail');
	var sEmail=oEmail.value;
	var oTitle=window.document.getElementById('ddlTitle');
	var sTitle=oTitle.options[oTitle.selectedIndex].text;
	var oFirstName=window.document.getElementById('txtFirstName')
	var sFirstName=oFirstName.value;
	var oLastName=window.document.getElementById('txtLastName')
	var sLastName=oLastName.value;
	var oHomeTelephone=window.document.getElementById('txtHomeTelephone');
	var sHomeTelephone=oHomeTelephone.value;
	var oMobileTelephone=window.document.getElementById('txtMobileTelephone');
	var sMobileTelephone=oMobileTelephone.value;
	var oCV=window.document.getElementById('filCV');
	var sCV=oCV.value;
		
	//do the validation
	var sWarn=''
	if (sEmail==''||IsEmail(sEmail)==false) {
		sWarn+='A Valid Email Address must be specified.\n'
		oEmail.className='textbox error'
	} else {
		oEmail.className='textbox';
	}
	
	if (sTitle=='') {
		sWarn+='A Title must be specified.\n'
		oTitle.className='dropdown error'
	} else {
		oTitle.className='dropdown';
	}
	
	if (sFirstName=='') {
		sWarn+='A First Name must be specified.\n'
		oFirstName.className='textbox error'
	} else {
		oFirstName.className='textbox';
	}
	
	if (sLastName=='') {
		sWarn+='A Last Name must be specified.\n'
		oLastName.className='textbox error'
	} else {
		oLastName.className='textbox';
	}
	
	if (sHomeTelephone=='' && sMobileTelephone=='') {
		sWarn+='A Home or Mobile Telephone must be specified.\n'
		oHomeTelephone.className='textbox error'
		oMobileTelephone.className='textbox error'
	} else {
		oHomeTelephone.className='textbox';
		oMobileTelephone.className='textbox';
	}
	
	if (sCV!='') {
		var sExt=sCV.substring(sCV.length-3,sCV.length)
		if (sExt!='doc' && sExt!='rtf' && sExt!='pdf' && sExt!='ocx') {
			sWarn+='An Invalid file type has been selected, only .doc, .docx, .rtf and .pdf types are permitted.\n';
			oCV.className='error';
		} else {
			oCV.className='';
		}
	}
	
	if (sWarn!='') {
		alert(sWarn)
	} else {
		oButton.disabled=true;
		Postback(oButton.id,0);
	}

}

function IsEmail(sEmail) {
	 var sEmailRegEx = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	 var o = new RegExp(sEmailRegEx);
	 return o.test(sEmail);
}


function DropDownSelect(oList, oEvent, sAutoPostBack) {
				
	var oStoreKeyPress=document.getElementById('hid'+oList.name);
	
	if ((oEvent.keyCode==13)&&(sAutoPostBack!='true')) {
		return;
	} else if ((oEvent.keyCode==13)&&(sAutoPostBack=='true')) {
		Postback(oList.name,'')
	} else if (oEvent.keyCode!=27) {
		oStoreKeyPress.value=oStoreKeyPress.value+String.fromCharCode(oEvent.keyCode);
	} else {
		oStoreKeyPress.value='';
	}
	
	//find it
	SelectItemFromSearch(oList, oStoreKeyPress.value);
	return false;

}

function DropDownBackSpace(oList,oEvent) {
	var oStoreKeyPress=document.getElementById('hid'+oList.name);
	
	if (oEvent.keyCode==8) {
		oStoreKeyPress.value=oStoreKeyPress.value.substring(0,oStoreKeyPress.value.length-1);
	} else {
		return;
	}
	
	//find it
	SelectItemFromSearch(oList, oStoreKeyPress.value);		
	return false;
}

function SelectItemFromSearch(oList, sSearch) {
	for (var i=0; i<oList.options.length; i++) {
		if (oList.options[i].text.toLowerCase().indexOf(sSearch.toLowerCase())==0) {
			oList.selectedIndex=i;				
			return;
		}
	}
	oList.selectedIndex=0;
}

function ClearSelection(oList) {
	
	document.getElementById('hid'+oList.name).value='';
}





