﻿
///**************************************************************
///
/// Utiltiy Functions
///
///**************************************************************

function alertMessage(message, title, width, height) {
    var m = message.replace(" ", "+");
    var t = title.replace(" ", "+");
    var qs = "message=" + m + "*%*ts=" + new Date().getTime().toString();
    var url = "/Popups/window.aspx?canMove=0&width=" + width + "&height=" + height + "&title=" + t + "&url=" + "/Popups/DisplayMessage.aspx&qs=" + qs;
    var loader = new net.ContentLoader(url, openModalWindow, null, "POST", m, 'application/x-www-form-urlencoded', width, height);
}
function slideShow(folder,title) {
    var qs = "folder=" + folder + "*%*ts=" + new Date().getTime().toString();
    var width = 666;
    var height = 650;
    var url = "/Popups/window.aspx?canMove=0&width=" + width + "&height=" + height + "&title=" + title + "+Slide+Show&url=" + "/Popups/SlideShow.aspx&qs=" + qs;
    var loader = new net.ContentLoader(url, openModalWindow, null, "POST", null, 'application/x-www-form-urlencoded', width, height);
}
function blockNonNumbers(obj, e, allowDecimal, allowNegative) {
    var key;
    var isCtrl = false;
    var keychar;
    var reg;

    if (window.event) {
        key = e.keyCode;
        isCtrl = window.event.ctrlKey
    }
    else if (e.which) {
        key = e.which;
        isCtrl = e.ctrlKey;
    }

    if (isNaN(key)) return true;

    keychar = String.fromCharCode(key);

    // check for backspace or delete, or if Ctrl was pressed
    if (key == 8 || isCtrl) {
        return true;
    }

    reg = /\d/;
    var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
    var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;

    return isFirstN || isFirstD || reg.test(keychar);
}

function disallowEntry(evt) {
    //allow tab in firefox
    if (evt.keyCode == 9) {
        return true;
    }
    else {
        return false;
    }
}

function blockNonCharacters(obj, e) {
    var regex = /^[0-9A-Za-z]+$/; //^[a-zA-z]+$/
    var txt;
    var key;
    if (window.event) {
        key = e.keyCode;
    }
    else if (e.which) {
        key = e.which;
    }
    if (isNaN(key)) return true;
    txt = String.fromCharCode(key);
    if (txt == " ") {
        return true;
    }
    else if (regex.test(txt)) {
        return true;
    } else {
        return false;
    }
}

function IsValidEmail(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false;
    }
    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false;
    }
    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false;
    }
    if (str.indexOf(at, (lat + 1)) != -1) {
        return false;
    }
    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false;
    }
    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false;
    }
    if (str.indexOf(" ") != -1) {
        return false
    }
    return true;
}

function trim(str) {
    var str = str.replace(/^\s+|\s+$/g, "");
    return str;
}

///**************************************************************
///
/// Popup Window Code
///
///**************************************************************
function openWindow() {
    var panel = document.getElementById("windowDiv");
    var myWidth = 0
    var myHeight = 0
    var top;
    var left;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    var height = 600;
    var width = 800;
    if (BrowserDetect.browser + ' ' + BrowserDetect.version == "Explorer 6") {
        top = 205;
        left = (myWidth - width) / 2;
        panel.style.cssText = 'float: none; display: block; position: absolute; background-color: #dddddd; left:' + left + 'px; top:' + top + 'px;';
    } else {
        top = (myHeight - height) / 2; //top was ignored in ie6
        left = (myWidth - width) / 2;
        panel.style.cssText = 'float: none; display: block; position: fixed; background-color: #dddddd; left:' + left + 'px; top:' + top + 'px;';
    }
    panel.innerHTML = this.req.responseText;
}

