// Mustard Generic Check functions

function st_retval(myselect)
{
	return( myselect.options[myselect.selectedIndex].value );
}

function st_valid_text( txt )
{
	var r1 = new RegExp("[^ ]+");
	return(r1.test(txt));
}

function st_valid_alpha( txt )
{
	var r1 = new RegExp("^[a-zA-Z]+$");
	return(r1.test(txt));
}

function st_valid_alphanumeric( txt )
{
	if( txt == "" )
		return true;
		
	var r1 = new RegExp("^[a-zA-Z0-9]+$");
	return(r1.test(txt));
}
function st_valid_alphanumericspace( txt )
{
	if( txt == "" )
		return true;
		
	var r1 = new RegExp("^[a-zA-Z0-9\\s]+$");
	return(r1.test(txt));
}


function st_valid_numeric( txt )
{
	var r1 = new RegExp("^\\d+$");
	return(r1.test(txt));

}

function st_min_chars( txt,min,max )
{	
	var r1 = new RegExp(".{" + min + "," + max + "}");	
 	return(r1.test(txt));
}

function st_illegal_chars( txt )
{
	var r1 = new RegExp("[^a-zA-Z0-9]");
 	return(r1.test(txt));
}


function st_valid_email( txt )
{
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	return(!r1.test(txt) && r2.test(txt));
}

function st_valid_postcode( txt )
{
	var r1 = new RegExp("^[a-zA-Z0-9]{2,4}(\\s)?[0-9][a-zA-Z]{2}$");
	return(r1.test(txt));
}

function st_valid_landline( txt )
{
	if( txt == "" )
		return true;
		
	var r1 = new RegExp("^0[12345689][0-9]{8,10}$");
	return(r1.test(txt));
}

function st_valid_mobile( txt )
{
	if( txt == "" )
		return true;
		
	var r1 = new RegExp("^07[0-9]{8,10}$");
	return(r1.test(txt));
}

function st_valid_phone( txt )
{
	
	var r1 = new RegExp("^0[123456789][0-9]{8,9}$");
	return(r1.test(txt));
}

function st_valid_selection( selector )
{
	return( valid_text(retval(selector)) );
}

function radio_selected( element )
{
	var radio_choice = false;
	
	for (var counter = 0; counter < element.length; counter++)
	{		
		if (element[counter].checked == true)		
			radio_choice = true; 
	}
	return( radio_choice );
}

//
// FORM A
//

function is_salacious_name( value )
{
	var test = value.toUpperCase();
		
	for( var i=0; i < salacious_names.length; i++ )
	{
		if( test == salacious_names[i] )	
			return true;
	}
	
	return false;
}

function is_hoax_name( firstname, lastname )
{
	var testfirst = firstname.toUpperCase();
	var testlast = lastname.toUpperCase();
		
	for( var i=0; i < hoax_names.length; i++ )
	{
		if( hoax_names[i][0] != "" )
		{
			if( testfirst == hoax_names[i][0] && testlast == hoax_names[i][1] )
				return true;
		}
		else
		{
			if( testlast == hoax_names[i][1] )
				return true;
		}			
	}
	return false;
}

