
// validate security form

var retVal=true;

function validateForm()
{
	retVal = true;
	clearPointers();
	var f = document.forms['mainform'];
	if (f.fname.value=='') al("First Name", "fname");
	if (f.lname.value=='') al("Last Name", "lname");
	if (f.company.value=='') al("Company Name", "company");
	if (f.addr1.value=='') al("Address Line 1", "addr1");
	if (f.postcode.value=='') al("postcode", "postcode");
	if (f.country.value=='') al("country", "country");
	if (f.phone.value=='') al("phone", "phone");
	if (f.email.value=='') al("email", "email");
	if (f.agree.checked!=true && retVal)
	{
		alert("You MUST have read and agreed to the Terms and Conditions before applying")
		retVal = false;
		createPointers("agree");
	}
	if ((f.email.value.indexOf('@')==-1 || f.email.value.indexOf('.')==-1) && retVal)
	{
		alert("The Email address you provided does not appear to be valid, please re-renter the information");
		retVal=false;
		createPointers("email");
	}
	if (f.phone.value!='' && retVal)
	{
		var x=-1;
		for (ctr=0; ctr<f.phone.value.length; ctr++)
		{
			x = ascii(f.phone.value.charAt(ctr));
			if (x!=32 && (x<48 || x>57))
				{
					retVal=false;
					alert("The telephone number you entered does not appear to be valid.  Please re-renter the infromation");
					createPointers("phone");
					break;
				}
		}
	}
	if (f.phone.value.length<6 && retVal)
	{
		retVal=false;
		alert("The telephone number you entered does not appear to be valid.  Please re-renter the infromation");
		createPointers("phone");
	}
	return retVal;
}

function al(s, i)
{
	createPointers(i);
	if (retVal==false) return;
	alert(s.toUpperCase() + " is a required field.  Please enter the relevant information and try again");
	retVal = false;
}

function createPointers(i)
{
	// add a red highlight to required fields
	document.forms['mainform'].elements[i].style.border="solid #FF0000 2px;";
}

function clearPointers()
{
	// remove all required field highlights
	document.forms['mainform'].elements["fname"].style.border="";
	document.forms['mainform'].elements["lname"].style.border="";
	document.forms['mainform'].elements["company"].style.border="";
	document.forms['mainform'].elements["addr1"].style.border="";
	document.forms['mainform'].elements["postcode"].style.border="";
	document.forms['mainform'].elements["country"].style.border="";
	document.forms['mainform'].elements["phone"].style.border="";
	document.forms['mainform'].elements["email"].style.border="";
	document.forms['mainform'].elements["agree"].style.border="";
}


function ascii(c)
{
	c = c . charAt (0);
	var i;
	for (i = 0; i < 256; ++ i)
	{
		var h = i . toString (16);
		if (h . length == 1)
			h = "0" + h;
		h = "%" + h;
		h = unescape (h);
		if (h == c)
			break;
	}
	return i;
}

