﻿var isClear = false;

function regValidation(source, arguments) {

    /* Get form elements by their ID */
    
    var inputMemType = document.getElementById('ddlMemberType');
    var labelMemType = document.getElementById('labelddlMemberType');
    var inputGenderM = document.getElementById('RblGender_0');
    var inputGenderF = document.getElementById('RblGender_1');
    var labelGender = document.getElementById('labelRblGender');
    var inputUser = document.getElementById('TxtUsername');
    var labelUser = document.getElementById('labelTxtUsername');
    var inputFName = document.getElementById('TxtFirstName');
    var inputLName = document.getElementById('TxtLastName');    
    var labelFName = document.getElementById('labelTxtFirstName');
    var labelLName = document.getElementById('labelTxtLastName');
    var inputPass = document.getElementById('TxtPassword');
    var labelPass = document.getElementById('labelTxtPassword');
    var inputPass2 = document.getElementById('TxtPassword2');
    var labelPass2 = document.getElementById('labelTxtPassword2');
    var inputAddress = document.getElementById('TxtHomeAddress1');
    var labelAddress = document.getElementById('labelTxtHomeAddress1');
    var inputCity = document.getElementById('TxtCity');
    var labelCity = document.getElementById('labelTxtCity');
    var inputState = document.getElementById('DdlStates');
    var labelState = document.getElementById('labelTxtDdlStates');
    var inputZip = document.getElementById('TxtZip');
    var labelZip = document.getElementById('labelTxtZip');
    var inputPhone1 = document.getElementById('TxtPhone1');
    var inputPhone2 = document.getElementById('TxtPhone2');
    var inputPhone3 = document.getElementById('TxtPhone3');
    var labelPhone = document.getElementById('labelTxtPhone');
    var inputEmail = document.getElementById('TxtEmail');
    var labelEmail = document.getElementById('labelTxtEmail');
    var inputEmail2 = document.getElementById('TxtEmail2');
    var labelEmail2 = document.getElementById('labelTxtEmail2');
    var inputBleedQ1 = document.getElementById('ddlQuestion1');
    var labelBleedQ1 = document.getElementById('labelddlBleedingType');
    

    /* Change form elements back to normal, non-error colors if user enters valid information */
    
    var colorNorm = "#000000"; 
    labelGender.style.color = colorNorm;
    inputMemType.style.borderColor = colorNorm;
    labelMemType.style.color = colorNorm;
    inputUser.style.borderColor = colorNorm;
    labelUser.style.color = colorNorm;
    inputFName.style.borderColor = colorNorm;
    inputLName.style.borderColor = colorNorm;
    labelFName.style.color = colorNorm;
    labelLName.style.color = colorNorm;
    inputPass.style.borderColor = colorNorm;
    labelPass.style.color = colorNorm;
    inputPass2.style.borderColor = colorNorm;
    labelPass2.style.color = colorNorm;
    inputAddress.style.borderColor = colorNorm;
    labelAddress.style.color = colorNorm;
    inputCity.style.borderColor = colorNorm;
    labelCity.style.color = colorNorm;
    inputState.style.borderColor = colorNorm;
    labelState.style.color = colorNorm;
    inputZip.style.borderColor = colorNorm;
    labelZip.style.color = colorNorm;
    inputPhone1.style.borderColor = colorNorm;
    inputPhone2.style.borderColor = colorNorm;
    inputPhone3.style.borderColor = colorNorm;
    labelPhone.style.color = colorNorm;
    inputEmail.style.borderColor = colorNorm;
    labelEmail.style.color = colorNorm;
    inputBleedQ1.style.borderColor = colorNorm;
    labelBleedQ1.style.color = colorNorm;
    

    /* Regular Expressions */

    var emailReg = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    var usernameReg = /[^A-Za-z0-9]/;
    var regExWhite = new RegExp(/^\s+$/);
    var nameReg = /^[a-zA-Z]+(([\'\,\.\- ][a-zA-Z ])?[a-zA-Z]*)*$/i;

    /* Other initial variables */
    var colorError = "#ff0000";
    var errorMsgDiv = document.getElementById('errors_div');
    var errorMsg = "";
    var isValid = true;

    var memType = document.getElementById('ddlMemberType');
    
    var regExWhite = new RegExp(/^\s+$/);

    /* Begin validation */
    
    if(memType.selectedIndex == 0) {
        isValid = false;
        errorMsg += "<li>Please select patient, parent or caregiver.</li>";
        inputMemType.style.borderColor = colorError;
        labelMemType.style.color = colorError;
    }

    if(inputGenderM.checked == false && inputGenderF.checked == false) {
        isValid = false;
        errorMsg += "<li>Gender is required.</li>";
        labelGender.style.color = colorError;
    }

    if(inputUser.value == "") {
        isValid = false;
        errorMsg += "<li>Username is a required.</li>";
        inputUser.style.borderColor = colorError;
        labelUser.style.color = colorError;
    }

    if (inputUser.value.match(usernameReg) || inputUser.value.length < 6 || inputUser.value.length > 10 || inputUser.value.match(" ")) {
        isValid = false;
        errorMsg += "<li>Your user name must be 6 to 10 characters, using only letters and/or numbers.</li>";
        inputUser.style.borderColor = colorError;
        labelUser.style.color = colorError;
    }

    if(inputFName.value == "") {
        isValid = false;
        errorMsg += "<li>First Name is a required.</li>";
        inputFName.style.borderColor = colorError;
        labelFName.style.color = colorError;
    }

    if(inputLName.value == "") {
        isValid = false;
        errorMsg += "<li>Last Name is a required.</li>";
        inputLName.style.borderColor = colorError;
        labelLName.style.color = colorError;
    }

    if (inputFName.value != "" && !inputFName.value.match(nameReg)) {
        isValid = false;
        errorMsg += "<li>Invalid First Name (only letters, spaces, hyphens, and apostrophes are allowed).</li>";
        inputFName.style.borderColor = colorError;
        labelFName.style.color = colorError;
    }

    if (inputLName.value != "" && !inputLName.value.match(nameReg)) {
        isValid = false;
        errorMsg += "<li>Invalid Last Name (only letters, spaces, hyphens, and apostrophes are allowed).</li>";
        inputLName.style.borderColor = colorError;
        labelLName.style.color = colorError;
    }

    if(inputPass.value == "") {
        isValid = false;
        errorMsg += "<li>Invalid password.</li>";
        inputPass.style.borderColor = colorError;
        labelPass.style.color = colorError;
    }
    if(inputPass.value.length < 6 || inputPass.value.length > 10 || inputPass.value.match(" ")) {
        
        isValid = false;
        errorMsg += "<li>Password must be 6 to 10 characters. Password cannot contain spaces.</li>";
        inputPass.style.borderColor = colorError;
        labelPass.style.color = colorError;
    }

    if(inputPass2.value != inputPass.value) {
        isValid = false;
        errorMsg += "<li>The confirmation password does not match the first password entered. Please make sure both password fields match.</li>";
        inputPass2.style.borderColor = colorError;
        labelPass2.style.color = colorError;
    }

    if(inputAddress.value == "") {
        isValid = false;
        errorMsg += "<li>Address 1 is required.</li>";
        inputAddress.style.borderColor = colorError;
        labelAddress.style.color = colorError;
    }

    if(inputCity.value == "") {
        isValid = false;
        errorMsg += "<li>City is required.</li>";
        inputCity.style.borderColor = colorError;
        labelCity.style.color = colorError;
    }

    if(inputState.selectedIndex == 0) {
        isValid = false;
        errorMsg += "<li>State is required.</li>";
        inputState.style.borderColor = colorError;
        labelState.style.color = colorError;
    }

    if(inputZip.value == "" || inputZip.value.length < 5 || isNaN(inputZip.value)) {
        isValid = false;
        errorMsg += "<li>Invalid Zip Code.</li>";
        inputZip.style.borderColor = colorError;
        labelZip.style.color = colorError;
    }
    
    if(isNaN(inputPhone1.value) || isNaN(inputPhone2.value) || isNaN(inputPhone3.value)){
        isValid = false;
        errorMsg += "<li>Invalid Telephone Number.</li>";
        inputPhone1.style.borderColor = colorError;
        inputPhone2.style.borderColor = colorError;
        inputPhone3.style.borderColor = colorError;
        labelPhone.style.color = colorError;
    }  
    
    if(inputPhone1.value == "" || inputPhone2.value == "" || inputPhone3.value == "") {
        isValid = false;
        errorMsg += "<li>Telephone Number is required.</li>";
        inputPhone1.style.borderColor = colorError;
        inputPhone2.style.borderColor = colorError;
        inputPhone3.style.borderColor = colorError;
        labelPhone.style.color = colorError;
    }
    
    if(!inputEmail.value.match(emailReg)) {
        isValid = false;
        errorMsg += "<li>Please enter a valid e-mail address.</li>";
        inputEmail.style.borderColor = colorError;
        labelEmail.style.color = colorError;
    }

    if(inputEmail.value.match(emailReg) && inputEmail2.value != inputEmail.value) {
        isValid = false;
        errorMsg += "<li>The confirmation e-mail does not match the first e-mail entered. Please make sure both e-mail fields match.</li>";
        inputEmail2.style.borderColor = colorError;
        labelEmail2.style.color = colorError;
    }

    /* Validate bleeding disorder questions */

    if(inputBleedQ1.selectedIndex == 0) {
        isValid = false;
        errorMsg += "<li>\"What type of bleeding disorder do you have?\" is a required question.</li>";
        inputBleedQ1.style.borderColor = colorError;
        labelBleedQ1.style.color = colorError;
    }

    if(inputBleedQ1.selectedIndex == 1) {

        var inputBleedQ2 = document.getElementById('ddlQuestion2');
        var labelBleedQ2 = document.getElementById('labelddlQuestion2');
        var inputBleedQ4 = document.getElementById('ddlQuestion4');
        var labelBleedQ4 = document.getElementById('labelddlTypeOfHemoHave');
        var inputBleedQ5 = document.getElementById('ddlQuestion5');
        var labelBleedQ5 = document.getElementById('labelddlCurrentLevel');
        var inputTreatCenter = document.getElementById('txtTreatmentCenter');
        var labelTreatCenter = document.getElementById('labeltxtTreatmentCenter');
        
        inputBleedQ2.style.borderColor = colorNorm;
        labelBleedQ2.style.color = colorNorm;
        inputBleedQ4.style.borderColor = colorNorm;
        labelBleedQ4.style.color = colorNorm;
        inputBleedQ5.style.borderColor = colorNorm;
        labelBleedQ5.style.color = colorNorm;
        inputTreatCenter.style.borderColor = colorNorm;
        labelTreatCenter.style.color = colorNorm;

        if (inputBleedQ2.selectedIndex == 0) {
            isValid = false;
            errorMsg += "<li>\"What kind of treatment are you currently on for Hemophilia A?\" is a required question.</li>"
            inputBleedQ2.style.borderColor = colorError;
            labelBleedQ2.style.color = colorError;
        }
        
        if (inputBleedQ4.selectedIndex == 0) {
            isValid = false;
            errorMsg += "<li>\"What type of Hemophilia A do you have?\" is a required question.</li>";
            inputBleedQ4.style.borderColor = colorError;
            labelBleedQ4.style.color = colorError;
        }

        if (inputBleedQ5.selectedIndex == 0) {
            isValid = false;
            errorMsg += "<li>\"What is your current level of physical activity?\" is a required question.</li>";
            inputBleedQ5.style.borderColor = colorError;
            labelBleedQ5.style.color = colorError;
        }

        if (inputTreatCenter.value == "") {
            isValid = false;
            errorMsg += "<li>\"Which local Hemophilia Treatment Center or local National Hemophilia Foundation chapter are you affiliated with?\" is a required question.</li>";
            inputTreatCenter.style.borderColor = colorError;
            labelTreatCenter.style.color = colorError;
        }
    }

    if(isValid == false) {
        errorMsgDiv.innerHTML = "<ul>" + errorMsg + "</ul>";
        arguments.IsValid = false;
    }
    else {
        arguments.IsValid = true;
    }
}

function clearEmailField() {
    
    var inputEmail = document.getElementById('TxtEmail');

    if (isClear == false) {
        isClear = true;
        inputEmail.value = "";
        inputEmail.style.color = "#333333";
    }
}

function popEmailField() {
    var inputEmail = document.getElementById('TxtEmail');

    if (isClear == true && inputEmail.value == "") {
        isClear = false;
        inputEmail.value = "Primary Household E-mail Address";
    }
}