// global variables
//
var isMozilla;
var objDiv = null;
var originalDivHTML = "";
var DivID = "";
var over = false;


function buildDimmerDiv()
{	
	var width = document.body.clientWidth - (isMozilla ? 100 : 0);
	var height = document.body.clientHeight - (isMozilla ? 100 : 0);	
	document.write("<div id='dimmer' class='dimmer' style='width:" + width + "px; height:" + height + "px;'></div>");
	var odimmer = document.getElementById("dimmer");
}


function displayFloatingDiv(divId, title, width, height, left, top, showCaption) 
{
	DivID = divId;
	var odlg = document.getElementById(divId);
	var odimmer = document.getElementById('dimmer');

	if(isMozilla)
	{
		odimmer.style.height = document.body.scrollHeight - 80;
		odimmer.style.width = document.body.clientWidth - 80;
	}
	else
	{
		odimmer.style.pixelHeight = document.body.scrollHeight;
		odimmer.style.pixelWidth = document.body.clientWidth;
	}
	
	odimmer.style.top = 0;
	odimmer.style.left = document.body.scrollLeft + "px";
	odimmer.style.visibility = "visible";	
	
    odlg.style.left = left + 'px';
    odlg.style.top = top + 'px';
	odlg.style.width = width + "px";
	odlg.style.height = height + "px";	
	
	var addHeader = "";	
	var addFooter = "";
	if (originalDivHTML == "")
	    originalDivHTML = odlg.innerHTML;
	
	if(typeof(dlg_style_image_path) == "undefined")
		dlg_style_image_path = "images/xptab";
	if(typeof(image_path) == "undefined")
		image_path = "images";
		
	if(showCaption!=null && showCaption && title.length)
	{
		addHeader = '<table border=0 width=100% cellspacing=0 cellpadding=0>';
		addHeader += '<tr>' +
						'<td height=26 valign="bottom">' + 
							'<table width=100% height="100%" cellpadding="0" cellspacing="0" border=0>' +
								'<tr>' +
									'<td height=26 width=3 background="' + dlg_style_image_path + '/dlg_bg.gif" style="background-repeat:no-repeat;"></td>' +
									'<td height=26 background="' + dlg_style_image_path + '/dlg_cap.gif" style="background-repeat:repeat-x;">' +
										'<table width=100% cellspacing=0 cellpadding=0>' +
											'<tr>' +
												'<td ondblclick="void(0);" onmouseover="over=true;" onmouseout="over=false;" style="cursor:move;height:18px;font-size:11px;font-family:Tahoma;font-weight:bold;">&nbsp;' + title + '</td>' +
												'<td style="width:18px;padding-right:1px;" align="right">' +
													'<img alt="Close" style="cursor:pointer;" src="' + image_path + '/sm_close.gif" border="0" onclick="hiddenFloatingDiv(\'' + divId + '\');return false;">' +
												'</td>' +
											'</tr>' +
										'</table>' +
									'</td>' +
									'<td height=26 width=3 background="' + dlg_style_image_path + '/dlg_end.gif" style="background-repeat:no-repeat;"></td>' +
								'</tr>' +
							'</table>' +
						'</td>' +						
					'</tr>';
		addHeader += '<tr>' +
					'<td id="hwndDialogClient" bgcolor="#FAFAFA" style="border:1px solid #919B9C;border-top:1px solid #CCCCCC;padding:2px;">';					
	}
	addFooter = '</td></tr></table>';
	
    // add to your div an header	
	odlg.innerHTML = addHeader + originalDivHTML + addFooter;
	odlg.className = 'dimming';
	odlg.style.display = "block";
	odlg.style.visibility = "visible";
	return document.getElementById("hwndDialogClient");
}


function hiddenFloatingDiv(divId) 
{
	var odlg = document.getElementById(divId);
	odlg.innerHTML = originalDivHTML;
	odlg.style.visibility='hidden';	
	
	var odimmer = document.getElementById('dimmer');
	odimmer.style.height = 0;
	odimmer.style.visibility = 'hidden';
	
	DivID = "";
	originalDivHTML = "";
}


function MouseDown(e) 
{
   	if(over)
    {
        if(isMozilla) 
		{          	
		    objDiv = document.getElementById(DivID);
            X = e.layerX;
            Y = e.layerY;
            return false;
        }
        else 
		{
			objDiv = document.getElementById(DivID);            
            X = event.offsetX;
            Y = event.offsetY;
        }
    }	
}


