﻿// JScript File
function ValidateProfile(type)
{    
    //clear message
    notification = "";
    
    //get textbox/dropdownlist values
    var fname = document.getElementById(GetClientID("txtFirstName")).value;
    var lname = document.getElementById(GetClientID("txtLastName")).value;
    var addr = document.getElementById(GetClientID("txtAddress")).value;
    var city = document.getElementById(GetClientID("txtCity")).value;
    var state = document.getElementById(GetClientID("ddlState"))[document.getElementById(GetClientID("ddlState")).selectedIndex].value;
    var zip = document.getElementById(GetClientID("txtZip")).value;
    var email = document.getElementById(GetClientID("txtEmail")).value;
    //var password = document.getElementById(GetClientID("txtPassword")).value;
    //var verifypassword = document.getElementById(GetClientID("txtVerifyPassword")).value;
    var phone = document.getElementById(GetClientID("txtPhone")).value;
    var bday = document.getElementById(GetClientID("txtBirthDate")).value;
    
    //get reference to labels
    var lfname = document.getElementById(GetClientID("lblFirstName"));
    var llname = document.getElementById(GetClientID("lblLastName"));
    var laddr = document.getElementById(GetClientID("lblAddress"));
    var lcity = document.getElementById(GetClientID("lblCity"));
    var lstate = document.getElementById(GetClientID("lblState"));
    var lzip = document.getElementById(GetClientID("lblZip"));
    var lemail = document.getElementById(GetClientID("lblEmail"));
    //var lpassword = document.getElementById(GetClientID("lblPassword"));
    //var lverifypassword = document.getElementById(GetClientID("lblVerifyPassword"));
    var lphone = document.getElementById(GetClientID("lblPhone"));
    var lbday = document.getElementById(GetClientID("lblBirthDate"));
    
    //validate values
    var count = 0;
    count += CheckValue(lfname, fname, true, null, null);
    count += CheckValue(llname, lname, true, null, null);
    count += CheckValue(laddr, addr, true, null, null);
    count += CheckValue(lcity, city, true, null, null);
    count += CheckValue(lstate, state, true, null, null);
    count += CheckValue(lzip, zip, true, "^\\d{5}-\\d{4}$|^\\d{5}$", "Zip must be in the format 00000 or 00000-0000");
    count += CheckValue(lemail, email, true, ".+@.+\\.[a-z]+", "Email must be in the format name@domain.com");
    
//    if (type == "register")
//    {
//        count += CheckValue(lpassword, password, true, "^\\w{5,15}$", "Password must be between 5 and 15 alphanumeric characters");
//        count += CheckValue(lverifypassword, verifypassword, true, "^\\w{5,15}$", "Password must be between 5 and 15 alphanumeric characters");
//    }
//    else
//    {
//        count += CheckValue(lpassword, password, false, "^\\w{5,15}$", "Password must be between 5 and 15 alphanumeric characters");
//        count += CheckValue(lverifypassword, verifypassword, false, "^\\w{5,15}$", "Password must be between 5 and 15 alphanumeric characters");
//    }
    
    count += CheckValue(lphone, phone, false, "^\\d{3}-\\d{3}-\\d{4}$|^\\d{3}-\\d{4}$", "Phone number must be in the format 000-0000 or 000-000-0000");
    count += CheckValue(lbday, bday, false, "^(?=\\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\\/|-|\\.)31)\\1|(?:(?:0?[1,3-9]|1[0-2])(\\/|-|\\.)(?:29|30)\\2))(?:(?:1[6-9]|[2-9]\\d)?\\d{2})|(?:0?2(\\/|-|\\.)29\3(?:(?:(?:1[6-9]|[2-9]\\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\\/|-|\\.)(?:0?[1-9]|1\\d|2[0-8])\\4(?:(?:1[6-9]|[2-9]\\d)?\\d{2}))($|\\ (?=\\d)))?(((0?[1-9]|1[012])(:[0-5]\\d){0,2}(\\ [AP]M))|([01]\\d|2[0-3])(:[0-5]\\d){1,2})?$", "Birth date must be in the format MM-DD-YYYY");
    
    //add notification if missing required fields
    if (count % 10 > 0)
        notification += "Missing one or more required values<br />";
        
    //add notification if password verification failed
//    if (password != verifypassword)
//    {
//        notification += "Password verification failed<br />";
//        lverifypassword.style.color = "red";
//        lverifypassword.style.fontWeight = "bold";
//        count++;
//    }

    //set notification text
    document.getElementById(GetClientID("lblProfileMessage")).innerHTML = notification;
        
    //return count
    if (count > 0)
        return false;
    else
        return true;
}

function CheckValue(control, value, required, expression, msg)
{
    var fail = 0;
    
    //test regular expression
    if (expression != null && value != "")
    {
        var regexp = new RegExp(expression);
        
        if (!value.match(regexp))
        {
            value = "";
            notification += msg + "<br />";
            fail = 9;
        }
    }
    
    if ((value == "" && required) || fail)
    {
        control.style.color = "red";
        control.style.fontWeight = "bold";
        return 1 + fail;
    }
    else
    {
        control.style.color = "black";
        control.style.fontWeight = "normal";
        return 0;
    }
}

function Alphanumeric()
{
    if ((event.keyCode < 45 || event.keyCode > 57) &&
            (event.keyCode < 64 || event.keyCode > 90) &&
            (event.keyCode < 97 || event.keyCode > 122) &&
            event.keyCode != 39 && event.keyCode != 8 && event.keyCode != 9 && event.keyCode != 32)
        event.returnValue = false;
}