// JavaScript Document
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;    
var iChars = "\'\"<>";
var telephoneFilter = /^((\d{3}[\-\.\s]?)|(\(\d{3}\)\s?))\d{3}[\-\.\s]?\d{4}$/;
var alphaFilter = /^[a-z]+$/i;
var alphaWithSpaceFilter = /^[a-z ]+$/i;
var numberFilter = /^[0-9]+$/;

function isText(str)
{
	var i = 0;
	if(str.charAt(0)==' ') i++;
	while(i<str.length)
	{
		if (str.charAt(i)>='0' && str.charAt(i)<= '9' || str.charAt(i)=='+' || str.charAt(i)=='-' || str.charAt(i)=='*' || str.charAt(i)=='/' || str.charAt(i)=='\\' || str.charAt(i)=='\'' || str.charAt(i)=='@' || str.charAt(i)=='!' || str.charAt(i)=='~' || str.charAt(i)=='`' || str.charAt(i)=='#' || str.charAt(i)=='$' || str.charAt(i)=='%' || str.charAt(i)=='^' || str.charAt(i)=='&' || str.charAt(i)=='(' || str.charAt(i)==')' || str.charAt(i)=='_' || str.charAt(i)=='=' || str.charAt(i)=='?' || str.charAt(i)=='.' || str.charAt(i)==',' || str.charAt(i)==';' || str.charAt(i)==':' || str.charAt(i)=='"' || str.charAt(i)=='{' || str.charAt(i)=='}' || str.charAt(i)=='[' || str.charAt(i)==']' || str.charAt(i)=='<' || str.charAt(i)=='>' || str.charAt(i)=='|')
		return false;
		i++;
	}
	return true;
}
            
function trim(str)
{
	return str.replace(/(^\s+)|(\s+$)/g, '');
}

function ltrim(str)
{
	return str.replace(/^\s+/, '');
}

function rtrim(str)
{
	return str.replace(/\s+$/, '');
}

function isAlpha(str)
{
    return alphaFilter.test(trim(str));
}

function isAlphaWithSpace(str)
{
    return alphaWithSpaceFilter.test(trim(str));
}

function isNumber(char)
{
	return (char >= '0' && char <= '9');
}

function isNumber(str)
{
	return numberFilter.test(trim(str));
}


function isUpper(char)
{
	return (char >= 'A' && char <= 'Z');
}

function isLower(char)
{
	return (char >= 'a' && char <= 'z');
}

function isTelephone(str)
{
	return telephoneFilter.test(trim(str));
}

function validation()
{
	if(document.getElementById("firstName").value == 0)
	{
		alert ("Please enter your first name and resubmit your request.");
		document.getElementById("firstName").focus();
		return false;
	}
	else if(!isAlpha(document.getElementById("firstName").value))
	{
		alert("First name cannot contain spaces or special characters.  Please fix the error and resubmit your request.");
		document.getElementById("firstName").focus();
		return false;
	}
	
	if(document.getElementById("lastName").value == 0)
	{
		alert ("Please enter your last name and resubmit your request.");
		document.getElementById("lastName").focus();
		return false;
	}
	else if(!isAlpha(document.getElementById("lastName").value))
	{
		alert("Last name cannot contain spaces or special characters.  Please fix the error and resubmit your request.");
		document.getElementById("lastName").focus();
		return false;
	}

	if(document.getElementById("address").value == 0)
	{
		alert ("Please enter your Adrress and resubmit your request.");
		document.getElementById("address").focus();
		return false;
	}
	
	for(var i = 0; i < document.getElementById("address").value.length; i++)
	{
		if(iChars.indexOf(document.getElementById("address").value.charAt(i)) != -1)
		{
			alert("Address cannot containt special characters(single/double quotes, <,>). Please fix the error and resubmit your request.");
			return false;
		}
	}
	if(document.getElementById("city").value == 0)
	{
		alert ("Please enter your city and resubmit your request.");
		document.getElementById("city").focus();
		return false;
	}
	else if(!isAlphaWithSpace(document.getElementById("city").value))
	{
		alert("Please enter a valid city name and resubmit your request.");
		document.getElementById("city").focus();
		return false;
	}
			
	if(document.getElementById("zipcode").value == 0)
	{
		alert ("Please enter your zipcode and resubmit your request.");
		document.getElementById("zipcode").focus();
		return false;
	}
	else if(!isNumber(document.getElementById("zipcode").value))
	{
		alert("Please enter a valid zipcode and resubmit your request.");
		document.getElementById("zipcode").focus();
		return false;
	}

	if(emailfilter.test(document.getElementById("email").value) == false)
	{
		alert ("Please enter a valid e-mail id and resubmit your request.");
		document.getElementById("email").focus();
		return false;
	}

	if(document.getElementById("phone").value == 0)
	{
		alert ("Please enter your telephone number and resubmit your request.");
		document.getElementById("phone").focus();
		return false;
	}
	
	if(document.getElementById("phone").value!="")
	{
		if(!isTelephone(document.getElementById("phone").value))
		{
			alert ("Please enter a valid telephone number and resubmit your request.");
			document.getElementById("phone").focus();
			return false;
		}
	}
	
	if(document.getElementById("message").value == 0)
	{
		alert ("Please enter your comments and resubmit your request.");
		document.getElementById("message").focus();
		return false;
	}
	
	if (document.getElementById("message").value!="")
	{
		var cou = document.getElementById("message").value.length;        
		if(cou > 400)
		{    
			alert ("Please enter your message that does not exceed 400 characters and resubmit your request.");
			document.getElementById("message").focus();
			return false;    
		}
	}
	
	for(var i = 0; i < document.getElementById("message").value.length; i++)
	{
		if(iChars.indexOf(document.getElementById("message").value.charAt(i)) != -1)
		{
			alert ("Please remove single/double quotes, < > before submitting your request.");
			return false;
		}
	}
	
	var uword = hex_md5(document.getElementById(jfldid).value);

	if (uword != cword[anum-1]) 
	{
		alert("Please enter the verification code as it is shown and resubmit your request.");
		document.getElementById(jfldid).focus();
		return false;
	}
	
	return true;
}