/**
 * jt_DialogBox.js - DHTML modal dialog box
 *
 * @version 23 Aug 2007
 * @author	Joseph Oster, wingo.com, Copyright (c) 2005-2007
 * @license http://www.wingo.com/dialogbox/license.html
 */
 
/* 
	NOTE:::	This is not the original version . Portions were modified and trimmed for my own purposes. 
				Please get the original version of this amazing script from the above-mentioned site. Thank you. -- REY				
*/

var jt_BodyZ = { /***** manages z-Index visibility */
	toTop: function(elm) { // adds any 'elm' to top of 'jt_BodyZ.que'
		if (!jt_BodyZ.que) {		
			jt_BodyZ.que = document.createElement("div");			
			document.body.appendChild(jt_BodyZ.que);
		}
		if (!elm.jt_BodyZZ) {
			elm.jt_BodyZZ = jt_BodyZ.nid++;
			jt_AddListener(elm, "mousedown", function() {
				jt_BodyZ.toTop(elm);
				if (jt_BodyZ.trace && (elm != jt_Trace.tDIV)) {jt_TraceObj.show(elm);}
			});
			jt_BodyZ.que.appendChild(elm);
			jt_BodyZ.set(elm);
		}
		else if (elm.jt_BodyZZ != jt_BodyZ.last.jt_BodyZZ) jt_BodyZ.set(elm);
	},
	// PRIVATE BELOW
	set: function(elm) {
		elm.style.zIndex = jt_BodyZ.nextZ++;
		jt_BodyZ.last = elm;
	},
	nid:1,
	nextZ:1
}

var jt_Veil = { /***** cooperates w/ 'jt_BodyZ', required by 'jt_DialogBox' */
	show: function(showIt) {
		jt_Veil.init();
		if (!showIt) jt_Veil.reqs.pop();
		else if (jt_BodyZ.last) jt_Veil.reqs.push(jt_BodyZ.last);
		if (jt_Veil.reqs.length > 0) {
			jt_Veil.fix();
			jt_Veil.veil.style.zIndex = jt_Veil.reqs[jt_Veil.reqs.length-1].style.zIndex-1;
			jt_BodyZ.que.insertBefore(jt_Veil.veil, jt_Veil.reqs[jt_Veil.reqs.length-1]);
		}
		jt_Veil.veil.style.display = (jt_Veil.reqs.length == 0) ? "none" : "block";
	},
	// PRIVATE BELOW
	fix: function() {
		jt_Veil.veil.style.width = jt_valPx(browserWidth()); // REY:: fix
		jt_Veil.veil.style.height = jt_valPx(browserHeight()); // REY:: fix
		//setTimeout('jt_Veil.fix()',150);
	},
	init: function() {
		if (typeof jt_Veil.veil == "undefined") { // once per page
			jt_Veil.reqs = [];
			jt_Veil.veil = document.createElement('div');						
			for (var prop in jt_Veil.css) jt_Veil.veil.style[prop] = jt_Veil.css[prop];
			jt_Veil.veil.innerHTML = "&nbsp;";
			if (jt_BodyZ.que) jt_BodyZ.que.insertBefore(jt_Veil.veil, jt_BodyZ.que.firstChild);
			else jt_BodyZ.toTop(jt_Veil.veil);
			jt_AddListener(window, "resize", jt_Veil.fix);
		}
	},
	css: {position:"absolute", display:"none", top:"0px", left:"0px", cursor:"not-allowed", backgroundColor:"#000000", filter:"alpha(opacity=20)", opacity:0.2}
}

var BrowserDetect = { // from http://www.quirksmode.org/js/detect.html
	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.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.userAgent, subString: "Netscape", identity: "Netscape"},	// for newer Netscapes (6+)
		{string: navigator.userAgent, subString: "MSIE", identity: "Explorer", versionSearch: "MSIE"},
		{string: navigator.userAgent, subString: "Gecko", identity: "Mozilla", versionSearch: "rv"},
		{string: navigator.userAgent, subString: "Mozilla", identity: "Netscape", versionSearch: "Mozilla"} // for older Netscapes (4-)
	],
	dataOS : [
		{string: navigator.platform, subString: "Win", identity: "Windows"},
		{string: navigator.platform, subString: "Mac", identity: "Mac"},
		{string: navigator.platform, subString: "Linux", identity: "Linux"}
	]
};

