var RAISED_POPUP_CONST = "RaisedPopup";

function SetMsgTxt ()
{
	//message(innerHTML) was sets on server side 
	if (document.all.m_msgTxt.innerHTML != "")
		return;
	
	
	var args = window.dialogArguments;
	if (args != null) {
		if (args.indexOf('|') == -1) {
			document.all.m_msgTxt.innerHTML = args;
			return;
		}
		var msg = args.substring(0, args.indexOf('|'));
		var icon = args.substring(args.indexOf('|') + 1);
		document.all.m_iconType.value = icon;
		document.all.m_msgTxt.innerHTML = msg;
	}
}
function OkClicked ()
{
	CloseDialog ("OK");
}

function CancelClicked ()
{
	CloseDialog ("CANCEL");
}

function CloseDialog (retVal)
{
	window.returnValue = retVal;
	window.close ();
}

function Question (questionMsg,extraHeight,txType)
{
	var top = String ((window.clientHeight / 2) - 100);
	var left = String ((window.clientWidth / 2) - 140);
	var dilaogH = 200;
	if (extraHeight)
		dilaogH += extraHeight;	
	var winToOpen = "../DialogForms/QuestionForm.aspx";
	if (IsNumeric(questionMsg))	{
		winToOpen += "?txCode=" + questionMsg;		
		questionMsg += ''; //convert to string
		if (txType != null && IsNumeric(txType))	{
			winToOpen += "&txType=" + txType;		
		}
	}
	var retVal;
	if (window.showModalDialog)// IE
		retVal = window.showModalDialog (winToOpen, 
									questionMsg,
									"dialogHeight: " + dilaogH + "px; dialogWidth: 280px; "+
									"dialogTop: " + top + "px; dialogLeft: " + left + "px; " + 
									"edge: Raised; center: Yes; help: No; resizable: yes; status: No;");

	else { // FireFox
		var messageString = questionMsg;
		if (IsNumeric(questionMsg)) {
			var message = AutoWeb.DialogLogic.GetMessageString (questionMsg);
			messageString = message.value;
		}
		var response = window.confirm (messageString);
		if (response)
			retVal = "OK";
		else
			retVal = "CANCEL";	
	}									
	return retVal;									
}
function AuthorizePrompt (promptMsg, attempt)
{
	var top = String ((window.clientHeight / 2) - 100);
	var left = String ((window.clientWidth / 2) - 140);
	var winToOpen = "DialogForms/AuthorizePromptForm.aspx?attempt=" + attempt;	
	if (IsNumeric(promptMsg))	{
		winToOpen += "&txCode=" + promptMsg 		
		promptMsg += ''; //convert to string
	}			
	if (window.showModalDialog) {// IE				
		retVal = window.showModalDialog (winToOpen, 
									promptMsg,
									"dialogHeight: 181px; dialogWidth: 300px; "+
									"dialogTop: " + top + "px; dialogLeft: " + left + "px; " + 
									"edge: Raised; center: Yes; help: No; resizable: No; status: No;scroll:No");
		
		
		return retVal;				
	} 
	else  { // FF
		var win;				
		win = window.prompt (promptMsg);
		return win
	}
									
}
function FitMessage (tableId)
{
	var NS = (navigator.appName=="Netscape")?true:false;
	var tbl = document.getElementById (tableId);
	if (tbl == null)
		return;
	var iWidth = (NS)?window.innerWidth:document.body.clientWidth;
	var iHeight = document.forms(0).offsetHeight + 25;
	window.dialogHeight = String (String (iHeight) + "px");
}
function MessageBox (msg, iconType)
{
	var top = String ((window.clientHeight / 2) - 100);
	var left = String ((window.clientWidth / 2) - 120);
	var winToOpen = "../DialogForms/MessageForm.aspx";
	if (IsNumeric(msg))	{
		winToOpen += "?txCode=" + msg + "&icon=" + iconType;		
		msg += ''; //convert to string
	}		
	if (window.showModalDialog)
		window.showModalDialog (winToOpen, 
									msg + '|' + iconType,
									"dialogHeight: 200px; dialogWidth: 240px; "+
									"dialogTop: " + top + "px; dialogLeft: " + left + "px; " + 
									"edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");
	else {
		var messageString = msg;
		if (IsNumeric(msg)) {
			var message = AutoWeb.DialogLogic.GetMessageString (msg);
			messageString = message.value;
		}
		alert (messageString);
	}
}