function check_a( theform ) 
{		
	if( theform.LO_v_Resp_Title.options[theform.LO_v_Resp_Title.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Title." );
		theform.LO_v_Resp_Title.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_FirstName.value ) )
	{
		alert( "You must provide a Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_alpha( theform.LO_FirstName.value ) )
	{
		alert( "Please check your Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( is_salacious_name( theform.LO_FirstName.value ) )
	{
		alert( "Please check your Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_LastName.value ) )
	{
		alert( "You must provide a Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( !st_valid_alpha( theform.LO_LastName.value ) )
	{
		alert( "Please check your Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( is_salacious_name( theform.LO_LastName.value ) )
	{
		alert( "Please check your Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( is_hoax_name(theform.LO_FirstName.value,  theform.LO_LastName.value ) )
	{
		alert( "Please check your name is correct." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_Postcode.value ) )
	{
		alert( "You must provide your Postcode." );
		theform.LO_Postcode.focus();
		return false;
	}
	else if( !st_valid_postcode( theform.LO_Postcode.value ) )
	{
		alert( "Please check your Postcode." );
		theform.LO_Postcode.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_Address1.value ) )
	{
		alert( "You must provide your House/Flat No.." );
		theform.LO_Address1.focus();
		return false;
	}
	else if( !st_valid_alphanumericspace( theform.LO_Address1.value ) )
	{
		alert( "Please check your House/Flat No." );
		theform.LO_Address1.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_d.options[theform.LO_Resp_DOB_d.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_d.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_m.options[theform.LO_Resp_DOB_m.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_m.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_y.options[theform.LO_Resp_DOB_y.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_y.focus();
		return false;
	}
	else if( !st_valid_email(theform.LO_Email.value))
	{
		alert( "Email Address is not valid." );
		theform.LO_Email.focus();
		return false;
	}	
	else if(  !radio_selected(theform.LO_EmailAddress_HomeWorkOther)  )
	{
		alert( "You must specify where your email address is at home, work or other." );
		theform.LO_Email.focus();
		return false;
	}

	else if( theform.coreg_1.checked == true && !st_valid_text( theform.LO_InputHomeTell.value )   )
	{
		alert( "You must provide your telephone number to be eligible for the Optical Express offer" );
		theform.LO_InputHomeTell.focus();
		return false;
	}	
	
	else if( theform.coreg_1.checked == true && !st_valid_phone( theform.LO_InputHomeTell.value )   )
	{
		alert( "The telephone number you have entered is invalid" );
		theform.LO_InputHomeTell.focus();
		return false;
	}
	else if( theform.con_optin.checked != true  )
	{
		alert( "You must check the box to accept the Terms and Conditions." );
		theform.LO_Resp_DOB_y.focus();
		return false;
	}

	return true;
}


function check_tbg( theform ) 
{		
	if( theform.LO_v_Resp_Title.options[theform.LO_v_Resp_Title.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Title." );
		theform.LO_v_Resp_Title.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_FirstName.value ) )
	{
		alert( "You must provide a Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_alpha( theform.LO_FirstName.value ) )
	{
		alert( "Please check your Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( is_salacious_name( theform.LO_FirstName.value ) )
	{
		alert( "Please check your Forename." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_LastName.value ) )
	{
		alert( "You must provide a Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( !st_valid_alpha( theform.LO_LastName.value ) )
	{
		alert( "Please check your Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( is_salacious_name( theform.LO_LastName.value ) )
	{
		alert( "Please check your Surname." );
		theform.LO_LastName.focus();
		return false;
	}
	else if( is_hoax_name(theform.LO_FirstName.value,  theform.LO_LastName.value ) )
	{
		alert( "Please check your name is correct." );
		theform.LO_FirstName.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_Postcode.value ) )
	{
		alert( "You must provide your Postcode." );
		theform.LO_Postcode.focus();
		return false;
	}
	else if( !st_valid_postcode( theform.LO_Postcode.value ) )
	{
		alert( "Please check your Postcode." );
		theform.LO_Postcode.focus();
		return false;
	}
	else if( !st_valid_text( theform.LO_Address1.value ) )
	{
		alert( "You must provide your House/Flat No.." );
		theform.LO_Address1.focus();
		return false;
	}
	else if( !st_valid_alphanumericspace( theform.LO_Address1.value ) )
	{
		alert( "Please check your House/Flat No." );
		theform.LO_Address1.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_d.options[theform.LO_Resp_DOB_d.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_d.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_m.options[theform.LO_Resp_DOB_m.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_m.focus();
		return false;
	}
	else if( theform.LO_Resp_DOB_y.options[theform.LO_Resp_DOB_y.selectedIndex].value <= 0  )
	{
		alert( "You must provide a Date of Birth." );
		theform.LO_Resp_DOB_y.focus();
		return false;
	}
	else if( !st_valid_email(theform.LO_Email.value))
	{
		alert( "Email Address is not valid." );
		theform.LO_Email.focus();
		return false;
	}	
	else if(  !radio_selected(theform.LO_EmailAddress_HomeWorkOther)  )
	{
		alert( "You must specify where your email address is at home, work or other." );
		theform.LO_Email.focus();
		return false;
	}

	else if( theform.coreg_1.checked == true && !st_valid_text( theform.LO_InputHomeTell.value )   )
	{
		alert( "You must provide your telephone number to be eligible for the Optical Express offer" );
		theform.LO_InputHomeTell.focus();
		return false;
	}	
	else if( theform.coreg_13.options[theform.coreg_13.selectedIndex].value != "" && !st_valid_text( theform.LO_InputHomeTell.value )   )
	{
		alert( "You must provide your telephone number to be eligible for the Talk Talk offer" );
		theform.LO_InputHomeTell.focus();
		return false;
	}
	else if( (theform.coreg_1.checked == true || theform.coreg_13.options[theform.coreg_13.selectedIndex].value != "") && !st_valid_phone( theform.LO_InputHomeTell.value )   )
	{
		alert( "The telephone number you have entered is invalid" );
		theform.LO_InputHomeTell.focus();
		return false;
	}
//	else if( theform.coreg_15.options[theform.coreg_15.selectedIndex].value == "Yes"  && !st_valid_phone( theform.LO_InputHomeTell.value )   )
//	{
//		alert( "You must provide your telephone number to be eligible for the life insurance offer" );
//		theform.LO_InputHomeTell.focus();
//		return false;
//	}	
//	else if( (theform.aim_1.options[theform.aim_1.selectedIndex].value == "Yes" ) && (theform.aim_2.options[theform.aim_2.selectedIndex].value == "Yes" ) && !st_valid_phone( theform.LO_InputHomeTell.value )   )
//	{
//		alert( "You must provide your telephone number to be eligible for the accident compensation offer" );
//		theform.LO_InputHomeTell.focus();
//		return false;
//	}
	else if( theform.con_optin.checked != true  )
	{
		alert( "You must check the box to accept the Terms and Conditions." );
		theform.LO_Resp_DOB_y.focus();
		return false;
	}

	return true;
}

function check_coreg_tel(theform)
{
	if( theform.coreg_1.checked == true || theform.coreg_10.checked == true )
		showme('DIV1');
	else
		hide('DIV1');
}


//
// FORM B
//

function check_children( element )
{
	if( element.options[element.selectedIndex].value == 1 )
		show('DIV3');
	else
		hide('DIV3');
}

function check_rtm_oevision( element )
{
	if( element.value == "YES"  )
		show('DIV7');
	else
		hide('DIV7');
}


function check_b( theform ) 
{	
    if( !radio_selected(theform.LO_HomeOwnership) )
	{
		alert( "You must specify your home ownership." );
		return false;
	}	
	else if( theform.LO_MaritalStatus.options[theform.LO_MaritalStatus.selectedIndex].value <= 0  )
	{
		alert( "You must provide your Marital Status." );
		theform.LO_MaritalStatus.focus();
		return false;
	}
	else if( theform.LO_Gender.options[theform.LO_Gender.selectedIndex].value <= 0  )
	{
		alert( "You must provide your Gender." );
		theform.LO_Gender.focus();
		return false;
	}
	else if( theform.LO_Occupation.options[theform.LO_Occupation.selectedIndex].value == ""  )
	{
		alert( "You must provide your Occupation." );
		theform.LO_Occupation.focus();
		return false;
	}
	
	
	return true;
}


function check_formb( theform ) 
{
	if( theform.cheapflights_1.checked == false && theform.lakedistrict_1.checked == false && theform.thinkmoney_1[0].checked == false  )
	{
		alert( "Please select at least one of these special offers to be eligible for a second entry into our prize draw." );
		return false;
	}
	else if( theform.lakedistrict_1.checked == true  && !st_valid_phone( theform.lakedistrict_2.value )   )
	{
		alert( "The telephone number you have entered for the Lake District offer is invalid" );
		theform.lakedistrict_2.focus();
		return false;
	}
	else if( theform.lakedistrict_1.checked == true  && theform.lakedistrict_3.value == ""   )
	{
		alert( "Please provide your Street Name." );
		theform.lakedistrict_3.focus();
		return false;
	}
	else if( theform.lakedistrict_1.checked == true  && theform.lakedistrict_4.value == ""   )
	{
		alert( "Please provide your Town." );
		theform.lakedistrict_4.focus();
		return false;
	}
	else if( theform.thinkmoney_1[0].checked == true  && theform.thinkmoney_2.checked == true && theform.thinkmoney_3.checked == true && !st_valid_phone( theform.thinkmoney_4.value )   )
	{
		alert( "The telephone number you have entered for the Think Monday offer is invalid" );
		theform.thinkmoney_4.focus();
		return false;
	}
	
	return true;
}

//
// FORM C
//

function check_rtm_pdv( element )
{
	if( element.value == "YES"  )
		show('DIV8');
	else
		hide('DIV8');
}

function check_mobile( theform )
{
	if( theform.LO_InputMobileTell.value == "" )
		hide('DIV5');
	else
		show('DIV5');
}

function check_accident( element )
{
	if( element.value == "YES"  )
		show('DIV6');
	else
		hide('DIV6');
}
	
function check_house_value( theform )
{
	if( theform.LO_HouseValue.options[theform.LO_HouseValue.selectedIndex].value == "08" || theform.LO_HouseValue.options[theform.LO_HouseValue.selectedIndex].value == "10" || theform.LO_HouseValue.options[theform.LO_HouseValue.selectedIndex].value == "A"  )
		return true;
	else
		return false;
}

function check_homeowner( theform )
{
	if( theform.LO_HomeOwnership[0].checked == true )
		return true;
	else
		return false;
}

function check_nw_postcode( theform, postcode )
{
	var test = postcode.toUpperCase();
		
	for( var i=0; i < nw_postcodes.length; i++ )
	{
		if( test.substring(0,nw_postcodes[i].length) == nw_postcodes[i] )
		{	
			theform.nw_postcode.value = "yes";			
			return;
		}
	}
}

function check_homeimprove( theform )
{
	if( theform.LO_HomeImprovementsHaveConsider_none.checked == true )
	{
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_bed.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_bed[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_awnings.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_awnings[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_blinds.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_blinds[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_conservatory.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_conservatory[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_glazing.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_glazing[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_fitted.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_fitted[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_garage.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_garage[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_shutter.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_shutter[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_gutter.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_gutter[counter].checked = false;
		for( var counter = 0; counter < theform.LO_HomeImprovementsHaveConsider_other.length; counter++ )
			theform.LO_HomeImprovementsHaveConsider_other[counter].checked = false;
	}
}

function check_c( theform ) 
{
	if( theform.LO_NoOfChildren.options[theform.LO_NoOfChildren.selectedIndex].value == ""  )
	{
		alert( "You must provide your number of children." );
		theform.LO_NoOfChildren.focus();
		return false;
	}
	else if( theform.LO_HomeBuildingsInsurance.options[theform.LO_HomeBuildingsInsurance.selectedIndex].value == ""  )
	{
		alert( "Please tell us in which month you renew your Buildings Insurance." );
		theform.LO_HomeBuildingsInsurance.focus();
		return false;
	}
	else if( theform.LO_HomeContentsInsurance.options[theform.LO_HomeContentsInsurance.selectedIndex].value == ""  )
	{
		alert( "Please tell us in which month you renew your Home Contents Insurance." );
		theform.LO_HomeContentsInsurance.focus();
		return false;
	}
	else if( !radio_selected(theform.aa_1) )
	{
		alert( "Please tell us whether anyone in your household has had an accident in the past couple of years that wasn't their fault." );
		return false;
	}
	else if( theform.aa_1[0].checked == true  && !radio_selected(theform.aa_3) )
	{			
	    alert( "Please tell us whether you received medical attention." );
	    return false;
	}
    else if(theform.aa_1[0].checked == true && !radio_selected(theform.aa_4) )
    {
	    alert( "Please tell us whether you claimed compensation." );
	    return false;
    }
    else if( theform.aa_1[0].checked == true && !radio_selected(theform.aa_5) )
    {
	    alert( "Please tell us whether you would like to be contacted by Accident Assistance." );
	    return false;
    }
	
	return true;
}

//
// FORM D
//

function check_rtm_guardian( element )
{
	if( element.value == "YES"  )
		show('DIV6');
	else
		hide('DIV6');
}

function check_rtm_beatquote( element )
{
	if( element.value == "YES"  )
		show('DIV9');
	else
		hide('DIV9');
}

function check_rtm_unwins( theform )
{
	if( theform.LO_Resp_DOB_y.value <= 1977  )
		show('DIV5');
	else
		hide('DIV5');
}


function check_newspaper( theform, element )
{
	// If NONE was selected
	if( element.name == "LO_DailyNewspapersReadn" )
	{
		if( element.checked == true )
		{
			// Uncheck the others
			theform.LO_DailyNewspapersRead1.checked = false;
			theform.LO_DailyNewspapersRead2.checked = false;
			theform.LO_DailyNewspapersRead3.checked = false;
			theform.LO_DailyNewspapersRead4.checked = false;
			theform.LO_DailyNewspapersRead5.checked = false;
			theform.LO_DailyNewspapersRead6.checked = false;
			theform.LO_DailyNewspapersRead7.checked = false;
			theform.LO_DailyNewspapersRead8.checked = false;
			theform.LO_DailyNewspapersRead9.checked = false;
			theform.LO_DailyNewspapersRead10.checked = false;
			theform.LO_DailyNewspapersRead11.checked = false;
			theform.LO_DailyNewspapersRead12.checked = false;
			theform.LO_DailyNewspapersRead13.checked = false;
			theform.LO_DailyNewspapersRead14.checked = false;		
							
			// Hide DIV 4
			hide('DIV4');	
		}
	}
	else
	{
		if( element.checked == true )
		{
			theform.LO_DailyNewspapersReadn.checked = false;
			show('DIV4');
		}
	}	
}

function check_sunday( theform, element )
{
	// If NONE was selected
	if( element.name == "LO_SundayNewspapersReadn" )
	{
		if( element.checked == true )
		{
			// Uncheck the others
			theform.LO_SundayNewspapersRead1.checked = false;
			theform.LO_SundayNewspapersRead2.checked = false;
			theform.LO_SundayNewspapersRead3.checked = false;
			theform.LO_SundayNewspapersRead4.checked = false;
			theform.LO_SundayNewspapersRead5.checked = false;
			theform.LO_SundayNewspapersRead6.checked = false;
			theform.LO_SundayNewspapersRead7.checked = false;
			theform.LO_SundayNewspapersRead8.checked = false;
			theform.LO_SundayNewspapersRead9.checked = false;
			theform.LO_SundayNewspapersRead10.checked = false;
			theform.LO_SundayNewspapersRead11.checked = false;
			theform.LO_SundayNewspapersRead12.checked = false;
			theform.LO_SundayNewspapersRead13.checked = false;
	
			// Hide DIV 6
			hide('DIV6');	
		}
	}
	else
	{
		if( element.checked == true )
		{
			theform.LO_SundayNewspapersReadn.checked = false;	
			show('DIV6');
		}
	}	
}

function check_cars( element )
{
	if( element.options[element.selectedIndex].value >= 1 )
		show('DIV3');
	else
		hide('DIV3');
}

function check_medical( element )
{
	if( element.options[element.selectedIndex].value == 1 || element.options[element.selectedIndex].value == 2 )
		show('DIV10');
	else
		hide('DIV10');
}

function check_d( theform )
{
	
	if( theform.LO_CarsHowMany.options[theform.LO_CarsHowMany.selectedIndex].value == ""  )
	{
		alert( "Please specify how many cars there are in your household." );
		theform.LO_CarsHowMany.focus();
		return false;
	}

	return true;
}

//
// FORM E
//

function check_rtm_awd( theform )
{
	if( theform.LO_Resp_DOB_y.value <= 1989 && theform.LO_Resp_DOB_y.value >= 1952 && theform.LO_HomeOwnership.value == 1 )
		show('DIV5');
	else
		hide('DIV5');
}

function check_creditcards( element )
{
	if( element.options[element.selectedIndex].value >= 1 )
		show('DIV3');
	else
		hide('DIV3');
}
	
function check_e( theform )
{	
	if( theform.LO_InternetAtHome.options[theform.LO_InternetAtHome.selectedIndex].value == ""  )
	{
		alert( "Please specify whether you have internet access at home." );
		theform.LO_InternetAtHome.focus();
		return false;
	}	
	else if( theform.LO_HouseholdIncome.options[theform.LO_HouseholdIncome.selectedIndex].value == ""  )
	{
		alert( "Please specify your houshold income." );
		theform.LO_HouseholdIncome.focus();
		return false;
	}	
	
	
	return true;
}

