// Firstname,Surname,Email,Phone,Mobile,Postcode,currentSalary,benefitsValue,currentPackage,requiredSalary,canRelocate,heardAboutUs

function checkPassword(of)
{
	var validated = false;
	var pwd1 = document.getElementById('newpwd').value;
	var pwd2 = document.getElementById('cfmpwd').value;

	if ( pwd1 != pwd2 ) {
		alert('Sorry, the passwords do not match. Please re-enter your new password');
		return false;
	} 
	
	if ( pwd1.length < 6 ) {
		alert('Sorry, your new password must be at least 6 characters');
		return false;
	}
	
	return true;	
}

function checkform(of)
{
	// Test if DOM is available and there is an element called required
	if(!document.getElementById || !document.createTextNode){return;}
	if(!document.getElementById('required')){return;}

	var validated = true;

	// Define error messages and split the required fields
	var errorID='errormsg';
	var errorClass='dropDownLoadErr';
	var defaultClass = 'dropDownLoad';

	var reqfields=document.getElementById('required').value.split(',');
	

	// Insist on relocation info if canRelocate is selected	
	if (document.getElementById('canRelocate') ) {	
		if ( document.getElementById('canRelocate').selectedIndex == 1 ) {
			reqfields.push("relocateInfo");
			reqfields.push("visaStatus");
		} else {
			document.getElementById('relocateInfo').className = defaultClass;
			document.getElementById('visaStatus').className = defaultClass;
		}
	}
	
	// Insist on a Charity Number if applicable
	if (document.getElementById('CompanyType') ) {	
		if ( document.getElementById('CompanyType').selectedIndex == 0 ) {
			reqfields.push("CharityNo");
		} else {
			document.getElementById('CharityNo').className = defaultClass;
		}
	}

	// remove old images and classes from the required fields
	for(var i=0;i<reqfields.length;i++)
	{
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		if(f.previousSibling && /img/i.test(f.previousSibling.nodeName))
		{
			f.parentNode.removeChild(f.previousSibling);
		}
		f.className=defaultClass;
	}
	// loop over required fields
	for(var i=0;i<reqfields.length;i++)
	{
		// check if required field is there
		var f=document.getElementById(reqfields[i]);
		if(!f){continue;}
		// test if the required field has an error, 
		// according to its type
		switch(f.type.toLowerCase())
		{
			case 'text':
				if(f.value=='' && f.id!='Email'){cf_adderr(f)}							
				// email is a special field and needs checking
				if(f.id=='Email' && !cf_isEmailAddr(f.value)){cf_adderr(f)}							
				break;
			case 'file':
				if(f.value==''){cf_adderr(f)}
				break;
			case 'textarea':
				if(f.value==''){cf_adderr(f)}							
				break;
			case 'checkbox':
				if(!f.checked){cf_adderr(f)}							
				break;
			case 'select-one':
				if(!f.selectedIndex && f.selectedIndex==0){cf_adderr(f)}							
				break;
		}
	}
	
	if ( !validated ) {
		alert("Please review this form and complete the fields that are marked in red.");
	}
	
	// The form has been validated, but now check they have agreed to our T's & C's

	if ( validated && (document.getElementById("agreeTCs").checked == false)) {
		alert("To complete your registration you must agree to the Allen & York Terms & Conditions");
		validated = false;
	}
	
	return validated;

	/* Tool methods */
	function cf_adderr(o)
	{
		o.className=errorClass;
		validated = false;
	}

	function cf_isEmailAddr(str) 
	{
	    return str.match(/^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/);
	}
}

function numbersonly(e){
	var unicode=e.charCode? e.charCode : e.keyCode
	if (unicode!=8){ //if the key isn't the backspace key (which we should allow)
		if (unicode<48||unicode>57) //if not a number
		return false //disable key press
	}
}	


function calcPackageValue() 
{
	var total = 0;
	var curSal = parseInt(document.getElementById("currentSalary").value);
	var benPac = parseInt(document.getElementById("benefitsValue").value);
	
	if (isNaN(curSal)==1) { curSal = 0; }
	if (isNaN(benPac)==1) { benPac = 0; }
		
	total = curSal + benPac;
	
	document.getElementById("currentPackage").value = total;
}

function checkNumeric(objName,minval, maxval,comma,period,hyphen)
{
	var numberfield = objName;
	if (chkNumeric(objName,minval,maxval,comma,period,hyphen) == false)
	{
		//numberfield.select();
		//numberfield.focus();
		numberfield.value = "";
		return false;
	}
	else
	{
		return true;
	}
}

function chkNumeric(objName,minval,maxval,comma,period,hyphen)
{
	// only allow 0-9 be entered, plus any values passed
	// (can be in any order, and don't have to be comma, period, or hyphen)
	// if all numbers allow commas, periods, hyphens or whatever,
	// just hard code it here and take out the passed parameters
	var checkOK = "0123456789" + comma + period + hyphen;
	var checkStr = objName;
	var allValid = true;
	var decPoints = 0;
	var allNum = "";
	
	for (i = 0;  i < checkStr.value.length;  i++)
	{
		ch = checkStr.value.charAt(i);
		for (j = 0;  j < checkOK.length;  j++)
		if (ch == checkOK.charAt(j))
		break;
		if (j == checkOK.length)
		{
			allValid = false;
			break;
		}
		if (ch != ",")
		allNum += ch;
	}
	if (!allValid)
	{	
		alertsay = "Please enter only these values \""
		alertsay = alertsay + checkOK + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
	
	// set the minimum and maximum
	var chkVal = allNum;
	var prsVal = parseInt(allNum);
	if (chkVal != "" && !(prsVal >= minval && prsVal <= maxval))
	{
		alertsay = "Please enter a value greater than or "
		alertsay = alertsay + "equal to \"" + minval + "\" and less than or "
		alertsay = alertsay + "equal to \"" + maxval + "\" in the \"" + checkStr.name + "\" field."
		alert(alertsay);
		return (false);
	}
}