/**************/

function jt_AddListener(obj, evType, fn) {
	if (obj.addEventListener) {obj.addEventListener(evType, fn, false);}
	else if (obj.attachEvent) {obj.attachEvent('on' + evType, fn);}
}

function jt_RemListener(obj, evType, fn) {
	if (obj.removeEventListener) {obj.removeEventListener(evType, fn, false);}
	else if (obj.detachEvent) {obj.detachEvent('on' + evType, fn);}
}
	
function jt_divOnScrn(divOnScrn,clientHeight) {
	var divPos = jt_getOffsetXY(divOnScrn);
	var newX = divPos.x;
	var newY = divPos.y;
	//*** REY ***
	var xoffset=jt_DialogBox.shadowOffset+2;
	var divWidth=divOnScrn.offsetWidth+xoffset;
	var divHeight=divOnScrn.offsetHeight+xoffset;	
	if (typeof clientHeight=="undefined") clientHeight=document.body.clientHeight;
	//**********
	if (divPos.x + divWidth - document.body.scrollLeft > document.body.clientWidth) newX = document.body.scrollLeft + document.body.clientWidth - divWidth;
	if (divPos.x < document.body.scrollLeft) newX = document.body.scrollLeft;
	// next line modified by REY
	// orginal: newY = document.body.scrollTop + clientHeight - divHeight;
	if (divPos.y + divHeight - document.body.scrollTop > clientHeight)  newY = document.body.scrollTop + divHeight;
	if (divPos.y < document.body.scrollTop) newY = document.body.scrollTop;		
	if ((newX!==divPos.x) || (newY!==divPos.y)) jt_moveTo(divOnScrn,newX,newY);
}
	
function jt_getOffsetXY(obj, findID, point) {
	// returns 'jt_Point' object with '.x' and '.y' offsets of 'obj' relative to page
	// or relative to optional 'findID', if 'findID' is found as a parent
	if (point) { // optional 'point' is re-used when valid; good practice when dragging
		point.x = obj.offsetLeft;
		point.y = obj.offsetTop;
	}
	else {point = new jt_Point(obj.offsetLeft, obj.offsetTop);}
	var parent = obj.offsetParent;
	while (parent !== null) {
		if (findID && (parent.id == findID)) break;
		point.x += parent.offsetLeft;
		point.y += parent.offsetTop;
		parent = parent.offsetParent;
	}
	return point;
}
	
function jt_Point(x, y) {
	// returns a "Point" object with '.x' and '.y' properties
	this.x = x;
	this.y = y;
}
	
function jt_fixE(ev) {
	var e = ev ? ev : window.event;
	return e;
}

function jt_moveTo(obj, x, y) {
	obj.style.left = jt_valPx(x);
	obj.style.top = jt_valPx(y);
}

function jt_scrollTop() {
	if (window.pageYOffset) {return window.pageYOffset;}
	else if (document.documentElement && (document.documentElement.scrollTop > 0)) {return document.documentElement.scrollTop;}
	else {return document.body.scrollTop;}
}

function jt_winW() {
	if (document.documentElement && (document.documentElement.clientWidth > 0)) {return document.documentElement.clientWidth;}
	else if (window.innerWidth) {return window.innerWidth;}
	else {return document.body.clientWidth;}
}

function jt_winH() {
	if (window.innerHeight) {return window.innerHeight;}
	else if (document.documentElement && (document.documentElement.clientHeight > 0)) {return document.documentElement.clientHeight;}
	else {return document.body.clientHeight;}
}

function jt_valPx(pixels) {
	return pixels + "px";
}

