//form validation script by Chris Dowdell
//see www.twighlightzone.com
//Twighlightzone form validation script 2006 
//If you use this script please leave comments intact


function Form1_Validator(prereg)
{

// check to see if the field is blank
if (prereg.User_name.value == "")
{
alert("You must enter a name.");
prereg.User_name.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.User_name.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Name\" field.");
prereg.User_name.focus();
return (false);
}

// check to see if the field is blank
if (prereg.User_surname.value == "")
{
alert("You must enter a surname.");
prereg.User_surname.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.User_surname.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Surname\" field.");
prereg.User_surname.focus();
return (false);
}


// check to see if the field is blank
if (prereg.user_company.value == "")
{
alert("You must enter a company name.");
prereg.user_company.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.user_company.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Company\" field.");
prereg.user_company.focus();
return (false);
}


// check to see if the field is blank
if (prereg.User_email.value == "")
{
alert("You must enter an email address.");
prereg.User_email.focus();
return (false);
}

// test if valid email address, must have @ and .
var checkEmail = "@.";
var checkStr = prereg.User_email.value;
var EmailValid = false;
var EmailAt = false;
var EmailPeriod = false;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkEmail.length;  j++)
{
if (ch == checkEmail.charAt(j) && ch == "@")
EmailAt = true;
if (ch == checkEmail.charAt(j) && ch == ".")
EmailPeriod = true;
	  if (EmailAt && EmailPeriod)
		break;
	  if (j == checkEmail.length)
		break;
	}
	// if both the @ and . were in the string
if (EmailAt && EmailPeriod)
{
		EmailValid = true
		break;
	}
}
if (!EmailValid)
{
alert("The \"emails\" field must contain an \"@\" and a \".\".");
prereg.User_email.focus();
return (false);
}

// check to see if the field is blank
if (prereg.account_name.value == "")
{
alert("You must enter a username.");
prereg.account_name.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.account_name.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Username\" field.");
prereg.account_name.focus();
return (false);
}

// check to see if the field is blank
if (prereg.password1.value == "")
{
alert("You must enter a password.");
prereg.password1.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.password1.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Password\" field.");
prereg.password1.focus();
return (false);
}

// check to see if the field is blank
if (prereg.password2.value == "")
{
alert("You must confirm the password.");
prereg.password2.focus();
return (false);
}

// allow ONLY alphanumeric keys, no symbols or punctuation
// this can be altered for any "checkOK" string you desire
var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ";
var checkStr = prereg.password2.value;
var allValid = true;
for (i = 0;  i < checkStr.length;  i++)
{
ch = checkStr.charAt(i);
for (j = 0;  j < checkOK.length;  j++)
if (ch == checkOK.charAt(j))
break;
if (j == checkOK.length)
{
allValid = false;
break;
}
}
if (!allValid)
{
alert("Please enter only letter and numeric characters in the \"Confirm Password\" field.");
prereg.password2.focus();
return (false);
}

// check to see if passwords match
if (prereg.password1.value !=
prereg.password2.value) {
alert("Passwords don't match, try again");
prereg.password1.focus();
prereg.password1.select();
return (false);
} 


// because this is a sample page, don't allow to exit to the post action
// comes in handy when you are testing the form validations and don't
// wish to exit the page
alertsay = "All required fields completed. "
alertsay = alertsay + "We will now submit your form."
alert(alertsay);
return (true);
// replace the above with return(true); if you have a valid form to submit to
}

