﻿/**
* jsUtil 1.0.5
* by Yosuke Suzuki 
*/

var jsUtil = new JavaScriptUtility();
function JavaScriptUtility() {
    var included = [];
    var map = [];

    /**
     * indlude a javascript file
     */
    this.include = function (path) {
        if (!hasScript(path)) {
            document.writeln("<script src='" + path + "?v=" + Math.random() + "' type='text/javascript'></script>");
            included.push(path);
        }
    }

    /**
     * invoke an event from the given element
     * (click, mouseover, mouseout, touchstart, ...)
     */
    this.fireEvent = function (element, event) {
        if (document.createEventObject) {
            // dispatch for IE
            var evt = document.createEventObject();
            return element.fireEvent('on' + event, evt)
        }
        else {
            // dispatch for firefox + others
            var evt = document.createEvent("HTMLEvents");
            evt.initEvent(event, true, true); // event type,bubbling,cancelable
            return !element.dispatchEvent(evt);
        }
    }

    /**
    * returns the width of an element (cross browser & zoom support)
    */
    this.getWidth = function (element) {
        var width = parseFloat(element.style.width);
        if (isNaN(width)) {
            width = parseFloat(this.getFromCurrentStyle("width", element));
        }
        return width;
    }

    /**
    * returns the height of an element (cross browser & zoom support)
    */
    this.getHeight = function (element) {
        var height = parseFloat(element.style.height);
        if (isNaN(height)) {
            height = parseFloat(this.getFromCurrentStyle("height", element));
        }
        return height;
    }

    /**
    * returns the offsetLeft of an element (cross browser & zoom support)
    */
    this.getLeft = function (element) {
        var offset = parseFloat(element.style.left);
        if (isNaN(offset)) {
            offset = parseFloat(this.getFromCurrentStyle("left", element));
        }
        return offset;
    }

    /**
    * returns the offsetTop of an element (cross browser & zoom support)
    */
    this.getTop = function (element) {
        var offset = parseFloat(element.style.top);
        if (isNaN(offset)) {
            offset = parseFloat(this.getFromCurrentStyle("top", element));
        }
        return offset;
    }

    /**
    * returns a CSS property value from currentStyle/computedStyle
    */
    this.getFromCurrentStyle = function (property, target) {
        if (target.currentStyle) {
            //Internet Explorer
            return target.currentStyle[__IeProperty(property)];
        } else {
            //other browser
            return document.defaultView.getComputedStyle(target, null).getPropertyValue(property);
        }
    }

    /**
    * returns the borderWidth of an element (cross browser & zoom support)
    * (object with .top, .left, .right, .bottom)
    */
    this.getBorderWidth = function (obj) {
        var border = {};
        if (window.navigator.userAgent.indexOf('Safari') == -1) {
            if (obj.currentStyle) { // IE, Opera
                border.top = obj.currentStyle.borderTopWidth;
                border.right = obj.currentStyle.borderRightWidth;
                border.bottom = obj.currentStyle.borderBottomWidth;
                border.left = obj.currentStyle.borderLeftWidth;
            } else { // Firefox, Camino
                border.top = getComputedStyle(obj, null).getPropertyValue('border-top-width');
                border.right = getComputedStyle(obj, null).getPropertyValue('border-right-width');
                border.bottom = getComputedStyle(obj, null).getPropertyValue('border-bottom-width');
                border.left = getComputedStyle(obj, null).getPropertyValue('border-left-width');
            }
        } else { // Safari
            border.top = obj.style.getPropertyValue('border-top-width');
            border.right = obj.style.getPropertyValue('border-right-width');
            border.bottom = obj.style.getPropertyValue('border-bottom-width');
            border.left = obj.style.getPropertyValue('border-left-width');
        }
        if (border.top == "thin") { border.top = "1"; }
        if (border.top == "medium") { border.top = "3"; }
        if (border.top == "thick") { border.top = "5"; }
        if (border.left == "thin") { border.left = "1"; }
        if (border.left == "medium") { border.left = "3"; }
        if (border.left == "thick") { border.left = "5"; }
        if (border.right == "thin") { border.right = "1"; }
        if (border.right == "medium") { border.right = "3"; }
        if (border.right == "thick") { border.right = "5"; }
        if (border.bottom == "thin") { border.bottom = "1"; }
        if (border.bottom == "medium") { border.bottom = "3"; }
        if (border.bottom == "thick") { border.bottom = "5"; }
        border.top = parseInt(border.top);
        border.left = parseInt(border.left);
        border.right = parseInt(border.right);
        border.bottom = parseInt(border.bottom);
        return border;
    }

    /**
    * returns the width in pixel for a given text
    * @targetId : ID of a hidden target element with your style
    */
    this.getTextWidth = function (text, targetId) {
        var target = getById(targetId);
        if (typeof text == "undefined" || target == null) {
            return -1;
        }
        text = text.toString();
        target.innerHTML = text;
        return this.getWidth(target);
    }

    /**
    * checks if a string contains any non-numerical letter
    */
    this.hasLetter = function (string) {
        var re = /^[0-9]*$/;
        if (!re.test(string)) {
            return true;
        }
        return false;
    }

    /**
    * returns a value for the given key from the asp hiddenfield containing data from csharp code behind
    */
    this.getData = function (key, dataSetId) {
        retrieveData(dataSetId);
        if (typeof map[key] != "undefined") {
            return map[key];
        } else {
            return null;
        }
    }

    /**
    * adds a pair of key and value to the asp hiddenfield used in csharp code behind
    */
    this.addData = function (key, value, dataSetId) {
        var dataToCsharp = getById('__dataToCsharp' + dataSetId);
        if (dataToCsharp != null) {
            dataToCsharp.value += (";" + key + ":" + value.toString().replace(/;/g, "{sem}").replace(/:/g, "{dpt}"));
        }
    }

    /**
    * checks the asp hiddenfield if there is a value for the given key
    */
    this.hasData = function (key, dataSetId) {
        retrieveData(dataSetId);
        if (typeof map[key] != "undefined") {
            return true;
        } else {
            return false;
        }
    }

    /**
    * clears the asp hiddenfield which contains data for the csharp code behind
    */
    this.clearData = function (dataSetId) {
        var dataToCsharp = getById('__dataToCsharp' + dataSetId);
        if (dataToCsharp != null) {
            dataToCsharp.value = "";
        }
    }

    var hasScript = function (path) {
        var n = included.length;
        for (var i = 0; i < n; i++) {
            if (included[i] == path) {
                return true;
            }
        }
        return false;
    }

    var retrieveData = function (dataSetId) {
        if (typeof dataSetId == "undefined") {
            dataSetId = "";
        }
        if (map.length == 0) {
            try {
                var dataFromCsharp = getById('__dataFromCsharp' + dataSetId);
                var temp1 = dataFromCsharp.value.substr(1, dataFromCsharp.value.length);
                var temp2 = temp1.split(';');
                for (var i = 0; i < temp2.length; i++) {
                    temp3 = temp2[i].split(':');
                    map[temp3[0]] = temp3[1].replace(/{sem}/g, ";").replace(/{dpt}/g, ":");
                }
            } catch (ex) {
            }
        }
    }
}

function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func
    } else {
        window.onload = function () {
            if (oldonload) {
                oldonload()
            }
            func()
        }
    }
}

/**
* returns the DOM element by id
*/
function getById(id) {
    if (document.getElementById)
        return document.getElementById(id);
    else
        return document.all[id];
}

/**
* offsetLeft for dynamically created elements
*/
function getStyleLeftD(element) {
    var left = "text";
    if (element.outerHTML) {
        //IE, Safari, Chrome, Opera
        var outer = element.outerHTML;
        if (outer.search(/left/i) != -1) {
            var temp1 = outer.substr(outer.search(/left/i) + 5, outer.length);
            var temp2 = temp1.substr(0, temp1.search(/"/));
            left = parseInt(temp2);
        } else {
            left = parseInt(element.style.left.substring(0, element.style.left.indexOf('px')));
        }
    } else {
        //firefox
        left = parseInt(element.style.left.substring(0, element.style.left.indexOf('px')));
    }
    if (typeof left == "number") {
        return left;
    } else {
        return element.offsetLeft;
    }
}

