﻿function txtBirthdate_Validate(source, args) {
    var monthID = document.getElementById('ctl00_ContentPlaceHolder1_txtMonth');
    var dayID = document.getElementById('ctl00_ContentPlaceHolder1_txtDay');
    var yearID = document.getElementById('ctl00_ContentPlaceHolder1_txtYear');

    var birthMonth = parseInt(monthID.value, 10);
    var birthDay = parseInt(dayID.value, 10);
    var birthYear = parseInt(yearID.value);

    if (!isNaN(birthMonth) && !isNaN(birthDay) && !isNaN(birthYear)) {
        var birth = new Date();
        var current = new Date();
        birth.setFullYear(birthYear, birthMonth - 1, birthDay);
        
        var testmonth = (birth.getMonth() + 1).toString();
        var testday = birth.getDate().toString();
        var testyear = birth.getFullYear().toString();

        if (birthYear < 1900) {
            source.errormessage = "Birth Year must be 1900 or after.";
            args.IsValid = false;
        }
        else if (testmonth != birthMonth || testday != birthDay) {
            source.errormessage = "Invalid Date. Please re-enter your birth date.";
            args.IsValid = false;
        }
        else if (birth > current) {
            source.errormessage = "Date of Birth cannot be a future date.";
            args.IsValid = false;
        }
    }
    else {
        source.errormessage = "Please re-enter your birth date.";
        args.IsValid = false;
    }

    if (args.IsValid) {
        $("#ctl00_ContentPlaceHolder1_txtMonth").removeClass("error");
        $("#ctl00_ContentPlaceHolder1_txtDay").removeClass("error");
        $("#ctl00_ContentPlaceHolder1_txtYear").removeClass("error");
    }
    else {
        $("#ctl00_ContentPlaceHolder1_txtMonth").addClass("error");
        $("#ctl00_ContentPlaceHolder1_txtDay").addClass("error");
        $("#ctl00_ContentPlaceHolder1_txtYear").addClass("error");
    }

}

function txtDay_Validate(source, args) {
        var valid = false;
        var txtDay = document.getElementById('ctl00_ContentPlaceHolder1_txtDay');
        var day = $.trim(txtDay.value);
        var regex = /^(0[1-9]|[12][0-9]|3[01])$/;

        txtDay.value = day;

        if (day !== "" && regex.test(day)) {
            valid = true;
            $(txtDay).removeClass("error");
        }
        else {
            valid = false;
            $(txtDay).addClass("error");
        }
        args.IsValid = valid;
}

function txtMonth_Validate(source, args) {
        var valid = false;
        var txtMonth = document.getElementById('ctl00_ContentPlaceHolder1_txtMonth');
        var month = $.trim(txtMonth.value);
        var regex = /^(0[1-9]|1[012])$/;

        txtMonth.value = month;

        if (month !== "" && regex.test(month)) {
            valid = true;
            $(txtMonth).removeClass("error");
        }
        else {
            valid = false;
            $(txtMonth).addClass("error");
        }
        args.IsValid = valid;
}

function txtYear_Validate(source, args) {
        var valid = false;
        var txtYear = document.getElementById('ctl00_ContentPlaceHolder1_txtYear');
        var year = $.trim(txtYear.value);
        var regex = /^[0-9]{4}$/;

        txtYear.value = year;

        if (year != "" && regex.test(year)) {
            valid = true;
            $(txtYear).removeClass("error");
        }
        else {
            valid = false;
            $(txtYear).addClass("error");
        }
        args.IsValid = valid;
}

function txtEmail_Validate(source, args) {
    args.IsValid = false;
    var txtEmail = document.getElementById('ctl00_ContentPlaceHolder1_txtEmail');
    var email = $.trim(txtEmail.value);
    var regex = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;

    txtEmail.value = email;

    if (regex.test(email) == true) {
        args.IsValid = true;
    }
    else {
        source.errormessage = "Invalid Email Address. Please re-enter using \"name@domain.com\" format";
    }

    if (args.IsValid) {
        args.IsValid = true;
        $(txtEmail).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtEmail).addClass("error");
    }
}