var waitWin = null;

function OpenWaitDlg ()
{
	if (waitWin != null)
		return;
		
	var winToOpen = "../DialogForms/WaitForm.aspx";
	waitWin = ShowModelessDialog (winToOpen, "WaitNotice", 300,200,180,280);
}

function CloseWaitDlg ()
{
	if (waitWin != null) {
		waitWin.close ();
		waitWin = null;
	}
}

var noticeWin = null;
function OpenNoticeDlg (msg)
{
	var winToOpen = "../DialogForms/NoticeForm.aspx";
	if (IsNumeric(msg))	{
		winToOpen += "?txCode=" + msg;		
		msg += ''; //convert to string
	}
	noticeWin = ShowModelessDialog (winToOpen, msg, 0,0,280,100);
}

function CloseNoticeDlg ()
{	
	if (noticeWin != null) {
		noticeWin.close ();
		noticeWin = null;
	}
}

function SetCookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )	{
		expires = expires * 1000 * 60 * 60;
	}
	var expires_date = new Date( today.getTime() + (expires) );

	var cookieStr = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
	document.cookie = cookieStr;
}

function GetCookie( name ) 
{
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )	{
		return null;
	}
	if ( start == -1 ) 
		return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) 
		end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

/************************************************/
/*			Popup Menu							*/
/************************************************/

var popUp       = null;
var relativeObj = null;
// Default Styles
// Use the PopupMenuSetStyles () Function to Change The Popup Styles
var stylePath           = "StyleSheets/MainStyles.css";
var bodyStyle           = "PopUpMenu";
var itemStyle           = "MenuCaption"
var itemStyleMouseOver  = "MenuCaption MenuMouseOver";
var itemStyleMouseOut   = "MenuCaption";

// popup properties
var popupWidth      = 150;
var popupItemHeight = 35;
var itemLeftPadding = 20;
var popupLeft		= 0;

// Change to False Couse The Popup Menu to Displayed Horizontally 
// It Can be change By pIsVertical Parameter In PopupMenuRaise() function
var isVertical	= true;

// Global Arrays 
var arrItems	= null;
var arrEvents	= null;


// Set The Popup menu Apperance
function PopupMenuSetStyles (pStylePath, pBodyStyle, pItemStyle, pItemStyleMouseOver, pItemStyleMouseOut)
{
	stylePath			= pStylePath;
	tableStyle			= pBodyStyle; 
	itemStyle			= pItemStyle;	
	itemStyleMouseOver	= pItemStyleMouseOver
	itemStyleMouseOut	= pItemStyleMouseOut
}

//  Show Popup Menu
function PopupMenuRaise (pRelativeObj, pArrItems, pArrEvents, pIsVertical, pLeft, pWidth, pItemHeight,pItemLeftPadding)
{
	arrItems						= pArrItems;
	arrEvents						= pArrEvents;
	isVertical						= pIsVertical;
	relativeObj						= pRelativeObj;
	popupWidth						= pWidth;
	popupLeft						= pLeft;
	popupItemHeight					= pItemHeight;
	itemLeftPadding					= pItemLeftPadding;
	
	if (document.getElementById && !(document.all)) { // FireFox
		var pg = parent.document.getElementById('m_down');
		var oDoc = (pg.contentWindow || pg.contentDocument);
		if (oDoc.document) 
			oDoc = oDoc.document;
		if (oDoc.getElementById (RAISED_POPUP_CONST) != null)
			oDoc.body.removeChild (oDoc.getElementById (RAISED_POPUP_CONST));
		var div = oDoc.createElement ("div");
		div.setAttribute ("style", "LEFT: " + relativeObj.offsetLeft + "px; VISIBILITY: visible; WIDTH: " + popupWidth + "px; POSITION: absolute; TOP: 0px; HEIGHT: 15px");
		div.setAttribute ("id", RAISED_POPUP_CONST);
		div.innerHTML = PopupBody ();
		oDoc.body.appendChild (div);
		oDoc.addEventListener ("click", function() { this.body.removeChild (this.getElementById (RAISED_POPUP_CONST));this.onclick = null;}, false);
		return;
	}
	else {
		popUp = window.createPopup();
		popUp.document.body.innerHTML	= PopupBody ();
		PopupMenuShow ();
	}
}


