/* 
    Copyright (c) 2001-2009 Laszlo Systems, Inc.
    All Rights Reserved.
*/
/**
 * @author lhenrywilkins
 */

function BrowserUtilities(){
    
    //TODO: move locale discovery into here
    
    this.isff = navigator.userAgent.indexOf("Firefox") != -1;
    this.isie = navigator.userAgent.indexOf('MSIE') != -1;
    this.issafari = navigator.userAgent.indexOf("Safari") != -1;
    this.isopera = navigator.userAgent.indexOf("Opera") != -1;
    this.isnetscape = navigator.userAgent.indexOf("Netscape") != -1;
    
    this.iswin = navigator.appVersion.indexOf("Win") != -1;
    this.ismac = navigator.appVersion.indexOf("Mac") != -1;
    this.ismacppc = navigator.platform.indexOf("PPC") != -1;
    this.isunix = navigator.appVersion.indexOf("X11") != -1;
    this.islinux = navigator.appVersion.indexOf("Linux") != -1;
        
    //holds reference to the framescollection once it's been found
    //see getFrameByName    
    this.framesColl = null;
    
    //set a cookie value
    this.setCookie = function (cookieName, cookieValue, nDays){
        var today = new Date();
        var expire = new Date();
        if (nDays == null || nDays == 0) 
            nDays = 1;
        expire.setTime(today.getTime() + 3600000 * 24 * nDays);
        document.cookie = cookieName + "=" + escape(cookieValue) +
        ";expires=" +
        expire.toGMTString() +
        ";path=/"; // Scopes the cookie to the same scope as cookies set by the server.
    }
    
    //read a cookie value
    this.readCookie = function(name){
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
                c = c.substring(1, c.length);
            }
            if (c.indexOf(nameEQ) == 0) {
                return unescape(c.substring(nameEQ.length, c.length));
            }
        }
        return null;
    }
    
    // Check that cookies are enabled.
    this.cookiesEnabled = function() {
        var v = Math.floor(1000 * Math.random());
        this.setCookie('_ce', v);
        return v == this.readCookie('_ce');
    }
    
    //get the value for the given variable name off the query string
    this.getQueryVariable = function(variable){
        var query = window.top.location.search.substring(1);
        var vars = query.split("&");
        for (var i = 0; i < vars.length; i++) {
            var pair = vars[i].split("=");
            if (pair[0] == variable) {
                return pair[1];
            }
        }
    }
    
    //Sets the URL of the top level window
    this.setLocation = function(url){
        top.window.location.href = url;
    }
    
    //Get the URL of the top level window
    this.getLocation = function(){
        return top.window.location;
    }
    
    //Opens a URL with the provided target and options. A wrapper around 
    //window.open.
    this.openUrlWithOptions = function(url, target, options){
        var test = window.open(url, target, options);
        if (test == null) {
            if (Lz.__BrowserDetect.isIE) {
                alert('Please disable your popup blocker to use this function.\r\rWarning: Internet Explorer may reload the application if you enable popups.  Be sure to save your work before doing so.');
            }
            else {
                alert('Please disable your popup blocker to use this function.');
            }
        }
    }
    
    this.getFrameByName = function(frameName){
        //get references to the window objects of the hidden frames via the
        //frames collection. document.getElementById would just give reference
        //to the iframe object which doesn't support printing
        if (!this['framesColl']) {
            if (document.frames) { //Opera, Internet Explorer
                this.framesColl = document.frames;
            }
            else {
                this.framesColl = window.frames; //Firefox, Safari, Netscape
            }
        }
        if (this.framesColl) {
            return this.framesColl[frameName];
        }
        return null;
    }

    //sends the user back to the page they were on before going to webtop. Need to 
    //go back multiple times in history stack because of multiple frames
    this.goBack = function() {
        if(this.isff) {
            window.history.go(-2);
        } else {
            window.history.go(-3);
        }
    }
}