function txtConfirmEmail_Validate(source, args) {
    args.IsValid = false;
    var txtConfirmEmail = document.getElementById('ctl00_ContentPlaceHolder1_txtConfirmEmail');
    var email = $.trim(txtConfirmEmail.value);
    var regex = /^[A-Za-z0-9._-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;

    txtConfirmEmail.value = email;

    if (email !== "" && regex.test(email)) {
        args.IsValid = true;
        $(txtConfirmEmail).removeClass("error");
    }
    else {
        $('#ctl00_ContentPlaceHolder1_txtEmail').addClass("error");
        args.IsValid = false;
        $(txtConfirmEmail).addClass("error");
    }

}

function txtPassword_Validate(source, args) {
    args.IsValid = false;
    if (document.getElementById('ctl00_ContentPlaceHolder1_txtPassword').value !== "") {
        args.IsValid = true;
        $('#ctl00_ContentPlaceHolder1_txtPassword').removeClass("error");
    }
    else {
        args.IsValid = false;
        $('#ctl00_ContentPlaceHolder1_txtPassword').addClass("error");
    }
}

function txtOldPassword_Validate(source, args) {
    args.IsValid = false;
    if (document.getElementById('ctl00_ContentPlaceHolder1_txtOldPassword').value !== "") {
        args.IsValid = true;
        $('#ctl00_ContentPlaceHolder1_txtOldPassword').removeClass("error");
    }
    else {
        args.IsValid = false;
        $('#ctl00_ContentPlaceHolder1_txtOldPassword').addClass("error");
    }
}

function txtConfirmPassword_Validate(source, args) {
    args.IsValid = false;
    if (document.getElementById('ctl00_ContentPlaceHolder1_txtConfirmPassword').value !== "") {
        args.IsValid = true;
        $('#ctl00_ContentPlaceHolder1_txtConfirmPassword').removeClass("error");
    }
    else {
        args.IsValid = false;
        $('#ctl00_ContentPlaceHolder1_txtConfirmPassword').addClass("error");
    }
}

function txtQuestion_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[^<>=&;%]+$/;
    var txtQuestion = document.getElementById('ctl00_ContentPlaceHolder1_txtQuestion');
    var question = $.trim(txtQuestion.value);

    txtQuestion.value = question;

    if (question !== "" && regex.test(question)) {

        args.IsValid = true;
        $(txtQuestion).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtQuestion).addClass("error");
    }
}

function txtAnswer_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[^<>=&;%?]+$/;
    var txtAnswer = document.getElementById('ctl00_ContentPlaceHolder1_txtAnswer');
    var answer = $.trim(txtAnswer.value);

    txtAnswer.value = answer;

    if (answer !== "" && regex.test(answer)) {
        args.IsValid = true;
        $(txtAnswer).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtAnswer).addClass("error");
    }
}

function txtFirstName_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[ A-Za-z\-\.\']+$/;
    var txtName = document.getElementById('ctl00_ContentPlaceHolder1_txtFirstName');
    var name = $.trim(txtName.value);

    if (name !== "" && regex.test(name)) {
        args.IsValid = true;
        $(txtName).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtName).addClass("error");
    }
}

function txtLastName_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[ A-Za-z\-\.\']+$/;
    var txtName = document.getElementById('ctl00_ContentPlaceHolder1_txtLastName');
    var name = $.trim(txtName.value);

    txtName.value = name;

    if (name !== "" && regex.test(name)) {
        args.IsValid = true;
        $(txtName).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtName).addClass("error");
    }
}