function MouseMove(e) 
{
    if (objDiv) 
	{
        if (isMozilla) 
		{
			var left = (e.pageX - X);
			var cx = parseInt(objDiv.style.width);
			var top = (e.pageY - Y);
			var cy = parseInt(objDiv.childNodes[0].offsetHeight) + parseInt(objDiv.style.height);			
			if(left<=1 || top<=1 || (left+cx >= document.body.clientWidth-20) || (top+cy>=document.body.scrollHeight+14))
				return false;
            objDiv.style.top = (e.pageY-Y) + 'px';
            objDiv.style.left = (e.pageX-X) + 'px';
            return false;
        }
        else 
        {
			try
			{			
				var left = ( event.clientX - X);
				var cx = parseInt(objDiv.offsetWidth);
				var top = ( event.clientY - Y);
				var cy = parseInt(objDiv.offsetHeight);
				if(left<=1 || top<=1 || (left+cx >= document.body.clientWidth-8) || (top+cy>=document.body.scrollHeight-8))
					return false;
				objDiv.style.pixelLeft = event.clientX-X + document.body.scrollLeft;
				objDiv.style.pixelTop = event.clientY-Y + document.body.scrollTop;
				return false;
			}catch(e){window.status=e.message;}
        }
    }
}


function MouseUp() 
{
    objDiv = null;
}