function PopupBody ()
{
	var body = "";
	body += "<html>"
	body +=  "<head></head>";
	body += "<body style='margin:0em;border-style:none;position:absolute;'>";
	body += "<link href='" + stylePath + "' type='text/css' rel='stylesheet'>"
	body += GetTable ();
	body += "</body>";
	body += "</html>";
	return body;	
}
function GetTable ()
{
	var strTable		= "";
	var tdOnMouseOver	= " onmouseover=\"this.className=\'" + itemStyleMouseOver + "\'\" ";
	var tdOnMouseOut	=  " onmouseout=\"this.className=\'" + itemStyleMouseOut + "\'\" ";
	
	if (document.getElementById && !(document.all))  // FireFox
		strTable+= "<table class='" + bodyStyle + "' width='" + popupWidth + "' cellpadding=0 cellspacing=0 border=0>";
	else
		strTable+= "<table class='" + bodyStyle + "' width='" + popupWidth + "' cellpadding=0 cellspacing=0 border=0 onclick='parent.PopupMenuHide ();'>";
	 
	if (!isVertical)
		strTable += "<tr>"	
	for ( var i=0; i< arrItems.length; i++){		
		if (isVertical)	
			strTable +=  "<tr>";
			
			strTable +=		"<td class = '" + itemStyleMouseOut + "' " + arrEvents[i] + " style='height:" + popupItemHeight + "px' class='" + itemStyle + "' " + tdOnMouseOver +  tdOnMouseOut + ">" ;
			strTable +=			"<span style='width:" + itemLeftPadding + "px'>&nbsp;</span><span>" + arrItems[i] + "</span>";  
			strTable +=		"</td>";
		
		if (isVertical)							
			strTable +=	 "</tr>";		
	}	
	
	if (!isVertical)
		strTable += "</tr>"	
	strTable += "</table>";
	return strTable;

}
function PopupMenuShow ()
{
	var hei = popupItemHeight + 2;
	if (isVertical)
		hei = popupItemHeight * arrItems.length + 2		
	if (popUp == null)
		popUp = window.createPopup();
	popUp.show (popupLeft, relativeObj.offsetHeight, popupWidth, hei, relativeObj);	
}
function PopupMenuHide ()
{
	if (popUp == null)
		return;
	popUp.hide ();
	popUp = null;
}

