﻿// JScript File

    document.onmousemove = mouseMove; 
	document.onmouseup   = mouseUp; 


/* Reusable code */
    
    /* Resize Flash Movie */
    function setApplicationSize(width,height,appName) {
        setApplicationWidth(width,appName);
        setApplicationHeight(height,appname);
    }
    function setApplicationWidth(width,appName) {
        var app = getElement(appName);
        app.width = width;
    }
    function setApplicationHeight(height,appName) {
        var app = getElement(appName);
        app.height = height;
    }
    /* resize html element */
    function setElementSize(width,height,Elem) {
        setElementWidth(width,Elem);
        setElementHeight(height,Elem);
    }
    function setElementWidth(width,Elem) {
        var elem = getElement(Elem);
        if (elem) {
            elem.style.width = width + "px";
        }
    }
    function setElementHeight(height,Elem) {
        var elem = getElement(Elem);
        if (elem) {
            elem.style.height = height + "px";
        }
    }

    function getScrollPosition() {
        var x = 0;
        var y = 0;
        if (BrowserDetect.isIE) {
            x = document.documentElement.scrollLeft;
            y = document.documentElement.scrollTop;
        } else {
            //window.pageXOffset;
            x = window.pageXOffset;
            y = window.pageYOffset;
        }
        return {x:x, y:y}
    }
 
    // DOM Functions 
    function addEventHandler(Elem,eType,eFunc) {
        if (Elem.addEventListener) {
            Elem.addEventListener (eType,eFunc,false);
            Elem.addEventListener (eType,eFunc,false);
        } else if (Elem.attachEvent) {
            Elem.attachEvent (eType,eFunc);
            Elem.attachEvent (eType,eFunc);
        } else {
            Elem[eType] = myFunction;
            Elem[eType] = myFunction;
        }
    }   
    //
    /* Drag Functions */
    //
	var dragObject  = null; 
	var mouseOffset = null; 

    function stopEvent(e) {
        //alert("here");
        var event = e || window.event;
        
        if (event.stopPropagation) {
            event.stopPropagation();
        } else {
            event.cancelBubble = true;
        }
    }

    function mouseCoords(ev){ 
        if(ev.pageX || ev.pageY){ 
	        return {x:ev.pageX, y:ev.pageY}; 
	    } 
	    return { 
	        x:ev.clientX + document.body.scrollLeft - document.body.clientLeft, 
	        y:ev.clientY + document.body.scrollTop  - document.body.clientTop 
	    }; 
	}

	function mouseMove(ev){ 
	    ev           = ev || window.event; 
	    var mousePos = mouseCoords(ev); 
	 
	    if(dragObject){ 
	        dragObject.style.position = 'absolute'; 
	        dragObject.style.top      = mousePos.y - mouseOffset.y; 
	        dragObject.style.left     = mousePos.x - mouseOffset.x; 
	 
	        return false; 
	    } 
	    
	} 

	function getMouseOffset(target, ev){ 
	    ev = ev || window.event; 
	 
	    var docPos    = getPosition(target); 
	    var mousePos  = mouseCoords(ev); 
	    return {x:mousePos.x - docPos.x, y:mousePos.y - docPos.y}; 
	} 
	 
	function getPosition(e){ 
	    var left = 0; 
	    var top  = 0; 
	 
	    while (e.offsetParent){ 
	        left += e.offsetLeft; 
	        top  += e.offsetTop; 
	        e     = e.offsetParent; 
	    } 
	 
	    left += e.offsetLeft; 
	    top  += e.offsetTop; 
	 
	    return {x:left, y:top}; 
	} 
	 
	
	function mouseUp(){ 
	    dragObject = null; 
	} 

    function makeDraggableById(Elem) {
        makeDraggable(getElement(Elem));
    }
	function makeDraggable(item){ 
	    if(!item) return; 
	    item.onmousedown = function(ev){ 
	        dragObject  = this; 
	        mouseOffset = getMouseOffset(this, ev); 
	        return false; 
	    } 
	}     
    function showDialog(Elem,center) {
        var dlg = getElement(Elem);
        if (dlg) {
            if (center) {
                var viewPort = getViewPort();
                var scrollPos = getScrollPosition();
                
                var size = getElementSize(Elem)
                var left = scrollPos.x;
                var top = scrollPos.y;
                
                if (viewPort.height > size.height) {
                    top += (viewPort.height - size.height) / 2;
                }
                if (viewPort.width > size.width) {
                    left += (viewPort.width - size.width) / 2;
                }
                dlg.style.left = left + "px";
                dlg.style.top = top + "px";
            }
            dlg.style.visibility = "visible";
        }
    }
    function hideDialog(Elem) {
        var dlg = getElement(Elem);
        if (dlg) {
            dlg.style.visibility = "hidden";
        }
    }

    function getViewPort() {
        var viewportwidth;
        var viewportheight;

        // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight

        if (typeof window.innerWidth != 'undefined')
        {
             viewportwidth = window.innerWidth,
             viewportheight = window.innerHeight
        }

        // IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
         else if (typeof document.documentElement != 'undefined'
            && typeof document.documentElement.clientWidth !=
            'undefined' && document.documentElement.clientWidth != 0)
        {
              viewportwidth = document.documentElement.clientWidth,
              viewportheight = document.documentElement.clientHeight
        }

        // older versions of IE

        else
        {
              viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
              viewportheight = document.getElementsByTagName('body')[0].clientHeight
        }
	    return {width:viewportwidth, height:viewportheight}; 
    }

