//document.onkeydown = isFunctionFive;
// captures F5 refresh action, prevents it from breaking modality
//function isFunctionFive() {
//	if (event && event.keyCode == 116) {
//		event.cancelBubble = true;
//		alert("sorry, sucka");
//	}
//}

// Expose hierarchy of windows using a similar syntax to "opener", e.g. "window.previous", window.previous.previous", etc.
topWin = (parent.parent.getTopWin) ? parent.parent.getTopWin() : (topWindow = (parent.getTopWin) ? parent.getTopWin() : getTopWin());
previous = topWin.getPrevious();
topDoc = topWin.document;

// Obtain the script context a certain number of dialog levels behind the current one.  
// If dialog level is not defined return the current one.
function getScriptExecutionContext(dialogLevel) {
	if (dialogLevel === undefined) {
		dialogLevel = 0;
	}
	var context;
	var iframeEl = topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()-dialogLevel].getFrame();
	if (iframeEl.contentDocument) {
	    context = iframeEl.contentDocument.defaultView;
	} else if (iframeEl.contentWindow) {
	    context = iframeEl.contentWindow;
	}
	return context;
}

// Obtain the dialog script context behind the current one in the stack  
// The previous context is at dialog level = 1
function getPreviousScriptExecutionContext() {
	return getScriptExecutionContext(1);
}

INDEX = (self.name.indexOf(topWin.DIALOG_PREFIX) == 0) ? parseInt(self.name.substring(topWin.DIALOG_PREFIX.length)) : ((topWin == self) ? self.INDEX : parent.INDEX);

var ModalScreen;
function getModalScreen() {
	if (window.modalScreenTO) clearTimeout(modalScreenTO);
	if (window.ModalGuard) ModalScreen = new ModalGuard();
	else ModalScreen = topWin.ModalScreen;
	if (ModalScreen.HTMLElem == null) modalScreenTO = setTimeout("getModalScreen();", 100);
}
getModalScreen();

var VC;
function getVC() {
	if (window.VCTO) clearTimeout(VCTO);
	VC = new topWin.VCGuard(self);
	if (VC.HTMLElem == null) VCTO = setTimeout("getVC();", 100);
	else VC.off(INDEX);
}
getVC();

