﻿
/**************************************
*  ZiLeex JavaScript Library
*  Development Version 1.0.0.0
*  (c)2009 Walter M. Soto Reyes
*************************************/
    

//***********************
// Library Core Functions
//***********************
var ZiLeex = {
    Version: '0.10',
    Page: {
        Height: function () {
            if (window.innerHeight) {
                return window.innerHeight;
            } else {
                return (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
            }
            return 0;
        },
        Width: function () {
            if (window.innerWidth) {
                return window.innerWidth;
            } else {
                return (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
            }
            return 0;
        },
        Fullscreen: function (url) {
            var paramaters = 'width=' + screen.width;
            paramaters += ', height=' + screen.height;
            paramaters += ', top=0, left=0';
            paramaters += ', fullscreen=yes';
            var win = window.open(url, "", paramaters);
            if (window.focus() !== null) {
                win.focus();
            }
        },
        QueryString: function (key) {
            var address = window.location.href;
            var args = address.split("?");
            if (args.length == 2) {
                var arg;
                if (args[1].indexOf("&") != -1) {
                    arg = args[1].split("&");
                    if (arg.length > 0) {
                        for (var i = 0; i < arg.length; i++) {
                            var sArg = arg[i].split("=");
                            if (sArg.length > 0) {
                                if ((sArg[0] == key)) {
                                    return sArg[1];
                                }
                            }
                        }
                    }
                } else {
                    arg = args[1].split("=");
                    if (arg.length > 0) {
                        if ((arg[0] == key)) {
                            return arg[1];
                        }
                    }
                }
            }
            return "";
        },
        OnError: function (_function) {
            //NOTE: Chrome does not implement this event (as of 07/23/2010)
            var _current = window.onerror;
            if (typeof window.onerror != 'function') {
                window.onerror = _function;
            } else {
                window.onerror = function (msg, url, line) {
                    if (_current) {
                        _current(msg, url, line);
                    }
                    _function(msg, url, line);
                };
            }
        },
        _ToExecuteOnReady: null,
        Ready: function (_function) {//DOM Ready
            if (typeof ZiLeex._ToExecuteOnReady != 'function') {
                ZiLeex._ToExecuteOnReady = _function;
            } else {
                var _current = ZiLeex._ToExecuteOnReady;
                ZiLeex._ToExecuteOnReady = function () {
                    if (_current) {
                        _current();
                    }
                    _function();
                };
            }
            var _DOMReady = false;
            var _wait = true;
            if (document.addEventListener) {
                document.addEventListener('DOMContentLoaded', function () {
                    ZiLeex._ToExecuteOnReady();
                    _DOMReady = true;
                    _wait = false;
                }, false);
            } else {
                //Internet Explorer
                document.onreadystatechange = function () {
                    if (document.readyState == "complete") {
                        ZiLeex._ToExecuteOnReady();
                        _DOMReady = true;
                        _wait = false;
                    }
                };
            }
            if (!_wait && !_DOMReady) {
                ZiLeex.Page.OnLoad(ZiLeex._ToExecuteOnReady); //use window.onload as last resort
            }
        },
        OnLoad: function (_function) {//Add multiple window.onload functions
            var _current = window.onload;
            if (typeof window.onload != 'function') {
                window.onload = _function;
            } else {
                window.onload = function () {
                    if (_current) {
                        _current();
                    }
                    _function();
                };
            }
        },
        AddEvent: function (_object, _event, _function) {
            _object = ZiLeex.$(_object);
            if (_object.addEventListener) {
                _object.addEventListener(_event, _function, false);
            } else if (_object.attachEvent) {
                if (_event.length > 2) {
                    if (_event.substring(0, 2) != "on") {
                        _event = 'on' + _event;
                    }
                }
                _object.attachEvent(_event, _function);
            }
        },
        RemoveEvent: function (_object, _event, _function) {
            _object = ZiLeex.$(_object);
            if (_object.removeEventListener) {
                _object.removeEventListener(_event, _function, false);
            } else if (_object.detachEvent) {
                if (_event.length > 2) {
                    if (_event.substring(0, 2) != "on") {
                        _event = 'on' + _event;
                    }
                }
                _object.detachEvent(_event, _function);
            }
        },
        AddCssClass: function (id, className) {
            if (ZiLeex.$(id).className.length <= 0) {
                ZiLeex.$(id).className = className;
            } else {
                if (ZiLeex.$(id).className.search(className) == -1) {
                    ZiLeex.$(id).className = ZiLeex.$(id).className + ' ' + className;
                }
            }
        },
        RemoveCssClass: function (id, className) {
            if (ZiLeex.$(id).className.length > 0) {
                if (ZiLeex.$(id).className.search(className) != -1) {
                    ZiLeex.$(id).className = ZiLeex.$(id).className.replace(className, "");
                }
            }
        },
        Css: function (id) {
            var styleTxt = '';
            if (arguments.length >= 2) {
                for (var i = 0; i < arguments[1].length; i++) {
                    styleTxt += arguments[1][i] + ';';
                }
            }
            if (ZiLeex.Browser.IsIE) {
                ZiLeex.$(id).style.cssText = styleTxt;
            } else {
                ZiLeex.Page.AddAttribute(id, 'style', styleTxt);
            }
        },
        RegisterScript: function (_resource) {
            var tag = document.createElement('script');
            tag.setAttribute('type', 'text/javascript');
            tag.setAttribute('src', _resource);
            document.getElementsByTagName('head')[0].appendChild(tag);
        },
        InsertElementAfter: function (newElement, currentElement) {
            currentElement.parentNode.insertBefore(newElement, currentElement.nextSibling);
        },
        InsertElementBefore: function (currentElement, newElement) {
            document.insertBefore(newElement, currentElement);
        },
        InsertElement: function (newElement, parentElement) {
            if (typeof (parentElement) == 'undefined') {
                document.body.appendChild(newElement);
            } else {
                ZiLeex.$(parentElement).appendChild(newElement);
            }
        },
        RemoveElement: function (currentElement, parentElement) {
            if (typeof (parentElement) == 'undefined') {
                document.removeChild(newElement);
            } else {
                ZiLeex.$(parentElement).removeChild(newElement);
            }
        },
        ReplaceElement: function (oldElement, newElement) {
            document.body.replaceChild(newElement, oldElement);
        },
        AddAttribute: function (id, attr, val) {
            ZiLeex.$(id).setAttribute(attr, val);
        },
        RemoveAttribute: function (id, attr) {
            ZiLeex.$(id).removeAttribute(attr);
        },
        WaitFor: function (_function, milliseconds) {
            if (typeof (milliseconds) == 'undefined') {
                milliseconds = 1000;
            }
            window.setTimeout(function () {
                if (typeof (_function) != 'undefined') {
                    if (typeof (_function) == 'function') {
                        _function();
                    }
                }
            }, milliseconds);
        }
    },
    $: function () {
        var elements = [];
        var e = null;
        for (var i = 0; i < arguments.length; i++) {
            var element = null;
            var arg = arguments[i];
            if (typeof arg == 'string') {
                element = document.getElementById(arg);
                if (element === null) {
                    element = document.getElementsByTagName(arg);
                    if (element.length == 1) {
                        element = element[0];
                    }
                }
                if (element.length === 0) {
                    element = document.getElementsByName(arg);
                    /* note: IE does not return this as an array so only the first element 
                    in the collection (only if more than one exists) will be accessible.*/
                    if (element.length === 1) {
                        element = element[0];
                    }
                    e = element;
                }
            } else {
                element = arg;
            }
            if (arguments.length == 1) {
                return element;
            }
            if (element !== null) {
                elements.push(element);
            }
        }
        return elements;
    },
    Utilities: {
        IsFunction: function (_obj) {
            if (ZiLeex.Utilities.IsString(_obj)) {
                return (typeof (window[_obj]) == 'function');
            } else {
                return (typeof (_obj) == 'function');
            }
        },
        IsDefined: function (_obj) {
            if ((typeof (_obj) != 'undefined')) {
                if (ZiLeex.Utilities.IsString(_obj)) {
                    return (typeof (window[_obj]) != 'undefined');
                } else {
                    return (typeof (_obj) != 'undefined');
                }
            } else {
                return (typeof (_obj) != 'undefined');
            }
        },
        IsArray: function (_obj) {
            if (typeof (_obj) == 'Array') {
                return true;
            }
            if (ZiLeex.Utilities.IsDefined(_obj.length) && ZiLeex.Utilities.IsDefined(_obj[_obj.length - 1])) {
                return true;
            }

            return false;
        },
        IsNull: function (_obj) {
            return (_obj === null);
        },
        IsUndefined: function (_obj) {
            return (!ZiLeex.Utilities.IsDefined(_obj));
        },
        IsString: function (_obj) {
            return (typeof (_obj) == "string");
        },
        IsNumber: function (_obj) {
            return (typeof (_obj) == "number");
        },
        IsInstance: function (_obj, _type) {
            return (_obj instanceof _type);
        }
    },
    Form: {
        RadioSelectedIndex: function (id) {
            if (typeof (ZiLeex.$(id).length) != 'undefined') {
                var index = -1;
                var _obj = ZiLeex.$(id);
                for (var i = 0; i < _obj.length; i++) {
                    if (_obj[i].checked) {
                        index = i;
                        break;
                    }
                }
                return index;
            }
            return -1;
        },
        RadioSelectedValue: function (id) {
            var index = ZiLeex.Form.RadioSelectedIndex(id);
            if (index != -1) {
                return ZiLeex.$(id)[index].value;
            }
            return "";
        },
        OptionSelectedValue: function (id) {
            if (typeof (ZiLeex.$(id).selectedIndex) != 'undefined') {
                if (ZiLeex.$(id).selectedIndex != -1) {
                    return ZiLeex.$(id).options[ZiLeex.$(id).selectedIndex].value;
                }
            }
            return -1;
        },
        OptionSelectedText: function (id) {
            if (typeof (ZiLeex.$(id).selectedIndex) != 'undefined') {
                if (ZiLeex.$(id).selectedIndex != -1) {
                    return ZiLeex.$(id).options[ZiLeex.$(id).selectedIndex].text;
                }
            }
            return -1;
        }
    },
    Security: {
        XssDefend: function (text) {
            return text.replace(/</g, '&lt;').replace(/>/g, '&gt;');
        }
    },
    Browser: {
        OS: function () {
            var str = navigator.platform;
            if (str.indexOf("Win") != -1) {
                return "Windows";
            }
            if (str.indexOf("Linux") != -1) {
                return "Linux";
            }
            if (str.indexOf("Mac") != -1) {
                return "Mac";
            }
            return str;
        },
        IsIE6: function () {
            return (typeof document.body.style.maxHeight == "undefined");
        },
        IsIE: window.attachEvent ? true : false,
        NavigatorString: function () {
            var str = navigator.userAgent;
            if (str.indexOf("Firefox") != -1) {
                return "Firefox";
            }
            if (str.indexOf("Chrome") != -1) {
                return "Chrome";
            }
            if (str.indexOf("MSIE") != -1) {
                return "Internet Explorer";
            }
            if (str.indexOf("Netscape") != -1) {
                return "Netscape";
            }
            if (str.indexOf("Gecko") != -1) {
                return "Mozilla";
            }
            if (str.indexOf("Mozilla") != -1) {
                return "Netscape";
            }
            return str;
        }
    },
    Cookie: {
        Set: function (key, val, days) {
            var expires = "";
            if (days) {
                var d = new Date();
                d.setDate(d.getDate() + days);
                expires = "; expires=" + d.toGMTString();
            }
            document.cookie = key + "=" + val + expires + "; path=/";
        },
        Read: function (key) {
            key = key + "=";
            var c = document.cookie.split(';');
            for (var i = 0; i < c.length; i++) {
                var con = c[i];
                while (con.charAt(0) === ' ') {
                    con = con.substring(1, con.length);
                }
                if (con.indexOf(key) === 0) {
                    return con.substring(key.length, con.length);
                }
            }
            return "";
        },
        Remove: function (key) {
            ZiLeex.Cookie.Set(key, "", -1);
        },
        Accepts: function () {
            var _cookie = '_zileex_cookie_test_';
            ZiLeex.Cookie.Set(_cookie, '1', 1);
            if (ZiLeex.Cookie.Read(_cookie) !== "") {
                ZiLeex.Cookie.Remove(_cookie);
                return true;
            }
            return false;
        }
    },
    Extend: function (_child) {
        for (var p in _child) {
            if (ZiLeex.Utilities.IsDefined(_child[p])) {
                ZiLeex[p] = _child[p];
            }
        }
    },
    OnLoad: function (_function) {
        ZiLeex.Page.OnLoad(_function);
    },
    Ready: function (_function) {
        ZiLeex.Page.Ready(_function);
    },
    OnError: function (_function) {
        ZiLeex.Page.OnError(_function);
    },
    WaitFor: function (_function, milliseconds) {
        ZiLeex.Page.WaitFor(_function, milliseconds);
    }
}; 
    //***********************
    // Register scripts
    //***********************
    ZiLeex.Extend({
        Using: function (_library) {
                if (typeof (_library) == 'string') {
                    ZiLeex.Page.RegisterScript(_library);
     
                }
        }
    });
   
    //***********************

    //***********************
    // Data Structures
    //***********************
    ZiLeex.Extend({
        DataStructures: {
            Hash: function () {
                this.Items = [];
                this.Size = 0;
                this.Add = function (key, obj) {
                    if (this.Exists(key)) {
                        this.Items[key] = obj; //Update
                    } else {
                        this.Size++;
                        this.Items[key] = obj;
                    }
                };
                this.Get = function (key) {
                    return this.Items[key];
                };
                this.Remove = function (key) {
                    var _temp;
                    if (ZiLeex.Utilities.IsDefined(this.Items[key])) {
                        _temp = this.Items[key];
                        delete this.Items[key];
                        this.Size--;
                    }
                    return _temp;
                };
                this.Exists = function (key) {
                    return ZiLeex.Utilities.IsDefined(this.Items[key]);
                };
                this.Clear = function () {
                    for (var i = 0; i < this.Size; i++) {
                        delete this.Items[i];
                    }
                    this.Size = 0;
                };
                this.ForEach = function (_callback) {
                    for (var i in this.Items) {
                        if (ZiLeex.Utilities.IsDefined(this.Items[i])) {
                            _callback(i, this.Items[i]);
                        }
                    }
                };
            }
        }
    });

    //***********************
    // Xml Parser
    //***********************
    ZiLeex.Extend({
        Xml: {
            Parse: function (text) {//text to xml object
                var xmlDoc;
                if (window.DOMParser) {
                    xmlParser = new DOMParser();
                    xmlDoc = xmlParser.parseFromString(text, "text/xml");
                } else { // Internet Explorer
                    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                    xmlDoc.async = "false";
                    xmlDoc.loadXML(text);
                }
                return xmlDoc;
            }
        }
    });

    //**********************
    //Validation Utilities
    //**********************
    ZiLeex.Extend({
        Validate:{
            RequiredText:function(id){
             if (z(id).Text.length > 0) {
                return true;
             } 
                return false;
            },
            MaxLength: function(id, max) {
                if (ZiLeex.$(id).value.length > max) {
                    ZiLeex.$(id).value = ZiLeex.$(id).value.substring(0, max);
                }
            }
        }
    });

   

    //***********************
    // ZiLeex Function
    //***********************
    ZiLeex.Extend({
        Instance: function () {
            ZiLeex.FnElements = arguments;
            return new ZiLeex.Fn();
        },
        FnElements: [],
        Fn: function () {
            if (arguments.length > 0) {
                ZiLeex.FnElements = arguments;
            }

            //------------------------------------
            //Set Value or innerHTML
            //------------------------------------
            this.SetValue = function (_text) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.$(ZiLeex.FnElements[i]).value = _text;
                }
                return this;
            };
            this.SetText = function (text) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    if (ZiLeex.$(ZiLeex.FnElements[i]).tagName.toLowerCase() == 'input' || ZiLeex.$(ZiLeex.FnElements[i]).tagName.toLowerCase() == 'textarea') {
                        this.SetValue(text);
                    } else {
                        this.SetInnerHTML(text);
                    }
                }
                return this;
            };
            this.AppendText = function (text) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    if (ZiLeex.$(ZiLeex.FnElements[i]).tagName.toLowerCase() == 'input' || ZiLeex.$(ZiLeex.FnElements[i]).tagName.toLowerCase() == 'textarea') {
                        ZiLeex.$(ZiLeex.FnElements[i]).value += text;
                    } else {
                        ZiLeex.$(ZiLeex.FnElements[i]).innerHTML += text;
                    }
                }
                return this;
            };
            this.SetInnerHTML = function (text) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.$(ZiLeex.FnElements[i]).innerHTML = text;
                }
                return this;
            };
            //------------------------------------

            //------------------------------------
            //Events
            //------------------------------------
            this.AddEvent = function (_event, _function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddEvent(ZiLeex.FnElements[i], _event, _function);
                }
                return this;
            };
            this.RemoveEvent = function (_event, _function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.RemoveEvent(ZiLeex.FnElements[i], _event, _function);
                }
                return this;
            };
            this.OnClick = function (_function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddEvent(ZiLeex.$(ZiLeex.FnElements[i]), 'click', _function);
                }
                return this;
            };
            this.OnDblClick = function (_function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddEvent(ZiLeex.$(ZiLeex.FnElements[i]), 'dblclick', _function);
                }
                return this;
            };
            this.OnMouseOver = function (_function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddEvent(ZiLeex.$(ZiLeex.FnElements[i]), 'mouseover', _function);
                }
                return this;
            };
            this.OnMouseOut = function (_function) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddEvent(ZiLeex.$(ZiLeex.FnElements[i]), 'mouseout', _function);
                }
                return this;
            };
            //------------------------------------

            //------------------------------------
            // CSS Functions
            //------------------------------------
            this.Css = function () {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.Css(ZiLeex.FnElements[i], arguments);
                }
                return this;
            };
            this.RemoveStyle = function () {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.RemoveAttribute(ZiLeex.FnElements[i], 'style');
                }
                return this;
            };
            this.AddCssClass = function (className) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.AddCssClass(ZiLeex.FnElements[i], className);
                }
                return this;
            };
            this.RemoveCssClass = function (className) {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ZiLeex.Page.RemoveCssClass(ZiLeex.FnElements[i], className);
                }
                return this;
            };

            //------------------------------------


            //------------------------------------
            // Misc
            //------------------------------------
            this.GetText = function () {
                if (ZiLeex.FnElements.length === 0) {
                    return "";
                }
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.$(ZiLeex.FnElements[0]).innerHTML;
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.$(ZiLeex.FnElements[i]).innerHTML);
                }
                return ret;
            };
            this.GetValue = function () {
                if (ZiLeex.FnElements.length === 0) {
                    return "";
                }
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.$(ZiLeex.FnElements[0]).value;
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.$(ZiLeex.FnElements[i]).value);
                }
                return ret;
            };

            this.RequiredText = function () {
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    return ZiLeex.Validate.RequiredText(ZiLeex.FnElements[i]);
                }
            };
            this.Chain = function () {
                if (arguments.length > 0) {
                    ZiLeex.FnElements = arguments;
                }
                return new ZiLeex.Fn();
            };

            this.WaitFor = function (_function, milliseconds) {
                if (typeof (milliseconds) == 'undefined') {
                    milliseconds = 1000;
                }
                ZiLeex.Page.WaitFor(_function, milliseconds);
                return this;
            };
            //------------------------------------

            //------------------------------------
            // Forms
            //------------------------------------ 
            this.OptionValue = function () {
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.Form.OptionSelectedValue(ZiLeex.FnElements[0]);
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.Form.OptionSelectedValue(ZiLeex.FnElements[i]));
                }
                return ret;
            };
            this.OptionText = function () {
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.Form.OptionSelectedText(ZiLeex.FnElements[0]);
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.Form.OptionSelectedText(ZiLeex.FnElements[i]));
                }
                return ret;
            };
            this.RadioValue = function () {
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.Form.RadioSelectedValue(ZiLeex.FnElements[0]);
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.Form.RadioSelectedValue(ZiLeex.FnElements[i]));
                }
                return ret;
            };
            this.RadioSelectedIndex = function () {
                if (ZiLeex.FnElements.length == 1) {
                    return ZiLeex.Form.RadioSelectedIndex(ZiLeex.FnElements[0]);
                }
                var ret = [];
                for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                    ret.push(ZiLeex.Form.RadioSelectedIndex(ZiLeex.FnElements[i]));
                }
                return ret;
            };
            //------------------------------------

            //------------------------------------
            // Page
            //------------------------------------
            this.QueryString = function (key) {
                if (ZiLeex.FnElements.length < 1) {
                    return ZiLeex.Page.QueryString(key);
                } else {
                    for (var i = 0; i < ZiLeex.FnElements.length; i++) {
                        var _zileexFunc = ZiLeex.CfnInstance(ZiLeex.FnElements[i]);
                        _zileexFunc.SetText(ZiLeex.Page.QueryString(key));
                    }
                    return this;
                }
            };

            //End of Fn
        }
    });

 
    //*************************
    // lower case alias
    //************************* 
    var zileex = ZiLeex;

   
