﻿// SHOULD BE LOADED BEFORE ALL OTHER SCRIPTS

/*************/
/* SHORTCUTS */
/*************/
if (typeof $get == 'undefined') var $get = function(id) {return document.getElementById(id);}
if (typeof $set == 'undefined') var $set = function(id,value) {$get(id).value=value;}
if (typeof $hide == 'undefined') var $hide = hideDiv;
if (typeof $show == 'undefined') var $show = showDiv;
if (typeof $center == 'undefined') var $center = centerDiv;
if (typeof $listen == 'undefined') var $listen = addEvent;
if (typeof $getX == 'undefined') var $getX = function(o) {
    var x = 0;
    if (o.offsetParent) {x = o.offsetLeft; while (o = o.offsetParent) x += o.offsetLeft;}
    return x;
}
if (typeof $getY == 'undefined') var $getY = function(o) {
    var y = 0;
    if (o.offsetParent) {y = o.offsetTop; while (o = o.offsetParent) y += o.offsetTop;}
    return y;
}
var $http = sendHttpRequest;

/*******************/
/* GENERAL SCRIPTS */
/*******************/
function sendHttpRequest(url, data, method, callback, delay, state) {
    if (!delay) delay = 0;
    if (!method) method = 'POST';
   
    var http = (navigator.appName == "Microsoft Internet Explorer" ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest());
    
    if (callback) {
        http.onreadystatechange = function() {
            if (http.readyState == 4) setTimeout(function() {callback(http, state)}, delay);
        };
    }
    
    if (method == 'GET') {
        if (data) http.open('GET', url + '?' + data, true);
        else http.open('GET', url, true);
        http.send(null);
    }
    else {
        http.open('POST', url, true);
        http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        http.send(data);
    }
}

function addEvent(o,evType,fn) {
    if (o.addEventListener) {o.addEventListener(evType, fn, false); return false;}
    else if (o.attachEvent) return o.attachEvent('on'+evType, fn);
    else return false;
}

function randomPassword(length) {
    var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    var password = '';
    
    for (var x = 0; x < length; x++) {
        var i = Math.floor(Math.random() * chars.length);
        password += chars.charAt(i);
    }
    return password;
}

function showDiv(divID, x, y) {
    var div = $get(divID);
    if (div) {
        if (x) div.style.left = x + 'px';
        if (y) div.style.top = y + 'px';
        div.style.visibility = 'visible';
        div.style.display = 'block';
    }
}

function showDivMessage(divID, message, x, y) {
    var div = $get(divID);
    if (div) {
        if (x) div.style.left = x + 'px';
        if (y) div.style.top = y + 'px';
        div.style.visibility = 'visible';
        div.style.display = 'block';
        div.innerHTML = message;
    }
}

function appendDivMessage(divID, message, scroll) {
    var div = $get(divID);
    if (div) {div.innerHTML += message; if (scroll) div.scrollTop = div.scrollHeight;}
}

function hideDiv(divID) {
    var div = $get(divID);
    if (div) {div.style.visibility = 'hidden'; div.style.display = 'none';}
}

function centerDiv(divID) {
    var div = $get(divID);
    if (div) {
        div.style.left = '50%';
        div.style.top = '50%';
        div.style.marginLeft = '-' + parseInt(div.offsetWidth / 2) + 'px';
        div.style.marginTop = '-' + parseInt(div.offsetHeight / 2) + 'px';
        div.style.visibility = 'visible';
        div.style.display = 'block';
    }
}