function openModalWindow() {
    var panel = document.getElementById("windowDivContainer");

    var Xwidth = this.param1;
    var Yheight = this.param2;

    var scrolledX, scrolledY;
    if (self.pageYoffset) {
        scrolledX = self.pageXoffset;
        scrolledY = self.pageYoffset;
    } else if (document.documentelement && document.documentelement.scrollTop) {
        scrolledX = document.documentelement.scrollLeft;
        scrolledY = document.documentelement.scrollTop;
    } else if (document.body) {
        scrolledX = document.body.scrollLeft;
        scrolledY = document.body.scrollTop;
    }
    // Next, determine the coordinates of the center of browser's window
    var centerX, centerY;
    if (self.innerHeight) {
        centerX = self.innerWidth;
        centerY = self.innerHeight;
    } else if (document.documentelement && document.documentelement.clientHeight) {
        centerX = document.documentelement.clientWidth;
        centerY = document.documentelement.clientHeight;
    } else if (document.body) {
        centerX = document.body.clientWidth;
        centerY = document.body.clientHeight;
    }

    // Xwidth is the width of the div, Yheight is the height of the 
    // div passed as arguments to the function: 
    var leftoffset = scrolledX + (centerX - Xwidth) / 2;
    var topoffset = scrolledY + (centerY - Yheight) / 2;
    // The initial width and height of the div can be set in the 
    // style sheet with display:none; divid is passed as an argument to // the function
    panel.style.cssText = 'float: none; display: block; position: absolute; left: ' + leftoffset + 'px; top: ' + topoffset + 'px;';
    panel.innerHTML = this.req.responseText;

    var panelTop = document.getElementById("windowDiv");
    var fullHeight
    if (document.body.scrollHeight > document.body.offsetHeight) {
        fullHeight = document.body.scrollHeight;
    }
    else {
        fullHeight = document.body.offsetHeight;
    }
    panelTop.style.cssText = 'float: none; display: block; position: absolute; width: 100%; height: ' + fullHeight + 'px; left: 0px; top: 0px;';
}

function closeWindow() {
    currentRollOver = "";
    var div = document.getElementById("windowDiv");
    div.style.cssText = "float: none; display: none;"
}

var BrowserDetect = {
    init: function () {
        this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
        this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
        this.OS = this.searchString(this.dataOS) || "an unknown OS";
    },
    searchString: function (data) {
        for (var i = 0; i < data.length; i++) {
            var dataString = data[i].string;
            var dataProp = data[i].prop;
            this.versionSearchString = data[i].versionSearch || data[i].identity;
            if (dataString) {
                if (dataString.indexOf(data[i].subString) != -1)
                    return data[i].identity;
            }
            else if (dataProp)
                return data[i].identity;
        }
    },
    searchVersion: function (dataString) {
        var index = dataString.indexOf(this.versionSearchString);
        if (index == -1) return;
        return parseFloat(dataString.substring(index + this.versionSearchString.length + 1));
    },
    dataBrowser: [
		{ string: navigator.userAgent,
		    subString: "OmniWeb",
		    versionSearch: "OmniWeb/",
		    identity: "OmniWeb"
		},
		{
		    string: navigator.vendor,
		    subString: "Apple",
		    identity: "Safari"
		},
		{
		    prop: window.opera,
		    identity: "Opera"
		},
		{
		    string: navigator.vendor,
		    subString: "iCab",
		    identity: "iCab"
		},
		{
		    string: navigator.vendor,
		    subString: "KDE",
		    identity: "Konqueror"
		},
		{
		    string: navigator.userAgent,
		    subString: "Firefox",
		    identity: "Firefox"
		},
		{
		    string: navigator.vendor,
		    subString: "Camino",
		    identity: "Camino"
		},
		{		// for newer Netscapes (6+)
		    string: navigator.userAgent,
		    subString: "Netscape",
		    identity: "Netscape"
		},
		{
		    string: navigator.userAgent,
		    subString: "MSIE",
		    identity: "Explorer",
		    versionSearch: "MSIE"
		},
		{
		    string: navigator.userAgent,
		    subString: "Gecko",
		    identity: "Mozilla",
		    versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
		    string: navigator.userAgent,
		    subString: "Mozilla",
		    identity: "Netscape",
		    versionSearch: "Mozilla"
		}
	],
    dataOS: [
		{
		    string: navigator.platform,
		    subString: "Win",
		    identity: "Windows"
		},
		{
		    string: navigator.platform,
		    subString: "Mac",
		    identity: "Mac"
		},
		{
		    string: navigator.platform,
		    subString: "Linux",
		    identity: "Linux"
		}
	]

};
BrowserDetect.init();

///**************************************************************
///
/// Ajax Calls
///
///**************************************************************
/*
url-loading object and a request queue built on top of it
*/

/* namespacing object */
var net = new Object();

net.READY_STATE_UNINITIALIZED = 0;
net.READY_STATE_LOADING = 1;
net.READY_STATE_LOADED = 2;
net.READY_STATE_INTERACTIVE = 3;
net.READY_STATE_COMPLETE = 4;


