
var jsAlert = window.jsAlert || new jscAlert();

function jscAlertTypes()
{
	this.OK			= 1;
	this.OKCancel	= 2;
	this.YesNo		= 3;
	this.Input		= 4;
}

function jscAlertResults()
{
	this.OK			= 1;
	this.Cancel		= 2;
	this.Yes		= 3;
	this.No			= 4;
}

function jscAlert()
{
	this.Types = new jscAlertTypes();
	this.Results = new jscAlertResults();

	this.Result	= null;
	this.Value	= null;
	this.Eval	= null;
	this.Type	= null;

	// preload the window's close button images
	this.CloseOver = new Image();
	this.CloseOver.src = "images/close_over.gif";
	this.CloseDown = new Image();
	this.CloseDown.src = "images/close_down.gif";

	// Capture Esc key press and close alert
	document.addevent("onkeydown",
		function(e) {
			try {
				var div = jsGetObj("CustomAlertDiv");
				if( div != null && div.style.visibility == "visible" )
				{
					// Enter Key
					if( e.keyCode == 13 ) {
						if( jsAlert.Type != jsAlert.Types.YesNo )
							jsAlert.Close(jsAlert.Results.OK);
						else 
							jsAlert.Close(jsAlert.Results.Yes);
						return false;
					}
					// Esc Key
					else if( e.keyCode == 27 )
						return jsAlert.Close(jsAlert.Results.Cancel);
					else if( jsAlert.Type != jsAlert.Types.Input ) {
						// O Key
						if( (e.keyCode == 0x4F || e.keyCode == 0x6F) && jsAlert.Type != jsAlert.Types.YesNo )
							return jsAlert.Close(jsAlert.Results.OK);
						// C Key
						else if( (e.keyCode == 0x43 || e.keyCode == 0x63) && jsAlert.Type != jsAlert.Types.YesNo )
							return jsAlert.Close(jsAlert.Results.Cancel);
						// Y Key
						else if( (e.keyCode == 0x59 || e.keyCode == 0x79) && jsAlert.Type == jsAlert.Types.YesNo )
							return jsAlert.Close(jsAlert.Results.Yes);
						// N Key
						else if( (e.keyCode == 0x4E || e.keyCode == 0x6E) && jsAlert.Type == jsAlert.Types.YesNo )
							return jsAlert.Close(jsAlert.Results.No);
					}
				}
			} catch(ex){}
		});

	// Resizes the alert masks on window resize event
	window.addevent("onresize", 
		function() {
			var mask = jsGetObj("CustomAlertMask");
			if(mask != null && mask.style.visibility == "visible") {
				mask.style.left = "0px";
				mask.style.top = "0px";
				mask.style.width = document.body.offsetWidth + "px";
				mask.style.height = document.body.offsetHeight + "px";
			}
			var iFrame = jsGetObj("CustomAlertIFrame");
			if(iFrame != null && iFrame.style.visibility == "visible") {
				iFrame.style.left = "0px";
				iFrame.style.top = "0px";
				iFrame.style.width = document.body.offsetWidth + "px";
				iFrame.style.height = document.body.offsetHeight + "px";
				iFrame.style.visibility = "visible";
			}
		});

	/// Show("Message")
	/// Show("Message", jsAlert.Types)
	/// Show("Message", jsAlert.Types, "Initial input Value")
	/// Show("Message", "Title")
	/// Show("Message", "Title", jsAlert.Types)
	/// Show("Message", "Title", jsAlert.Types, "Initial input Value")
	/// Show("Message", "Title", "Eval")
	/// Show("Message", "Title", "Eval", jsAlert.Types)
	/// Show("Message", "Title", "Eval", jsAlert.Types, "Initial input Value")
	this.Show = function(message) {
		this.AlertResult = null;
		
		try {
			this.CreateDivs();
			
			this.ShowMask();
			
			// Set the message
			if(message == null || message == "") message = "&nbsp;";
			var alertMessage = jsGetObj("CustomAlertContents");
			while(alertMessage.childNodes.length > 0)
				alertMessage.removeChild(alertMessage.firstChild);
			alertMessage.innerHTML = message.toString();
			
			var legend = "Alert";
			var inputValue = "";
			var type = this.Types.OK;
			
			if(arguments.length > 1) {
				if(typeof(arguments[1]) == "string" || arguments[1] == null) {
					if(arguments[1] != null)
						legend = arguments[1];
					
					if(arguments.length > 2) {
						if(typeof(arguments[2]) == "string" || arguments[2] == null) {
							this.Eval = arguments[2];
							
							if(arguments.length > 3) {
								type = arguments[3];
								
								if(arguments.length > 4 && typeof(arguments[4]) == "string")
									inputValue = arguments[4];
							}
						}
						else {
							type = arguments[2];
							
							if(arguments.length > 3 && typeof(arguments[3]) == "string")
								inputValue = arguments[3];
						}
					}
				}
				else {
					type = arguments[1];
					
					if(arguments.length > 2 && typeof(arguments[2]) == "string")
						inputValue = arguments[2];
				}
			}
			
			// Set the alert title, input default and show it
			jsGetObj("CustomAlertTitle").innerHTML = legend;
			jsGetObj("CustomInputValue").value = inputValue;
			this.SetTypeAndShow(type);
		}
		catch(e) {
			this.HideMask();
			throw e;
		}
	}

	this.SetTypeAndShow = function(type) {
		jsGetObj("CustomAlertButtonYes").style.position = "absolute";
		jsGetObj("CustomAlertButtonYes").style.visibility = "hidden";
		
		jsGetObj("CustomAlertButtonNo").style.position = "absolute";
		jsGetObj("CustomAlertButtonNo").style.visibility = "hidden";
		
		jsGetObj("CustomAlertButtonOK").style.position = "absolute";
		jsGetObj("CustomAlertButtonOK").style.visibility = "hidden";
		
		jsGetObj("CustomAlertButtonCancel").style.position = "absolute";
		jsGetObj("CustomAlertButtonCancel").style.visibility = "hidden";
		
		jsGetObj("CustomInputValue").style.position = "absolute";
		jsGetObj("CustomInputValue").style.visibility = "hidden";
		
		jsGetObj("CustomAlertContents").style.textAlign = "center";
		
		this.Type = type;
		switch(type) {
			case this.Types.Input: {
				jsGetObj("CustomAlertContents").style.textAlign = "left";
		
				jsGetObj("CustomInputValue").style.position = "";
				jsGetObj("CustomInputValue").style.visibility = "";
			}
			case this.Types.OKCancel: {
				jsGetObj("CustomAlertButtonOK").style.position = "";
				jsGetObj("CustomAlertButtonOK").style.visibility = "";
				
				jsGetObj("CustomAlertButtonCancel").style.position = "";
				jsGetObj("CustomAlertButtonCancel").style.visibility = "";
				
				this.ShowAlert();
				if(type == this.Types.Input)
					jsGetObj("CustomInputValue").focus();
				else
					jsGetObj("CustomAlertButtonOK").focus();
			} break;
			case this.Types.YesNo: {
				jsGetObj("CustomAlertButtonYes").style.position = "";
				jsGetObj("CustomAlertButtonYes").style.visibility = "";
				
				jsGetObj("CustomAlertButtonNo").style.position = "";
				jsGetObj("CustomAlertButtonNo").style.visibility = "";
				
				this.ShowAlert();
				jsGetObj("CustomAlertButtonYes").focus();
			} break;
			default:
			case this.Types.OK: {
				this.Type = this.Types.OK;
				
				jsGetObj("CustomAlertButtonOK").style.position = "";
				jsGetObj("CustomAlertButtonOK").style.visibility = "";
				
				this.ShowAlert();
				jsGetObj("CustomAlertButtonOK").focus();
			} break;
		}
	}

	this.ShowAlert = function() {
		var alertDiv = jsGetObj("CustomAlertDiv");
		alertDiv.style.visibility = "visible";
		alertDiv.style.width = "";
		alertDiv.style.height = "";
		
		// if the div is wider than the window
		if(alertDiv.firstChild.offsetWidth > document.body.offsetWidth) {
			// Some widths that look nice
			if(document.body.offsetWidth < 265)
				alertDiv.style.width = "212px";
			else if(document.body.offsetWidth > 1000)
				alertDiv.style.width = "800px";
			else // for large screen resolutions
				alertDiv.style.width = parseInt(document.body.offsetWidth * .8) + "px";
		}
		else
			alertDiv.style.width = alertDiv.firstChild.offsetWidth + "px";
		alertDiv.style.height = alertDiv.firstChild.offsetHeight + "px";
		this.Center(alertDiv);
	}

	this.Close = function(alertMsg) {
		var alertDiv = jsGetObj("CustomAlertDiv");
		alertDiv.style.visibility = "hidden";
		alertDiv.style.top = "-10000px";
		alertDiv.style.width = "";
		alertDiv.style.height = "";
		
		this.HideMask();
		
		this.Result = alertMsg;
		this.Value = jsGetObj("CustomInputValue").value;
		if(this.Eval != null) {
			var amsEval = this.Eval;
			this.Eval = null;
			eval(amsEval);
		}
		
		return false;
	}

	this.ShowMask = function() {
		var mask = jsGetObj("CustomAlertMask");
		if (isWindowsIE) {
			mask.style.backgroundColor = "White";
			mask.style.filter = "Alpha(Opacity=1)";
		}
		mask.style.left = "0px";
		mask.style.top = "0px";
		mask.style.width = document.body.offsetWidth + "px";
		mask.style.height = document.body.offsetHeight + "px";
		mask.style.visibility = "visible";
		
		var iFrame = jsGetObj("CustomAlertIFrame");
		iFrame.style.left = "0px";
		iFrame.style.top = "0px";
		iFrame.style.width = document.body.offsetWidth + "px";
		iFrame.style.height = document.body.offsetHeight + "px";
		iFrame.style.visibility = "visible";
	}

	this.HideMask = function() {
		var mask = jsGetObj("CustomAlertMask");
		mask.style.backgroundColor = "";
		mask.style.filter = "";
		mask.style.width = "1px";
		mask.style.height = "1px";
		mask.style.visibility = "hidden";
		
		var iFrame = jsGetObj("CustomAlertIFrame");
		iFrame.style.width = "1px";
		iFrame.style.height = "1px";
		iFrame.style.visibility = "hidden";
	}
	
	this.Center = function() {
		var div = jsGetObj("CustomAlertDiv");
		var centerX = document.body.clientWidth / 2 + document.body.scrollLeft;
		var centerY = document.body.clientHeight / 2 + document.body.scrollTop;
		for(var i=0; i<div.childNodes.length; i++)
			if(div.childNodes[i].tagName != null && div.childNodes[i].tagName.toLowerCase() == "table")
			{
				centerX -= div.childNodes[i].offsetWidth / 2;
				centerY -= div.childNodes[i].offsetHeight / 2;
				break;
			}
		if(centerY < document.body.scrollTop) centerY = document.body.scrollTop;
		div.style.left = centerX + "px";
		div.style.top = centerY + "px";
	}
	
	this.CreateDivs = function() {
		// An iframe that goes behind the mask for 
		// IE6 to hide any <select> on the page.
		//     If you have just the iframe and no 
		// div on top of it, it messes with the 
		// window drag feature.
		if(jsGetObj("CustomAlertIFrame") == null) {
			var iFrame = document.createElement("IFRAME");
			iFrame.id = "CustomAlertIFrame";
			iFrame.frameBorder = 0;
			iFrame.style.left = "0px";
			iFrame.style.top = "0px";
			iFrame.style.width = "1px";
			iFrame.style.height = "1px";
			iFrame.style.position = "absolute";
			iFrame.style.visibility = "hidden";
			iFrame.style.filter = "Alpha(Opacity=0)";
			iFrame.style.zIndex = 98;
			document.body.insertBefore(iFrame, document.body.firstChild);
		}
		// The div mask that goes on top of the iframe
		if(jsGetObj("CustomAlertMask") == null) {
			var mask = document.createElement("DIV");
			mask.id = "CustomAlertMask";
			mask.style.left = "0px";
			mask.style.top = "0px";
			mask.style.width = "1px";
			mask.style.height = "1px";
			mask.style.position = "absolute";
			mask.style.visibility = "hidden";
			mask.style.zIndex = 99;
			document.body.insertBefore(mask, document.body.firstChild);
		}
		// The actual alert
		if(jsGetObj("CustomAlertDiv") == null) {
			var div = document.createElement("DIV");
			div.id = "CustomAlertDiv";
			div.style.left = "0px";
			div.style.top = "0px";
			div.style.width = "1px";
			div.style.height = "1px";
			div.style.position = "absolute";
			div.style.visibility = "hidden";
			div.style.display = "inline";
			div.style.zIndex = 100;
			document.body.insertBefore(div, document.body.firstChild);
			
			div.innerHTML =
				'<table cellpadding="0" cellspacing="0" bgcolor="#F0F0F0" style="border-left: #B4B4B4 1px solid; border-top: #B4B4B4 1px solid; border-right: #787878 1px solid; border-bottom: #787878 1px solid">' +
				'	<tr>' +
				'		<td></td>' +
				'		<td style="cursor: default"><table style="WIDTH: 100%" cellpadding="0" cellspacing="0" width="100%" border="0">' +
				'				<tr class="styleTableHeader">' +
				'					<td id="CustomAlertTitle" align="left" width="100%" onmousedown="MoveObj(event,this);return(false);">BarAlliance</td>' +
				'					<td style="padding-right: 3px" align="right"><img src="images/close.gif" width="17" height="17" hspace="1" onclick="jsAlert.Close(jsAlert.Results.Cancel);" onmouseover="this.src=\'images/close_over.gif\';" onmousedown="this.src=\'images/close_down.gif\';return(false);" ondrag="return(false);" onmouseout="this.src=\'images/close.gif\';"></td>' +
				'				</tr>' +
				'			</table></td>' +
				'	</tr>' +
				'	<tr>' +
				'		<td></td>' +
				'		<td><img src="images/blank.gif" width="210" height="1"></td>' +
				'	</tr>' +
				'	<tr>' +
				'		<td style="padding: 0px"><img src="images/blank.gif" width="1" height="110"></td>' +
				'		<td style="padding: 0px" height="100%"><table cellspacing="0" border="0" width="100%" height="100%">' +
				'				<tr>' +
				'					<td id="CustomAlertContents" style="padding: 10px; text-align: center" valign="middle" height="100%">&nbsp;</td>' +
				'				</tr>' +
				'				<tr>' +
				'					<td style="padding: 0px"><input id="CustomInputValue" type="text" size="75" style="width: 408px; margin-left: 10px; margin-right: 10px; margin-bottom: 10px; position: absolute; visibility: hidden"></td>' +
				'				</tr>' +
				'				<tr>' +
				'					<td style="padding-left: 10px; padding-right: 10px; padding-bottom: 10px; text-align: center" align="center" nowrap><input id="CustomAlertButtonOK" style="FONT-SIZE: 10pt; WIDTH: 75px" onclick="jsAlert.Close(jsAlert.Results.OK);" type="button" value="OK"><span id="CustomAlertButtonCancel">&nbsp;&nbsp;<input style="FONT-SIZE: 10pt; WIDTH: 75px" onclick="jsAlert.Close(jsAlert.Results.Cancel);" type="button" value="Cancel"></span><input id="CustomAlertButtonYes" style="FONT-SIZE: 10pt; WIDTH: 75px" onclick="jsAlert.Close(jsAlert.Results.Yes);" type="button" value="Yes"><span id="CustomAlertButtonNo">&nbsp;&nbsp;<input style="FONT-SIZE: 10pt; WIDTH: 75px" onclick="jsAlert.Close(jsAlert.Results.No);" type="button" value="No"></span></td>' +
				'				</tr>' +
				'			</table></td>' +
				'	</tr>' +
				'</table>';
		}
	}
}