function jt_currStyle(divToRead) { // return current (derived) CSS style object
	var cs = divToRead.style;
	if (window.getComputedStyle) {cs = window.getComputedStyle(divToRead,null);}
	else if (divToRead.currentStyle) {cs = divToRead.currentStyle;}
	return cs;
}

function jt_width(divToRead, currStyle) {
	var wide = currStyle ? currStyle.width : jt_currStyle(divToRead).width;
	return (wide = 'auto') ? divToRead.offsetWidth : parseInt(wide);
}

function jt_height(divToRead, currStyle) {
	var high = currStyle ? currStyle.height : jt_currStyle(divToRead).height;
	return (high = 'auto') ? divToRead.offsetHeight : parseInt(high);
}

function jt_ShowHideElm(elm, showIt) {
	if (elm) {elm.style.visibility = (showIt) ? "visible" : "hidden";}
}
	
BrowserDetect.init();
var jt_FixIE6 = (BrowserDetect.browser == 'Explorer') && (BrowserDetect.version < 7);
var jt_SelectTags = {
	hide: function() {
		if (jt_FixIE6) {
			// REY: you may want to refresh .sels for every call
			if (!jt_SelectTags.sels) jt_SelectTags.sels = document.body.getElementsByTagName('select');
			for (var i=0; i<jt_SelectTags.sels.length; i++) jt_ShowHideElm(jt_SelectTags.sels[i]);
		}
	},
	show: function() {
		if (jt_FixIE6 && jt_SelectTags.sels) {
			for (var i=0; i<jt_SelectTags.sels.length; i++) jt_ShowHideElm(jt_SelectTags.sels[i], true);
		}
	}
}
	
// added by REY
function browserWidth() {
	return Math.max(document.body.scrollWidth,jt_winW());
}

// added by REY
function browserHeight() {
	return Math.max(document.body.scrollHeight,jt_winH());
}

// added by REY
function getBrowserSize() {
	var bodyWidth=document.documentElement.clientWidth;
	var bodyHeight=document.documentElement.clientHeight;
	//var bodyWidth,bodyHeight; 
	if (self.innerHeight){ // all except Explorer 		 
		bodyWidth=self.innerWidth; 
		bodyHeight=self.innerHeight; 
	}  
	else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode 		 
		bodyWidth=document.documentElement.clientWidth; 
		bodyHeight=document.documentElement.clientHeight; 
	} else if (document.body) {// other Explorers 		 
		bodyWidth = document.body.clientWidth; 
		bodyHeight = document.body.clientHeight; 
	} 
	return [bodyWidth,bodyHeight];
}

/**************/

jt_DialogBox = function(isModal) {
	// CONSTRUCTOR for 'jt_DialogBox' object
	if (arguments.length==0) return;
	this.isModal=isModal;
	this.container=document.createElement('div');	
	this.container.className=jt_DialogBox.className;
	this.container.dialogBox=this;
	var mainTable=document.createElement('table');
	// note:: IE6 requires attributes to be camelized
	mainTable.setAttribute('cellSpacing','0');
	mainTable.setAttribute('cellPadding','0');
	mainTable.setAttribute('border','0');	
	var tBodyM = document.createElement('tbody');
	//*** begin title row ***
	var rowM = document.createElement('tr');
	var cellM = document.createElement('td');	
	var titleTable = document.createElement('table');
	titleTable.setAttribute('cellSpacing','0');
	titleTable.setAttribute('cellPadding','0');
	titleTable.setAttribute('border','0');
	titleTable.setAttribute('width','100%');
	var tBodyT = document.createElement('tbody');
	var rowT = document.createElement('tr');
	var cellT = document.createElement('td');
	cellT.className = "tbLeft";        
	rowT.appendChild(cellT);
	this.titleCell = document.createElement('td');
	this.titleCell.className = "tbTitle";
	rowT.appendChild(this.titleCell);
	cellT = document.createElement('td');
	cellT.className = "tbRight";
	jt_DialogBox.initCloseIcon();
	var closeIcon = document.createElement('img');
	closeIcon.src = jt_DialogBox.closeIcon.src;
	closeIcon.setAttribute('border','0');
	closeIcon.dialogBox = this;
	var aLink = document.createElement('A');
	aLink.setAttribute('href','#');
	aLink.appendChild(closeIcon);
	aLink.onclick = jt_DialogBox.closeBox;
	cellT.appendChild(aLink);
	rowT.appendChild(cellT);
	tBodyT.appendChild(rowT);
	titleTable.appendChild(tBodyT);	
	cellM.appendChild(titleTable);
	rowM.appendChild(cellM);
	tBodyM.appendChild(rowM);
	//*** end title row ***
	//*** begin content row ***
	rowM = document.createElement('tr');
	cellM = document.createElement('td');
	cellM.className = "MainPanel";
	this.contentArea = document.createElement('div');
	this.contentArea.className = "ContentArea";
	cellM.appendChild(this.contentArea);
	rowM.appendChild(cellM);
	tBodyM.appendChild(rowM);
	//*** end content row ***
	mainTable.appendChild(tBodyM);
	this.container.appendChild(mainTable);	
	//******** Shadow::: added by REY ********	
	this.shadowOffset=jt_DialogBox.shadowOffset;
	this.shadow=document.createElement('div');
	this.shadow.className='modalDialog_shadow ';
	this.container.appendChild(this.shadow);
	//******** End shadow::: added by REY ********	
	document.body.appendChild(this.container);	
}