ModalDialog = function(thisUrl, thisSize) {
	this.type = "ModalDialog";
	this.url = (thisUrl) ? thisUrl : "about:blank";
	this.sizeName = thisSize;
	this.height = this.getSize(thisSize).height;
	this.width = this.getSize(thisSize).width;
	return this;
}
ModalDialog.prototype = new topWin.MainContent;
ModalDialog.prototype.getFrame = function() { return topDoc.getElementById(topWin.DIALOG_PREFIX + topWin.OpenDialogs.getIndex(this)); }
ModalDialog.prototype.getFrameWin = function() { return this.getOuterWin(); }
ModalDialog.prototype.getHeight = function() { return this.height; }
ModalDialog.prototype.getOuterWin = function() { return topWin.frames[topWin.DIALOG_PREFIX + topWin.OpenDialogs.getIndex(this)]; }
ModalDialog.prototype.getSize = function(someSize) {
	if (!someSize) return {width:590, height:470};
	if (typeof(someSize) == "string") {
		switch (someSize) {
			case "xlrg": return {width:970, height:540};
			case "lrg": return {width:740, height:540};
			case "med": return {width:590, height:470};
			case "sml": return {width:385, height:355};
			default: return {width:590, height:470};
		}
	} else {
		if (!someSize.width || !someSize.height) return null;
		return {width:someSize.width, height:someSize.height};
	}
}
ModalDialog.prototype.getSizeName = function() { return this.sizeName; }
ModalDialog.prototype.getUrl = function() { return this.url; }
ModalDialog.prototype.getWidth = function() { return this.width; }
ModalDialog.prototype.open = function(overrideUrl) {
	// add to the list
	topWin.OpenDialogs.push(this);
	// prepare frame
	var targetFrame = this.getFrame();
	targetFrame.src = (overrideUrl) ? overrideUrl : this.url;
	this.setFrameDimensions();
	this.setFramePosition();
	// display new pane
	targetFrame.className = "loaded";
	// guard the other panes
	ModalScreen.shuffle();
	topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()].getFrame().style.zIndex = (topWin.OpenDialogs.getTopIndex() + 1);
	topWin.ModalDrag.initDragHandlers(this);
}
ModalDialog.prototype.resize = function(newSize) {
	this.height = this.getSize(newSize).height;
	this.width = this.getSize(newSize).width;
	this.setFrameDimensions();
	this.setFramePosition();
}
ModalDialog.prototype.setFrameDimensions = function() {
	var thisFrame = this.getFrame();
	thisFrame.style.width = this.getWidth();
	thisFrame.style.height = this.getHeight();
}
ModalDialog.prototype.setFramePosition = function() {
	var thisFrame = this.getFrame();
	var winWidth = (topDoc.width) ? topDoc.width : topDoc.body.clientWidth;
	var winHeight = (topDoc.height) ? topDoc.height : topDoc.body.clientHeight;
	thisFrame.style.left = (winWidth - this.getWidth()) / 2;
	thisFrame.style.top = (winHeight - this.getHeight()) / 2;
}
ModalDialog.prototype.setHeight = function(newHeight) { this.height = newHeight; }
ModalDialog.prototype.setUrl = function(newUrl) { this.url = newUrl; }
ModalDialog.prototype.setWidth = function(newWidth) { this.width = newWidth; }
ModalDialog.prototype.unload = function() {
	// prepare frame
	var thisFrame = this.getFrame();
	thisFrame.src = "";
	thisFrame.className = "unloaded";
	// remove from the list
	topWin.OpenDialogs.pop();
	// move the guard down a level
	ModalScreen.shuffle();
	topWin.ModalDrag.initDragHandlers(topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()]);
}

ModalMessage = function(thisUrl, confirmAction, cancelAction, custSize) {
	this.type = "ModalMessage";
	this.url = (thisUrl) ? thisUrl : "about:blank";
	this.confirmAction = (confirmAction) ? confirmAction : "submitWithVC();";
	this.cancelAction = (cancelAction) ? cancelAction : null;
	this.width = (custSize && custSize.width) ? this.getSize(custSize).width : 485;
	this.height = (custSize && custSize.height) ? this.getSize(custSize).height : 180;
}
ModalMessage.prototype = new ModalDialog;
ModalMessage.prototype.getSize = function(custSize) {
	var w = (custSize.width) ? custSize.width : 485;
	var h = (custSize.height) ? custSize.height : 180;
	return {width:w, height:h};
}
ModalMessage.prototype.open = function(overrideUrl) {
	// add to the list
	topWin.OpenDialogs.push(this);
	// prepare frame
	var targetFrame = this.getFrame();
	targetFrame.src = (overrideUrl) ? overrideUrl : this.url;
	this.setFrameDimensions();
	this.setFramePosition();
	// display new pane
	targetFrame.className = "loaded";
	// guard the other panes
	ModalScreen.shuffle();
	topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()].getFrame().style.zIndex = (topWin.OpenDialogs.getTopIndex() + 1);
	topWin.ModalDrag.initDragHandlers(this);
	// message-specific code
	window.openModalMessage = this;
}
ModalMessage.prototype.unload = function(returnVal) {
	// prepare frame
	var thisFrame = this.getFrame();
	thisFrame.src = "";
	thisFrame.className = "unloaded";
	// remove from the list
	topWin.OpenDialogs.pop();
	// move the guard down a level
	ModalScreen.shuffle();
	topWin.ModalDrag.initDragHandlers(topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()]);
	// message-specific code
	if (returnVal && window.openModalMessage.confirmAction) eval(window.openModalMessage.confirmAction);
	else if (window.openModalMessage.cancelAction) eval(window.openModalMessage.cancelAction);
	window.openModalMessage = null;
}