function init_dimming_div()
{
    isMozilla = (document.all) ? 0 : 1;
    if (isMozilla) 
    {
        document.captureEvents(Event.MOUSEDOWN | Event.MOUSEMOVE | Event.MOUSEUP);
    }
		
    document.onmousedown = MouseDown;
    document.onmousemove = MouseMove;
    document.onmouseup = MouseUp;
	
    // add the div
    // used to dim the page
	buildDimmerDiv();
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// dialog - misc
var g_hWndCurrentDialog = "";

//dialog
function dialogBox(hDlg, title, cx, cy)
{
	if(!hDlg || !hDlg.length)
		return null;
		
	var w, h, l, t;
	var odlg = document.getElementById(hDlg);
	if(!odlg)
		return;
	
	if(cx==null || cx<=0)
	{
		w = parseInt(odlg.offsetWidth);
		if(!w)
			w = parseInt(odlg.style.width);
	}
	else
		w = cx;

	if(cy==null || cy<=0)
	{
		h = parseInt(odlg.offsetHeight);		
		if(!h)
			h = parseInt(odlg.style.height);
	}
	else
		h = cy;
	l = (document.body.clientWidth/2) - w/(odlg.style.display=="none" ? 3 : 2);
	t = (parseInt(document.body.clientHeight)-h)/4;		

	g_hWndCurrentDialog = hDlg;
	return displayFloatingDiv(hDlg, title?title:"", w, h, l, t, true);
}	

function showLoadingBox(cy, message)
{
	var w, h;
	var hDlg = "hwndLoadingMsg";
	var odlg = document.getElementById(hDlg);
	var msgText = document.getElementById("loadingMsgText");
	if(!odlg)
		return;
	if(odlg.style.display=="block")
	{
		if(msgText)
			msgText.innertHTML = message;
	}
	
	w = parseInt(odlg.offsetWidth);
	if(!w)
		w = parseInt(odlg.style.width);
	w = w>160 ? 160 : w;
		
	if(!cy || cy<=0)
		h = 24;
	else
		h = cy;		
	
	if(message!=null)
		msgText.innerHTML = message;
	else
		msgText.innerHTML = "Loading...";
		
	var r = document.body.clientWidth - w - 8;
	displayFloatingDiv("hwndLoadingMsg", "", w, h, r/*document.body.clientWidth - w - 2*/, document.body.scrollTop+2, false);
	g_hWndCurrentDialog = hDlg;
}

function endDialog()
{
	//window.scrollTo(0, 0);
	hiddenFloatingDiv(g_hWndCurrentDialog);
	g_hWndCurrentDialog = "";
}

//js custom-dialog
function jsCustomDialog(title, lpContent, lpfnOnInitCallback, lpfnOnCloseCallback)
{
	var odlg = dialogBox("hwndDialog", title, 0, 100);
	if(!odlg)
		return null;
	
	odlg.innerHTML = "<table width=100%><tr><td>" + lpContent + "</td></tr></table>";
	
	if(typeof lpfnOnInitCallback == "function")
		lpfnOnInitCallback(odlg);
	
	return odlg;
}

//js messagebox
function jsMessageBox(title, message, cx, cy, utype, lpfnOKCallback)
{	
	var odlg = dialogBox("hwndDialog", title, cx, cy);
	var image = "info.gif";
	if(utype=="warning")
		image = "warning.gif";
	else if(utype=="error")
		image = "error.gif";
	else
		image = "info.gif";
	
	var lptext = "<table width=100%><tr><td align=center>" +
					"<table width=100%><tr>" +
					  "<td align=right style='padding-left:22px;'><img src='images/" + image + "' align=absmiddle></td>" +
					  "<td align=center style='font-size:11px;font-family:Tahoma;font-weight:bold;'>" + message + "</td>" +
					  "<td style='padding-right:22px;'>&nbsp;</td>" +
					  "</tr>" + 
					  "<tr><td colspan=3 height=6></td></tr>" +
					  "<tr><td colspan=3 align=center style='padding-bottom:8px;'>" +
					  "<button type='button' id='IDOK' name='IDOK' class='toolbarBtn'>" +
						"<table cellspacing=0 cellpadding=1><tr><td style='padding-left:12px;padding-right:12px;font-size:11px;font-weight:bold;font-family:Verdana'>OK</td></tr></table>" +
					  "</button>" +
					  "</td></tr></table>" +
				  "</td></tr></table>";		
	odlg.innerHTML = lptext;
	var cmdOK = document.getElementById("IDOK");
	cmdOK.onmouseover = function(){this.className="toolbarBtnOver";}
	cmdOK.onmouseout = function(){this.className="toolbarBtn";}
	if(typeof lpfnOKCallback == "function")
		cmdOK.onclick = lpfnOKCallback;
	else
		cmdOK.onclick = endDialog;
	cmdOK.focus();
}

function setTextBoxFocus(index)
{
	var items = document.getElementsByTagName("input");
	var len = 0;
	if(!items)
		return;
	if(!items.length)
		len = 1;
	else
		len = items.length;			
	if(!index || index<0)
		index = 0;
	var i = 0, idx = 0;			
	while(i < len)
	{
		if(items[i].type != "text")
		{
			++i;continue;
		}
		
		if(items[i].type == "text")
		{
			if(idx==index)
			{
				var eml = items[i];
				eml.focus();
				eml.value = eml.value + "";
				eml.select();					
				break;
			}
			else
				++idx;
		}
	}
}
	
function setFocus(controlName)
{	
	var ctrl = document.all(controlName);
	if(!ctrl && !(ctrl = document.getElementById(controlName)))
		return;
	if(ctrl.length)
		ctrl = ctrl[0];
	if(ctrl.type=="text")
	{
		ctrl.focus();
		ctrl.value = ctrl.value + "";
		ctrl.select();
	}
	else
	{
		ctrl.focus();
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//tab control
function tabItem_Clicked(tabid)
{
	tabItem_OnSelChanged(tabid);
	
	//callback
	WindowProc(tabid, "", 0, 0);
}

function tabItem_GetCurrentItem()
{
	var header = document.getElementById("tabHeader");	
	if(header)
	{		
		return header.cells[0].childNodes[0].childNodes[0].rows[0].id;
	}
	return "list";
}

function tabItem_SetSel(tabid)
{
	tabItem_OnSelChanged(tabid);
}

function tabItem_OnSelChanged(newitemid)
{
	if(!prevTabItem)
	{			
		prevTabItem = tabItem_GetCurrentItem();
	}		
	tabItem_SetBkg(prevTabItem, false);
	tabItem_SetBkg(newitemid, true);
	prevTabItem = newitemid;		
}

function tabItem_SetBkg(tabid, hilite)
{
	var theItems = document.getElementById(tabid).cells;
	if(theItems)
	{
		if(document.all)
		{
			theItems[0].background = "images/xptab/" + (hilite ? "tab_sel_bg.gif" : "tab_bg.gif");
			theItems[1].background = "images/xptab/" + (hilite ? "tab_sel_end.gif" : "tab_end.gif");
		}
		else
		{
			theItems[0].style.background = "url(images/xptab/" + (hilite ? "tab_sel_bg.gif" : "tab_bg.gif") + ")";
			theItems[1].style.background = "url(images/xptab/" + (hilite ? "tab_sel_end.gif" : "tab_end.gif") + ")";
		}
		
		var itemText = theItems[0].childNodes[0].innerHTML;
		if(hilite)
		{					
			theItems[0].innerHTML = "<a class='tabHeaderSelText' style='cursor:default;'>" + itemText + "</a>";
		}
		else
		{	
			theItems[0].innerHTML = "<a href='#' onclick=\"tabItem_Clicked('" + tabid + "');return false;\" class='tabHeaderText'>" + itemText + "</a>";
		}
	}
}

function tabItem_SetText(tabid, text)
{
	if(document.getElementById(tabid))
	{
		document.getElementById(tabid).cells[0].childNodes[0].innerHTML = text;
	}
}

function tabItem_Enable(tabid, flag)
{
	if(document.getElementById(tabid))
	{			
		var oItem = document.getElementById(tabid).cells[0].childNodes[0];
		if(!flag)
		{
			document.getElementById(tabid).cells[0].innerHTML = "<a class='tabHeaderDisabled'>" + oItem.innerHTML + "</a>";
		}
		else
		{		
			oItem.href = "javascript:tabItem_Clicked('" + tabid + "');";
			oItem.className = "tabHeaderText";
		}
	}
}