/************ BEGIN: Public Methods ************/
jt_DialogBox.imagePath = "/images/"; // set by application; directory path to 'window_close.gif' in titleBar

// added by REY
jt_DialogBox.prototype.fixShadow = function() {	
	this.shadow.style.left=this.shadowOffset+"px";
	this.shadow.style.top=this.shadowOffset+"px";		
	this.shadow.style.height=this.container.offsetHeight+"px";
	this.shadow.style.width=this.container.offsetWidth+"px";
	this.shadow.style.display="block";
}

jt_DialogBox.prototype.show = function() {
	//WARNING:: container.offsetHeight & container.offsetWidth does not return actual/real values on first call
  var clientHeight=document.body.clientHeight; // value varies for each call to jt_divOnScrn() so save it	
	jt_BodyZ.toTop(this.container);
	// cover entire scroll area. jt_divOnScrn() will reposition dialog within visible area
	if (this.isModal) jt_Veil.show(true); 
	this.container.style.display="block";	
	jt_divOnScrn(this.container,clientHeight); // call before shadow
	/*******/	
	this.fixShadow();
	// make sure shadow is properly displayed
	window.thisObj=this;
	// this.container.offsetWidth() and offsetHeight() values change after a while, so redisplay shadow later, 150 seems optimal
	setTimeout('window.thisObj.fixShadow()',150); 
	var maxX=browserWidth()-this.container.offsetWidth-jt_DialogBox.shadowOffset-1;
	var maxY=browserHeight()-this.container.offsetHeight-jt_DialogBox.shadowOffset-1;	
	// o,oRoot,minX,maxX,minY,maxY,bSwapHorzRef,bSwapVertRef,fXMapper,fYMapper
	Drag.init(this.titleCell,this.container,0,maxX,0,maxY);	
	// hide underlying select tags before showing dialog
	jt_SelectTags.hide();
	if (this.selectElement) this.selectElement.style.visibility="visible";
	// buttons must be immediately visible
	if (this.buttonDIV) {
		try {
			this.buttonDIV.firstChild.focus();
			this.buttonDIV.firstChild.blur();
		}
		catch(e) {}
	}
	/*******/	
}

jt_DialogBox.prototype.hide = function(ok) {
	this.container.style.display="none";	
	/*********/
	this.shadow.style.display="none";	
	/*********/
	if (this.isModal) jt_Veil.show(false);
	if (ok) {
		if (this.callOK)
			if (this.returnData) this.callOK(this.returnData);
			else this.callOK();
	}
	else if (this.callCancel) this.callCancel();
	jt_SelectTags.show();
}