/*--- content loader object for cross-browser requests ---*/
net.ContentLoader = function (url, onload, onerror, method, params, contentType, params1, params2, params3, params4, params5, params6, params7, params8, params9, params10) {
    this.req = null;
    this.onload = onload;
    if (onerror == 0) {
        onerror = this.defaultError;
    }
    this.onerror = (onerror) ? onerror : this.defaultError;

    this.loadXMLDoc(url, method, params, contentType);
    this.param1 = params1;
    this.param2 = params2;
    this.param3 = params3;
    this.param4 = params4;
    this.param5 = params5;
    this.param6 = params6;
    this.param7 = params7;
    this.param8 = params8;
    this.param9 = params9;
    this.param10 = params10;
}

net.ContentLoader.prototype = {
    loadXMLDoc: function (url, method, params, contentType) {
        if (!method) {
            method = "GET";
        }
        if (!contentType && method == "POST") {
            contentType = 'application/x-www-form-urlencoded';
        }
        if (window.XMLHttpRequest) {
            this.req = new XMLHttpRequest();
        } else if (window.ActiveXObject) {
            this.req = new ActiveXObject("Microsoft.XMLHTTP");
        }
        if (this.req) {
            try {
                var loader = this;
                this.req.onreadystatechange = function () {
                    loader.onReadyState.call(loader);
                }
                this.req.open(method, url, true);
                if (contentType) {
                    this.req.setRequestHeader('Content-Type', contentType);
                }
                this.req.send(params);
            } catch (err) {
                this.onerror.call(this);
            }
        }
    },

    onReadyState: function () {
        var req = this.req;
        var ready = req.readyState;
        if (ready == net.READY_STATE_COMPLETE) {
            var httpStatus = req.status;
            if (httpStatus == 200 || httpStatus == 0) {
                this.onload.call(this);
            } else {
                this.onerror.call(this);
            }
        }
    },

    defaultError: function () {
        //document.write(this.req.responseText);
        alert("error fetching data!"
            + "\n\nmessage:" + this.req.responseText
            + "\n\nreadyState:" + this.req.readyState
            + "\nstatus: " + this.req.status
            + "\nheaders: " + this.req.getAllResponseHeaders());
    }
}



/*--- queue system for efficient transfer of many small commands in a single request ---*/
net.cmdQueues = new Array();


net.CommandQueue = function (id, url, onUpdate, freq) {
    this.id = id;
    net.cmdQueues[id] = this;
    this.url = url;
    this.queued = new Array();
    this.sent = new Array();
    this.onUpdate = onUpdate;
    if (freq) {
        this.repeat(freq);
    }
    this.lastUpdateTime = 0;
}

net.CommandQueue.STATUS_QUEUED = -1;
net.CommandQueue.STATE_UNINITIALIZED = net.READY_STATE_UNINITIALIZED;
net.CommandQueue.STATE_LOADING = net.READY_STATE_LOADING;
net.CommandQueue.STATE_LOADED = net.READY_STATE_LOADED;
net.CommandQueue.STATE_INTERACTIVE = net.READY_STATE_INTERACTIVE;
net.CommandQueue.STATE_COMPLETE = net.READY_STATE_COMPLETE;
net.CommandQueue.STATE_PROCESSED = 5;

net.CommandQueue.PRIORITY_NORMAL = 0;
net.CommandQueue.PRIORITY_IMMEDIATE = 1;


net.CommandQueue.prototype = {
    addCommand: function (command) {
        if (this.isCommand(command)) {
            this.queue.append(command, true);
            if (command.priority == net.CommandQueue.PRIORITY_IMMEDIATE) {
                this.fireRequest();
            }
        }
    },

    fireRequest: function () {
        if (!this.onUpdate && this.queued.length == 0) {
            return;
        }
        var data = "lastUpdate=" + this.lastUpdateTime + "&data=";
        for (var i = 0; i < this.queued.length; i++) {
            var cmd = this.queued[i];
            if (this.isCommand(cmd)) {
                data += cmd.toRequestString();
                this.sent[cmd.id] = cmd;
            }
        }
        this.queued = new Array();
        this.loader = new net.ContentLoader(
    this.url,
    net.CommandQueue.onload,
    net.CommandQueue.onerror,
    "POST", data
  );
    },

    isCommand: function (obj) {
        return (
    obj.implementsProp("id")
    && obj.implementsProp("priority")
    && obj.implementsFunc("toRequestString")
    && obj.implementsFunc("parseResponse")
  );
    },

    repeat: function (freq) {
        this.unrepeat();
        if (freq > 0) {
            this.freq = freq;
            var cmd = "net.cmdQueues[" + this.id + "].fireRequest()";
            this.repeater = setInterval(cmd, freq * 1000);
        }
    },

    unrepeat: function () {
        if (this.repeater) {
            clearInterval(this.repeater);
        }
        this.repeater = null;
    }
}

