/* -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

	Function 	: validate.js
	Version		: 1.00
	Last Update	: 08-08-2011 
	Latest CCNo	:
	----------------------------

	Copyright (c) Y2Pods Solutions. All rights reserved.

 -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*/


//	----------------------------------
// Scripts to validate various forms
//	----------------------------------

//	----------------------------------
// Generic form input field validation

function nothing_input(inptxt)
{
	if ( inptxt.length == 0 || inptxt == "Type here" || is_spaces(inptxt) )
		return true;
	else
		return false;
}

//	----------------------------------
// Generic form input field validation

function isvalid_infield(inptxt,fieldtype,maxlength)
{
	if ( fieldtype.length == 0 )
		fieldtype = 'text';

	if ( maxlength.length == 0 )
		maxlength = 255;

	if ( inptxt.length > maxlength)
		return false;

	switch (fieldtype)
	{
		case 'email' :
			pattern = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
			break;

		case 'addr' :
			// check an address entry is valid - uppercase, lowercase letters,
			//	digits 0-9, fullstops, hyphens and underscore only (used on address lines and pstcd)
			pattern = /[^-a-zA-Z0-9_ ,@#&\*\+\.\/) (]+/i;
			break;

		case 'pstcd' :
			// check a postcode entry is valid - uppercase, lowercase letters,
			//	digits 0-9 and spaces only (used on address lines and pstcd)
			pattern = /^[a-z]{1,2}[0-9]{1,2}[a-z]{0,1}\s*[0-9][a-z]{2}$/i;
			break;

		case 'phone' :
			// check phone number entry is valid
			pattern = /[^-0-9 \(\+\)]+/i;
			break;

		case 'file' :
			// check an upload file name to ensure that the format is correct
			//	ie. xxx.xxx
			pattern = /^([-a-zA-Z0-9\/_\\ :])+\.([a-zA-Z]){3,4}$/i;
			break;

		case 'pwd' :
			// check password entry is valid - uppercase, lowercase letters and digits 0-9 only & max len = 20
			pattern = /[^a-zA-Z0-9]+/i;
			break;

		case 'user' :
			// check username entry is valid - uppercase, lowercase letters and digits 0-9 only & min = 2 & max len = 20
			if (inptxt.length < 2)
				return false;
			else
				pattern = /[^-a-zA-Z0-9_@.]+/i;
			break;

		default:
			pattern = /[\f\0\v]+/m;
			break;
	}

	if (fieldtype == 'email' || fieldtype == 'file' || fieldtype == 'pstcd' )
	{
		result = pattern.test(inptxt);
		if (!result)
			return false;
	}
	else
	{
		result = inptxt.match(pattern);

		if (result != null)
			return false;
	}

	return true;
}


function validateform(type,frm)
{
	var result;
	switch (type)
	{
		case 'contactus' :
			result = clean_contactus(frm);
			break;

		case 'forgotten' :
			result = clean_forgotten(frm);
			break;

		case 'login' :
			result = clean_login(frm);
			break;

		case 'notify_subscribe' :
			result = clean_notifyme(frm);
			break;
	}
	return result;
}


//	-------------------
//	--	CLEAN_CONTACTUS
//	------------------

function clean_contactus(frm)
{
  $begin ="Contact Us\n  ---> ";
	if ( frm.fullname.value.length == 0 || frm.fullname.value == "Type here" || is_spaces(frm.fullname.value) )
	{
		alert($begin + "Please enter your name");
		return false;
	}
	if ( !isvalid_infield(frm.fullname.value,'text',255) )
	{
		alert($begin + frm.fullname.value + " cannot be accepted as a name");
		return false;
	}

	if ( frm.email.value.length == 0 || frm.email.value == "Type here" || is_spaces(frm.email.value) )
	{
		alert($begin + "Please enter your email address.");
		return false;
	}
	if ( !isvalid_infield(frm.email.value,'email',255) )
	{
		alert($begin + frm.email.value + " is not a valid email address");
		return false;
	}

	if ( !isvalid_infield(frm.organisation.value,'text',255) )
	{
		alert($begin + frm.organisation.value + " cannot be accepted as an organisation name");
		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($begin + txt);
		return false;
	}

	if ( frm.message.value.length == 0 || frm.message.value == "Type your message here"
			|| is_spaces(frm.message.value)
		)
	{
		alert($begin + "Please enter a message");
		return false;
	}
	if ( !isvalid_infield(frm.message.value,'text',1000) )
	{
		txt = 'Please enter a valid message';
		alert($begin + txt);
		return false;
	}

	msg = "ATTENTION!\n\nIf you are sure that you typed the security code at the end of the " +
			"page correctly, then click OK to continue."+
			"\n\nYou can refresh the code by clicking the circular arrows or "+
			"listen to the code, if you are having problems reading it.";
	if ( confirm(msg) )
		return true;
	else
		return false;
}



//	---------------------------------------------------------------------------

//	--	CLEAN_LOGIN
//	--------------
function clean_login(frm)
{
// ---- Validates login fields to prevent illegal character entry

  $begin ="Login\n  ---> ";

	if ( frm.username.value.length == 0 || 
			frm.username.value == "Type here" || 
				is_spaces(frm.username.value) )
	{
		alert($begin + "Please enter your username");
		return false;
	}

	if ( !isvalid_infield(frm.username.value,'email',255) )
	{
		txt = "Your username " + frm.username.value + " is invalid";
		alert($begin + txt);
		return false;
	}

	if ( !isvalid_infield(frm.password.value,'pwd',25) )
	{
		txt = 'Please enter a valid password.  Passwords are case-sensitive';
		alert($begin + txt);
		return false;
	}

	return true;
}

//	--------------------
//	--	CLEAN_FORGOTTEN
//	--------------------

function clean_forgotten(frm)
{
	// ---- Validates login fields to prevent illegal character entry
	//	---------------------------------------------------------------

  $begin ="Forgotten Password\n  ---> ";
	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($begin + 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 login detail reminder will be sent to it";
		alert($begin + txt);
		return false;
	}

	return true;
}

//	---------------------------------------------------------------------------
//	--	CLEAN_NOTIFYME -- Newsletter subcription
//	----------------------
function clean_notifyme(frm)
{
  $begin ="Newsletter Subscription\n  ---> ";

	if ( frm.nl_name.value.length == 0 || 
			frm.nl_name.value == "Type here" || 
				is_spaces(frm.nl_name.value) )
	{
		alert($begin + "Please enter your name.");
		return false;
	}
	if ( !isvalid_infield(frm.nl_name.value,'text',150) )
	{
		alert($begin + "Please enter your name.");
		return false;
	}

	if ( frm.nl_email.value.length == 0 || 
			frm.nl_email.value == "Type here" || 
				is_spaces(frm.nl_email.value) )
	{
		alert($begin + "Please enter your email address.");
		return false;
	}
	if ( !isvalid_infield(frm.nl_email.value,'email',255) )
	{
		alert($begin + "The email address " + frm.nl_email.value + " is invalid.");
		return false;
	}

	return true;
}