jt_DialogBox.prototype.moveTo = function(x, y) {
	var aBrowserSize=getBrowserSize();
	if (x == -1) x = Math.round((aBrowserSize[0] - this.container.offsetWidth) / 2);
	if (y == -1) y = Math.round((aBrowserSize[1] - this.container.offsetHeight) / 2) + jt_scrollTop();
	this.container.style.left = x + "px";
	this.container.style.top = y + "px";
}

jt_DialogBox.prototype.setTitle = function(title) {
	this.titleCell.innerHTML = title;
}

jt_DialogBox.prototype.setUrl = function(url, height) {
	// creates one IFRAME above 'setContent()' area, updates 'url'
	if (!this._jtDialogBIF) {
		this._jtDialogBIF=document.createElement('IFRAME');
		this._jtDialogBIF.setAttribute('frameBorder', 'no');
		this._jtDialogBIF.style.width = "100%";
		if (height) this._jtDialogBIF.style.height = height;
		this.contentArea.parentNode.insertBefore(this._jtDialogBIF, this.contentArea);
	}
	this._jtDialogBIF.src=url;
}

jt_DialogBox.prototype.getUrl = function() {
	if (this._jtDialogBIF) {
		//jt_TraceObj.show(this._jtDialogBIF);
		var url = this._jtDialogBIF.src;
		if (this._jtDialogBIF.contentWindow) {
			try {url = this._jtDialogBIF.contentWindow.location.href;}
			catch(e) {}
		}
		return url;
	}
}

jt_DialogBox.prototype.setContent = function(htmlContent) {
	this.contentArea.innerHTML = htmlContent;
}

jt_DialogBox.prototype.setWidth = function(width) {
	this.contentArea.style.width = width + "px";
}

jt_DialogBox.prototype.setCallOK = function(callOK) {
	// set by application as needed
	this.callOK = callOK;
}

jt_DialogBox.prototype.setCallCancel = function(callCancel) {
	// set by application as needed
	this.callCancel = callCancel;
}

jt_DialogBox.prototype.getContentNode = function() {
	// expose 'contentArea' DOM node for direct manipulation
	return this.contentArea;
}
/************ END: Public Methods ************/

/************ BEGIN: Private Methods ************/
jt_DialogBox.className = "jtDialogBox"; // CSS className
jt_DialogBox.closeIcon = null;
jt_DialogBox.maxDepth = 5; // optimize search of parent nodes
jt_DialogBox.shadowOffset = 4;

jt_DialogBox.initCloseIcon = function() {
	// pre-fetch this icon so it doesn't distort dialog box size
	if (jt_DialogBox.closeIcon == null) {
		jt_DialogBox.closeIcon = new Image();
		jt_DialogBox.closeIcon.src = jt_DialogBox.imagePath + "window_close.gif";
	}
}

jt_DialogBox.closeBox = function(e) {	
	e=jt_fixE(e);
	var node = e.target ? e.target : e.srcElement;
	var count = 0;
	while ((node != null) && (count < jt_DialogBox.maxDepth)) {
		if (node.dialogBox) {
			node.dialogBox.hide();
			return false;
		}
		node = node.parentNode;
		count++;
	}
	return false;
}

/**************/

jt_AppAlert = function(icon) {
	// CONSTRUCTOR for 'jt_AppAlert' object - EXTENDS 'jt_DialogBox'
	if (arguments.length==0) return;
	this.base=jt_DialogBox;
	this.base(true);
	var dialogTable=document.createElement('table');
	// note:: IE6 requires attributes to be camelized
	dialogTable.setAttribute('cellSpacing','0');
	dialogTable.setAttribute('cellPadding','0');
	dialogTable.setAttribute('border','0');	
	var tBody=document.createElement('tbody');
	var row=document.createElement('tr');
	var cell=document.createElement('td');
	cell.setAttribute("vAlign","top");
	if (icon) {
		this.iconImage=document.createElement('img');
		this.iconImage.style.margin="0px 10px 0px 0px";
		this.setIcon(icon);
		cell.appendChild(this.iconImage);
	}
	row.appendChild(cell);
	this.contentCell=document.createElement('td');
	this.contentCell.className = "ContentArea";
	row.appendChild(this.contentCell);
	tBody.appendChild(row);
	dialogTable.appendChild(tBody);
	this.contentArea.appendChild(dialogTable);
	this.buttonDIV=document.createElement('div');
	this.buttonDIV.setAttribute("align","center");
	this.buttonDIV.style.margin="10px 0px 0px 0px";	
	this.buttonDIV.style.whiteSpace="nowrap"; // REY	
	this.contentArea.appendChild(this.buttonDIV);	
	jt_AppAlert.addButton(this, jt_AppAlert.lblOK, 1);
}

