//###############################################################################
// JIPB Script functions
// JJL, SCS - 12-Jul-2007
// Copyright 2007 Blackwell Publishing Ltd
//###############################################################################
// Change log
//###############################################################################
// Function to validate the length of the string .
function check_length(strText, intmaxLen)
{
	return (strText.value.length <= intmaxLen);
}
var strErr;

// Function to validate all elements in the form
function validateForms(form)
{
	var strTitle;
	var strFirstName;
	var strFamilyName;
	var strAddress;
	var strTown;
	var strProvince;
	var intCountry;
	var strPhone;
	var strEmail;
	var strConEmail;
	var strComment;

	strErr = "";
	strTitle = String(form.title.value);
	strFirstName = String(Trim(form.firstname.value));
	strFamilyName = String(Trim(form.surname.value));
	strAddress =  String(Trim(form.address.value));
	strTown = String(Trim(form.town.value));
	strProvince =  String(Trim(form.province.value));
	strZip = String(Trim(form.zipcode.value));
	intCountry = parseInt(form.country.value);
	strPhone =  String(Trim(form.phone.value));
	strEmail =  String(Trim(form.email.value));
	strConEmail =  String(Trim(form.conf_email.value));
	strComment = String(Trim(form.comments.value));
	
	if (strTitle == "" )
	{
	   strErr = "Please provide a Title <br />";
	}
	if (strFirstName == "")
	{
	   strErr = strErr + "Please provide a First name <br />";
	}
	else if (!ValidateLetters(strFirstName))
	{
		   strErr = strErr + "Please provide valid First name <br />";
	}
	if (strFamilyName == "")
	{
	    strErr = strErr +  "Please provide a Family name <br />";
	}
	else if  (!ValidateLetters(strFamilyName))
	{
	   strErr = strErr + "Please provide valid Family name <br />";
	}
	if (strAddress == "")
	{
	   strErr = strErr +  "Please provide an Address <br />";
	}
	else if  (!ValidateText(strAddress) || (isValidURL(strAddress)) ) 
	{
		   strErr = strErr +  "Please provide valid Address <br />";
	}
	if (strTown == "")
	{
	   strErr = strErr +  "Please provide a Town/City <br />";
	}
	else if (!ValidateText(strTown) || (isValidURL(strTown)) ) 
	{
		    strErr = strErr +  "Please provide valid Town/City <br />";
	}

	if (strProvince == "")
	{
	   strErr = strErr + "Please provide a Province  <br />";
	}
	else if  (!ValidateText(strProvince) || (isValidURL(strProvince)) ) 
	{
		   strErr = strErr + "Please provide valid Province  <br />";
	}

	if (strZip == "") 
	{
	   strErr = strErr +  "Please provide a Zipcode <br />";
	}
	else if (!ValidateZipcode(strZip) || (isValidURL(strZip)) ) 
	{
		  strErr = strErr +  "Please provide valid Zipcode <br />";
	}
	if (intCountry <= 0 )
	{
	    strErr = strErr +  "Please select a Country <br />";
	}

	if (strPhone == "" )
	{
	    strErr = strErr +  "Please provide your Telephone number <br />";
	}
	else if (!ValidatePhone(strPhone) || (!validateEmail(strPhone))  || (isValidURL(strPhone)))
	{
		  strErr = strErr + "Please provide valid Telephone number <br />";
	}
	if (strEmail == "")
	{
	    strErr = strErr +  "Please provide an E-mail <br />";
	}
	else if (validateEmail(strEmail)) 
	{
			 strErr = strErr +  "Please provide valid E-mail <br />";
	}
	if (strConEmail == "") 
	{
	    strErr = strErr +  "Please provide a Confirm E-mail <br />";
	}
	else if (validateEmail(strConEmail))
	{
	    strErr = strErr + "Please provide valid Confirm E-mail <br />";
	}

	if (((strEmail != "") && (strConEmail != "")) && (strEmail != strConEmail))
	{
			 strErr = strErr +  "Email and Confirm Email must be same <br />";
	}

	if ((strComment != "") && (!ValidComment(strComment)))
	{
			 strErr = strErr +  "Please provide valid Comments <br />";		
	}
	else if ((strComment != "") && (strComment.length > 2000))
	{
			 strErr = strErr + "Comments text exceeds maximum limit"	;
	}

	if (strErr == "")
		return true;
	else
		return false;
}

// Function to validate for Email address
function validateEmail(strText)
{
	var strFilter=/^.+@.+\..{2,3}$/;
	if (!strFilter.test(strText)) 
		return true; 
	else
		return false;
	
}

// Function to validate for URL
function isValidURL(strText){ 
    var RegExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/; 
    if(RegExp.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 Validate the form (contact.asp and subscription_form.asp)
function JValidateForm(form)
{
	if (!validateForms(form))
			{
				document.getElementById("js_error").innerHTML = strErr;
				strErr = "";
				return false;
			}
	return true;
}

// 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 (loi.asp)
function ValidateSearchForm(form, intIndex)
{
	var strSearch;
	if (intIndex == 1)
	{
		strSearch = String(Trim(form.SearchTerm.value));
		if (strSearch == "")
		{
			document.getElementById("js_error").innerHTML = "<p class='warning'>Please enter a search term</p>";
			return false;
		}
		return true;
	}
	else if (intIndex == 2)
	{
		strSearch = String(Trim(form.key.value));
		if (strSearch == "")
		{
			document.getElementById("js_error1").innerHTML = "<p class='warning'>Please enter a search term</p>";
			return false;
		}
		return true;
	}
}