<!--
// Validation Javascript

var emptyString = /^\s*$/;
var illegalChars = /[\W_]/; // allow only letters and numbers
var alphanum = /[a-zA-Z]+\w*\d+\w*/;
var alphanumpass = /[a-zA-Z]+\w*\d+\w*/;
var numbers = /^[1-2]{1}[9,0]{1}[0-9]{1}[0-9]{1}/;
var postcode = /^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) {0,1}[0-9][A-Za-z]{2})$/;
var nbsp = 160;
var level = 0;
var SHORT = 'SHORT';
var WEAK = 'WEAK';
var MEDIUM = 'MEDIUM';
var STRONG = 'STRONG';

// Function to
function remove(string)
{
  return string.replace(/^\s+|\s+$/g, '');
}

function emailval(email, info) // This checks if the email submitted contains alphanumerics before an '@' sign, followed by more alphanumerics followed by a'.' followed by more alphanumerics
{
// E-mail Validation
var e = remove(email.value);
var email = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
if (!email.test(e)) {  // If the email string fails against the email regular exspression
msg(info, 'Not a valid email address.', 'error');} //Send to message function the CSS Id (info), the chosen error message, and the CSS class information
else {msg(info, 'Email Accepted', 'accept');}   // Else send to message function, success information
}  //END fuction

function emptyval(entered, info)
{
with (entered)
      {
        if (entered.value == "")
        {
          msg(info, 'This field is Required.','error');
        }
        else {msg(info, ' ',' ');}
      }
}

function postval(post, info)
{
if (post.value == "")
            {
            msg(info, 'Postcode is Required.', 'error');
            }
    else if (postcode.test(post.value))
            {
            msg(info, 'Postcode Accepted.', 'accept');

            }
    else {msg(info, 'Invalid Postcode.', 'error'); }
}

function userval(username, info)
{
if (username.value == "")
            {
            msg(info, 'Username is Required.', 'error');
            }
        else if ((username.value.length < 5) || (username.value.length > 15))
            {
            msg(info, 'Username too short.', 'error');
            }
     else if (illegalChars.test(username.value))
            {
                msg(info, 'Username contains illegal characters.','error');
            }
        else if (!alphanum.test(username.value))
            {
            msg(info, 'Username must contain at least one numeral.', 'error');
            }
        else {msg(info, 'Username Accepted.', 'accept');}
}

function yearval(year, info)
{
if (year.value == "")
            {
            msg(info, 'Year is Required.', 'error');
            }
        else if (year.value.length < 4)
            {
            msg(info, 'Four Didgit Year Please.', 'error');
            }
     else if (illegalChars.test(year.value))
            {
                msg(info, 'Username contains illegal characters.','error');
            }
        else if (!numbers.test(year.value))
            {
            msg(info, 'Unusual Year, please check and try again.', 'error');
            }
        else {msg(info, 'Year Accepted.', 'accept');}
}

function small (pass)
{
    if(pass.length < 5 ) {

    return true;    }

    else {

        return false;

    }
}

function medium (pass)
{
    if(pass.length <7 ) {

    return true;    }

    else {

        return false;

    }
}

function passnum (pass)
{
    var passnum = false;

    for(var count=0; count < pass.length; count++)
    {
        if((!isNaN(pass.charAt(count)))&(pass.length > 9))
        {
        passnum = true;
        }
    }
return passnum;
}



function updatepass (pass, info) {

if(small(pass)){
    level = SHORT;
    }
    else if(medium(pass)){
    level = WEAK;
    }
    else if(passnum(pass)){
    level = STRONG;
    }
    else {
    level = MEDIUM;
    }
    msg(info,'Password Strength: '+ level, level);
}

function passval(pass, info, info2) {

    if (pass.value == "")
            {
                msg(info, 'Password is Required.', 'error');
            }
     else if ((pass.value.length < 5) || (pass.value.length > 20))
            {
                msg(info, 'Password too short.','error');
            }
     else if (illegalChars.test(pass.value))
            {
                msg(info, 'Password contains illegal characters.','error');
            }
    else if (!alphanumpass.test(pass.value))
            {
                msg(info, 'Password must contain at least one numeral','error');
            }
    else if(document.regx.pass1.value==document.regx.pass2.value)
            {
                msg(info, 'Password Accepted','accept');
                msg(info2, 'Password Accepted','accept');
            }
    else{msg(info, 'Passwords do not match','error');
         msg(info2, 'Passwords do not match','error');
        }
}

function passvalmini(pass, info) {

    if (pass.value == "")
            {
                msg(info, 'Password is Required.', 'error');
            }
     else if ((pass.value.length < 5) || (pass.value.length > 20))
            {
                msg(info, 'Password too short.','error');
            }
     else if (illegalChars.test(pass.value))
            {
                msg(info, 'Password contains illegal characters.','error');
            }
    else if (!alphanumpass.test(pass.value))
            {

                msg(info, 'Password must contain at least one numeral','error');
            }
    else { }
}

function msg(info, message, type)
{
var display;

if (emptyString.test(message))
   display = String.fromCharCode(nbsp);
   else
   display = message;
   var el = document.getElementById(info);
   el.firstChild.nodeValue = message;
   el.className = type;
}

// Limits text areas to a specific length
function ismaxlength(obj){

    var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""

    if (obj.getAttribute && obj.value.length>mlength)

    obj.value=obj.value.substring(0,mlength)

}