jt_AppAlert.prototype = new jt_DialogBox();

/************ BEGIN: 'jt_AppAlert' Public Methods ************/
jt_AppAlert.Warning = "warning.gif"; 	// 'icon' param to 'jt_AppAlert' constructor
jt_AppAlert.Error = "error.gif";     	// 'icon' param to 'jt_AppAlert' constructor
jt_AppAlert.Info = "info.gif";       	// 'icon' param to 'jt_AppAlert' constructor
jt_AppAlert.lblOK = "OK"; 						// label for "OK" button (for i18N)
jt_AppAlert.lblCancel = "Cancel"; 		// label for "Cancel" button (for i18N)

jt_AppAlert.prototype.setContent = function(htmlContent) {
	this.contentCell.innerHTML = htmlContent;
}

jt_AppAlert.prototype.setIcon = function(icon) {
	this.iconImage.src = jt_DialogBox.imagePath + icon;
}
/************ END: 'jt_AppAlert' Public Methods ************/

/************ BEGIN: 'jt_AppAlert' Private Methods ************/
jt_AppAlert.addButton = function(parent, buttonText, buttonNum) {
	var button = document.createElement("button");
	// REY::: button style moved to dialogbox.css
	button.innerHTML = buttonText;
	button.linkNum = buttonNum;
	button.appDialog = parent;
	button.onclick = jt_AppAlert.clickLink;
	parent.buttonDIV.appendChild(button);
}

jt_AppAlert.clickLink = function(e) {		
	e = jt_fixE(e); // REY:: fix for IE6
	var node = e.target?e.target:e.srcElement;
	var linkNum = node.linkNum;
	var count = 0;
	while ((node != null) && (count < jt_DialogBox.maxDepth)) {
		if (node.appDialog) {
			switch (linkNum) {
				case 1: {
					node.appDialog.hide(true);
					break;
				}
				case 2: {
					node.appDialog.hide();
					break;
				}
			}
			return false;
		}
		node = node.parentNode;
		count++;
	}
	return false;
}

jt_AppAlert.prototype.trace = function() {
	alert(objToString(this.contentArea));
}

/*************/

/**************/

jt_AppConfirm = function(icon, callOK, callCancel) {
  // CONSTRUCTOR for 'jt_AppConfirm' object - EXTENDS 'jt_AppAlert'
  if (arguments.length==0) return;
  this.base = jt_AppAlert;
  this.base(icon);
  this.callOK = callOK;
  this.callCancel = callCancel;
  jt_AppAlert.addButton(this, jt_AppAlert.lblCancel, 2);
}

jt_AppConfirm.prototype = new jt_AppAlert();

/************ BEGIN: 'jt_AppConfirm' Public Methods ************/
jt_AppConfirm.prototype.askUser = function(htmlContent) {
  this.setContent(htmlContent);
  this.show();
}
/************ END: 'jt_AppConfirm' Public Methods ************/

jt_AppPrompt = function(icon, callOK, callCancel, htmlContent) {
  // CONSTRUCTOR for 'jt_AppPrompt' object - EXTENDS 'jt_AppConfirm'
  if (arguments.length==0) return;
  this.base=jt_AppConfirm;
  this.base(icon,callOK,callCancel);
  this.returnData=new Object();	
  this.fInput=document.createElement('input');
	this.fInput.type="text";  
	this.setContent(htmlContent);
	this.contentCell.appendChild(this.fInput);  
  this.fInput.appDialog=this;
  this.fInput.onkeypress=jt_AppPrompt.keyPress;	
}