JSAlert = function() {
	this.superClass("jsalert.do?");
	this.type = "JSAlert";
	return this;
}
JSAlert.prototype = new ModalMessage;
JSAlert.prototype.superClass = ModalMessage;
JSAlert.prototype.open = function(titleKey, messageKeyArray) {
	// add to the list
	topWin.OpenDialogs.push(this);
	// prepare frame
	var targetFrame = this.getFrame();
	var alertUrl = this.url;
	alertUrl += "titleKey=" + titleKey;
	if (typeof messageKeyArray == "string") {
		alertUrl += "&message=" + messageKeyArray;
	} else {
		for (var i=0; i<messageKeyArray.length; i++) alertUrl += "&messageKeys=" + messageKeyArray[i];
	}
	targetFrame.src = alertUrl;
	this.setFrameDimensions();
	this.setFramePosition();
	// display new pane
	targetFrame.className = "loaded";
	// guard the other panes
	ModalScreen.shuffle();
	topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()].getFrame().style.zIndex = (topWin.OpenDialogs.getTopIndex() + 1);
	topWin.ModalDrag.initDragHandlers(this);
	window.openModalMessage = this;
}
jsalert = new JSAlert();

closeDialog = function(confirmVal) {
	var targetDialog = topWin.OpenDialogs[topWin.OpenDialogs.getTopIndex()];
	if (targetDialog) targetDialog.unload(confirmVal);
}
submitWithVC = function(someForm, newTargetWin, newAction) {
	if (!someForm) var someForm = document.forms[0];
	if (newTargetWin) someForm.target = newTargetWin.name;
	if (newAction) someForm.action = newAction;
	// capture target window name before form submission
	var newTarget = (someForm.target) ? someForm.target : window.name;
	someForm.submit();
	// if target is frame or frameset in main window, call the top's instance of VC (framesets don't have it)
	if (!newTargetWin) var newTargetWin = (someForm.target) ? ((topWin[someForm.target]) ? topWin[someForm.target] : topWin) : window;
	VCWin = (newTargetWin.name.indexOf(topWin.DIALOG_PREFIX) == 0) ? newTargetWin : topWin;
	VCWin.VC.on(window, INDEX);
	// if target is not current window, or parent dialog, or nested frame, close the current dialog
	// this may be buggy with nested frames in the top window
	if ((newTarget != self.name) && (newTarget != parent.name) && !window.frames[newTarget]) closeDialog();
}
linkWithVC = function(someUrl, targetWin) {
	if (!targetWin) var targetWin = window;
	targetWin.document.location.href = someUrl;
	// if target is frame or frameset in main window, call the top's instance of VC (framesets don't have it)
	VCWin = (targetWin.name.indexOf(topWin.DIALOG_PREFIX) == 0) ? targetWin : topWin;
	VCWin.VC.on(window, INDEX);
}
closeBtnOut = function(e, img) {
	if (e) e.cancelBubble = true;
	img.src = img.src.replace(/(.*)on(\.gif)/, "$1\off$2");
}
closeBtnOn = function(e, img) {
	if (e) e.cancelBubble = true;
	img.src = img.src.replace(/(.*)off(\.gif)/, "$1\on$2");
}

function setDragInit() {
	if (!previous) return;
	if (window.dragTO) clearTimeout(dragTO);
	if (document.getElementById("titlebar")) document.getElementById("titlebar").onmousedown = topWin.ModalDrag.mousedown;
	else dragTO = setTimeout("setDragInit();", 100);
}
setDragInit();

function openHelp(url) {
	window.open(url,"ppPopUp","toolbar=no,width=600,height=600,status=no,scrollbars=yes,resize=no,menubar=no");
}
