var type;
function chkViewForm()
{
	var ret_val = true;

	var myTelNo = document.getElementById('telephone').value;
	// If invalid number, report back error
	type = "telephone";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('telephone').focus()",1);

	}

	var myTelNo = document.getElementById('mobile').value;
	// If invalid number, report back error
	type = "mobile";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('mobile').focus()",1);

	}

	var email = document.getElementById("email").value;
	if(email=="" || !email.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/))
	{
		alert("Please provide proper E-mail address");
		ret_val = false;
		document.getElementById("email").focus();
	}
	return ret_val;
}


function chkbookForm()
{
	var ret_val = true;

	var myTelNo = document.getElementById('telephone').value;
	// If invalid number, report back error
	type = "telephone";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('telephone').focus()",1);

	}

	var myTelNo = document.getElementById('mobile').value;
	// If invalid number, report back error
	type = "mobile";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('mobile').focus()",1);

	}

	var email = document.getElementById("email").value;
	if(email=="" || !email.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/))
	{
		alert("Please provide proper E-mail address");
		ret_val = false;
		document.getElementById("email").focus();
	}
	return ret_val;
}

function chkcallbackForm()
{
	var ret_val = true;

	var myTelNo = document.getElementById('mobile').value;
	// If invalid number, report back error
	type = "mobile";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('mobile').focus()",1);

	}

	var email = document.getElementById("email").value;
	if(email=="" || !email.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/))
	{
		alert("Please provide proper E-mail address");
		ret_val = false;
		document.getElementById("email").focus();
	}
	return ret_val;
}


function chkBrochureForm()
{
	var ret_val = true;

	var myTelNo = document.getElementById('telephone').value;
	// If invalid number, report back error
	type = "telephone";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('telephone').focus()",1);

	}

	var myTelNo = document.getElementById('mobile').value;
	// If invalid number, report back error
	type = "mobile";
	if (!checkUKTelephone(myTelNo,type)) {
		alert (telNumberErrors[telNumberErrorNo]);
		ret_val = false;
		setTimeout("document.getElementById('mobile').focus()",1);

	}

	var email = document.getElementById("email").value;
	if(email=="" || !email.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/))
	{
		alert("Please provide proper E-mail address");
		ret_val = false;
		document.getElementById("email").focus();
	}
	return ret_val;
}
function checkUKTelephone (telephoneNumber) {

	// Convert into a string and check that we were provided with something
	var telnum = telephoneNumber + " ";
	if (telnum.length == 1)  {
		if(type=='telephone'){
			telNumberErrorNo = 1;
		}
		else
		{
			telNumberErrorNo = 7;
		}
		return false
	}
	telnum.length = telnum.length - 1;

	// Don't allow country codes to be included (assumes a leading "+")
	var exp = /^(\+)[\s]*(.*)$/;
	if (exp.test(telnum) == true) {
		if(type=='telephone'){
			telNumberErrorNo = 2;
		}
		else
		{
			telNumberErrorNo = 8;
		}
		return false;
	}

	// Remove spaces from the telephone number to help validation
	while (telnum.indexOf(" ")!= -1)  {
		telnum = telnum.slice (0,telnum.indexOf(" ")) + telnum.slice (telnum.indexOf(" ")+1)
	}

	// Remove hyphens from the telephone number to help validation
	while (telnum.indexOf("-")!= -1)  {
		telnum = telnum.slice (0,telnum.indexOf("-")) + telnum.slice (telnum.indexOf("-")+1)
	}

	// Now check that all the characters are digits
	exp = /^[0-9]{10,11}$/;
	if (exp.test(telnum) != true) {
		if(type=='telephone'){
			telNumberErrorNo = 3;
		}
		else
		{
			telNumberErrorNo = 9;
		}
		return false;
	}

	// Now check that the first digit is 0
	exp = /^0[0-9]{9,10}$/;
	if (exp.test(telnum) != true) {
		if(type=='telephone'){
			telNumberErrorNo = 4;
		}
		else
		{
			telNumberErrorNo = 10;
		}
		return false;
	}

	// Disallow numbers allocated for dramas.

	// Array holds the regular expressions for the drama telephone numbers
	var tnexp = new 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}$/);
	tnexp.push (/^07700900[0-9]{3}$/);
	tnexp.push (/^08081570[0-9]{3}$/);
	tnexp.push (/^09098790[0-9]{3}$/);
	tnexp.push (/^03069990[0-9]{3}$/);

	for (var i=0; i<tnexp.length; i++) {
		if ( tnexp[i].test(telnum) ) {
			if(type=='telephone'){
				telNumberErrorNo = 5;
			}
			else
			{
				telNumberErrorNo = 11;
			}
			return false;
		}
	}

	// Finally check that the telephone number is appropriate.
	exp = (/^(01|02|03|05|070|071|072|073|074|075|07624|077|078|079)[0-9]+$/);
	if (exp.test(telnum) != true) {
		if(type=='telephone'){
			telNumberErrorNo = 5;
		}
		else
		{
			telNumberErrorNo = 11;
		}
		return false;
	}

	// Telephone number seems to be valid - return the stripped telehone number
	return telnum;
}



var telNumberErrorNo = 0;
var telNumberErrors = new Array ();
telNumberErrors[0] = "Valid UK telephone number";
telNumberErrors[1] = "Telephone number not provided";
telNumberErrors[2] = "UK telephone number without the country code, please";
telNumberErrors[3] = "UK telephone numbers should contain 10 or 11 digits";
telNumberErrors[4] = "The telephone number should start with a 0";
telNumberErrors[5] = "The telephone number is either invalid or inappropriate";
telNumberErrors[6] = "Valid UK Mobile number";
telNumberErrors[7] = "Mobile number not provided";
telNumberErrors[8] = "UK Mobile number without the country code, please";
telNumberErrors[9] = "UK Mobile numbers should contain 10 or 11 digits";
telNumberErrors[10] = "The Mobile number should start with a 0";
telNumberErrors[11] = "The Mobile number is either invalid or inappropriate";
