// Madgex Limited
// Copyright (c) 2004 Madgex Limited. All Rights Reserved.
// validation.js - New Media Age Web Register
// 16 March 2006
// Version 1.0.0

function ValidateForgottenPassword()
{
	var empty_fields = "";
	var sUserName = document.Logon.EmailAddress.value.Trim();
	
	if ( sUserName == '' )
	{
		empty_fields += "\n        - Email Address";	
		document.Logon.EmailAddress.focus();
	}

	
	return alertUser(empty_fields);

}

function ValidateLogon()
{
	var empty_fields = "";
	var sUserName = document.Logon.UserName.value.Trim();
	var sPassword = document.Logon.Password.value.Trim();
	
	if ( sUserName == '' )
	{
		empty_fields += "\n        - Email Address";	
		document.Logon.UserName.focus();
	}
	
	if ( sPassword == '' )
	{
		empty_fields += "\n        - Password";	
	}
	
	return alertUser(empty_fields);

}
function ValidateAccountRequest()
{
	var empty_fields = "";
	var sAgencyName = document.AccountRequest.sAgencyName.value.Trim();
	var sTelephone = document.AccountRequest.sTelephone.value.Trim();
	var sTitle = document.AccountRequest.sTitle.value.Trim();
	var sFirstName = document.AccountRequest.sFirstName.value.Trim();
	var sLastName = document.AccountRequest.sLastName.value.Trim();
	var sEmail = document.AccountRequest.sEmail.value.Trim();
	
	if ( sAgencyName == '' )
	{
		empty_fields += "\n        - Agency Name";	
		document.AccountRequest.sAgencyName.focus();
	}
	
	if ( sTelephone == '' )
	{
		empty_fields += "\n        - Telephone Number";	
	}
	
	if ( sTitle == '' )
	{
		empty_fields += "\n        - Title";	
	}
	
	if ( sFirstName == '' )
	{
		empty_fields += "\n        - First name";	
	}
	
	if ( sLastName == '' )
	{
		empty_fields += "\n        - Last name";	
	}
	
	
	if ( sEmail == '' )
	{
		empty_fields += "\n        - Email Address";	
		if (! empty_fields)
		{
			document.AccountRequest.sEmail.focus();
			document.AccountRequest.sEmail.select();
		}
	}
	else
	{
		if ( !CheckValidEmail(sEmail) )
		{
			empty_fields += "\n        - Invalid Email Address";
			if (! empty_fields)
			{
				document.AccountRequest.sEmail.focus();
				document.AccountRequest.sEmail.select();
			}
		}
	}
	
	

	return alertUser(empty_fields);
}

//Functions used by Validation scripts

	function alertUser(empty_fields)
	{
		var msg="";
		
		if (empty_fields){
			msg += "Please enter the following required information:\n"
					+ empty_fields + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}

	function alertUserDates(invalid_Dates)
	{
		var msg="";
		
		if (invalid_Dates){
			msg += "Invalid date range:"
					+ invalid_Dates + "\n";
			alert(msg);	
			return false;
		}else{ 
			return true;	
		}
	}


function CheckValidEmail( sText )
{
		
	// Assumes sText is not empty
	var d = sText.split('@')

	if ( d.length != 2 )
	{
		return false;
	}
	else
	{
		var dom = d[1].split('.') ;
		
		if ( dom.length < 2 )
			return false;
	}
	
	return true;
}

String.prototype.IsWhiteSpace = function()
{
	return this == ' ' || this == '\t';
}

String.prototype.TrimLeft = function()
{
	var i=0;
	
	while (this.charAt(i).IsWhiteSpace())
		i++;
		
	return this.substr(i)
}

String.prototype.TrimRight = function()
{
	var i=this.length;
	
	while (this.charAt(i).IsWhiteSpace())
		i--;
		
	return this.substr(0,i+1)
}

String.prototype.Trim = function()
{
	return this.TrimLeft().TrimRight();
}
