//###############################################################################
// ES Script functions
// FLR, SCS - 08-Aug-2007
// Copyright 2007 Blackwell Publishing Ltd
//###############################################################################
// Change log
// 27/09/2007 FLR, SCS 056ES - Removed the special character validation for 
//								strSeminarName and strAdmin as requested by Jake Farr
//###############################################################################

var strErr;

//Validates the Registration form details
function ValidatesRegistrationInputs(form)
{

	strErr = "";
	var strEmail = String(Trim(form.email.value));
	var strConEmail = String(Trim(form.confirm_email.value));
	var strPassword = String(Trim(form.password.value));
	var strReTypePassword = String(Trim(form.confirm_password.value));
	var strTitle = String(Trim(form.title.value));
	var strFirstName = String(Trim(form.firstname.value));
	var strLastName = String(Trim(form.lastname.value));
	var strJobTitle = String(Trim(form.jobtitle.value));
	var strProfession = String(Trim(form.profession.value));
	var strInstitution = String(Trim(form.institution_organization.value));
	var strAddress1 = String(Trim(form.address1.value));
	var strAddress2 = String(Trim(form.address2.value));
	var strAddress3 = String(Trim(form.address3.value));
	var strCity = String(Trim(form.city.value));
	var strState = String(Trim(form.state.value));
	var strZip = String(Trim(form.zip.value));
	var strCountry = String(Trim(form.country.options[form.country.selectedIndex].value));
	var strPhone = String(Trim(form.phone.value));
	
	if ( (ValidateEmail(strEmail, "E-mail")) & (ValidateEmail(strConEmail, "Confirm E-mail")) )
	{
		if (strEmail != strConEmail)
		{
			 strErr = strErr +  "E-mail and Confirm E-mail must be same <br />";
		}
	}

	if ( (ValidMandatoryValue(strPassword, 8, 160, "Password")) & (ValidMandatoryValue(strReTypePassword, 8, 160, "Retype Password")) )
	{
		if (strPassword != strReTypePassword)
		{
			 strErr = strErr +  "Password and Retype Password must be same <br />";
		}
	}
	
	ValidMandatoryValue(strTitle, 1, 160, "Title");
	ValidMandatoryValue(strFirstName, 1, 160, "First Name");
	ValidMandatoryValue(strLastName, 1, 160, "Last Name");
	ValidNonMandatoryValue(strJobTitle, 1, 160, "Job Title");
	ValidMandatoryValue(strProfession, 1, 160, "Profession");
	ValidNonMandatoryValue(strInstitution, 1, 160, "Institution/Organization");
	ValidNonMandatoryValue(strAddress1, 1, 160, "Address line 1");
	ValidNonMandatoryValue(strAddress2, 1, 160, "Address line 2");
	ValidNonMandatoryValue(strAddress3, 1, 160, "Address line 3");
	ValidNonMandatoryValue(strCity, 1, 160, "City/Town");
	ValidNonMandatoryValue(strState, 1, 160, "State/County");
	ValidNonMandatoryValue(strZip, 1, 160, "Zip Code/Post Code");
	ValidMandatoryValue(strCountry, 1, 160, "Country");
	ValidNonMandatoryValue(strPhone, 1, 160, "Phone Number");

	if (strErr == "")
	{
		return true;
	}
	else
	{
		DisplayErrorMessages(strErr);
		return false;
	}

}


//Validates the Forgotten Password form details
function ValidateForgottenForm(form)
{
	strErr = "";
	var strEmail = String(Trim(form.email.value));

	ValidateEmail(strEmail, "Email ")
	
	if (strErr == "")
	{
		return true;
	}
	else
	{
		DisplayErrorMessages("This is not a valid email address, please try again");
		return false;
	}
}


//Validates the Login form details
function ValidateLoginForm(form)
{
	strErr = "";

	var strEmail = String(Trim(form.email.value));
	var strPassword = String(Trim(form.password.value));

	ValidateEmail(strEmail, "Email ")
	ValidMandatoryValue(strPassword, 8, 160, "Password")

	if (strErr == "")
	{
		return true;
	}
	else
	{
		DisplayErrorMessages(strErr);
		return false;
	}
}


