﻿function throwError(input_id, message) {

    var errorCssClass = 'inputError';

    // Get associated label for this input element
    var labels = document.getElementsByTagName("label");
    var input = document.getElementById(input_id);
    var type = input.type;

    for (x = 0; x < labels.length; x++) {

        // If the label's "for" attribute matches this input's client ID...
        if (labels[x].htmlFor == input_id) {

            // Alert user of error
            var labelText = labels[x].innerText || labels[x].textContent;
            input.className = errorCssClass;
            alert(message);
            input.focus();
            var rgx = /select/;
            if (type.search(rgx) == -1) { input.select(); }
            return false;

        } // End If

    } // End For
}

function isEmpty(str) {
    if (str == null || str.length == 0) { return true; }
    return false;
}

function isEmail(str) {
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    return reg.test(str);
}

function isPhone(str) {
    var reg = /^(([0-9]{1})*[- .(]*([0-9a-zA-Z]{3})*[- .)]*[0-9a-zA-Z]{3}[- .]*[0-9a-zA-Z]{4})+$/;
    return reg.test(str);
}

function isDate(str) {
    var reg = /^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;
    var match = str.match(reg);
    if (match) {
        var tmpdate = new Date(match[3], parseInt(match[1]) - 1, match[2]);
        if (tmpdate.getDate() == parseInt(match[2], 10) && tmpdate.getFullYear() == parseInt(match[3], 10) && (tmpdate.getMonth() + 1) == parseInt(match[1], 10)) {
            return true;
        }
    }
    return false;
}

function isZipCode(str) {
    var reg = /^([0-9]){5}$/;
    return reg.test(str);
}

function isUrl(str) {
    var reg = new RegExp("^(http[s]?://|ftp://)?(www\.)?[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$");
    return reg.test(str);
}

function validate(ClientName) {

    // XMod 'ClientName'Form Properties holder (Form elements client ID's)
    var properties = [];

    // Convert passed ClientName to its object form
    var cName = eval(ClientName);

    // Push cName object properties into holder
    for (var name in cName) {
        properties.push(name);
    }

    // Loop through properties holder to perform actions on inputs
    for (var i in properties) {

        var inputElemId = eval("cName." + properties[i]);
        var inputElem = document.getElementById(inputElemId);

        if (inputElem) {
            switch (inputElem.type) {
                case "select":
                    var inputElemValue = inputElem.options[inputElem.selectedIndex].value;
                    break;
                case "select-one":
                    var inputElemValue = inputElem.options[inputElem.selectedIndex].value;
                    break;
                default:
                    var inputElemValue = inputElem.value;
                    break;
            }

            var doValidate = document.getElementById(inputElemId).getAttribute("validate");
        
        }

        if (doValidate) {
        
            var vMethods = doValidate.split(",");
            inputElem.className = '';

            for (var x in vMethods) {

                var IsEmpty = isEmpty(inputElemValue);
                var vm = vMethods[x].toLowerCase();

                switch (vm) {

                    case "required":
                        if (IsEmpty) { return throwError(inputElemId, "This is a required field."); }
                        break;
                    case "email":
                        if (!isEmail(inputElemValue) && !IsEmpty) { return throwError(inputElemId, "You must enter a vaild email address."); }
                        break;
                    case "phone":
                        if (!isPhone(inputElemValue) && !IsEmpty) { return throwError(inputElemId, "You must enter a valid phone number in (123)456-7890 format."); }
                        break;
                    case "date":
                        if (!isDate(inputElemValue) && !IsEmpty) { return throwError(inputElemId, "You must enter a valid date in mm/dd/yyyy format."); }
                        break;
                    case "url":
                        if (!isUrl(inputElemValue) && !IsEmpty) { return throwError(inputElemId, "You must enter a valid URL."); }
                        break;
                    case "zip":
                        if (!isZipCode(inputElemValue) && !IsEmpty) { return throwError(inputElemId, "You must enter a valid US Zip Code."); }
                        break;

                } // End Switch

            } // End For  
                  
        } // End If

    } // End For

    // All validation checks passed
    return true;

}