function txtAddress_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[A-Za-z0-9 \.\-\,\#]{5,}$/;
    var txtAddress = document.getElementById('ctl00_ContentPlaceHolder1_txtAddress');
    var address = $.trim(txtAddress.value);

    txtAddress.value = address;

    if (address !== "" && regex.test(address)) {
        args.IsValid = true;
        $(txtAddress).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtAddress).addClass("error");
    }
}

function txtAddress2_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[A-Za-z0-9 \.\-\,\#]+$/;
    var txtAddress = document.getElementById('ctl00_ContentPlaceHolder1_txtAddress2');
    var address = $.trim(txtAddress.value);

    txtAddress.value = address;

    if (address !== "" && regex.test(address)) {
        args.IsValid = true;
        $(txtAddress).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtAddress).addClass("error");
    }
}

function txtCity_Validate(source, args) {
    args.IsValid = false;
    var regex = /^[^<>=&;%?]{2,}$/;
    var txtCity = document.getElementById('ctl00_ContentPlaceHolder1_txtCity');
    var city = $.trim(txtCity.value);

    txtCity.value = city;

    if (city !== "" && regex.test(city)) {
        args.IsValid = true;
        $(txtCity).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtCity).addClass("error");
    }
}

function ddlState_Validate(source, args) {
    args.IsValid = false;
    var ddlState = document.getElementById('ctl00_ContentPlaceHolder1_ddlState');
    if (ddlState.value !== "" && ddlState.value != "Please select a state...") {
        args.IsValid = true;
        $(ddlState).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(ddlState).addClass("error");
    }
}

function txtZip_Validate(source, args) {
    args.IsValid = false;
    var txtZip = document.getElementById('ctl00_ContentPlaceHolder1_txtZip');
    var zip = $.trim(txtZip.value);
    var regex = /^[0-9]{5}(-[0-9]{4})?$/;

    txtZip.value = zip;

    if (zip !== "" && regex.test(zip)) {
        args.IsValid = true;
        $(txtZip).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtZip).addClass("error");
    }
}

function txtAreaCode_Validate(source, args) {
    args.IsValid = false;
    var txtAreaCode = document.getElementById('ctl00_ContentPlaceHolder1_txtAreaCode');
    var areaCode = $.trim(txtAreaCode.value);
    var regex = /^[0-9]{3}$/;

    txtAreaCode.value = areaCode;

    if (areaCode !== "" && regex.test(areaCode)) {
        args.IsValid = true;
        $(txtAreaCode).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtAreaCode).addClass("error");
    }
}

function txtPhone1_Validate(source, args) {
    args.IsValid = false;
    var txtPhone1 = document.getElementById('ctl00_ContentPlaceHolder1_txtPhone1');
    var phoneText = $.trim(txtPhone1.value);
    var regex = /^[0-9]{3}$/;

    txtPhone1.value = phoneText;

    if (phoneText !== "" && regex.test(phoneText)) {
        args.IsValid = true;
        $(txtPhone1).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtPhone1).addClass("error");
    }
}

function txtPhone2_Validate(source, args) {
    args.IsValid = false;
    var txtPhone2 = document.getElementById('ctl00_ContentPlaceHolder1_txtPhone2');
    var phoneText = $.trim(txtPhone2.value);
    var regex = /^[0-9]{4}$/;

    txtPhone2.value = phoneText;

    if (phoneText !== "" && regex.test(phoneText)) {
        args.IsValid = true;
        $(txtPhone2).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtPhone2).addClass("error");
    }
}

function txtExt_Validate(source, args) {
    args.IsValid = false;
    var txtExt = document.getElementById('ctl00_ContentPlaceHolder1_txtExt');
    var phoneText = $.trim(txtExt.value);
    var regex = /^[0-9]{1,7}$/;

    txtExt.value = phoneText;

    if (phoneText !== "" && regex.test(phoneText)) {
        args.IsValid = true;
        $(txtExt).removeClass("error");
    }
    else {
        args.IsValid = false;
        $(txtExt).addClass("error");
    }
}

