﻿
// Enable and disable the if other box
function EnableOtherBox() {
    
    var other = document.getElementById("prof_other");
    var selection = document.getElementById("prof");
    
    if (selection.value == 13) {
        other.disabled = false;
        other.focus();
    } else {
    other.disabled = true;
    }
}

// Validate the required form values
function ValidateForm(form) {
    /*
     FormChek.js Functions
            isblank()
            isEmail()
   */
   
   // Loop thru all of the fors controls
    for (var x = 0; x < form.elements.length; x++) {
        var element = form.elements[x];

        if (!CheckIsBlank(element)) {
            return false;
        }

        if (!CheckPhoneNumbers(element)) {
            return false;
        }
        
        if (!CheckIsEmail(element, "email1")) {
            return false;
        }
        
    }
    
    return true;
}

//Check to see if ccontrol is not blank (null)
function CheckIsBlank(element) {

    // Verify that the user has entered data
    if (element.type == "text") {

        if (element.name != "prof_other" && element.name != "exp2" &&
                element.name != "exp3" && element.name != "exp4") {
            if (isblank(element.value)) {
                alert("Invalid or Empty Required Field. Please correct and try again.");

                element.focus();
                
                return false;
            }
        }
    }

    if (element.type == "select-one") {
        if (element.value == 0) {
            alert("Invalid Selection. Please check your selections and try again.");

            element.focus();
            
            return false;
        }

        // Make sure that the other box is not blank 
        if (element.name == 'prof' && element.value == '13') {
            var other = document.getElementById("prof_other");
          
            if (isblank(other.value)) {
                alert("Invalid or Empty Required Field. Please correct and try again.");

                other.focus();
                
                return false;
            }
        }
    }
    return true;
}

function CheckPhoneNumbers(element) {
    var count = 0;

    if (element.name == 'o_phone' || element.name == 'm_phone') {
        // Count all of the integers
        for (i = 0; i < element.value.length; i++) {
            // Check that current character is number.
            var c = element.value.charAt(i);

            if (isDigit(c)) count++;
        }
       
        //Phone should be a 7 or 10 digit number
        if (count != 10) {
            alert("Invalid Phone Number. Must be a 10 digit number. Please coreect and try again.");

            element.focus();
            return false;
        }
    }

    return true;
}

//Chenck to see if it is a valid rmail address
function CheckIsEmail(element,ctrlName) {

    if (element.name == ctrlName) {
        // Validate email address
        if (!isEmail(element.value)) {
            alert("Invalid Email Address. Please coreect and try again.");

            element.focus();
            
            return false;
        }
    }
    
    return true;
}
