﻿//
// Used to validate field values
//
function isValid(field,name,mandatory,check)
    {
    var errorfield = "Y";
    var msg = "";
    var val = "";
    if(mandatory)
        {
        // Check to see if mandatory field is blank
        var emptyfieldcheck = stripBlanks(field);
        if(emptyfieldcheck=="")         { errorfield = "N"; msg="Field cannot be blank."; }
        // Check to see if mandatory field is more than or less than X number of characters
        if(name=="membership_number")   { val=/^[0-9]{1,4}$/;    if (!val.test(field)) { errorfield = "N"; msg=msg+" Field must be between 1 and 4 digits."; } }
        if(name=="zipcode")             { val=/^[0-9]{5}$/;    if (!val.test(field)) { errorfield = "N"; msg=msg+" Field must be a 5 digit zip code."; } }
        //if(name=="state")               { if(field.length > 2 || field.length < 2) { errorfield = "N"; msg=msg+" Field can only contain a 2 character state abbreviation."; } }
        if(name=="password1")           { val=/^[A-Za-z0-9]{4,8}$/;    if (!val.test(field)) { errorfield = "N"; msg=msg+" Password must be between 4 and 8 characters."; } }
        if(name=="password2")           { val=/^[A-Za-z0-9]{4,8}$/;    if (!val.test(field)) { errorfield = "N"; msg=msg+" Password must be between 4 and 8 characters."; } }
        }

    // VALIDATE FUNCTION:
    //      isValid(this.value,this.name,1,0);
    //      1 - is numeric          4 - is alphabetic         7 - is phone number
    //      2 - is lower case       5 - is alphanumeric       8 - password
    //      3 - is upper case       6 - is email              9 -
    // Check to see if field value fits the correct parameters
    /* Number   */ if(check=="1") { val=/^[0-9]/;    if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field can only be numeric."; } }
    /* Lower    */ if(check=="2") { val=/^[a-z]/;    if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field can only be lower case letters."; } }
    /* Upper    */ if(check=="3") { val=/^[A-Z]/;    if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field can only be upper case letters."; } }
    /* Alpha    */ if(check=="4") { val=/^[A-Za-z]/;    if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field can only be alphabetic."; } }
    /* Alphanum */ if(check=="5") { val=/^[a-zA-Z0-9]/; if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field can only be alphanumeric."; } }
    /* Email    */ if(check=="6") { val=/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/; if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field is not a valid email address."; } }
    /* Phone    */ if(check=="7") { val=/^\(?([1-9]\d{2})(\) ?|[.-])?(\d{3})[.-]?(\d{4})$/; if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field is not a valid email address."; } }
    /* Date     */ if(check=="8") { val=/^\d{1,2}\/\d{1,2}\/\d{4}$/; if (!val.test(field) && !field=="") { errorfield = "N"; msg=msg+" Field is not a valid date (mm/dd/yyyy."; } }

    // ERROR CHECK
    //      IF ERROR then it gives an alert and highlights the field red
    //      IF VALID then it highlights the field white but no error

    if(errorfield=="N") { document.getElementById(""+name+"").style.background='#FF0000';  return false; }
    else { document.getElementById(""+name+"").style.background='#FFFFFF'; return true; }
    }

function checkform ( form )
{
    var required = new Array ("last_name", "first_name", "password1", "password2", "membership_number", "address", "city", "zipcode", "county", "phone", "emailaddress", "birthdate", "licno",  "electronic_signature", "signature_date");
    var FormElms = form.elements;
    var errorinfield = "";
    for (var i=0; i<FormElms.length; i++)
    {
        for (var j=0; j<required.length; j++)
        {
            if (FormElms[i].name == required[j] && FormElms[i].value == "")
                {
                document.getElementById(""+FormElms[i].name+"").style.background='#FF0000';
                errorinfield = "YES";
                }
        }
    }
    if( errorinfield == "YES")
        {
        alert("The field that are highlighted in red must be filled out.");
        //document.getElementById('pidbutton[0]').disabled = true;
        //document.getElementById('pidbutton[1]').disabled = true;
        //document.getElementById('pidbutton_sq').disabled = true;
        return false;
        }
    else {
         //document.getElementById('pidbutton[0]').disabled = false;
         //document.getElementById('pidbutton[1]').disabled = false;
         //document.getElementById('pidbutton_sq').disabled = false;
         return true;
         }
}







// USED BY ISVALID FUNCTION
function stripBlanks(fld)
    {
    var result = "";
    var c = 0;
    for (i=0; i < fld.length; i++)
        {
        if (fld.charAt(i) != " " || c > 0)
            {
            result += fld.charAt(i);
            if (fld.charAt(i) != " ") c = result.length;
            }
        }
    return result.substr(0,c);
    }


/*
USED IN:
    membership_information
*/
function openWindow ( url ) { popupWin = window.open ( url, 'remote', 'width=500,height=350' ); }

/*
USED IN:
    membership_information
*/
function openWindowLg (url)
{
  var option = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=800, height=580";
  var urlWindow = window.open( url, "NewWindow", option );
}