function rbDependent_Validate(source, args) {
    args.IsValid = false;
    var dependent = document.getElementById('ctl00_ContentPlaceHolder1_rbDependent_0');
    var self = document.getElementById('ctl00_ContentPlaceHolder1_rbDependent_1');
    var value = $("input:radio:checked").val();
    if (value == "no") {
        $(".selfQs").css("display", "block");
        $(".dependentQs").css("display", "none");
        args.IsValid = true;
    }
    else if (value == "yes") {
        $(".dependentQs").css("display", "block");
        $(".selfQs").css("display", "none");
        args.IsValid = true;
    }

}

function ddlHealthGoals_Validate(source, args) {
    args.IsValid = false;
    var healthGoalsID = document.getElementById('ctl00_ContentPlaceHolder1_ddlHealthGoals');
    var otherTxtID = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherGoal');
    if (healthGoalsID.value == "Other (specify)." || healthGoalsID.value == "Avoid/prevent the start of any specific illness/disease (specify).") {
        $(otherTxtID).css("display", "block");
    }
    else {
        $(otherTxtID).css("display", "none");
        args.IsValid = true;
    }
}

function ddlGoalMotivation_Validate(source, args) {
    args.IsValid = false;
    var goalMotivationID = document.getElementById('ctl00_ContentPlaceHolder1_ddlGoalMotivation');
    var otherTxtID = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherMotivation');
    if (goalMotivationID.value == "Other (specify).") {
        $(otherTxtID).css("display", "block");
    }
    else {
        $(otherTxtID).css("display", "none");
        args.IsValid = true;
    }
}

