// JavaScript Document

function _Homeowner()
{
	var x = document.getElementById("divForm");
	
	if (x)
	{
		
		if (document.form1.homeowner.value == 'Yes')
		{
			var y = document.getElementById("divFormCopy");
			if (y) {x.innerHTML = y.innerHTML;}
		}
		else
			{ x.innerHTML = ''; }
	}
}

function _Validate()
{
	var msg="";
	var focusObj;
	
	with (document.form1)
	{
		if (homeowner.value == 0)
		{
			msg += "Please specify if you are a homeowner\n";
			if( !focusObj )
				focusObj = homeowner;
		}
		var x = document.getElementById("divForm");
		if( x && homeowner.value == 'Yes' && x.innerHTML.length == 0 )
		{
			_Homeowner();
		}
		if( homeowner.value == 'Yes' && home_value.value.length == 0 )
		{
			msg += "Please specify your home value\n";
			if( !focusObj )
				focusObj = home_value;
		}
		if( homeowner.value == 'Yes' && secured_debt_amount.value.length == 0 )
		{
			msg += "Please specify your secured debt amount\n";
			if( !focusObj )
				focusObj = secured_debt_amount;
		}
		if( homeowner.value == 'Yes' &&  mortgage_payments_missed.value == '-1' )
		{
			msg += "Please specify if you have missed any mortgage payments\n";
			if( !focusObj )
				focusObj = mortgage_payments_missed;
		}
		if( unsecured_debt_amount.value.length == 0 )
		{
			msg += "Please specify your unsecured debt amount\n";
			if( !focusObj )
				focusObj = unsecured_debt_amount;
		}
		
		if( companies_owed.value == '-1' )
		{
			msg += "Please specify how many comanies you owe\n";
			if( !focusObj )
				focusObj = companies_owed;
		}
		
		if( ccjs.value == '-1' )
		{
			msg += "Please specify how many CCJs you have received in the last 3 years\n";
			if( !focusObj )
				focusObj = ccjs;
		}
		
		if( monthly_disposable_income.value == '-1' )
		{
			msg += "Please specify what your monthly disposable income is\n";
			if( !focusObj )
				focusObj = monthly_disposable_income;
		}
		
		if( title.value == '0' )
		{
			msg += "Please specify what your title is\n";
			if( !focusObj )
				focusObj = title;
		}
		
		//******** Check Firstname
		if( first_name.value.length == 0 )
		{
			msg += "Please enter a valid First Name\n";
			if( !focusObj )
				focusObj = first_name;
		}
		else if( !isName(first_name.value) )
		{
			msg += "Please enter a valid First Name\n";
			if( !focusObj )
				focusObj = first_name;
		}	
	//******** Check Surname
		if( last_name.value.length == 0 )
		{
			msg += "Please enter a valid last name\n";
			if( !focusObj )
				focusObj = last_name;
		}
		else if( !isName(last_name.value) )
		{
			msg += "Please enter a valid last name\n";
			if( !focusObj )
				focusObj = last_name;
		}
		
		//******** Check Telephone
		if( phone.value.length == 0 )
		{
			msg += "Please enter your telephone number\n";
			if( !focusObj )
				focusObj = phone;
		}
		else if( !isTelephoneNum(phone.value) )
		{
			msg += "Please enter a valid telephone number\n";
			if( !focusObj )
				focusObj = phone;
		}
		//******** Check Telephone
		if( alt_phone.value.length == 0 )
		{
			msg += "Please enter your work or mobile telephone number\n";
			if( !focusObj )
				focusObj = alt_phone;
		}
		else if( !isTelephoneNum(alt_phone.value) )
		{
			msg += "Please enter a valid work or mobile telephone number\n";
			if( !focusObj )
				focusObj = alt_phone;
		}
			
		if( email.value.length == 0 )
		{
			msg += "Please enter a valid email address\n";
			if( !focusObj )
				focusObj = email;
		}
		else if( !isEmail(email.value) )
		{
			msg += "Please enter a valid email address\n";
			if( !focusObj )
				focusObj = email;
		}
			
		if( postcode.value.length == 0 )
		{
			msg += "Please enter your postcode\n";
			if( !focusObj )
				focusObj = postcode;
		}
		else if( !isPostcode(postcode.value) )
		{
			msg += "Please enter a valid postcode\n";
			if( !focusObj )
				focusObj = postcode;
		}	
			
		//******** Compile Error Msg		
		if (msg.length > 0 )
		{
			focusObj.focus();
			alert(msg);
		}
		else
		{				
			action = "apply.php";
			submit();
		}	
	}
}
function isTelephoneNum(pStr)
{
    var i;
	var j = pStr;
	var strValidChars = "0123456789 ";
	pStr = j.replace(/ +/g,'');	
	
    for (i = 0; i < pStr.length; i++)
    {
        var c = pStr.charAt(i);
		
		if(strValidChars.indexOf(c) < 0)  
		{	return false;
		}
    }

	var idxi = pStr.indexOf("01");
	var idxj = pStr.indexOf("02");
	var idxk = pStr.indexOf("07");
 	var len = pStr.length;

	return ( (len > 9 && len < 12) && ((idxi==0)||(idxj==0)||(idxk==0)) );
}

function isName(pStr)
{
	var nameexp = /^([a-z,']+\s*\-*)+$/i;
	
	return( nameexp.test(pStr) );
}

function isEmail(pStr)
{
	var objText=pStr;
	var reg01 = /(@.*@)|(\.\.)|(@\.)|\,|\;|\\|\!|\%|\$|\&|\*|\(|\)|\=|\s|\'|\"|(\.@)|(^\.)/;   // not valid
	var reg02 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; // is valid
		
	return( (!reg01.test(objText) && reg02.test(objText)) );	
}

function isPostcode(pStr)
{
	var postcodeexp = /^[a-z]{1,2}[0-9]{1,2}\s*\D{0,2}[0-9]{1,1}[a-z]{1,2}$/i;
	var objText=pStr;
	
	return( objText.match(postcodeexp) );
}
