﻿//###################################################################################################################
function validateCommunicationPreferences(sender, args) {
    var $cnt = 0;
    var $checkboxes = $("#CommunicationPreferences tr td input:checkbox");
    $checkboxes.each(function() {
        if ($(this).is(":checked")) {
            $cnt++;
        }
    });

    args.IsValid = ($cnt > 0);
}
//###################################################################################################################
function validateParentCommunicationPreferences(sender, args) {
    var $cnt = 0;
    var $checkboxes = $("#ParentCommunication tr td input:checkbox");
    $checkboxes.each(function() {
        if ($(this).is(":checked")) {
            $cnt++;
        }
    });

    args.IsValid = ($cnt > 0);
}
//###################################################################################################################
function validateMajors(sender, args) {
    var $cnt = 0;
    var $checkboxes = $("#majorsDiv div input:checkbox");
    $checkboxes.each(function() {
        if ($(this).is(":checked")) {
            $cnt++;
        }
    });

    args.IsValid = ($cnt > 0);
}
//###################################################################################################################
function validateHighSchoolName(sender, args) {

    var HighSchoolName = $("#txtHiddenHighSchoolName").val();
    if (HighSchoolName.length > 0) {
        $("#txtHighSchoolName").val(HighSchoolName);
    }
    
    var isValid = false;
    var $txtHighSchoolName = $("#txtHighSchoolName");
    var $txtHiddenHighSchoolName = $("#txtHiddenHighSchoolName");

    if ($txtHighSchoolName.val().length > 0 || $txtHiddenHighSchoolName.val().length > 0) {
        isValid = true;
    }

    args.IsValid = isValid;
}
//###################################################################################################################
function validateMajors_MFA(sender, args) {
    var $cnt = 0;
    var $checkboxes = $("#majorsDiv div input:checkbox");
    $checkboxes.each(function() {
        if ($(this).is(":checked")) {
            $cnt++;
        }
    });

    args.IsValid = ($cnt > 0);
}
//###################################################################################################################
function validateEmail(sender, args) {
    var isOK = true;
    var $customValidator = $("#" + sender.id);
    var $emailInput = $("#" + $customValidator.attr('controltovalidate'));

    var $emailAddress = $emailInput.val();

    if ($emailAddress.length > 0) {
        isOK = isValidEmailAddress($emailAddress);
    }
    else {
        // Required field; length must be > 0
        isOK = false;
    }

    args.IsValid = isOK;
}
//###################################################################################################################
function compareVerifyEmail(sender, args) {
    // Ensure that the txtEmail email address matches the txtVerifyEmail email address
    var isOK = true;
    var $emailInput = $("#txtEmail");
    var $verifyEmailInput = $("#txtVerifyEmail");

    var $emailAddress = $emailInput.val();
    var $verifyEmailAddress = $verifyEmailInput.val();

    if ($emailAddress != $verifyEmailAddress) {
        isOK = false;
    }
    args.IsValid = isOK;
}
//###################################################################################################################
function isValidEmailAddress(emailAddressToValidate) {
    var emailRegEx = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return emailRegEx.test(emailAddressToValidate);
}
//###################################################################################################################
function isValidDate($dateToValidate) {
    var dateRegEx = /^(?:(?:(?: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})/;
    return dateRegEx.test($dateToValidate);
}
//###################################################################################################################
function validateDateOfBirth(sender, args) {
    var $month = $("#ddlDOBMonth").val();
    var $day = $("#ddlDOBDay").val();
    var $year = $("#ddlDOBYear").val();

    // If anything is enter into one of the input fields, validate that the date is in the form of MM/DD/YYYY
    if ($month === '-Choose-' || $day === '-Choose-' || $year === '-Choose-') {
        args.IsValid = true;
    }
    else {
        var $dateToValidate = $month + '/' + $day + '/' + $year;
        args.IsValid = isValidDate($dateToValidate);
    }
}
//###################################################################################################################
$(document).ready(function() {

    var $CCSAContactID = $("#hiddenContactID").val();

    var HSName = $("#txtHiddenHighSchoolName").val();
    if (HSName.length > 0) {
        $("#txtHighSchoolName").val(HSName);
    }

    // Do client-side validation before allowing form submission. Call Page_ClientValidate('Group1') to invoke the ASP.NET validation controls.
    $("#Form1").submit(function() {
        Page_ClientValidate('Group1');
        return Page_IsValid;
    });

    $("#CCSLogo a").click(function() {
        // regular JavaScript method
        window.close();
    });

    $("#logo a").click(function() {
        // regular JavaScript ==> See common.js
        open_url('http://www.collegeforcreativestudies.edu/');
    });

});
//###################################################################################################################