function getElement(Elem) {
    if (typeof Elem == 'string') {
	    if (BrowserDetect.isNetscape4) {
		    return getObjNN4(document, Elem);
	    } else {
		    if(document.getElementById) {
			    return document.getElementById(Elem);
		    } else if (document.all){
			    return document.all[Elem];
		    }
        }
    } else {
        return Elem;
    }
}

function getElementSize(Elem) {
    return {width:getElementWidth(Elem), height:getElementHeight(Elem)}
}

function getElementHeight(Elem) {
    var elem = getElement(Elem);
    var height = 0;
	if (BrowserDetect.isOpera5) { 
		height = elem.style.pixelHeight;
	} else {
		height = elem.offsetHeight;
	}
	return height;
}

function getElementWidth(Elem) {
    var elem = getElement(Elem);
    var width = 0;
	if (BrowserDetect.isOpera5) {
		width = elem.style.pixelWidth;
	} else {
		width = elem.offsetWidth;
	}
	//addtrace("HERE");
	return width;
	
}

/*========================================*/
/*      Trace Stuff                     */
/*=======================================*/

    var traceMessage = "";
    var traceDialog = null;
    var tbTrace = null;
    function closeTrace() {
        traceDialog.style.visibility = "hidden";
    }
    function addtrace(text) {
        if (traceDialog == null) {
            traceDialog = createTraceDialog();
            showDialog(traceDialog,true);
        }
        
        traceMessage += text + "\n";
/*        if (!tbTrace) {
            alert("couldn't find trace box!");
            //tbTrace = document.getElementById("traceDialog");// = traceMessage;
            //td.value = traceMessage;
        }*/
        if (tbTrace) {
            tbTrace.value = traceMessage;
        } else {
        //alert(traceDialog);
            alert("couldn't find trace box!");
        }
    }
    function clearTrace() {
        //addtrace("hello world");
        tbTrace.value = "";
        traceMessage = "";
        
    }
    function createTraceDialog() {

        // define the outer div
        var div = document.createElement("div");//"<table><tr><td>hello world</td></tr></table>");
        div.style.position = "absolute";
        div.style.zIndex = "9000";
        div.style.top = "100px";
        div.style.left = "100px";
        div.style.width = "600px";
        div.style.height = "300px";
        div.style.backgroundColor = "#cccccc";
        div.style.border = "solid 1px black";
        makeDraggable(div);

        // define the outer table
        var tbl = document.createElement("table");
        var tbody = document.createElement("tbody");
        tbl.style.width = "600px";
        tbl.style.height = "300px";

/*        
        // Defint the title table
        var titleTable = document.createElement("table");
        var titleTbody = document.createElement("tbody");
        titleTable.style.width = "600px";
        titleTable.style.height = "22";
        titleTable.borderWidth = "0px";
        */
        // Define the rows
        var titleRow = document.createElement("tr");
        //var titleTableRow = document.createElement("tr");
        var contentRow = document.createElement("tr");
        
        // Define the title cell
        var titleCell = document.createElement("td");
        titleCell.innerHTML = '<table width="100%" height="22" border="0" cellpadding="0" cellspacing="0">' +
        '<tr><td><strong>Trace Dialog</strong></td>' +
        '<td align="right">' +
        '<img src="/Defaults/Images/btClose16-up.png" onclick="closeTrace()"' +
        ' onmousedown="{stopEvent(event);this.src=\'/Defaults/Images/btClose16-down.png\';}"' +
        ' onmouseup="{this.src=\'/Defaults/Images/btClose16-hover.png\';}"' +
        ' onmouseout="{this.src=\'/Defaults/Images/btClose16-up.png\';}"' +
        ' onmouseover="{this.src=\'/Defaults/Images/btClose16-hover.png\';}" />' +
                '</td></tr></table>';
        titleCell.style.height = "22px";
        titleCell.style.padding = "0px 0px 0px 0px";
        /*titleCell.style.color = "#cccccc";*/
        /*titleCell.style.backgroundColor = "#333333";*/
        /*
        // define close cell
        var titleCloseCell = document.createElement("td");
        titleCloseCell.innerHTML = "<strong>X</strong>";
        titleCell.style.height = "22px";
        titleCell.style.padding = "0px 0px 0px 0px";
        */
        var contentCell = document.createElement("td");
        contentCell.style.padding = "0px 0px 0px 0px";
        contentCell.style.border = "solid 1px black";
        
        // define the textbox
        var ta = document.createElement("textarea");
        ta.style.height = "262px";
        ta.style.width = "588px";
        
        // add everything to the page        
        titleRow.appendChild(titleCell);
        contentRow.appendChild(contentCell);
        
        ta.value = "hello world";
        addEventHandler(ta,"onmousedown",stopEvent);
        
        contentCell.appendChild(ta);
        tbody.appendChild(titleRow);
        tbody.appendChild(contentRow);
        tbl.appendChild(tbody);
        div.appendChild(tbl);
        document.body.appendChild(div);
        tbTrace = ta;
        
        return div;
    }