jt_AppPrompt.prototype = new jt_AppConfirm();
jt_AppPrompt.superClass = jt_AppConfirm.prototype;

/************ BEGIN: 'jt_AppPrompt' Public Methods ************/
jt_AppPrompt.prototype.askUser = function(stDefault) {	
	this.fInput.value=stDefault;	
	this.show();
	this.fInput.focus();
}

jt_AppPrompt.prototype.focus = function() {
	this.fInput.focus();
}

jt_AppPrompt.prototype.hide = function(ok) {
	if (ok) this.returnData.value=this.fInput.value;
	jt_AppPrompt.superClass.hide.call(this,ok);
}

// this function is shared with selectPrompt()
jt_AppPrompt.keyPress = function(e) {	
	e = jt_fixE(e);
	var node = e.target ? e.target : e.srcElement;
	var key = e.keyCode ? e.keyCode : e.which;	
	if (key==13) node.appDialog.hide(true);
	if (key==27) node.appDialog.hide();	
}

/************ END: 'jt_AppPrompt' Public Methods ************/

selectPrompt = function(icon, callOK, callCancel, htmlSelection, selectID, htmlContent) {
  if (arguments.length==0) return;
  this.base=jt_AppConfirm;
  this.base(icon,callOK,callCancel);
  this.returnData=new Object();	
	this.setContent(htmlContent);	
  this.selectionDiv=document.createElement('div');
	this.selectionDiv.innerHTML=htmlSelection;	
	this.contentCell.appendChild(this.selectionDiv);		
	this.selectElement=document.getElementById(selectID);	
	this.selectElement.appDialog=this;	
	this.selectElement.onkeypress=jt_AppPrompt.keyPress;
}

selectPrompt.prototype = new jt_AppConfirm();
selectPrompt.superClass = jt_AppConfirm.prototype;

selectPrompt.prototype.askUser = function(stDefault) {	
	if (this.selectElement.value!=stDefault) {
		for (var i=0; i<this.selectElement.options.length; i++) {
			if (this.selectElement.options[i].value==stDefault) {
				this.selectElement.selectedIndex=i;
				break;
			}
		}
	}		
	this.show();	
	this.selectElement.focus();
}

selectPrompt.prototype.focus = function(stDefault) {
	this.selectElement.focus();
}

selectPrompt.prototype.hide = function(ok) {
	if (ok) this.returnData.value=this.selectElement.value;	
	selectPrompt.superClass.hide.call(this, ok);
}

/**************/

var alertDialog = null;
var alertX = -1; // centers dialog in window!
var alertY = -1;
var i18N_MyAppAlert = "USA Grade Entry System"; // can be changed by application

function MyAppAlertSetXY(x,y) {
	// call prior to 'MyAppAlert()'
	alertX = x;
	alertY = y;
}

function MyAppAlertXY(ev) {
	// call using 'onclick' prior to 'MyAppAlert()'
	var e = jt_fixE(ev);
	MyAppAlertSetXY(e.clientX, e.clientY + jt_scrollTop());	
	return true;
}

function MyAppAlertInit(title, icon) {
	// 'title' and 'icon' are optional; both may be changed using 'MyAppAlert()'
	var locTitle = i18N_MyAppAlert;
	if (title) locTitle += " - " + title;
	if (alertDialog == null) {
		alertDialog = new jt_AppAlert(icon ? icon : jt_AppAlert.Error);
		alertDialog.setTitle(locTitle);
	}
	else {
		if (title) alertDialog.setTitle(locTitle);
		if (icon) alertDialog.setIcon(icon);
	}
}

function MyAppAlert(msg,title,icon) {
	// 'title' and 'icon' are optional
	MyAppAlertInit(title,icon);	
	alertDialog.setContent(msg);	
	alertDialog.show();		
	//if ((alertX == -1) || (alertY == -1)) 
	alertDialog.moveTo(alertX, alertY);	
}