function txtOtherGoal_Validate(source, args) {
    args.IsValid = false;
    var txtOtherGoal = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherGoal');
    var ddlHealthGoals = document.getElementById('ctl00_ContentPlaceHolder1_ddlHealthGoals');
    var goal = $.trim(txtOtherGoal.value);
    var regex = /^[a-zA-Z .']{1,40}$/;

    txtOtherGoal.value = goal;

    if ($(ddlHealthGoals).val() == "Other (specify)." ||
        $(ddlHealthGoals).val() == "Avoid/prevent the start of any specific illness/disease (specify).") {
        if (goal != "" && regex.test(goal)) {
            args.IsValid = true;
            $(txtOtherGoal).removeClass("error");
        }
        else {
            args.IsValid = false;
            $(txtOtherGoal).addClass("error");
        }
    }
    else {
        args.IsValid = true;
        $(txtOtherGoal).removeClass("error");
    }
}

function txtOtherMotivation_Validate(source, args) {
    args.IsValid = false;
    var txtOtherMotivation = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherMotivation');
    var ddlGoalMotivation = document.getElementById('ctl00_ContentPlaceHolder1_ddlGoalMotivation');
    var motivation = $.trim(txtOtherMotivation.value);
    var regex = /^[a-zA-Z .']{1,40}$/;

    txtOtherMotivation.value = motivation;

    if ($(ddlGoalMotivation).val() == "Other (specify).") {
        if (motivation != "" && regex.test(motivation)) {
            args.IsValid = true;
            $(txtOtherMotivation).removeClass("error");
        }
        else {
            args.IsValid = false;
            $(txtOtherMotivation).addClass("error");
        }
    }
    else {
        args.IsValid = true;
        $(txtOtherMotivation).removeClass("error");
    }
}

// Month and Day Mask: Translates single digits into 2, e.g. "3" becomes "03"
$(document).ready(function() {
    var regex = /^[0-9]{1}$/;
    $(".mask-month-day").each(function() {
        $(this).blur(function() {
            var text = $(this).get(0).value;
            if (regex.test(text)) {
                $(this).get(0).value = '0' + text;
            }
        });
    });
});

// Year Mask: Translates a 2-digit year into a 4-digit year.
// Because this is for a birthdate field, and a user must be
// 18 to register, this mask assumes a prefix of "19".
$(document).ready(function() {
    var regex = /^[0-9]{2}$/;
    $(".mask-year").each(function() {
        $(this).blur(function() {
            var text = $(this).get(0).value;
            if (regex.test(text)) {
                $(this).get(0).value = '19' + text;
            }
        });
    });
});
// Text Mask: Only allows letters, space, apostrophe, hyphen, period.
// ASCII Values:
//  65-90 = A-Z
//  97-122 = a-z
//  32 = space
//  39 = apostrophe
//  45 = hyphen
//  46 = period
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
$(document).ready(function() {
    $(".mask-text").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 32 || e.which == 39 || e.which == 45 ||
            e.which == 46 || (e.which > 64 && e.which < 91) || (e.which > 96 && e.which < 123)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});


// Number-Mask: Allows only numbers to be entered into a text field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
$(document).ready(function() {
    $(".mask-numbers").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});

// Zip-Mask: Allows only numbers and hyphens to be entered into a text field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
//  45 = "-"
$(document).ready(function() {
    $(".mask-zip").each(function() {
        $(this).keypress(function(e) {
            if (e.which == 0 || e.which == 8 || e.which == 9 || e.which == 127 || e.which == 45 || (e.which > 47 && e.which < 58)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});

// Password-Mask: Does not allow "<" or ">" or "/" or "\" to be entered in the password field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
//  45 = "-"
$(document).ready(function() {
    $(".mask-password").each(function() {
        $(this).keypress(function(e) {
            if ((e.which != 47) && (e.which != 60) && (e.which != 61) && (e.which != 62) && (e.which != 92)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});


// Email-Mask: Does not allow "<" or ">" or "/" or "\" to be entered in the password field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
//  45 = "-"
$(document).ready(function() {
    $(".mask-email").each(function() {
        $(this).keypress(function(e) {
            if ((e.which != 47) && (e.which != 60) && (e.which != 61) && (e.which != 62) && (e.which != 92)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});


// Script-Mask: General use mask for text fields to not allow "<" or ">" or "/" or "\" to be entered in the password field.
// ASCII Values:
//  8 = Backspace
//  0 = Null
//  9 = Tab
//  127 = Delete
//  48-57 = 0-9
//  45 = "-"
$(document).ready(function() {
    $(".mask-script").each(function() {
        $(this).keypress(function(e) {
            if ((e.which != 47) && (e.which != 60) && (e.which != 61) && (e.which != 62) && (e.which != 92)) {
                return true;
            }
            else {
                return false;
            }
        });
    });
});


$(document).ready(function() {
    var healthGoalsID = document.getElementById('ctl00_ContentPlaceHolder1_ddlHealthGoals');
    var otherTxtID = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherGoal');
    if (healthGoalsID != null) {
        if (healthGoalsID.value == "Other (specify)." || healthGoalsID.value == "Avoid/prevent the start of any specific illness/disease (specify).") {
            $(otherTxtID).css("display", "block");
        }
        else {
            $(otherTxtID).css("display", "none");
        }
    }


    var dependent = document.getElementById('ctl00_ContentPlaceHolder1_rbDependent_0');
    var self = document.getElementById('ctl00_ContentPlaceHolder1_rbDependent_1');
    var value = $("input:radio:checked").val();
    if (value != null) {
        if (value == "no") {
            $(".selfQs").css("display", "block");
            $(".dependentQs").css("display", "none");
        }
        else if (value == "yes") {
            $(".dependentQs").css("display", "block");
            $(".selfQs").css("display", "none");
        }
    }

    var healthGoalsID = document.getElementById('ctl00_ContentPlaceHolder1_ddlGoalMotivation');
    var otherTxtID = document.getElementById('ctl00_ContentPlaceHolder1_txtOtherMotivation');
    if (healthGoalsID != null) {
        if (healthGoalsID.value == "Other (specify).") {
            $(otherTxtID).css("display", "block");
        }
        else {
            $(otherTxtID).css("display", "none");
        }
    }
});

