//**********************************************************************************************
//  PAGE_VALIDATION
//  Kyle Huynh : mrkylehuynh@hotmail.com
//  v 1.0.0
//**********************************************************************************************
String.prototype.trim = function() { return this.replace(/^\s*|\s*$/g, ""); } // Strip leading and trailing white-space
PAGE_VALIDATION = new function() { //singleton
    this.cssValid = "text";
    this.cssInvalid = "text_error";
    this.msg_page_validation_error_id = "The id tag is not set for one of the elements you want to validate.\nThe page validation can be completed.";

    this.count = 0;
    this.valid_array = [];
    this.id_array = [];
    this.function_array = [];
    this.error_message = '';
    this.message_array = [];


    this.addToList = function(elements) {
        // loop through all input elements in form 
        for (var i = 0; i < elements.length; i++) {
            var obj = elements.item(i);
            if (obj.getAttribute('page_validation') == "true") {
                var err_msg = obj.getAttribute('error_message');
                var js = obj.getAttribute('onblur');
                var id = obj.id;
                this.id_array[this.count] = id;
                this.function_array[this.count] = js;
                this.valid_array[this.count] = false;
                if (err_msg != null && err_msg !== "undefined") {
                    this.message_array[id] = err_msg;
                }

                if (js.toString().indexOf('validate_numeric_type') > -1 || js.toString().indexOf('validate_range') > -1) { //adds a key prese event
                    obj.attachEvent("onkeypress", isDecimalKey(event, obj, 2, true)); //IE only
                }

                this.count += 1;
            }
        }

    }

    this.setup = function() {
        var elements = document.getElementsByTagName('input');
        var select_elements = document.getElementsByTagName('select');
        this.addToList(elements);
        this.addToList(select_elements);
    };

    //*************************************************************************************
    // validation of all page elements that was attached with tag page_validation="true"
    //*************************************************************************************    
    this.validate_entire_page = function(show_alert) {
        //http://jehiah.cz/archive/firing-javascript-events-properly
        if (typeof show_alert === "undefined") { show_alert = false } //makes the param optional
        var isPageValid = true;
        for (var i = 0; i < this.count; i++) {
            var obj = this.id_array[i];
            if (obj == "") { alert(this.msg_page_validation_error_id); return false; }
            obj = document.getElementById(obj);
            obj.onblur(); //event firing
            if (this.valid_array[i] == "undefined" || !(this.valid_array[i])) { isPageValid = false; } //this line needs to be after the event firing
        }
        if (show_alert && !isPageValid) {
            // we loop through the valid_array and find all false and find corresponding message_array
            var m = 'The page is not valid, please review the page and try again.\n';
            for (var i = 0; i < this.count; i++) {
                if (!(this.valid_array[i])) m = m + '\n ** ' + this.message_array[this.id_array[i]]
            }
            alert(m);
        }
        return isPageValid;
    };


    //*************************************************************************************
    //validation functions
    //*************************************************************************************
    this.validate_required = function(o) {
        var r = false;
        if (o.value.trim() == "") { o.className = this.cssInvalid; r = false; }
        else { o.className = this.cssValid; r = true; }

        this.setValidArray(o.id, r);
        return r;
    };

    this.validate_select_option = function(ddl, option) {//selection option
        var r = false;
        ddl = document.getElementById(ddl.id);
        if (ddl.options[ddl.selectedIndex].text !== option) {
            ddl.className = this.cssValid; r = true;
        } else {
            ddl.className = "error"; r = false;
        }
        this.setValidArray(ddl.id, r);
        return r;
    };

    this.validate_range = function(o, required, min, max) {//number between
        if (typeof required === "undefined") { required = false } //makes the param optional
        var r = false;
        if (required) {
            if (!(this.validate_required(o))) {
                this.setValidArray(o.id, false); return false;
            }
        }
        if (o.value.trim() !== "") {
            r = !isNaN(o.value.trim())
            if (r) {
                var n = parseFloat(o.value.trim());
                if (n > min && n < max) {
                    o.className = this.cssValid; r = true;
                } else {
                    o.className = this.cssInvalid; r = false;
                }
            } else {
                o.className = this.cssInvalid;
            }
        } else {
            o.className = this.cssValid; r = true;
        }

        this.setValidArray(o.id, r);
        return r;
    };

    this.validate_numeric_type = function(o, required, show_alert) {//numeric type validation
        if (typeof required === "undefined") { required = false } //makes the param optional
        if (typeof show_alert === "undefined") { show_alert = false } //makes the param optional
        var r = false;
        if (required) {
            if (!(this.validate_required(o))) {
                this.setValidArray(o.id, false); return false;
            }
        }
        if (o.value.trim() !== "") {
            r = !isNaN(o.value.trim())
            if (r) { o.className = this.cssValid; } else { o.className = this.cssInvalid; }
        } else {
            o.className = this.cssValid; r = true;
        }

        this.setValidArray(o.id, r);
        return r;
    };


    this.validate_date_type = function(o, required, show_alert) { //date type validation
        if (typeof required === "undefined") { required = false } //makes the param optional
        if (typeof show_alert === "undefined") { show_alert = false } //makes the param optional
        var r = false;
        if (required) {
            if (!(this.validate_required(o))) {
                this.setValidArray(o.id, false); return false;
            }
        }
        if (o.value.trim() !== "") {
            var b = isValidDate(o.value.trim(), show_alert);
            if (!b) { o.className = this.cssInvalid; r = false; }
            else { o.className = this.cssValid; r = true; }
        } else { o.className = this.cssValid; r = true; }

        this.setValidArray(o.id, r);
        return r;
    };


    this.setValidArray = function(id, result) {
        for (var i = 0; i < this.count; i++) {
            if (this.id_array[i] == id) { this.valid_array[i] = result }
        }
    };


}
window.onload=function(){PAGE_VALIDATION.setup();}