var TOOL_TIP = "2";
var STATUS_BAR = "1";
var lastSender		= null;
function  RaiseToolTip (sndr, toolTipString)
{
	if (lastSender != null) {
		try{
			lastSender.parentNode.onmouseout ();
			lastSender.onmouseout ();
			lastSender = null;
		}catch(err){}
	}
	if (toolTipString == "undefined" || toolTipString == null || toolTipString == "")	
		return;
		
	var body = "";	
	body += "<table style='border-right: #aca899 1px solid;border-top: #aca899 1px solid;border-left: #aca899 1px solid;border-bottom: #aca899 1px solid;background-color: #ffffcc;' id='tooltipTable' align='center' cellpadding='0' cellspacing='0'>";
	body += "<tr><td style='font-size: 8pt;color: #000000;font-family: arial;text-decoration: none;'>";
	body +=  toolTipString;
	body += "</td></tr></table>";
	
	var topInt = sndr.offsetHeight + sndr.offsetTop + 2;
	var spanHolder = "";
	spanHolder = spanHolder + "<span id='spanHolder' ";
	spanHolder = spanHolder + "style='visibility:hidden;position:absolute;z-index:500;"
	spanHolder = spanHolder + "top:" + topInt + ";'></span>";
	var spanHolderItem = GetSpanHolder (sndr);
	if (spanHolderItem == null) {
		sndr.innerHTML = spanHolder + sndr.innerHTML;
		spanHolderItem = GetSpanHolder (sndr);
	}

	sndr.style.position = '';
	spanHolderItem.innerHTML		= body;
	spanHolderItem.style.zIndex		= 500;
	spanHolderItem.style.visibility	= 'visible';
	lastSender = sndr;
}

function CloseToolTip (sndr)
{
	var spanHolderItem = GetSpanHolder (sndr);
	if (spanHolderItem == null || spanHolderItem == "undefiend")
		return;
	spanHolderItem.style.visibility = 'hidden';
}
function GetSpanHolder (sndr)
{
	var childList = sndr.childNodes;
	var spanHolder = null;
	for(var i=0; i<childList.length; i++)
		if (childList.item(i).tagName == 'SPAN') {
			spanHolder = childList.item(i);
			return spanHolder;
		}	
	return spanHolder;		
}	

/****************************************/
/*			TOOL TIP SCRIPT				*/
/****************************************/ 

var intervalDocumentLoaded = window.setInterval("IsDocumentLoaded ()",1000)

function IsDocumentLoaded ()
{
	if (document.readyState != "complete")
		return;
	
	window.clearInterval (intervalDocumentLoaded);
	AddStatusMessageSupport ();							
	intervalDocumentLoaded = null;
}
				
function AddStatusMessageSupport()
{
	var coll = GetBrowserElementsByTagName (document,"INPUT");
	
	if (coll.length == undefined || coll.length == 0)
		return;
	
	if (coll.length == 1 && 
		(coll[0].type.toUpperCase () == "TEXT" || coll[0].type.toUpperCase () == "PASSWORD") && 
		IsHasHelpTip (coll[0]) == true) {
		coll[0].attachEvent('onfocus', ShowStatusBarMessage);
		coll[0].attachEvent('onblur', ClearStatusMessage); 	
		if (coll[0] == document.activeElement) {			
			ShowStatusBarMessage ();							
		}			
					
	}
	else {
		for (var i=0; i<coll.length; i++) {
			if ((coll[i].type.toUpperCase () == "TEXT" || coll[i].type.toUpperCase () == "PASSWORD") && 
			     IsHasHelpTip (coll[i]) == true) {												
				coll[i].attachEvent('onfocus', ShowStatusBarMessage);
				coll[i].attachEvent('onblur', ClearStatusMessage);
				if (coll[i] == document.activeElement) {					
					ShowStatusBarMessage ();
				}
			} 	 							
		}
	}
	
}
function IsHasHelpTip (obj)
{

	if (obj.HelpObjTip != undefined && obj.HelpObjTip != "" && obj.readOnly == false )
		return true;
	else
		return false;
	
}
function ShowStatusBarMessage ()
{
	
	obj = document.activeElement;
		
	var i = 0;
	var crr = window;
	if (opener != undefined) 		
		crr = opener
	
	while (i++ < 5) {
	
		if (crr.status != null ) {
			crr.status = obj.HelpObjTip;											
			return;
		}
		
		if (parent.crr == null || parent.crr == undefined)
			return;
			
		crr = parent.crr;
	}
		
}

function ClearStatusMessage ()
{
		
	var crr = window;
	
	if (opener != undefined)
		crr = opener
	
	var i = 0;
	while (i++ < 5) {
		if (crr.status != null ) {
			crr.status = "";									
			return;
		}
		
		crr = parent.crr;
	}	
}

