var today = new Date();
//var dayTextShort = new Array("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");
//var monthTextShort = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
var dayText = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
var monthText = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");



function gotoAnchor() {
    if (location.hash == '' && location.existsParam('anchor')) location.replace("#" + location.getParam('anchor'));
}



function popupWindow(url, winName, winProperties) {

    if (!winName || winName == "") var winName = "popupWindow";

    var options = {};
    options["top"] = 0;
    options["left"] = 0;
    options["menubar"] = 0;
    options["toolbar"] = 0;
    options["resizable"] = 0;
    options["location"] = 0;
    options["status"] = 0;
    options["scrollbars"] = 0;
    options["width"] = Math.round(screen.availWidth * 0.9);
    options["height"] = Math.round(screen.availHeight * 0.8);

    if (winProperties) {
        var properties = winProperties.split(",");
        for (var i = 0; i < properties.length; i++) {
            var prop = properties[i].split("=");
            options[prop[0]] = prop[1];
        }
    }

    if (options["top"] == 0) options["top"] = (screen.availHeight - options["height"]) / 4;
    if (options["left"] == 0) options["left"] = (screen.availWidth - options["width"]) / 2;

    options["screeny"] = options["top"];
    options["screenx"] = options["left"];

    var strOptions = "";
    for (var opt in options) {
        strOptions += (strOptions == "" ? "" : ",") + opt + "=" + options[opt];
    }

    window.open(url, winName, strOptions);
    return false;

}



function posterEnlarge(image) {
    window.open("poster.html?image=" + image, "Poster", "menubar=0,toolbar=0,resizable=1,location=0,status=0,scrollbars=1");
}



location.parameterMap = {};
var oParams = location.search.substring(1).split("&");
for(var i = 0; i < oParams.length; i++) {
    var pair = oParams[i].split("=");
    location.parameterMap[pair[0]] = pair[1];
}

location.existsParam = function(key) {
    return this.parameterMap[key] != undefined;
}

location.getParam = function(key) {
    if (!this.existsParam(key)) return '';
    return this.parameterMap[key];
}



//function addressDecode() {
//    var mtoLoc;
//    for (i=0; i<=(document.links.length-1); i++) {
//        mtoLoc = document.links[i].href.toLowerCase().indexOf('[code:mto]');
//        if (mtoLoc > -1) {
//			document.links[i].href = document.links[i].href.substr(mtoLoc, document.links[i].href.length - mtoLoc);
//            document.links[i].href = document.links[i].href.replace('[code:mto]','mailto');
//            document.links[i].href = document.links[i].href.replace('[code:colon]',':');
//            document.links[i].href = document.links[i].href.replace('[code:at]','@');
//            document.links[i].href = document.links[i].href.replace('[code:dot]','.');
//            document.links[i].href = document.links[i].href.replace('[code:dot]','.');
//            document.links[i].href = document.links[i].href.replace('[code:dot]','.');
//            document.links[i].href = document.links[i].href.replace('[code:ca]','ca');
//            document.links[i].href = document.links[i].href.replace('[code:com]','com');
//            if (document.links[i].innerHTML == '') document.links[i].innerHTML = '<nobr>' + document.links[i].href.split(':')[1] + '</nobr>';
//        }
//    }
//}



Date.prototype.dateToString = function() {
    return dayText[this.getDay()] + ", " + monthText[this.getMonth()] + " " + this.getDate();
}

Date.prototype.pastDue = function(numDays) {
    if (!numDays) numDays = 1;
    return (((today - this)/86400000) > numDays);
}

function pastDue(strDate, numDays) {
    return (new Date(strDate)).pastDue(numDays);
}

String.prototype.left = function(len) {
// return the (len) left most characters of string
    return(this.substr(0, len));
}



function insertwebtxt(title, display, link) {
    var str = '<a href="" onClick="buildwebtxt(event, \'' + title + '\', \'' + display + '\'';
    if (link) str += ', \'' + link + '\'';
    str += '); return false;">' + title + '</a>';
    document.write(str);
}

function buildwebtxt(event, title, display, link) {
    display = webtxt(display);
    link = link ? webtxt(link) : display;
    link = ':' + link;
    link = 'o' + link;
    link = 't' + link;
    link = 'l' + link;
    link = 'i' + link;
    link = 'a' + link;
    link = 'm' + link;
    createPopUp(event, title, '<a href="' + link + '">' + display + '</a>');
}

function webtxt(id) {
    var rtnText = id.replace('[code:at]','@');
    rtnText = rtnText.replace(RegExp('\\[code:dot\\]','g'),'.');
    rtnText = rtnText.replace('[code:ca]','.ca');
    rtnText = rtnText.replace('[code:com]','.com');
//    rtnText = rtnText.replace('[code:domain]','@' + domain);
    return rtnText;
}

function createPopUp(event, title, content) {
	var div = document.createElement('div');
    div.className='popupdiv';

    var innerHTML = '<table width="100%" cellspacing="0px"><tr><td style="background-color: LightGrey;">';
    innerHTML += '<div style="float: left; font-weight: bold; padding: 2px;">' + title + '</div>';
    innerHTML += '<div style="float: right;"><button type="button" style="color: white; font-weight: bold; background-color: darkred; padding-left: 7px; padding-right: 7px;" onclick="document.body.removeChild(this.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode);">X</button></div>';
    innerHTML += '</td></tr></table><br/>';

    innerHTML += content;
   
    div.innerHTML = innerHTML;

    var cursorPosition = getCursorPosition(event);
    
    div.style.left = cursorPosition.x - 150;
    div.style.top = cursorPosition.y - 50;

	document.body.appendChild(div);
}

function getCursorPosition(e) {
    var cursor = {x:0, y:0};
    e = e || window.event;
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    }
    else {
        if (e.clientX || e.clientY) {
            var de = document.documentElement;
            var b = document.body;
            cursor.x = e.clientX + (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
            cursor.y = e.clientY + (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
        }
    }
    return cursor;
}
