﻿//Passes JSLint http://www.jslint.com/

function get_cookie(cookie_name) {
    var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');
    if (results)
        return (unescape(results[1]));
    else
        return null;
}

function cookieExists(cookie_name) {
    var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');
    if (results)
        return true;
    else
        return false;
}

function Form_Validator(theForm) {
    var password = document.getElementById('Password');
    var passwordConfirm = document.getElementById('PasswordConfirm');

    if (password !== null && passwordConfirm !== null && password.value != passwordConfirm.value) {
        alert("Password entries do not match.");
        password.focus();
        return false;
    }
    
//    var lawSchoolStudent = document.getElementById('LawSchoolStudent');
//    var lawSchool = document.getElementById('LawSchool');
//    var lawSchoolGraduationYear = document.getElementById('LawSchoolGraduationYear');
//    if (lawSchoolStudent === null || lawSchoolStudent.checked === false || lawSchool === null || lawSchoolGraduationYear === null) {
//        return (true); //Not checked or missing fields
//    }
//    else if (lawSchool.value === '' || lawSchoolGraduationYear.value === '') {
//        alert('Both Law School and Year of Graduation are required');
//        return (false); //Checked but blank
//    }
//    else {
//        return (true);  //Checked and not blank
//    }
}


function validRequired(formField, fieldLabel) {
    var result = true;

    if (formField.value === '') {
        alert('Please enter a value for the "' + fieldLabel + '" field.');
        formField.focus();
        result = false;
    }

    return result;
}

function isEmailAddr(email) {
    var result = false;
    if (email == null || email.length < 3)
        return result;
    var theStr = email;
    var index = theStr.indexOf("@");
    if (index > 0) {
        var pindex = theStr.indexOf(".", index);
        if ((pindex > index + 1) && (theStr.length > pindex + 1)) {
            result = true;
        }
    }
    return result;
}

function validEmail(formField, fieldLabel, required) {
    var result = true;

    if (required && !validRequired(formField, fieldLabel)) {
        result = false;
    }

    if (result && !isEmailAddr(formField.value)) {
        alert("Please enter a complete e-mail address in the form: yourname@yourdomain.com");
        formField.focus();
        result = false;
    }

    return result;

}

function validateLoginForm(theForm) {
    if (!validRequired(theForm.uid, "Email/ID")) {
        return false;
    }

    if (!validRequired(theForm.upass, "Password")) {
        return false;
    }
}

function validateChangeEmailForm(theForm) {
    // Customize these calls for your form

    // Start ------->
    if (!validRequired(theForm.oldemail, "Old e-mail address")) {
        return false;
    }

    if (!validRequired(theForm.password, "Password")) {
        return false;
    }

    if (!validRequired(theForm.newemail, "New e-mail address")) {
        return false;
    }

    if (!validRequired(theForm.confirmnewemail, "Confirm new e-mail address")) {
        return false;
    }

    if (!validEmail(theForm.oldemail, "Old e-mail address", true)) {
        return false;
    }

    if (!validEmail(theForm.newemail, "New e-mail address", true)) {
        return false;
    }

    if (!validEmail(theForm.confirmnewemail, "Confirm new e-mail address", true)) {
        return false;
    }

    if (theForm.confirmnewemail.value != theForm.newemail.value) {
        alert("The new e-mail address entries do not match.");
        theForm.confirmnewemail.focus();
        return false;
    }

    if (theForm.oldemail.value == theForm.newemail.value) {
        alert("The old e-mail address is the same as the new e-mail address.");
        theForm.newemail.focus();
        return false;
    }

    // <--------- End

    return true;
}

function validateChangePasswordForm(theForm) {
    // Start ------->
    if (!validRequired(theForm.email, "E-mail address")) {
        return false;
    }

    if (!validRequired(theForm.password, "Password")) {
        return false;
    }

    if (!validRequired(theForm.newpassword, "New password")) {
        return false;
    }

    if (!validRequired(theForm.confirmnewpassword, "Confirm new password")) {
        return false;
    }

    if (theForm.confirmnewpassword.value != theForm.newpassword.value) {
        alert("The new password entries do not match.");
        theForm.newpassword.focus();
        return false;
    }
    // <--------- End

    return true;
}

function validateUEmailField(theForm) {
    if (!validEmail(theForm.u, "email address", true)) {
        return false;
    }
    else {
        return true;
    }
}




function isNumberKey(evt) {
    evt = evt || event;
    if (!evt) {
        return true;
    }
    var charCode = evt.keyCode || evt.which || null;
    if (charCode === null) {
        return true;
    }
    //var charCode = (evt.which) ? evt.which : event.keyCode; //Wasn't compatible with .onkeypress = function (event) {return isNumberKey(event)} method in IE
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        return false;
    }

    return true;
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        };
    }
}

