/*
	File Type:  JavaScript
	File:       validateform.js
	Purpose:    Provides form validation
	Date:       15 Feb 2010
	Author:     P G Swain
*/

//  Function:	remSeparators
//	Removes spaces and hyphens from phone numbers
function remSeparators(telNum)
{
	while (telNum.indexOf(" ") != -1)
	{
		telNum = telNum.slice(0, telNum.indexOf(" ")) + telNum.slice(telNum.indexOf(" ")+1);
	}

	while (telNum.indexOf("-") != -1)
	{
		telNum = telNum.slice(0, telNum.indexOf("-")) + telNum.slice(telNum.indexOf("-")+1);
	}
	return telNum;
}

//	Function:	trimString
//	Removes leading and trailing white space in 'str'
function trimString(str)
{
	return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

//	Function: checkUKLandlineNumber(teleNumber)
//	Checks for a valid UK Landline number
//	Returns a code corresponding to the error (0 = no error)
function checkUKLandlineNumber(teleNumber)
{
	var telNum = teleNumber;
  	var tnexp = new Array ();  // To contain phone number regular expressions
	
	// Confirm number starts with a zero
	exp = /^0/;
	if (exp.test(telNum) != true)
	{
		return 1;
	}
	
	// Confirm number has 10 or 11 digits
	
	// Set up array to validate number for 10 and 11 digits
	tnexp.length=0;  // Clear the regular expression array
	tnexp.push (/^0[0-9]{10}$/);
	tnexp.push (/^01204[0-9]{5}$/);
	tnexp.push (/^01208[0-9]{5}$/);
	tnexp.push (/^01254[0-9]{5}$/);
	tnexp.push (/^01276[0-9]{5}$/);
	tnexp.push (/^01297[0-9]{5}$/);
	tnexp.push (/^01298[0-9]{5}$/);
	tnexp.push (/^01363[0-9]{5}$/);
	tnexp.push (/^01364[0-9]{5}$/);
	tnexp.push (/^01384[0-9]{5}$/);
	tnexp.push (/^01386[0-9]{5}$/);
	tnexp.push (/^01404[0-9]{5}$/);
	tnexp.push (/^01420[0-9]{5}$/);
	tnexp.push (/^01460[0-9]{5}$/);
	tnexp.push (/^01461[0-9]{5}$/);
	tnexp.push (/^01480[0-9]{5}$/);
	tnexp.push (/^01488[0-9]{5}$/);
	tnexp.push (/^01524[0-9]{5}$/);
	tnexp.push (/^01527[0-9]{5}$/);
	tnexp.push (/^01562[0-9]{5}$/);
	tnexp.push (/^01566[0-9]{5}$/);
	tnexp.push (/^01606[0-9]{5}$/);
	tnexp.push (/^01629[0-9]{5}$/);
	tnexp.push (/^01635[0-9]{5}$/);
	tnexp.push (/^01647[0-9]{5}$/);
	tnexp.push (/^01659[0-9]{5}$/);
	tnexp.push (/^01695[0-9]{5}$/);
	tnexp.push (/^01726[0-9]{5}$/);
	tnexp.push (/^01744[0-9]{5}$/);
	tnexp.push (/^01750[0-9]{5}$/);
	tnexp.push (/^01768[0-9]{5}$/);
	tnexp.push (/^01827[0-9]{5}$/);
	tnexp.push (/^01837[0-9]{5}$/);
	tnexp.push (/^01884[0-9]{5}$/);
	tnexp.push (/^01900[0-9]{5}$/);
	tnexp.push (/^01905[0-9]{5}$/);
	tnexp.push (/^01935[0-9]{5}$/);
	tnexp.push (/^01946[0-9]{5}$/);
	tnexp.push (/^01949[0-9]{5}$/);
	tnexp.push (/^01963[0-9]{5}$/);
	tnexp.push (/^01995[0-9]{5}$/);

	var validNumber = false;  // Initially mark number as invalid
	
	for (var i=0; i<tnexp.length; i++) // Test for correspondence
	{
		if ( tnexp[i].test(telNum) )
		{
			validNumber = true; // Found valid format
		}
	}

	if (validNumber != true)
	{
		return 2
	}

	// Confirm number is a valid geographic number starting with 01 or 02.
	exp = /^0[1,2]/;
	if (exp.test(telNum) != true)
	{
		return 3;
	}

	// Disallow numbers allocated for dramas.
	// Ofcom has set out a range of Telephone Numbers recommended for drama purposes
	// The array holds the regular expressions for the drama telephone numbers
	tnexp.length=0;  // Clear the regular expression array
	tnexp.push (/^(0113|0114|0115|0116|0117|0118|0121|0131|0141|0151|0161)(4960)[0-9]{3}$/);
	tnexp.push (/^02079460[0-9]{3}$/);
	tnexp.push (/^01914980[0-9]{3}$/);
	tnexp.push (/^02890180[0-9]{3}$/);
	tnexp.push (/^02920180[0-9]{3}$/);
	tnexp.push (/^01632960[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) // Test for correspondence
	{
		if ( tnexp[i].test(telNum) )
		{
			return 4;
		}
	}
	return 0;
}


//	Function: checkUKMobileNumber(teleNumber)
//	Checks for a valid UK Mobile number
//	Returns a code corresponding to the error (0 = no error)
function checkUKMobileNumber(teleNumber)
{
	var telNum = teleNumber;

	// Confirm number starts with a zero
	exp = /^0/;
	if (exp.test(telNum) != true)
	{
		return 1;
	}
	
	// Confirm number has 11 digits.
	exp = /^0[0-9]{10}$/;
	if (exp.test(telNum) != true)
	{
		return 2;
	}

	// Confirm number is a valid mobile number starting with 07.
	exp = /^07[1-5,7-9]/;
	if (exp.test(telNum) != true)
	{
		return 3;
	}

	// Disallow numbers allocated for dramas.
	// Ofcom has set out a range of Telephone Numbers recommended for drama purposes
	// The array holds the regular expressions for the drama telephone numbers
  	var tnexp = new Array ();
		tnexp.push (/^07700900[0-9]{3}$/);
	
	for (var i=0; i<tnexp.length; i++) // Test for correspondence
	{
		if ( tnexp[i].test(telNum) )
		{
			return 4;
		}
	}
	return 0;
}

/* =========================================================================== */

// Validate Enquiry Submision Form
// *******************************

function validForm(passForm)
{
	// Set form field backgrounColor to white
	$("input").css('background-color', 'white');
	$("textarea").css('background-color', 'white');
	
	// Test for first name
	if (passForm.firstname.value == "")
	{
		alert("Please enter your first name");
		$("input[name='firstname']").css('background-color', '#ffccff');
		passForm.firstname.focus();
		return false;
	}
	else
	{
		$("input[name='firstname']").css('background-color', 'white');
	}
	
	//Test for last name
	if (passForm.lastname.value == "")
	{
		alert("Please enter your last name");
		$("input[name='lastname']").css('background-color', '#ffccff');
		passForm.lastname.focus();
		return false;
	}
	else
	{
		$("input[name='lastname']").css('background-color', 'white');
	}

/*
 // Test for street address
 if (passForm.address.value == "")
 {
  alert("Please enter your street address");
  passForm.address.focus();
  return false;
 }

 // Test for zip or postcode
 if (passForm.zip.value == "")
 {
  alert("Please enter your postcode");
  passForm.zip.focus();
  return false;
 }
*/

	// Test for landline or mobile phone numbers
	if (passForm.landline.value == "" && passForm.mobile.value == "")
	{
		alert("Please enter a contact telephone number");
		$("input[name='landline']").css('background-color', '#ffccff');
		$("input[name='mobile']").css('background-color', '#ffccff');
		passForm.landline.focus();
		return false;
	}
	else
	{
		$("input[name='landline']").css('background-color', 'white');
		$("input[name='mobile']").css('background-color', 'white');
	}


	// Validate the land line line number
	if (passForm.landline.value != "")
	{
		passForm.landline.value = remSeparators(passForm.landline.value);
		switch (checkUKLandlineNumber(passForm.landline.value))
		{
			case 1:
				alert("Not a valid UK land line number - should start with a '0'.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;
		
			case 2:
				alert("Not a valid UK land line number - should contain 10 or 11 digits.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;
		
			case 3:
				alert("Not a valid UK land line number - invalid geographic number.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;

			case 4:
				alert("Not a valid UK land line number - reserved number.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;

			default:
				$("input[name='landline']").css('background-color', 'white');
		 }
	}

	// Validate the mobile number
	if (passForm.mobile.value != "")
	{
		passForm.mobile.value = remSeparators(passForm.mobile.value);
		switch (checkUKMobileNumber(passForm.mobile.value))
		{
			case 1:
				alert("Not a valid UK mobile number - should start with a '0'.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;
		
			case 2:
				alert("Not a valid UK mobile number - should contain 11 digits.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;
		
			case 3:
				alert("Not a valid UK mobile number - invalid mobile number.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;

			case 4:
				alert("Not a valid UK mobile number - reserved number.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;

			default:
				$("input[name='mobile']").css('background-color', 'white');
		} 
	}

	// Test for email addresses
	if (passForm.email1.value == "" || passForm.email2.value == "")
	{
		alert("Please enter your email address in both fields");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

	// The emails should be the same
 	if (passForm.email1.value !== passForm.email2.value)
	{
		alert("Please re-enter your email addresses");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

	// Test for correct email format
 	var mailMask = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	if (!mailMask.test(passForm.email1.value))
	{
		alert("Please check your entered email addresses");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

 	// Test for an entered enquiry
 	if (passForm.enquiry.value == "")
 	{
		alert("Please enter your enquiry");
		$("textarea[name='enquiry']").css('background-color', '#ffccff');
		passForm.enquiry.focus();
		return false;
	}
		else
	{
		$("textarea[name='enquiry']").css('background-color', 'white');
	}

	//Test for entered verification code
	if (passForm.captcha_code.value == "")
	{
		alert("Please enter the verification code");
		$("input[name='captcha_code']").css('background-color', '#ffccff');
		passForm.captcha_code.focus();
		return false;
	}
	else
	{
		$("input[name='captcha_code']").css('background-color', 'white');
	}
	return true;
} // end function validForm

/* =========================================================================== */

// Validate Footpath Problem Submision Form
// ****************************************

function validProblemForm(passForm)
{
	// Clear field backgrounColor
	$("input").css('background-color', 'white');
	$("textarea").css('background-color', 'white');
	
	// Test for first name
	if (passForm.firstname.value == "")
	{
		alert("Please enter your first name");
		$("input[name='firstname']").css('background-color', '#ffccff');
		passForm.firstname.focus();
		return false;
	}
	else
	{
		$("input[name='firstname']").css('background-color', 'white');
	}


 	//Test for last name
	if (passForm.lastname.value == "")
	{
		alert("Please enter your last name");
		$("input[name='lastname']").css('background-color', '#ffccff');
		passForm.lastname.focus();
		return false;
	}
	else
	{
		$("input[name='lastname']").css('background-color', 'white');
	}

 	// Test for landline or mobile phone numbers
	if (passForm.landline.value == "" && passForm.mobile.value == "")
	{
		alert("Please enter a contact telephone number");
		$("input[name='landline']").css('background-color', '#ffccff');
		$("input[name='mobile']").css('background-color', '#ffccff');
		passForm.landline.focus();
		return false;
	}
	else
	{
		$("input[name='landline']").css('background-color', 'white');
		$("input[name='mobile']").css('background-color', 'white');
	}

	// Validate the land line line number
	if (passForm.landline.value != "")
	{
		passForm.landline.value = remSeparators(passForm.landline.value);
		switch (checkUKLandlineNumber(passForm.landline.value))
		{
			case 1:
				alert("Not a valid UK land line number - should start with a '0'.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;
		
			case 2:
				alert("Not a valid UK land line number - insufficient number of digits.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;
		
			case 3:
				alert("Not a valid UK land line number - invalid geographic number.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;

			case 4:
				alert("Not a valid UK land line number - reserved number.");
				$("input[name='landline']").css('background-color', '#ffccff');
				passForm.landline.focus(); 
				return false;

			default:
				$("input[name='landline']").css('background-color', 'white');
		 }
	}

	// Validate the mobile number
	if (passForm.mobile.value != "")
	{
		passForm.mobile.value = remSeparators(passForm.mobile.value);
		switch (checkUKMobileNumber(passForm.mobile.value))
		{
			case 1:
				alert("Not a valid UK mobile number - should start with a '0'.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;
		
			case 2:
				alert("Not a valid UK mobile number - should contain 11 digits.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;
		
			case 3:
				alert("Not a valid UK mobile number - invalid mobile number.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;

			case 4:
				alert("Not a valid UK mobile number - reserved number.");
				$("input[name='mobile']").css('background-color', '#ffccff');
				passForm.mobile.focus(); 
				return false;

			default:
				$("input[name='mobile']").css('background-color', 'white');
		} 
	}

	// Test for email addresses
	if (passForm.email1.value == "" || passForm.email2.value == "")
	{
		alert("Please enter your email address in both fields");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

	// The emails should be the same
 	if (passForm.email1.value !== passForm.email2.value)
	{
		alert("Please re-enter your email addresses");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

	// Test for correct email format
 	var mailMask = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;

	if (!mailMask.test(passForm.email1.value))
	{
		alert("Please check your entered email addresses");
		$("input[name='email1']").css('background-color', '#ffccff');
		$("input[name='email2']").css('background-color', '#ffccff');
		passForm.email1.focus();
		return false;
	}
	else
	{
		$("input[name='email1']").css('background-color', 'white');
		$("input[name='email2']").css('background-color', 'white');
	}

// Test date format
	var dateMask = /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}/;
	if (!dateMask.test(passForm.txtDate.value))
	{
		alert("Please enter a date in the format \"dd\/mm\/yyyy\" when the problem was found");
		$("input[name='txtDate']").css('background-color', '#ffccff');
		passForm.txtDate.focus();
		return false;
	}
	else
	{
		$("input[name='txtDate']").css('background-color', 'white');
	}
 
	// Test time format
	var timeMask =/^[0-9]{2}:[0-9]{2}$/
	if (!timeMask.test(passForm.txtTime.value))
	{
		alert("Please enter a time in the format \"hh\:mm\" when the problem was found");
		$("input[name='txtTime']").css('background-color', '#ffccff');
		passForm.txtTime.focus();
		return false;
	}
	else
	{
		$("input[name='txtTime']").css('background-color', 'white');
	}

	// Test for a location
	if (passForm.location.value == "")
	{
		alert("Please enter path location");
		$("textarea[name='location']").css('background-color', '#ffccff');
		passForm.location.focus();
		return false;
	}
	else
	{
		$("textarea[name='location']").css('background-color', 'white');
	}

	// If there is a grid reference it should be of the correct format
	var gridRefMask = /^[a-zA-Z]{2}[0-9]{6}/;
	if (passForm.gridRef.value != "")
	{
		if (!gridRefMask.test(passForm.gridRef.value))
		{
			alert("Please check the entered grid reference");
			$("input[name='gridRef']").css('background-color', '#ffccff');
			passForm.gridRef.focus();
			return false;
		}	 
		else
		{
			$("input[name='gridRef']").css('background-color', 'white');
		}
	}

	// Test for an entered problem
	if (passForm.problem.value == "")
	{
		alert("Please enter problem details");
		$("textarea[name='problem']").css('background-color', '#ffccff');
		passForm.problem.focus();
		return false;
	}
	else
	{
		$("textarea[name='problem']").css('background-color', 'white');
	}

	//Test for entered verification code
	if (passForm.captcha_code.value == "")
	{
		alert("Please enter the verification code");
		$("input[name='captcha_code']").css('background-color', '#ffccff');
		passForm.captcha_code.focus();
		return false;
	}
	else
	{
		$("input[name='captcha_code']").css('background-color', 'white');
	}

	return true;
}
 // end function validProblemForm