/*================ Browser detection ========================*/
var BrowserDetect = {
	init: function () {
	    // initialize browser values
		this.isNetscape = false;
		this.isNetscape4 = false;
		this.isIE = false;
		this.isFireFox = false;
		this.isOpera5 = false;
		this.isOpera = false;
        // set browser values;
		this.browser = this.searchString(this.dataBrowser,true) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS,false) || "an unknown OS";
		if (this.isOpera) {
		    if (Number(this.version) < 6) {
		        this.isOpera5 = true;
		    }
		}
	},
	searchString: function (data,forbrowser) {
		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) {
				    if (forbrowser) {
				        //addtrace("here: " + data[i].isie);
			            this.isNetscape = data[i].isnetscape || false;
			            this.isNetscape4 = data[i].isnetscape4 || false;
			            this.isIE = data[i].isie || false;
			            this.isFireFox = data[i].isfirefox || false;
			            this.isOpera = data[i].isopera || false;
			        }
					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: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera",
			isopera: true
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox",
			isfirefox: true
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape",
			isnetscape: true
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE",
			isie: true
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv",
			isnetscape: true
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla",
			isnetscape4: true
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();
//==============================================//
//    Cross Browser Communication functions     //
//==============================================//

function doClose() {
	sendDataToParent("BYE","so long");
}
function isAlive() {
	return true;
}
var children = new Array();
function openChild(childname,url) {
	//alert('opening...');
	try {
    	var child = new Object();
    	var isNew = true;
    	for (var i=0;i< children.length; i++) {
    	    if (children[i].name == childname) {
    	        child = children[i];
    	        isNew = false;
    	    }
    	}
    	//alert("here");
    	if (child.window) {
    		// do nothing
    		//alert("gotwindow");
    		//alert("val: " + Boolean(child.window.isAlive()));
    		try {
    		    if (child.window.isAlive()) {
    			    //alert("isalive!");
   			    }
    		} catch(err2) {
    			//alert("not alive :(");
				child.name = childname;
	    		child.window = window.open(url,childname);
	    		if (isNew) {
	    			children[children.length] = child;
	    			//alert("isnew");
	    		}
    		}
    	} else {
    		//alert("no window");
    		//var win = window.open(url,name);
			child.name = childname;
    		child.window = window.open(url,childname);
    		if (isNew) {
    			children[children.length] = child;
    			//alert("isnew");
    		}
    	}
    	//alert('open!');
    } catch(err) {
    	alert('no can open child window');
    }
}
function sendDataToChild(childname,dataType,data) {
	try {
    	for (var i=0;i< children.length; i++) {
    	    if (children[i].name == childname) {
    	        children[i].window.getDataFromParent(dataType,data);
    	    }
    	}
    } catch(err) {}
}
function sendDataToParent(dataType,data) {
	try {
		window.opener.getDataFromChild(dataType,data);
	} catch(err) {
		//alert('error sending data to parent');
	}
}
function getFlexApp()
{
  if (navigator.appName.indexOf ("Microsoft") !=-1)
  {
    return window["${application}"];
  } 
  else 
  {
    return document["${application}"];
  }
}
function getDataFromParent(dataType,data) {
    try {
    	getFlexApp().receiveData(dataType,data);
    }
    catch(err) {
        //alert("type: " + dataType + " no talkie to child");
    }
}
function getDataFromChild(dataType,data) {
    try {
    	getFlexApp().receiveData(dataType,data);
    }
    catch(err) {
       // alert("type: " + dataType + " no talkie to parent");
    }
}
/*
 * Date Format 1.2.3
 * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
 * MIT license
 *
 * Includes enhancements by Scott Trenda <scott.trenda.net>
 * and Kris Kowal <cixar.com/~kris.kowal/>
 *
 * Accepts a date, a mask, or a date and a mask.
 * Returns a formatted version of the given date.
 * The date defaults to the current date/time.
 * The mask defaults to dateFormat.masks.default.
 */

var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};