net.CommandQueue.onload = function () {
    var xmlDoc = this.req.responseXML;
    var elDocRoot = xmlDoc.getElementsByTagName("responses")[0];
    var lastUpdate = elDocRoot.attributes.getNamedItem("updateTime");
    if (parseInt(lastUpdate) > this.lastUpdateTime) {
        this.lastUpdateTime = lastUpdate;
    }
    if (elDocRoot) {
        for (i = 0; i < elDocRoot.childNodes.length; i++) {
            elChild = elDocRoot.childNodes[i];
            if (elChild.nodeName == "command") {
                var attrs = elChild.attributes;
                var id = attrs.getNamedItem("id").value;
                var command = net.commandQueue.sent[id];
                if (command) {
                    command.parseResponse(elChild);
                }
            } else if (elChild.nodeName == "update") {
                if (this.implementsFunc("onUpdate")) {
                    this.onUpdate.call(this, elChild);
                }
            }
        }
    }
}

net.CommandQueue.onerror = function () {
    alert("problem sending the data to the server");
}

///**************************************************************
///
/// Drag Functionality
///
///**************************************************************
// Global object to hold information.
var browser = new Browser();
var dragObj = new Object();
dragObj.zIndex = 0;


//drag code
function Browser() {

    var ua, s, i;

    this.isIE = false;
    this.isNS = false;
    this.version = null;

    ua = navigator.userAgent;

    s = "MSIE";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isIE = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    s = "Netscape6/";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = parseFloat(ua.substr(i + s.length));
        return;
    }

    // Treat any other "Gecko" browser as NS 6.1.

    s = "Gecko";
    if ((i = ua.indexOf(s)) >= 0) {
        this.isNS = true;
        this.version = 6.1;
        return;
    }
}

function dragStart(event, id) {

    var el;
    var x, y;

    // If an element id was given, find it. Otherwise use the element being
    // clicked on.

    if (id)
        dragObj.elNode = document.getElementById(id);
    else {
        if (browser.isIE)
            dragObj.elNode = window.event.srcElement;
        if (browser.isNS)
            dragObj.elNode = event.target;

        // If this is a text node, use its parent element.

        if (dragObj.elNode.nodeType == 3)
            dragObj.elNode = dragObj.elNode.parentNode;
    }

    // Get cursor position with respect to the page.

    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }

    // Save starting positions of cursor and element.

    dragObj.cursorStartX = x;
    dragObj.cursorStartY = y;
    dragObj.elStartLeft = parseInt(dragObj.elNode.style.left, 10);
    dragObj.elStartTop = parseInt(dragObj.elNode.style.top, 10);

    if (isNaN(dragObj.elStartLeft)) dragObj.elStartLeft = 0;
    if (isNaN(dragObj.elStartTop)) dragObj.elStartTop = 0;

    // Update element's z-index.

    dragObj.elNode.style.zIndex = ++dragObj.zIndex;

    // Capture mousemove and mouseup events on the page.

    if (browser.isIE) {
        document.attachEvent("onmousemove", dragGo);
        document.attachEvent("onmouseup", dragStop);
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS) {
        document.addEventListener("mousemove", dragGo, true);
        document.addEventListener("mouseup", dragStop, true);
        event.preventDefault();
    }
}

function dragGo(event) {

    var x, y;

    // Get cursor position with respect to the page.

    if (browser.isIE) {
        x = window.event.clientX + document.documentElement.scrollLeft
      + document.body.scrollLeft;
        y = window.event.clientY + document.documentElement.scrollTop
      + document.body.scrollTop;
    }
    if (browser.isNS) {
        x = event.clientX + window.scrollX;
        y = event.clientY + window.scrollY;
    }

    // Move drag element by the same amount the cursor has moved.

    dragObj.elNode.style.left = (dragObj.elStartLeft + x - dragObj.cursorStartX) + "px";
    dragObj.elNode.style.top = (dragObj.elStartTop + y - dragObj.cursorStartY) + "px";

    if (browser.isIE) {
        window.event.cancelBubble = true;
        window.event.returnValue = false;
    }
    if (browser.isNS)
        event.preventDefault();
}

function dragStop(event) {

    // Stop capturing mousemove and mouseup events.

    if (browser.isIE) {
        document.detachEvent("onmousemove", dragGo);
        document.detachEvent("onmouseup", dragStop);
    }
    if (browser.isNS) {
        document.removeEventListener("mousemove", dragGo, true);
        document.removeEventListener("mouseup", dragStop, true);
    }
}


