﻿/* ================================================================== */
/* SensorMapUtility.js                                                */
/*                                                                    */
/* This file contains a collection of utility prototypes and          */
/* functions that the SensorMap UI uses.                              */
/*                                                                    */
/* ================================================================== */

/* ============================================================ */
/* Float Panel.                                                 */
/* This object is a generic wrapper around an html div.         */
/* ============================================================ */

function FloatPanel(id, css)
{
    this.panelDiv = document.createElement("div");
    this.panelDiv.setAttribute("id", id);
    this.panelDiv.className = css;
    
    this.x = 0;
    this.y = 0;

    document.body.appendChild(this.panelDiv);
}

FloatPanel.prototype.getElement = function()
{
    return this.panelDiv;
}

FloatPanel.prototype.getPosition = function()
{
    var position = new Object();
    position.x = this.x;
    position.y = this.y;
    return position;
}

FloatPanel.prototype.setPosition = function(x, y)
{
    this.x = x;
    this.y = y;
}

FloatPanel.prototype.setSize = function(width, height)
{
    this.panelDiv.style.width = width + "px";
    this.panelDiv.style.height = height + "px";
}

FloatPanel.prototype.setBody = function(body)
{
    this.panelDiv.innerHTML = body;
}

FloatPanel.prototype.show = function()
{
    this.panelDiv.style.left = this.x + "px";
    this.panelDiv.style.top = this.y + "px";
    this.panelDiv.style.display = "block";
}

FloatPanel.prototype.hide = function()
{
    this.panelDiv.style.display = "none";
}

FloatPanel.prototype.isVisible = function()
{
    return (this.panelDiv.style.display != "none");
}


/* ============================================================ */
/* General Cookie Routines                                      */
/* ============================================================ */
function DeleteCookie(id, content)
{
    document.cookie = id + "=" + escape(content) 
        + "; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function GetCookieName(cookie)
{
    var cookieId = cookie.substr(0, cookie.indexOf("="));
    return unescape(cookieId);
}

function GetCookieValue(cookie)
{
    var cookieValue = cookie.substr(cookie.indexOf("=") + 1);
    return unescape(cookieValue);
}

function ReadCookie(id)
{
    var ck = "" + document.cookie;
    var content = null;
    var indexStart = ck.indexOf(id);
    
    if (indexStart != -1) 
    {
        var indexEnd = ck.indexOf(";", indexStart);
        indexEnd = (indexEnd == -1) ? ck.length : indexEnd;
        
        if (indexEnd != -1)
        {
            content = unescape(ck.substring(indexStart + id.length + 1, indexEnd));
        }
    }
    
    return content;
}

function ReadAllCookies()
{
    return document.cookie.split(';');
}

function WriteCookie(id, content)
{
    var today = new Date();
    var expire = new Date();
    
    expire.setTime(today.getTime() + 31536000000);  // one year
    document.cookie = id + "=" + escape(content) + "; expires=" + expire.toGMTString();
}


/* ============================================================ */
/* UI Utility Routines                                          */
/* ============================================================ */
function CheckForEnter(event, f)
{
    // Netscape/Mozilla family
    if (navigator.appName == "Netscape")
    {
        if (event && (event.which == 13))
        {
            f();
        }  
    }
    
    // Internet Explorer
    if (navigator.appName.indexOf("Microsoft") != -1)
    {
        if (window.event && (window.event.keyCode == 13))
        {
            f();
        }   
    }
}

/*
 * IsUserPublisher
 *
 * Determines if the user is a publisher by reading the value of the hidden
 * field that is set by the server.
 */
 
function IsUserPublisher()
{
   // alert("in IsUserPublisher");
    var swID = GetUserLogin();
    // alert("swID = " + swID);   
    return (swID != "");
}

function GetUserLogin()
{
    return document.getElementById(swClientID).value;
}

function GetUserName() {
    return document.getElementById(swUserNameClientID).value;
}

function GetPassCode()
{
    return document.getElementById(swPassCodeClientID).value;
}