// JavaScript Document

//  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;
}

// Removes leading and trailing white space
	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
// *******************************

// Validates each form field
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 House and Street
	if (passForm.address.value == "")
	{
		alert("Please enter your street address");
	    $("input[name='address']").css('background-color', '#ffccff');
        passForm.address.focus();
        return false;
	}
	else
	{
		$("input[name='address']").css('background-color', 'white');
	}

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

	 if (passForm.landline.value == "" && passForm.mobile.value == "")
	 {
      alert("Please enter a contact telephone number");
      passForm.landline.focus();
      $("input[name='landline']").css('background-color', '#ffccff');
	  $("input[name='mobile']").css('background-color', '#ffccff');
      return false;
     }
	 else
	 {
		 $("input[name='firstname']").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');
		} 
	}


	 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');
	}

	 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');
	}

	 if (passForm.inquiry.value == "")
	 {
      alert("Please enter your inquiry");
	  $("textarea[name='inquiry']").css('background-color', '#ffccff');
	  passForm.inquiry.focus();
      return false;
      }
	  	else
	{
		$("textarea[name='inquiry']").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;

     return true;
    } // end function validForm