function centerPopup(url, title, anchor, width, height) {
    var popup = $get('popup');
    if (!popup) {popup = document.createElement('div'); popup.id = 'popup'; popup.className = 'popup'; document.forms[0].appendChild(popup);}
    
    popup.style.title = title;
    popup.style.width = '100px';
    popup.style.height = '20px';
    popup.style._width = width + 'px';
    popup.style._height = height + 'px';
    popup.innerHTML = '<img src="' + vpath + '/images/icon_loading.gif" width="16" height="16"/>&nbsp;Loading...';
    centerDiv('popup');
    
    var cb = function(http) {
        popup.style.width = popup.style._width;
        popup.style.height = popup.style._height;
        popup.innerHTML = '<div class="popupTitle">' + popup.style.title + '</div><div class="popupContent">' + http.responseText + '</div>' + '<div class="popupFooter"><a href="javascript:$hide(\'popup\');">close</a></div>';
        centerDiv('popup');
    };
    
    $http(url, null, 'GET', cb, 0);
}

function showPopup(url, title, anchor, width, height, xOff, yOff) {
    var popup = $get('popup');
    if (!popup) {popup = document.createElement('div'); popup.id = 'popup'; popup.className = 'popup'; document.forms[0].appendChild(popup);}
    
    if (!xOff) xOff = 0;
    if (!yOff) yOff = 0;
    
    popup.style.title = title;
    popup.style.left = ($getX(anchor) + xOff) + 'px';
    popup.style.top = ($getY(anchor) + yOff) + 'px';
    popup.style.width = '100px';
    popup.style.height = '20px';
    popup.style._width = width + 'px';
    popup.style._height = height + 'px';
    popup.style.visibility = 'visible';
    popup.style.display = 'block';
    popup.innerHTML = '<img src="' + vpath + '/images/icon_loading.gif" width="16" height="16"/>&nbsp;Loading...';
    
    var cb = function(http) {
        popup.style.width = popup.style._width;
        popup.style.height = popup.style._height;
        popup.innerHTML = '<div class="popupTitle">' + popup.style.title + '</div><div class="popupContent">' + http.responseText + '</div>' + '<div class="popupFooter"><a href="javascript:$hide(\'popup\');">close</a></div>';
    };
    
    $http(url, null, 'GET', cb, 0);
}

/********************/
/* COOKIE FUNCTIONS */
/********************/
function setCookie(name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");

    document.cookie = curCookie;
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else begin += 2;
  
    var end = document.cookie.indexOf(";", begin);
    
    if (end == -1) end = dc.length;
  
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

/*******************/
/* UTILITY SCRIPTS */
/*******************/
Array.prototype.indexOf = function(v,b,s) {
    for (var i = +b || 0, l = this.length; i < l; i++) if (this[i]===v || s && this[i]==v) return i;
    return -1;
};

var _monthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var _dayNames = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

Date.prototype.format = function(f) {
    if (!this.valueOf()) return '&nbsp;';
    var d = this;
    return f.replace(/(yyyy|MMMMM|MMM|MM|dddd|ddd|dd|hh|mm|ss|tt)/gi,
        function($1) {
            switch ($1) {
            case 'yyyy': return d.getFullYear();
            case 'MMMM': return _monthNames[d.getMonth()];
            case 'MMM':  return _monthNames[d.getMonth()].substr(0, 3);
            case 'MM':   return zeroFill(d.getMonth() + 1, 2);
            case 'dddd': return _dayNames[d.getDay()];
            case 'ddd':  return _dayNames[d.getDay()].substr(0, 3);
            case 'dd':   return zeroFill(d.getDate(), 2);
            case 'hh':   return zeroFill((h = d.getHours() % 12) ? h : 12, 2);
            case 'mm':   return zeroFill(d.getMinutes(), 2);
            case 'ss':   return zeroFill(d.getSeconds(), 2);
            case 'tt':  return (d.getHours() < 12 ? 'AM' : 'PM');
            }
        }
    );
}

function zeroFill(val, length) {
    var zf = val;
    for (var i = val.length; i < length; i++) zf = '0' + zf;
    return zf;
}

function writeEmail(email, host, text) {
    document.write("<a href=" + "mail" + "to:" + email + "@" + host + ">" + text + "</a>");
}



