// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
//
//	Function 	: validate.js
//	Version 		: 1.00
// Last Update	: 24-3-09
//	Latest CCNo	:
//	----------------------------
//
//	Copyright (c) Y2Pods Solutions. All rights reserved.
//
// -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


//	----------------------------------
// Scripts to validate various forms
//	----------------------------------

function validateform(type,frm)
{
	var result;
	switch (type)
	{
		case 'contactus' :
			result = clean_contactus(frm);
			break;

		case 'custdetails' :
			result = clean_acctdetails(frm);
			break;

		case 'emailchange' :
			result = clean_emailchange(frm);
			break;

		case 'forgotten' :
			result = clean_forgotten(frm);
			break;

		case 'login' :
			result = clean_login(frm);
			break;

		case 'register' :
			result = clean_registration(frm);
			break;

		case 'notify_subscribe' :
			result = clean_notifyme(frm);
			break;
	}
	return result;
}


//	-------------------
//	--	CLEAN_CONTACTUS
//	------------------

function clean_contactus(frm)
{
	if ( frm.fullname.value.length == 0 || frm.fullname.value == "Type here" || is_spaces(frm.fullname.value) )
	{
		alert("Please enter your name.");
		return false;
	}
	if ( !isvalid_infield(frm.fullname.value,'text',255) )
	{
		alert("Your name is too long " + frm.fullname.value + ".");
		return false;
	}

	if ( frm.email.value.length == 0 || frm.email.value == "Type here" || is_spaces(frm.email.value) )
	{
		alert("Please enter your email address.");
		return false;
	}
	if ( !isvalid_infield(frm.email.value,'email',255) )
	{
		alert(frm.email.value + " is not a valid email address.");
		return false;
	}

	if ( !isvalid_infield(frm.organisation.value,'text',255) )
	{
		alert("Your organisation " + frm.organisation.value + " is too long.");
		return false;
	}

	if ( !isvalid_infield(frm.telephone.value,'phone',50) && frm.telephone.value != "Type here" )
	{
		txt = frm.telephone.value + " contains invalid characters." +
				'\n' + "Numbers, ), -, (, + and spaces only.";
		alert(txt);
		return false;
	}

	if ( frm.message.value.length == 0 || frm.message.value == "Type your message here"
			|| is_spaces(frm.message.value)
		)
	{
		alert("Please enter a message.");
		return false;
	}
	if ( !isvalid_infield(frm.message.value,'text',1000) )
	{
		txt = 'The message is too long (1000 max.) or ' +
				'\n' + "it contains unprintable characters.";
		alert(txt);
		return false;
	}

	return true;
}


//	----------------------
//	--	CLEAN_REGISTRATION
//	----------------------

