// JavaScript Document

	function reportError(msg,url,line) {
		var str = "Err: " + msg + " on line: " 
		+ line + "\nURL: " + url  + "\nWeb: " 
		+ navigator.appName + " " + navigator.appName 
		+ " " + navigator.appVersion;
		alert(str);
		return true;
	}
	
	var whitespace = " \t\n\r";
    var defaultEmptyOK = false;
    var assign = "";

	function isEmpty(s)
	{
		return ((s == null) || (s.length == 0))
	}

	function TrimString(inputStringTrim)
	{
		fixedTrim = "";
		lastCh = " ";
		for (x=0; x < inputStringTrim.length; x++)
		{
			ch = inputStringTrim.charAt(x);
			if ((ch != " ") || (lastCh != " "))
			{
				fixedTrim += ch;
			}
			lastCh = ch;
		}
		if (fixedTrim.charAt(fixedTrim.length - 1) == " ")
		{
			fixedTrim = fixedTrim.substring(0, fixedTrim.length - 1);
		}
		return fixedTrim
	}

	function isWhitespace (s)
	{
		 var i;
 		if (isEmpty(s)) return true;
 		for (i = 0; i < s.length; i++)
 		{
 			var c = s.charAt(i);
		    if (whitespace.indexOf(c) == -1) return false;
 		}
	    return true;
	}

     function isEmail (s)
     {
		if (isEmpty(s))
        if (isWhitespace(s)) return false;
        var i = 1;
        var sLength = s.length;
        while ((i < sLength) && (s.charAt(i) != "@"))
		{
			i++
		}
        if ((i >= sLength) || (s.charAt(i) != "@")) return false;
        else i += 2;
        while ((i < sLength) && (s.charAt(i) != "."))
        {
			i++
        }
        if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
        else return true;
	}
	
	function checkAtTheRate (s)
  	{
		var i,cnt;
		cnt=0;
		for (i = 0; i < s.length; i++)
		{
			if (s.charAt(i) == "@") cnt++;
		}
		if (cnt > 1) return false;
		else return true;
	}
	
    function isCharsInBag (s, bag)
  	{
		var i;
		for (i = 0; i < s.length; i++)
		{
			var c = s.charAt(i);
			if (bag.indexOf(c)==-1) return false;
		}
		return true;
	}
	
	function BuildStr(s, s1)
	{
		if (s.length > 0) s = s + ", ";
		s = s + s1;
		return s;
	}
	
	// This function  returns value in numbers and dots e.g 9998888.00
	function check(fieldvalue) 
	{
		var a = fieldvalue.split("");
		var b = "";
		var c = "";
		for (var i in a)
		{					
			if ((!isNaN(a[i])) || (a[i] == "."))
			{
				c  += a[i]
			}
		}		 

		while (c.length>1 && b.charAt(0) == "0") c=c.substr(1,c.length)
			return c;
	}
			
	//=========================================
	function Validator(theForm)
	{

	var txtName = TrimString(theForm.txtName.value);
	
	if ( txtName.length == 0 )
	{
   		alert("Please enter Name");
   		theForm.txtName.value = ""
   		theForm.txtName.focus();
      	return false;
	}
	
	

var Email = theForm.txtEmail.value= theForm.txtEmail.value.toLowerCase();
	Email = TrimString(Email)
	if ( Email.length == 0 )
	{
   		alert("Please enter Email Address" );
   		theForm.txtEmail.focus();
    	return false;
	}
   	if(!isEmail( Email ))
	{
		alert("Invalid Email address");
		theForm.txtEmail.focus();
		return false;
	}
	if(!checkAtTheRate( Email ))
	{
		alert("Invalid Email address");
		theForm.txtEmail.focus();
		return false;
	}
	if ( !isCharsInBag( Email, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.-_@" ))
	{
		alert( "Invalid Email address" );
		theForm.txtEmail.focus();
		return false;
	}
	else if ( !isCharsInBag( Email.charAt(Email.length - 1), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") )
	{
		alert( "Invalid Email address");
		theForm.txtEmail.focus();
		return false;
	}
	else if ( !isCharsInBag( Email.charAt(0), "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") )
	{
		alert( "Invalid Email address");
		theForm.txtEmail.focus();
		return false;
	}
	
	var arrEmail = Email.split(".");
	var eachDot = "True"
	for (x=0; x < arrEmail.length; x++) {
		if (arrEmail[x].length == 0) {
			eachDot = "False";
		}
	}
	if (eachDot == "False")
	{
		alert( "Invalid Email address");
		theForm.txtEmail.focus();
		return false;
	}

	var txtComments2 = theForm.txtComments.value;
	txtComments2 = TrimString(txtComments2);
	var actual = 0;
	
	
		//THE ENTER CHARACTER OCCUPIES SPACE IN THE DATABASE AND NEEDS TO BE ACCOUNTED FOR IN IE
		//IT IS ACCOUNTED FOR IN NETSCAPE.
		var content = txtComments2;
		var total = 0;
		var i=0;
		var numberofwords=1;
		
		while (i <= content.length)
		{
			if (content.substring(i,i+1) == "\n") 
			{
				numberofwords++;
				i++;	// extra i++ makes it skip double spaces, or space/return
			}
			i++;
		}
		
		total = numberofwords;
		actual = txtComments2.length + ((total-1) * 2)
	
		
	if ( actual >  200)
	{
		alert("Comments can be a maximum of 200 characters. Your entry is " + actual + " characters.");
		theForm.txtComments.focus();
		theForm.txtComments.select();
		return false;
	}
	
		if ( actual == 0)
		{
			alert("Please enter Comments.");
			theForm.txtComments.focus();
			theForm.txtComments.select();
			return false;
		}
		
		return true;
	}
	
	//Check errors
	window.onerror = reportError;
