/* * BrowserHandler.js * Copyright (c) 2005 MetroSpark LLC.  All rights reserved. *//** * BrowserHandler provides common methods and variables for browsers. * * Requires: EventHandlerUtil.js * * Author: Sunder Srinivasan | sunder@metrospark.com */var BrowserHandler = {    /**     * Holds var for current browser type.     */    isIE : (navigator.userAgent.indexOf("MSIE") >= 0) ? true : false,    /**     * Retrieves the current width of the page.     *     * returns page width in pixels     */    getPageWidth : function () {        var pageWidth = 0;		if (this.isIE) {            pageWidth = document.documentElement.clientWidth;		} else {            pageWidth = window.innerWidth;		}		        return pageWidth;    },    /**     * Retrieves the current height of the page.     *     * returns page height in pixels     */    getPageHeight : function () {        var pageHeight = 0;		if (this.isIE) {            pageHeight = document.documentElement.clientHeight;		} else {            pageHeight = window.innerHeight;		}        return pageHeight;    },        /**     * Retrieves the current vertical/horizontal scroll position of the page.     *     * returns page width in pixels     */    getPageScroll : function () {        var scrollX = 0;        var scrollY = 0;		if (document.documentElement && document.documentElement.scrollTop) {            scrollX = document.documentElement.scrollLeft;            scrollY = document.documentElement.scrollTop;		} else {            scrollX = document.body.scrollLeft;            scrollY = document.body.scrollTop;		}        return [scrollX,scrollY];      },    /**     * Retrieves the current vertical scroll position of the page.     *     * returns page width in pixels     */    getPageScrollTop : function () {        var scrOfY = 0;		if (document.documentElement && document.documentElement.scrollTop) {            scrOfY = document.documentElement.scrollTop;		} else {            scrOfY = document.body.scrollTop;		}        return scrOfY;      },    /**     * Retrieves the current horiszontal scroll position of the page.     *     * returns page height in pixels     */    getPageScrollLeft : function () {        var scrOfX = 0;		if (document.documentElement && document.documentElement.scrollLeft) {            scrOfX = document.documentElement.scrollLeft;		} else {            scrOfX = document.body.scrollLeft;		}        return scrOfX;      },    /**     * Function to set a cookie in the browser.  The only required paramterts     * are name and value.  The other params will be set to their default      * implementation if null.      *     * name     the name of the cookie that is being set     * value    the value of the cookie being set     * expires  expiration date of the cookie (defaults to end of current session)     * path     path for which the cookie is valid (defaults to path of calling document)     * domain   domain for which the cookie is valid (defaults to domain of calling document)     * secure   Boolean value indicating if the cookie transmission requires a secure transmission     */    setCookieValue : function(name, value, expires, path, domain, secure) {        // set cookie text based on the vals passed.  Null or non-existant values        // will not be set in the cookie text.        var cookieText = name + "=" + escape(value) +            ((expires) ? "; expires=" + expires.toGMTString() : "") +            ((path) ? "; path=" + path : "") +            ((domain) ? "; domain=" + domain : "") +            ((secure) ? "; secure" : "");        document.cookie = cookieText;    },    /**     * Function to get a cookie in the browser.     *     * name         the name of the cookie that we want to get     * cookieValue  the value of the cookie     */    getCookieValue : function(name) {        // initialize vars        var cookieText = (document.cookie) ? document.cookie : "";        var cookieValue = null;        // split the cookie based on the ';' and '=' characters and the        // spaces around them.        var arrCookie = cookieText.split(/\s*[;=]\s*/);        // cycle through the cookie array to find the val of the         //         for ( i = 0 ; i < arrCookie.length ; i = i + 2 ) {            if (arrCookie[i] == name) {                cookieValue = arrCookie[i + 1];            }        }                return cookieValue;    },        /**     * Function will remove a cookie with a specified name.     *      * name    name of the cookie     * path    path of the cookie (must be same as path used to create cookie)     * domain  domain of the cookie (must be same as domain used to create cookie)     */    deleteCookie : function (name, path, domain) {        // if the cookie exists then invalidate it.        if ( this.getCookieValue(name) ) {            var cookieText =  name + "=" +                ((path) ? "; path=" + path : "") +                ((domain) ? "; domain=" + domain : "") +                "; expires=Thu, 01-Jan-70 00:00:01 GMT";            document.cookie = cookieText;        }    },    /**     * Function will retrieve the text of an html element.     */    getInnerText : function (element) {        // Inner text property returns the element's content        // without html tags.  If the browser has this property,        // return it.        var text = "";                if (element.innerText) {            text = element.innerText || "";        } else {            // else get the node's value to prevent the             // text from containing html text.            text = this.getHTMLFreeText(element);        }        return text;    },            /**     * Function will retrieve the text of an html element.     */    getInnerNumber : function (element) {        // create a variable which is going to hold the cells text value        var elementData = "";                // Inner text property returns the element's content        // without html tags.  If the browser has this property,        // return it.        if (element.innerText) {            elementData =  element.innerText;        } else {            // else use the node's value to get the inner text            elementData = this.getHTMLFreeText(element);        }            // check if cellData contains "(" and ")" as first and last characters. If so        // that means that it's a number and it's negative        elementData = elementData.replace(/\(/g, "-");        elementData = elementData.replace(/\)/g, "");            // remove all non numeric chars        elementData = elementData.replace(/[^0-9.\-]/g,'');            // obtain a float from the element data's text        var newNumber = parseFloat(elementData);                // set to zero if is not a number.        if (isNaN(newNumber)) {            newNumber = 0;        }                return newNumber;    },                /**     * Function will retrieve the text of an html element     * for those browsers that do not have innerText attribute     * on nodes.     */    getHTMLFreeText : function (element) {        //init vars        var text = "";        var elementChildren = element.childNodes;        var l = elementChildren.length;            // loop through each html element until you get its         // text value.        for (var i = 0; i < l ; i++) {            switch (elementChildren[i].nodeType) {              case 1:                 // Type is an element                text = text + this.getHTMLFreeText(elementChildren[i]);                break;              case 3:                // Type is text                text = text + elementChildren[i].nodeValue;                break;              default:                break;            }        }        return text;    }}