﻿//******************************************************
// functions to support the submission of the Happy Dog Happy Owner registration form
//******************************************************

String.prototype.Trim = trimString; // Extend the string object to include a trim function

function checkValidDate(day, month, year) {
    var blnLeapYear = (year % 4 == 0);
    var lngMaxDays;

    switch (Number(month)) {
        case 1:
            lngMaxDays = 31;
            break;
        case 2:
            if (blnLeapYear == true) {
                lngMaxDays = 29;
            }
            else {
                lngMaxDays = 28;
            }
            break;
        case 3:
            lngMaxDays = 31;
            break;
        case 4:
            lngMaxDays = 30;
            break;
        case 5:
            lngMaxDays = 31;
            break;
        case 6:
            lngMaxDays = 30;
            break;
        case 7:
            lngMaxDays = 31;
            break;
        case 8:
            lngMaxDays = 31;
            break;
        case 9:
            lngMaxDays = 30;
            break;
        case 10:
            lngMaxDays = 31;
            break;
        case 11:
            lngMaxDays = 30;
            break;
        case 12:
            lngMaxDays = 31;
            break;
        default:
            return false;
            break;
    }

    if (day > lngMaxDays) {
        return false;
    }

    if (day == 00) {
        return false;
    }

    if (day < 0) {
        return false;
    }

    if (year < 1753) {
        return false;
    }
    if (year > 2200) {
        return false
    }

    return true;
}

function clearRadioButtons(field) {
    var objRadio = document.getElementsByName(field);

    for (var i = 0; i < objRadio.length; i++) {
        objRadio[i].checked = false;
    }
}

function evaluateClassType(classType) {
    switch (classType) {
        case 0:
            document.getElementById("divStandardClasses").style.display = "inline";
            document.getElementById("divWaterClasses").style.display = "none";
            document.getElementById("divStandardClassLocationLabel").style.display = "inline";
            document.getElementById("divWaterPackageLabel").style.display = "none";
            document.getElementById("divStandardClassLocation").style.display = "inline";
            document.getElementById("divWaterPackage").style.display = "none";
            document.getElementById("divNoClassType").style.display = "none";
            break;
        case 1:
            document.getElementById("divStandardClasses").style.display = "none";
            document.getElementById("divWaterClasses").style.display = "inline";
            document.getElementById("divStandardClassLocationLabel").style.display = "none";
            document.getElementById("divWaterPackageLabel").style.display = "inline";
            document.getElementById("divStandardClassLocation").style.display = "none";
            document.getElementById("divWaterPackage").style.display = "inline";
            document.getElementById("divNoClassType").style.display = "none";
            break;
    }
}

function evaluateClassLocation(locationId) {
    switch (locationId) {
        case 0:
        case 1:
            document.getElementById("txtOtherLocation").value = "";
            document.getElementById("txtOtherLocation").disabled = "disabled";
            break;
        case 2:
            document.getElementById("txtOtherLocation").disabled = "";
            break;
    }
}

function getRadioButtonValue(field) {
    var objRadio = document.getElementsByName(field);

    for (var i = 0; i < objRadio.length; i++) {
        if (objRadio[i].checked == true) {
            return objRadio[i].value;
        }
    }
    return "";
}

function isEmail(field) {
    var strInput = document.getElementById(field).value.Trim();

    if (strInput == "") {
        return true;
    }

    if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(strInput))) {
        return false;
    }

    return true;
}

function resetForm() {
    document.getElementById("txtFirstName").value = "";
    document.getElementById("txtLastName").value = "";
    document.getElementById("txtEmail").value = "";
    document.getElementById("txtPhone").value = "";
    clearRadioButtons("optClassType");
    document.getElementById("cboStandardClass").selectedIndex = 0;
    clearRadioButtons("optStandardClassLocation");
    document.getElementById("txtOtherLocation").value = "";
    document.getElementById("cboWaterClass").selectedIndex = 0;
    clearRadioButtons("optWaterPackage");
    clearRadioButtons("optPaymentMethod");
    document.getElementById("txtNotes").value = "";
    document.getElementById("chkAgreeToTerms").checked = false;

    document.getElementById("divStandardClasses").style.display = "none";
    document.getElementById("divWaterClasses").style.display = "none";
    document.getElementById("divStandardClassLocationLabel").style.display = "none";
    document.getElementById("divWaterPackageLabel").style.display = "none";
    document.getElementById("divStandardClassLocation").style.display = "none"
    document.getElementById("divWaterPackage").style.display = "none";
    document.getElementById("divNoClassType").style.display = "inline";
}

