
var exclude=/[^@\-\.\w]|^[_@\.\-]|[\._\-]{2}|[@\.]{2}|(@)[^@]*\1/;
var check=/@[\w\-]+\./;
var checkend=/\.[a-zA-Z]{2,3}$/;
var phoneexclude=/[0-9]{10,11}/;

function checkFields(form){
	var error = "";
	var todo = "";
	if ( form.Life_Cover_Amount.value == "" || form.Life_Cover_Amount.value < 5000 || form.Life_Cover_Amount.value > 10000000 || form.Life_Cover_Amount.value.search(/\D/)>-1){
		error= "Sorry - Please enter a valid amount of Life Cover between 5,000 - 10,000,000\n\n";
		todo="form.Life_Cover_Amount.focus();form.Life_Cover_Amount.select()";

		switchError('on','Life_Cover_Amount');

// Dimsey addition
	document.getElementById('two').style.display="none";
	document.getElementById('one').style.display="block"
// End of Dimsey addition
	}
	else switchError('off','Life_Cover_Amount');
	// product type: options 1=life insurance only; 3=life insurance with critical illness cover
	form.select_cover_type.value=(form.Critical_illness_cover.checked==true)?7:5;
	// level or decreasing cover: options 1=level; 2=decreasing
	form.lifecover_Liability_repayment_mortgage.value=(form.To_cover_mortgage.checked==true)?"Decrease":"Level";
	// guaranteed premium: options: Guaranteed or Reviewable
	form.Type_of_Lifecover_Premiums.value=(form.Guaranteed_premium.checked==true)?"Guaranteed":"Reviewable";
	if ( form.Name_App1.value == "" ){
		if(error=="")error= "Sorry - Applicant FIRST NAME not entered\n\n";
		if(todo=="")todo="form.Name_App1.focus()";
		switchError('on','Name_App1');
	}
	else switchError('off','Name_App1');
	if ( form.Surname_App1.value == "" ){
		if(error=="")error= "Sorry - Applicant SURNAME not entered\n\n";
		if(todo=="")todo="form.Surname_App1.focus()";
		switchError('on','Surname_App1');
	}
	else switchError('off','Surname_App1');
	if ( form.App1_DOB_Day.value == "dd" || form.App1_DOB_Month.value == "mm" || form.App1_DOB_Year.value == "yyyy"){
		if(error=="")error= "Sorry - Applicant BIRTH DATE not entered\n\n";
		//todo=( form.App1_DOB_Day.value == "dd" &&todo=="")"form.App1_DOB_Day.focus()":"";
		if ( form.App1_DOB_Day.value == "dd" ){
			if(todo=="")todo="form.App1_DOB_Day.focus()";
			switchError('on','App1_DOB_Day');
		}
		else switchError('off','App1_DOB_Day');
		
		if ( form.App1_DOB_Month.value == "mm" ){
			if(todo=="")todo="form.App1_DOB_Month.focus()";
			switchError('on','App1_DOB_Month');
		} 
		else switchError('off','App1_DOB_Month');
		
		if ( form.App1_DOB_Year.value == "yyyy" ){
			if(todo=="")todo="form.App1_DOB_Year.focus()";
			switchError('on','App1_DOB_Year');
		}
		else switchError('off','App1_DOB_Year');
	}
	else{
		switchError('off','App1_DOB_Day');
		switchError('off','App1_DOB_Month');
		switchError('off','App1_DOB_Year');
	}

	if ( form.Joint_App.checked == true ){
		if ( form.Name_App2.value == "" ){
			if(error=="")error= "Sorry - Second Applicant FIRST NAME not entered\n\n";
			if(todo=="")todo="form.Name_App2.focus()";
			switchError('on','Name_App2');
		}
		else switchError('off','Name_App2');
		if ( form.Surname_App2.value == "" ){
			if(error=="")error= "Sorry - Second Applicant SURNAME not selected\n\n";
			if(todo=="")todo="form.Surname_App2.focus()";
			switchError('on','Surname_App2');
		}
		else switchError('off','Surname_App2');
		if ( form.App2_DOB_Day.value == "dd" || form.App2_DOB_Month.value == "mm" || form.App2_DOB_Year.value == "yyyy"){
			if(error=="")error= "Sorry - Second Applicant BIRTH DATE not entered\n\n";
			if ( form.App2_DOB_Day.value == "dd" ){
				if(todo=="")todo="form.App2_DOB_Day.focus()";
				switchError('on','App2_DOB_Day');
			}
			else switchError('off','App2_DOB_Day');
			if ( form.App2_DOB_Month.value == "mm" ){
				if(todo=="")todo="form.App2_DOB_Month.focus()";
				switchError('on','App2_DOB_Month');
			}
			else switchError('off','App2_DOB_Month');

			if ( form.App2_DOB_Year.value == "yyyy" ){
				if(todo=="")todo="form.App2_DOB_Year.focus()";
				switchError('on','App2_DOB_Year');
			}
			else switchError('off','App2_DOB_Year');
		}
		else{
			switchError('off','App2_DOB_Day');
			switchError('off','App2_DOB_Month');
			switchError('off','App2_DOB_Year');
		}
	}
	var code1 = "";
	var checkCode = form.HomePhone.value;
	for ( i = 0; i <= checkCode.length; i++ )if ( checkCode.charAt(i) != " " )code1 += checkCode.charAt(i);
	if ( code1.search(phoneexclude) == -1 || code1.charAt(1) == "0" || code1.charAt(0) != "0" || code1 == "0123456789" || code1 == "01234567890" || code1 == "01252728800" || form.HomePhone.value.search(/\D/)>-1){
		if(error=="")error= "Sorry - HOME Phone Number not recognised : Example '01252728800'\n\n";
		if(todo=="")todo="form.HomePhone.focus();form.HomePhone.select()";
		switchError('on','HomePhone');
	}
	else switchError('off','HomePhone');
	var code1 = "";
	var checkCode = form.WorkPhone.value;
	for ( i = 0; i <= checkCode.length; i++ )if ( checkCode.charAt(i) != " " )code1 += checkCode.charAt(i);
	if ( code1.search(phoneexclude) == -1 || code1.charAt(1) == "0" || code1.charAt(0) != "0" || code1 == "0123456789" || code1 == "01234567890" || code1 == "01252728800" || form.WorkPhone.value.search(/\D/)>-1){
		if(error=="")error= "Sorry - WORK Phone Number not recognised : Example '01252728800'\n\n";
		if(todo=="")todo="form.WorkPhone.focus();form.WorkPhone.select()";
		switchError('on','WorkPhone');
	}
	else switchError('off','WorkPhone');
	if ( form.MobilePhone.value != "" ){
		var code1 = "";
		var checkCode = form.MobilePhone.value;
		for ( i = 0; i <= checkCode.length; i++ )if ( checkCode.charAt(i) != " " )code1 += checkCode.charAt(i);
		if ( code1.search(phoneexclude) == -1 || code1.charAt(1) == "0" || code1.charAt(0) != "0" || code1 == "0123456789" || code1 == "01234567890" || code1 == "01252728800" || form.MobilePhone.value.search(/\D/)>-1){
			if(error=="")error= "Sorry - MOBILE Phone Number not recognised : Example '01252728800'\n\n";
			if(todo=="")todo="form.MobilePhone.focus();form.MobilePhone.select()";
			switchError('on','MobilePhone');
		}
		else switchError('off','MobilePhone');
	}
	if ((((form.Email.value.search(exclude) != -1)||(form.Email.value.search(check)) == -1)||(form.Email.value.search(checkend) == -1))&&form.Email.value!=""){
		if(error=="")error= "Sorry - Valid EMAIL ADDRESS not entered\n\n";
		if(todo=="")todo="form.Email.focus();form.Email.select()"; 
		switchError('on','Email');
	}
	else switchError('off','Email');

	if(error=="") return true;
	else{
		alert(error);
		eval(todo);
	return false;
	}
}
function amountFilter (input){
	if (input.value != ""){
		if(input.value.search(/k/i)>-1){
			s=input.value.replace(/k/i,"000");
			input.value=s;
		}
		if(input.value.search(/\.00+/)>-1){
			s=input.value.replace(/\.00+/gi,"");
			input.value=s;
		}
		if(input.value.search(/\D/)>-1){
			s=input.value.replace(/\D/gi,"");
			input.value=s;
		}
		return;
	}
}
function disp_toggle(form){
	c=(form.Joint_App.checked==true)?"":"none";
	d=(form.Joint_App.checked==true)?"none":"";
	document.getElementById('App2_hda').style.display=c;
	document.getElementById('App2_hdb').style.display=c;
	document.getElementById('App2_hdc').style.display=c;
	document.getElementById('App2_hdd').style.display=c;
	document.getElementById('App2_hde').style.display=c;
	document.getElementById('App2_hdf').style.display=c;
	document.getElementById('App2_hdg').style.display=c;
	document.getElementById('App2_hdh').style.display=c;
	document.getElementById('App2_hdi').style.display=c;
	document.getElementById('App2_hdj').style.display=c;
	document.getElementById('App2_hdk').style.display=c;
	document.getElementById('five_pounds').style.display=d;
}
function switchError(val, fieldName){
	document.getElementById(fieldName).style.backgroundColor=(val=="on")?"#FFD3D3":"#FFFFFF";

}
function stringFilt(input){
	if (input.value != ""){
		if(input.value.search(/\D/)>-1){
			s=input.value.replace(/\D/gi,"");
			input.value=s;
		}
		return;
	}
}
function checkPhoneNum(tel){
	if ( tel.value.length == 10 ){
		alert("10 DIGIT PHONE NUMBER - please check this is correct?");
		txt="*10 Digit Number";
		document.getElementById(tel.name+"_span").innerHTML="<span id=\""+tel.name+"_span\" style='text-align:left;font-size:9px; color: red;\'>"+txt+"</span>";
	}
	else if( tel.value.length == 11 ){
		document.getElementById(tel.name+"_span").innerHTML="<span id=\""+tel.name+"_span\" style='text-align:left;font-size:9px; color: red;\'>&nbsp;</span>";
	}
}


function disp_toggle_a(form){
	a=(form.Joint_App.checked==true)?"":"none";
	document.getElementById('asecond').style.display=a;
}

function next_part_of_form(form){
	document.getElementById('one').style.display="none";
	document.getElementById('two').style.display="block";


}

function previous_part_of_form(form){
	document.getElementById('two').style.display="none";
	document.getElementById('one').style.display="block";


}