function ValidNonMandatoryValue(value, minLength, maxlength, name)
{
	var strValidNonMandatoryValueErrMsg = "";

	if (value != "") 
	{
		if (!check_length(value, minLength, maxlength))
		{
			strValidNonMandatoryValueErrMsg =  name + " has an invalid length (" + minLength + " - " + maxlength + " characters) <br />";
		}
	}

	if (strValidNonMandatoryValueErrMsg == "")
	{
		return true;
	}
	else
	{
		strErr = strErr + strValidNonMandatoryValueErrMsg;
		return false;
	}	

}

function ValidMandatoryValue(value, minLength, maxlength, name)
{
	var strValidMandatoryValueErrMsg = "";

	if (value == "") 
	{
	    strValidMandatoryValueErrMsg = name + " is missing <br />";
	}
	else if (!check_length(value, minLength, maxlength))
	{
		strValidMandatoryValueErrMsg = name + " has an invalid length (" + minLength + " - " + maxlength + " characters) <br />";
	}

	if (strValidMandatoryValueErrMsg == "")
	{
		return true;
	}
	else
	{
		strErr = strErr + strValidMandatoryValueErrMsg;
		return false;
	}	
}

//checks the email address field
function ValidateEmail(Email, Name)
{
	var strCheckEmailErrMsg = "";

	if (Email == "")
	{
	    strCheckEmailErrMsg = Name + " Address is missing <br />";
	}
	else if (ValidateEmailFormat(Email)) 
	{
		strCheckEmailErrMsg = strCheckEmailErrMsg + Name + " Address is invalid <br />";
	}
	else if (!check_length(Email, 1, 160))
	{
		strCheckEmailErrMsg = strCheckEmailErrMsg + name + " Address has an invalid length (" + minLength + " - " + maxlength + " characters) <br />";
	}

	if (strCheckEmailErrMsg == "")
	{
		return true;
	}
	else
	{
		strErr = strErr + strCheckEmailErrMsg;
		return false;
	}
}

// Function to validate the length of the string .
function check_length(strText, intminLength, intmaxLen)
{
	var strInnerText = String(strText + "");
		
	if ( (strInnerText.length >= intminLength) && (strInnerText.length <= intmaxLen) )
		return true;
	else
		return false;
}

// Function to display the error message in the form
function DisplayErrorMessages(ErrMsg)
{
	document.getElementById("js_error").innerHTML = ErrMsg;
	ErrMsg = "";
}


