var constraints = new Array( /^[1-9][0-9]{3,5}$/, //Amount
							 /^[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
							 /^[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
							 /(^[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,26}$/i, //House No. / Name
							 /^[0-9a-z\s]{1,40}$/i, //Street
							 /^[a-z\s]{1,40}$/i, //Town
							 /^[1-9]{1}[0-8]{0,1}$/i, //County
							 /^[a-z]{1,2}[0-9][0-9a-z]{0,1} {1}[0-9][a-z]{2}$/i, //Postcode
							 /^[1-6]$/, // Residency Status
							 /^y{1}$/i ); //Accept Terms

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 == 1 )
	{
		if( element.checked )
			element.value = "1";
		else if( document.getElementById( "in" + elId + "a" ).checked )
			element.value = "2";
		else
			element.value = "0";
	}else if( elId == 18 ){//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 == 20 ){//Accept terms
		element.checked ? element.value = "Y" : element.value = "N";
	}

	if( isValid( constraints[ elId ], element.value ) )
	{
		showValid( true, elId );
		return true;
	
	}else{
		showValid( false, elId );
		return false;
	}
	}catch(err){ /*alert( 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 == 12 || elId == 13 )
				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( !( /^[6-9]$/.test( i ) ) || (/^[6-9]$/.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 = document.getElementById('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;
}