var constraints = new Array( /^50$|^500$|^[1-9][0-9]{3,5}$/, //Loan Amount
							 /^[1-9][0-9]{0,1}$/, //Loan Term
							 /^[1-9]$|^[1][0-4]$/, //Purpose of loan
							 /^[1-5]$/, //Title
							 /^[a-z\s]{1,15}$/i, //Firstname
							 /^[a-z\s]{1,20}$/i, //Surname
							 /^0[1-9]$|^[1-2][0-9]$|^[3][0-1]$/, //DOB Day
							 /^0[1-9]$|^[1][0-2]$/, //DOB Month
							 /^19[3-9][0-9]$/, //DOB Year (1930-1999)
							 /^[1-7]$/, //Marital Status
							 /^[0-9\s]{10,15}$/, //Phone
							 /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z0-9]([a-z0-9_\.]*)@([a-z0-9_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i, //Email
							 /^[0-9a-z\s]{1,26}$/i, //House No/Name
							 /^[0-9a-z\s]{1,40}$/i, //Street
							 /^[0-9a-z\s]{0,30}$/i, //Area
							 /^[0-9a-z\s]{1,20}$/i, //Town
							 /^[1-9]{1}[0-8]{0,1}$/, //County
							 /^[a-z]{1,2}[0-9][0-9a-z]{0,1} {1}[0-9][a-z]{2}$/i, //Postcode
							 /^[1-8]$/, //Residency Status
							 /^[1-5]$/, //Time at address
							 /^[0-9a-z\s]{1,26}$/i, //Prev House No/Name
							 /^[0-9a-z\s]{1,40}$/i, //Prev Street
							 /^[0-9a-z\s]{0,30}$/i, //Prev Area
							 /^[0-9a-z\s]{1,20}$/i, //Prev Town
							 /^[1-9]{1}[0-8]{0,1}$/, //Prev County
							 /^[a-z]{1,2}[0-9][0-9a-z]{0,1} {1}[0-9][a-z]{2}$/i, //Prev Postcode
							 /^[1-7]$/, //Employment Status
							 /^[1-9]$|^[1][0-3]$/, //Occupation
							 /^[0-9a-z\s]{0,30}$/i, //Employer Name
							 /^[0-9a-z\s]{1,30}$/i, //Employer Street
							 /^[0-9a-z\s]{0,30}$/i, //Employer Area
							 /^[0-9a-z\s]{1,20}$/i, //Employer Town
							 /^[1-9]{1}[0-8]{0,1}$/, //Employer County
							 /^[a-z]{1,2}[0-9][0-9a-z]{0,1} {1}[0-9][a-z]{2}$/i, //Employer Postcode
							 /^[1-5]$/, //Time with employer
							 /^[0-9a-z\s]{0,30}$/i, //Prev Employer Name
							 /^[0-9a-z\s]{1,30}$/i, //Prev Employer Street
							 /^[0-9a-z\s]{0,30}$/i, //Prev Employer Area
							 /^[0-9a-z\s]{1,20}$/i, //Prev Employer Town
							 /^[1-9]{1}[0-8]{0,1}$/, //Prev Employer County
							 /^[a-z]{1,2}[0-9][0-9a-z]{0,1} {1}[0-9][a-z]{2}$/i, //Prev Employer Postcode
							 /^[1-5]$/, //Time with prev employer
							 /^y{1}$/i ); //Accept Terms

var dob_was_valid = false;

var previous_address = false;
var employer = false;
var previous_employer = false;

if (document.images)
{
	wait_btn= new Image(257,49); 
	wait_btn.src="images/btn_wait.gif"; 
}

function validate( elId )//, element )
{
	try{
	element = document.getElementById( "in" + elId );
	
	if( elId == 17 || elId == 25 || elId == 33 || elId == 40 ){//Postcode is a special case as needs formatting before the regex
		pcode = element.value;
		pcode = pcode.replace( / /, "" );
		pcode = (pcode.substr( 0, pcode.length -3 ) + " " + pcode.substr( pcode.length -3, pcode.length)).toUpperCase();
		element.value = pcode;
	}else if( elId == 42 ){//Accept terms
		element.checked ? element.value = "Y" : element.value = "N";
	}

	if( elId == 6 || elId == 7 || elId == 8 ){//DOB
		if( compoundValidate( 6,7,8 ) )
		{
			showValid( true, 6 );
			return true;
		}else{
			showValid( false, 6 );
			return false;
		}		
	}else{

		if( isValid( constraints[ elId ], element.value ) )
		{
			showValid( true, elId );
			return true;
		
		}else{
			showValid( false, elId );
			return false;
		}
	}
	}catch(err){}
}

function compoundValidate( one, two, three )
{
	try{
		e1 = document.getElementById( "in" + one );
		e2 = document.getElementById( "in" + two );
		e3 = document.getElementById( "in" + three );

		if( isValid( constraints[ one ], e1.value ) && isValid( constraints[ two ], e2.value ) && isValid( constraints[ three ], e3.value ) )
		{
			showValid( true, one );
			dob_was_valid = true;
			return true;
		}else if( dob_was_valid ){
			showValid( false, one );
			return false;
		}else{
			return false;
		}

	}catch(err){}
}

function isValid( constraint, value )
{	
	return constraint.test( value );
}

function showValid( state, elId )
{
	try{
		if( state )
			document.getElementById( "el" + elId ).src = "images/status/valid.gif";
		else{
			
			if( elId == 14 || elId == 22 || elId == 30 || elId == 37 )
				document.getElementById( "el" + elId ).src = "images/status/warn.gif";
			else
				document.getElementById( "el" + elId ).src = "images/status/invalid.gif";
		}
	}catch(err){}//Element is hidden

}

function lock( element )
{
	//element.disabled = true;
}

function validateForm()
{
	valid = true;
	for( i = 0; i < constraints.length; i++ )
	{
		if( !( /^2[0-5]$|^2[8-9]$|^3[0-4]$|^3[5-9]$|^4[0-1]$/.test( i ) ) || 
			(/^2[0-5]$/.test( i ) && previous_address) ||
			(/^2[8-9]$|^3[0-4]$/.test( i ) && employer) ||
			(/^3[5-9]$|^4[0-1]$/.test( i ) && previous_employer) )
		{
			if( !validate( i ) )
				valid = false;
		}
	}

	if( valid )
	{
		document.getElementById('submit').src = wait_btn.src;
		document.getElementById('submit').onmouseout = "";
		document.getElementById('submit').onmouseover = "";
	}

	return valid;
}

function setPreviousAddress( state )
{
	previous_address = state;
}
function setEmployer( state )
{
	employer = state;
}
function setPreviousEmployer( state )
{
	previous_employer = state;
}