// Function to validate for Email address
function ValidateEmailFormat(strText)
{
	var strFilter = new RegExp("^\\w+([-+.!#$%&'*/=?^`{|}~]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
	if (!strFilter.test(strText)) 
		return true; 
	else
		return false;
	
}

// Function to validate for string which accepts alphanumeric (not email id and url)
function ValidateText(strText)
{
if (strText.match(/^[a-zA-Z0-9'( ).#,]+$/))
	return true;
else
	return false;
}

// Function to validate for Zip code
function ValidateZipcode(strText)
{
	if (strText.match(/^[a-zA-Z0-9'( )_-]+$/))
		return true; 
	else
		return false;
}

// Function to validate for phone numbers
function ValidatePhone(strText)
{
		if (strText.match(/^[a-zA-Z( )0-9]+$/))
		return true; 
	else
		return false;
}

// Function to validate the comment string to check for spam words.
function ValidComment(strText)
{
	if ((strText.indexOf("Content-Type:") >= 0 ) ||  (strText.indexOf("multipart/alternative") >= 0 ) ||  (strText.indexOf("Content-Transfer-Encoding") >= 0 ))
		return false;	
	else
		return true;
}

// Function to validate the string to accept only characters.
function ValidateLetters(strText)
{
if (strText.match(/^[a-zA-Z'( ).]+$/))
	return true;
else
	return false;
}

// Function to trim white spaces from the beginning and the end of a string. 
function Trim(str)
{  
	while(str.charAt(0) == (" ") )
	{  
		str = str.substring(1);
	}
	while(str.charAt(str.length-1) == " " )
	{  
		str = str.substring(0,str.length-1);
	}

  return str;
}

// Function to Validate the form (contact.asp and subscription_form.asp)
function JValidateForm(form)
{
	if (!ValidateSeminarInputs(form))
			{
				DisplayErrorMessages("<br /> " +  strErr);
				return false;
			}
	return true;
}

// Function to validate all elements in the form
function ValidateSeminarInputs(form)
{
	var strSeminarName;
	var strLocation;
	var strAdmin;
	var intSeminarDay;
	var intSeminarMonth;
	var intSeminarYear;
	var strSeminarContent;
	var strSemDate;
	
	strErr = "";
	strSeminarName = String(Trim(form.Seminar_name.value));
	strLocation = String(Trim(form.location.value));
	strAdmin = String(Trim(form.admin.value));
	intSeminarDay = parseInt(form.selDate_D.value);
	intSeminarMonth = parseInt(form.selDate_M.value);
	intSeminarYear = parseInt(form.selDate_Y.value);
	strSeminarContent = String(form.fldP1reference.value);

	if (strSeminarName == "") 
	{
	   strErr = "The Seminar Name field is empty.<br />";
	}
	/* 27/09/2007 FLR, SCS 056ES: Start
	else if (!ValidateText(strSeminarName))
	{
		   strErr = "Please enter a valid Seminar Name. <br />";
	} 27/09/2007 FLR, SCS 056ES: End */
	if (!checkDate(intSeminarDay, intSeminarMonth, intSeminarYear))
	{
	   strErr = strErr + "Please enter a valid Date. <br />";
	}

	if (strLocation == "") 
	{
	   strErr = strErr + "The Seminar Location field is empty. <br />";
	}
	else if (!ValidateText(strLocation))
	{
		   strErr = strErr + "Please enter a valid Seminar Location. <br />";
	}

	if (strAdmin == "") 
	{
	   strErr = strErr + "The Seminar Administrator field is empty. <br />";
	}
	/* 27/09/2007 FLR, SCS 056ES: Start
	else if (!ValidateText(strAdmin))
	{
		   strErr = strErr + "Please enter a valid Seminar Administrator. <br />";
	} 27/09/2007 FLR, SCS 056ES: End */

	if (strSeminarContent == "")
	{
		   strErr = strErr + "Seminar resource page content field is empty. <br />";
	}
	
	if (strErr == "")
		return true;
	else
		return false;
}


// Function to validate if the parameter passes is a valid date
function checkDate(intday, intMonth, intYear )
{
	if (intMonth>12 || intMonth<1) 
		return false;
	if ((intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12) && (intday > 31 || intday < 1)) 
		return false;
	if ((intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11) && (intday > 30 || intday < 1)) 
		return false;
	if (intMonth == 2)
	{
		if (intday < 1)
			return false;
		if (isLeapYear(intYear) == true) 
		{
			if (intday > 29) 
				return false;
		}
		else 
		{
			if (intday > 28) 
				return false;
		}
	}
	return true;
}

// Function to check if year is a leap year
function isLeapYear(yr) {
  return new Date(yr,2-1,29).getDate()==29;
}

// Function to open a new windows loading the URL parameter passed
function popUp(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=380,height=220,left = 380,top = 212');");
}

// Function to reset the form to "blank" values
function ResetForm(intFormNumber) {
    for(i=0; i<document.forms[intFormNumber].elements.length; i++)
    {
        switch (document.forms[intFormNumber].elements[i].type) {
            case 'text': case 'password': case "textarea":
                document.forms[intFormNumber].elements[i].value = '';
                break;
            case 'select-one':
                document.forms[intFormNumber].elements[i].selectedIndex = 0;
                break;
			case "radio": case "checkbox":
				if (document.forms[intFormNumber].elements[i].name == 'vegetarian_meal')
				{
					if (document.forms[intFormNumber].elements[i].value == 0)
					{
						eval(document.forms[intFormNumber].elements[i].checked=1);
					}
				}
				else 
				{
					eval(document.forms[intFormNumber].elements[i].checked=0);
				}
                break;
        }
    }
}   
