var constraints = new Array( /^[1-9][0-9]{4,6}$/, //Mortgage Amount
							 /^[1-6]{1}$/, //Purpose
							 /^[1-2]{1}$/, //Joint/Single App
							 /^[1-4]{1}$/, //App 1 Title
							 /^[a-z\s]{1,15}$/i, //App 1 Firstname
							 /^[a-z\s]{1,20}$/i, //App 1 Surname
							 /^[1-8]{1}$/, //App 1 Employment Status
							 /^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-5]{1}$/, //App 2 Title
							 /^[a-z\s]{1,15}$/i, //App 2 Firstname
							 /^[a-z\s]{1,20}$/i, //App 2 Surname
							 /^[1-8]{1}$/, //App 2 Employment Status
							 /^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)
							 /(^[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-9\s]{10,15}$/, //Day Phone
							 /^[0-9\s]{10,15}$|^$/, //Work Phone
							 /^[0-9\s]{10,15}$|^$/, //Mobile Phone
							 /^[0-9a-z\s]{1,40}$/i, //Address 1
							 /^[a-z\s]{1,40}$/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
							 /^y{1}$/i ); //Accept Terms

var dob1_was_valid = false;
var dob2_was_valid = false;
var full_check = false;

var second_applicant = 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 == 2 )
	{
		if( element.checked )
			element.value = "1";
		else if( document.getElementById( "in" + elId + "a" ).checked )
			element.value = "2";
		else
			element.value = "0";
	}else if( elId == 24 ){//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 == 25 ){//Accept terms
		element.checked ? element.value = "Y" : element.value = "N";
	}

	if( elId == 7 || elId == 8 || elId == 9 ){//DOB
		if( compoundValidate( 7,8,9 ) )
		{
			showValid( true, 7 );
			dob1_was_valid = true;
			return true;
		}else if( dob1_was_valid || full_check){
			showValid( false, 7 );
			return false;
		}		
	}else if( elId == 14 || elId == 15 || elId == 16 ){//DOB
		if( compoundValidate( 14,15,16 ) )
		{
			showValid( true, 14 );
			dob2_was_valid = true;
			return true;
		}else if( dob2_was_valid || full_check ){
			showValid( false, 14 );
			return false;
		}
	}else{

		if( isValid( constraints[ elId ], element.value ) )
		{
			showValid( true, elId );
			return true;
		
		}else{
			showValid( false, elId );
			return false;
		}
	}
	}catch(err){ /*alert( 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 ) )
		{
			return true;
		}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 == 19 || elId == 20 )
				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()
{
	full_check = true;
	valid = true;
	for( i = 0; i < constraints.length; i++ )
	{
		if( !( /^1[0-6]$/.test( i ) ) || (/^1[0-6]$/.test( i ) && second_applicant) )
		{
			if( !validate( i ) )
				valid = false;
		}
	}

	if( valid )
	{
		document.getElementById('submit').src = wait_btn.src;
		document.getElementById('submit').onmouseout = "";
		document.getElementById('submit').onmouseover = "";
	}

	return valid;
}

var el;

function init_applicant()
{
	el = document.getElementById('second_applicant');
	hide_second_applicant();
}

function hide_second_applicant()
{
	el.style.visibility="hidden";
	el.style.display="none";
	second_applicant = false;
}
						
function show_second_applicant()
{
	el.style.visibility="visible";
	el.style.display="block";
	second_applicant = true;
}