function setRadioButtonFocus(field) {
    var objRadio = document.getElementsByName(field);

    objRadio[0].focus();
}

function submitForm() {
    if (validateForm() == true) {
        document.getElementById("frmRegistration").submit();
    }
}

function trimString() {
    var lngChar;
    var lngCount;
    var strValue = this;

    lngChar = strValue.length - 1;
    lngCount = -1;

    while (strValue.charAt(lngChar) == ' ' && lngChar > lngCount) {
        --lngChar;
    }

    if (lngChar != (strValue.length - 1)) {
        strValue = strValue.slice(0, lngChar + 1);
    }

    lngChar = 0;
    lngCount = strValue.length - 1;

    while (strValue.charAt(lngChar) == ' ' && lngChar < lngCount) {
        ++lngChar;
    }

    if (lngChar != 0) {
        strValue = strValue.slice(lngChar, strValue.length);
    }

    return strValue;
}

function validateDate(value) {
    var aryDateTime;
    var lngDay;
    var lngMonth;
    var lngYear;

    if (value == "") {
        return true;
    }

    if (value.match(/"/) != null | value.match(/'/) != null) {
        return false;
    }

    if (!(/\d{1,2}\/\d{1,2}\/\d{4}/.test(value))) {
        return false;
    }
    else {
        aryDateTime = value.split("/");
        if (aryDateTime.length != 3) {
            return false;
        }
        lngMonth = aryDateTime[0];
        lngDay = aryDateTime[1];
        lngYear = aryDateTime[2];

        if (lngYear.length != 4 && lngYear.length != 2) {
            return false;
        }
        else {
            if (checkValidDate(lngDay, lngMonth, lngYear) == false) {
                return false;
            }
        }
    }

    return true;
}

function validateForm() {
    var lngIndex;
    var strClassType = getRadioButtonValue("optClassType");
    var strOrder = "txtFirstName,txtLastName,txtEmail,txtPhone,optClassType";

    if (document.getElementById("txtFirstName").value.Trim() == "") {
        alert("First Name is required in order to submit the registration form.");
        document.getElementById("txtFirstName").focus();
        return false;
    }
    if (document.getElementById("txtLastName").value.Trim() == "") {
        alert("Last Name is required in order to submit the registration form.");
        document.getElementById("txtLastName").focus();
        return false;
    }
    if (document.getElementById("txtEmail").value.Trim() == "" || isEmail("txtEmail") == false) {
        alert("A valid Email address is required in order to submit the registration form.");
        document.getElementById("txtEmail").focus();
        return false;
    }
    if (document.getElementById("txtPhone").value.Trim() == "") {
        alert("Phone # is required in order to submit the registration form.");
        document.getElementById("txtPhone").focus();
        return false;
    }
    if (strClassType == "") {
        alert("Class Type is required in order to submit the registration form.");
        setRadioButtonFocus("optClassType");
        return false;
    }
    
    if (strClassType.indexOf("Water", 0) == -1) {
        if (document.getElementById("cboStandardClass").selectedIndex == 0) {
            alert("Class Name must be selected in order to submit the registration form.");
            document.getElementById("cboStandardClass").focus();
            return false;
        }
        if (getRadioButtonValue("optStandardClassLocation") == "") {
            alert("Class Location is required in order to submit the registration form.");
            setRadioButtonFocus("optStandardClassLocation");
            return false;
        }
        strOrder += ",cboStandardClass,optStandardClassLocation";
        
        if (getRadioButtonValue("optStandardClassLocation") == "Other") {
            strOrder += ",txtOtherLocation";
        }
    }
    else {
        if (document.getElementById("cboWaterClass").selectedIndex == 0) {
            alert("Class Name must be selected in order to submit the registration form.");
            document.getElementById("cboWaterClass").focus();
            return false;
        }
        if (getRadioButtonValue("optWaterPackage") == "") {
            alert("Package is required in order to submit the registration form.");
            setRadioButtonFocus("optWaterPackage");
            return false;
        }
        strOrder += ",cboWaterClass,optWaterPackage";
    }
    if (getRadioButtonValue("optPaymentMethod") == "") {
        alert("Payment Method is required in order to submit the registration form.");
        setRadioButtonFocus("optPaymentMethod");
        return false;
    }
    strOrder += ",optPaymentMethod,txtStartDate,txtNotes";
    
    if (document.getElementById("chkAgreeToTerms").checked == false) {
        alert("You must agree to the terms in order to submit the registration form.");
        setRadioButtonFocus("chkAgreeToTerms");
        return false;
    }
    strOrder += ",chkAgreeToTerms";

    document.getElementById("order").value = strOrder;

    return true;
}