function clean_registration(frm)
{
// ---- Validates compulsory input fields 

	if ( frm.firstname.value.length == 0 || frm.firstname.value == "Type here"
			|| is_spaces(frm.firstname.value)
		)
	{
		alert("Please enter your name.");
		return false;
	}
	if ( !isvalid_infield(frm.firstname.value,'text',150) )
	{
		alert("Your name " + frm.firstname.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( frm.surname.value.length == 0 || frm.surname.value == "Type here" || is_spaces(frm.surname.value) )
	{
		alert("Please enter your surname.");
		return false;
	}
	if ( !isvalid_infield(frm.surname.value,'text',150) )
	{
		alert("Your name " + frm.surname.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( frm.email.value.length == 0 || frm.email.value == "Type here" || is_spaces(frm.email.value) )
	{
		txt = "Please enter a valid email address." +
				'\n' + "Your account activation details will be sent to it."
		alert(txt);
		return false;
	}
	if ( !isvalid_infield(frm.email.value,'email',255) )
	{
		txt = "Your email address " + frm.email.value + " is not valid.  " + '\n' +
				"Please correct. Your account activation details will be sent to it."
		alert(txt);
		return false;
	}

	//	--	clean other fields

	if ( !isvalid_infield(frm.title.value,'text',25) )
	{
		alert("Your title " + frm.title.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( !isvalid_infield(frm.mobile.value,'phone',50) && frm.mobile.value != "Type here" )
	{
		alert("Your mobile " + frm.mobile.value + " is too long or contains invalid characters.");
		return false;
	}

	if ( !isvalid_infield(frm.telephone.value,'phone',50) && frm.telephone.value != "Type here" )
	{
		alert("Your telephone " + frm.telephone.value + " is too long or contains invalid characters.");
		return false;
	}

	if ( !isvalid_infield(frm.address1.value,'addr',255) )
	{
		alert("Your address " + frm.address1.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( !isvalid_infield(frm.address2.value,'addr',255) )
	{
		alert("Your address " + frm.address2.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( !isvalid_infield(frm.towncity.value,'addr',100) )
	{
		alert("Your town/city " + frm.towncity.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( !isvalid_infield(frm.county.value,'addr',100) )
	{
		alert("Your county " + frm.county.value + " is too long or contains unprintable characters.");
		return false;
	}

	if ( !isvalid_infield(frm.postcode.value,'pstcd',15) )
	{
		alert("Your postcode " + frm.postcode.value + " is too long or contains invalid characters.");
		return false;
	}

	if ( !isvalid_infield(frm.country.value,'addr',50) )
	{
		alert("Your country " + frm.country.value + " is too long or contains unprintable characters.");
		return false;
	}

//	--	validate and clean the passwords

	if ( frm.password1.value.length == 0 || frm.password1.value == "Type here" || is_spaces(frm.password1.value) )
	{
		alert("Please enter a password.");
		return false;
	}

	if ( frm.password1.value.length < 5 )
	{
		alert("Your password MUST contain at least 5 characters long.");
		return false;
	}

	if ( !isvalid_infield(frm.password1.value,'pwd',25) )
	{
		txt = 'Your password is too long (25 max.) or contains invalid characters.' +
					'\n' + 'The password MUST contain a MINIMUM of 8 characters that includes both numbers " +
								'and a mixture of upper and lowercase characters. Avoid words in the dictionary.';
		alert(txt);
		return false;
	}

	if ( frm.password1.value != frm.password2.value )
	{
		alert("The passwords do not match. Please carefully retype both.");
		return false;
	}

	if ( !frm.contract.checked )
	{
		txt =	"TERMS AND CONDITIONS" + '\n' +
				"Please confirm that you have read, understood and agree" + '\n' +
				"with our terms and conditions for this order."
		alert(txt);
		return false;
	}

	return true;
}

//	---------------------------------------------------------------------------

//	--	CLEAN_LOGIN
//	--------------
function clean_login(frm)
{
// ---- Validates login fields to prevent illegal character entry

	if ( frm.username.value.length == 0 || frm.username.value == "Type here" || is_spaces(frm.username.value) )
	{
		alert("Please enter your username.");
		return false;
	}

	if ( !isvalid_infield(frm.username.value,'email',255) )
	{
		txt = "Your username " + frm.username.value + " is invalid.";
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.password.value,'pwd',25) )
	{
		txt = 'Your password is too long (25 max.) or contains invalid characters.' +
					'\n' + 'The password MUST contain a MINIMUM of 8 characters that includes both numbers " +
								'and a mixture of upper and lowercase characters. Avoid words in the dictionary.';
		alert(txt);
		return false;
	}

	return true;
}

//	--------------------
//	--	CLEAN_FORGOTTEN
//	--------------------

function clean_forgotten(frm)
{
	// ---- Validates login fields to prevent illegal character entry
	//	---------------------------------------------------------------

	if ( frm.email.value.length == 0 || frm.email.value == "Type here" || is_spaces(frm.email.value) )
	{
		txt = "Please enter a valid email address. " + '\n' +
				"Your password details will be sent to it." + '\n' +
				"Please note - This must match the one we have on record for you.";
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.email.value,'email',255) )
	{
		txt = "Your email address " + frm.email.value + " is invalid.  " + '\n' +
				"Please enter a valid email address." + '\n' +
				"Your password details will be sent to it.";
		alert(txt);
		return false;
	}

	return true;
}
//	---------------------------------------------------------------------------

//	-- CLEAN_ACCTDETAILS
//	--------------------
function clean_acctdetails(frm)
{
// ---- Validates compulsory input fields 

	if ( frm.firstname.value.length == 0 || frm.firstname.value == "Type here" || is_spaces(frm.firstname.value) )
	{
		alert("Please enter your name.");
		return false;
	}
	if ( !isvalid_infield(frm.firstname.value,'text',255) )
	{
		txt = "Your name " + frm.firstname.value +
				'\n' + "is too long or contains unprintable characters.";
		alert(txt);
		return false;
	}

	if ( frm.surname.value.length == 0 || frm.surname.value == "Type here" || is_spaces(frm.surname.value) )
	{
		alert("Please enter your surname.");
		return false;
	}
	if ( !isvalid_infield(frm.surname.value,'text',255) )
	{
		txt = "Your surname " + frm.surname.value +
				'\n' + "is too long or contains unprintable characters.";
		alert(txt);
		return false;
	}

	//	--	clean other fields

	if ( !isvalid_infield(frm.title.value,'text',25) )
	{
		txt = "Your title " + frm.title.value +
				'\n' + "is too long or contains unprintable characters.";
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.mobile.value,'phone',50) && frm.mobile.value != "Type here" )
	{
		alert("Your mobile " + frm.mobile.value + " is too long or contains invalid characters.");
		return false;
	}

	if ( !isvalid_infield(frm.telephone.value,'phone',50) && frm.telephone.value != "Type here" )
	{
		alert("Your telephone " + frm.telephone.value + " is too long or contains invalid characters.");
		return false;
	}

	if ( !isvalid_infield(frm.address1.value,'addr',255) )
	{
		txt = "Your address " + frm.address1.value + '\n' +
				"is too long or contains unprintable characters."
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.address2.value,'addr',255) )
	{
		txt = "Your address " + frm.address2.value + '\n' +
				"is too long or contains unprintable characters."
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.towncity.value,'addr',100) )
	{
		txt = "Your town/city " + frm.towncity.value + '\n' +
				"is too long or contains unprintable characters."
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.county.value,'addr',100) )
	{
		txt = "Your county " + frm.county.value + '\n' +
				"is too long or contains unprintable characters."
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.postcode.value,'pstcd',15) )
	{
		txt = "Your postcode " + frm.postcode.value + '\n' +
				"is too long or contains invalid characters."
		alert(txt);
		return false;
	}

	if ( !isvalid_infield(frm.country.value,'addr',50) )
	{
		txt = "Your country " + frm.country.value + '\n' +
				" is too long or contains unprintable characters."
		alert(txt);
		return false;
	}

// ---- Validates emailchange fields to prevent illegal character entry

	if ( frm.email.value.length != 0 && frm.email.value != "Type here" && !is_spaces(frm.email.value) )
	{
		if ( !isvalid_infield(frm.email.value,'email',255) )
		{
			txt = "The email address " + frm.email.value + " is invalid." +
					'\n' + "Please enter a valid email address."
			alert(txt);
			return false;
		}

		if ( frm.emailpassword.value.length == 0 || frm.emailpassword.value == "Type here" || is_spaces(frm.emailpassword.value) )
		{
			txt = 'Please type a valid password in the ' + '\n' + 
					'Login Detail section, to change your email.';
			alert(txt);
			return false;
		}
		if ( !isvalid_infield(frm.emailpassword.value,'pwd',25) )
		{
			txt = 'Your password is too long (25 max.) or contains invalid characters.' +
					'\n' + 'The password MUST contain a MINIMUM of 8 characters that includes both numbers " +
								'and a mixture of upper and lowercase characters. Avoid words in the dictionary.';
			alert(txt);
			return false;
		}
	}

//	--	validate and clean the passwords

	if (  frm.password.value != "Type here" && frm.password.value.length != 0 && !is_spaces(frm.password.value)  )
	{
		if ( frm.password.value.length < 5 || !isvalid_infield(frm.password.value,'pwd',20) )
		{
			alert("Invalid current password. Please correct.");
			return false;
		}

		if ( frm.password1.value.length < 5 )
		{
			alert("Your new password MUST be at least 5 characters long.");
			return false;
		}

		if ( !isvalid_infield(frm.password1.value,'pwd',20) && frm.password1.value != "Type here")
		{
			txt = 'Your new password is too long (20 max.) or contains invalid characters.' +
					'\n' + 'The password MUST contain a MINIMUM of 8 characters that includes both numbers " +
								'and a mixture of upper and lowercase characters. Avoid words in the dictionary.';
			alert(txt);
			return false;
		}

		if ( frm.password1.value != frm.password2.value )
		{
			alert("The new passwords do not match. Please carefully retype both.");
			return false;
		}
	}

	return true;
}

//	---------------------------------------------------------------------------
//	--	CLEAN_NOTIFYME
//	----------------------
function clean_notifyme(frm)
{
	if ( frm.name.value.length == 0 || frm.name.value == "Type here" || is_spaces(frm.name.value) )
	{
		alert("Notify me: Please enter your name.");
		return false;
	}
	if ( !isvalid_infield(frm.name.value,'text',150) )
	{
		txt = 'Notify me: Your name is too long (150 max.) or ' +
				'\n' + 'it contains unprintable characters.';
		alert(txt);
		return false;
	}

	if ( frm.email.value.length == 0 || frm.email.value == "Type here" || is_spaces(frm.email.value) )
	{
		alert("Notify me: Please enter your email address.");
		return false;
	}
	if ( !isvalid_infield(frm.email.value,'email',255) )
	{
		alert("Notify me: Your email address " + frm.email.value + " is invalid.");
		return false;
	}

	if ( !frm.mode[0].checked && !frm.mode[1].checked)
	{
		txt =	"Please indicate if you are subscribing or unsubscribing.";
		alert(txt);
		return false;
	}

	return true;
}

