var currentModalDialog
var currentModalOpener
var currentModalCloseAction

window.onfocus = fnWindowFocus
window.onmousedown = fnWindowFocus

var savedKeyDown
	
	
// Hides HTML select elements that are overlapping the given menu group 
function fnHideSelectElements()
{
  if (document.getElementsByTagName) 
  {    
    var arrElements = document.getElementsByTagName('select'); 
    for (var i = 0; i < arrElements.length; i++) 
      arrElements[i].style.visibility = 'hidden';          
  }
}

// Restores all HTML select elements on the page 
function fnRestoreSelectElements()
{
  if (document.getElementsByTagName) 
  {
    var arrElements = document.getElementsByTagName('select'); 
    for (var i = 0; i < arrElements.length; i++) 
      arrElements[i].style.visibility = 'visible'; 
  }
}
			
function fnWindowFocus()
{
	setTimeout("fnCheckModalDialogFocus()",50)
	//return false
}			
			
function fnOpenPopup(strUrl, intWidth, intHeight, btResizable)
{
	
	var newWindow = window.open(strUrl,'_blank', fnGetWinOptions(intWidth, intHeight, btResizable, 0))
	newWindow.focus()
	return newWindow
}   

function fnOpenScrollablePopup(strUrl, intWidth, intHeight, btResizable)
{

	
	var newWindow = window.open(strUrl,'_blank', fnGetWinOptions(intWidth, intHeight, btResizable, 1))
	newWindow.focus()
		return newWindow
}   

/* use this function if you want to open a window with only one instance. if another instance of the window exists it will be closed */
function fnOpenPopupnamed(strUrl, strName, intWidth, intHeight, btResizable)
{
	
	//var newWindow  = window.open('',strName,'')
	//newWindow.close()
	newWindow = window.open(strUrl,strName, fnGetWinOptions(intWidth, intHeight, btResizable,0))
	newWindow.focus()
}    


function fnOpenModalPopup(strUrl, strCloseAction, intWidth, intHeight, btResizable)
{       
	
	if(fnCheckModalDialogFocus())
	{
		currentModalDialog = window.open(strUrl, '_blank', fnGetWinOptions(intWidth, intHeight, btResizable,0))
		if (strCloseAction != "")
		{
			currentModalCloseAction = strCloseAction
		}
		fnCheckModalDialogFocus()
		document.getElementById("documentDisableLayer").style.display= 'block'
		fnHideSelectElements()
		
	}
}

function fnOpenModalPopupNamed(strUrl, strName, strCloseAction, intWidth, intHeight, btResizable)
{       
	
	if(fnCheckModalDialogFocus())
	{
		currentModalDialog = window.open('',strName,'')
		currentModalDialog.close()
		currentModalDialog = window.open(strUrl, strName, fnGetWinOptions(intWidth, intHeight, btResizable,0))
		if (strCloseAction != "")
		{
			currentModalCloseAction = strCloseAction
		}
		fnCheckModalDialogFocus()
		document.getElementById("documentDisableLayer").style.display= 'block'
		fnHideSelectElements()
		
	}
	
}

function fnShowPromptSimple(strBasePath, opener, strTitle, strMessage, strConfirmAction, strCancelAction, intIcon)
{
	
	fnShowPrompt(strBasePath, opener, strTitle, strMessage, strConfirmAction, strCancelAction,  "","", intIcon)
}

function fnShowPrompt(strBasePath, opener, strTitle, strMessage, strConfirmAction, strCancelAction, strConfirmCaption, strCancelCaption, intIcon)
{
	
	var strQueryString = "?strMessage=" +escape(strMessage)
	strQueryString += "&strTitle=" + escape(strTitle)
	strQueryString += "&strConfirmAction=" + escape(strConfirmAction)
	strQueryString += "&strCancelAction=" + escape(strCancelAction)
	if (intIcon > 0)
	{
		strQueryString += "&intIcon=" + intIcon
	}
	if (strConfirmCaption != "")
	{
			strQueryString += "&strConfirmCaption=" + escape(strConfirmCaption)
	}
	if (strCancelCaption != "")
	{
			strQueryString += "&strCancelCaption=" + escape(strCancelCaption)
	}
	
	fnOpenModalPopup(strBasePath + '/applications/globalPopup/prompt.aspx' + strQueryString, '',500,150,0)
	currentModalOpener = opener
}



function fnShowAlert(strBasePath, strMessage, intIcon)
{
	
	var strQueryString = "?strMessage=" +strMessage
	if (intIcon > 0)
	{
		strQueryString += "&intIcon=" + intIcon
	}
	fnOpenModalPopup(strBasePath + '/applications/globalPopup/alertbox.aspx' + strQueryString, '',350,150,0)
}

function fnShowAlertWithAction(strBasePath, strMessage, intIcon, strAction)
{
	
	var strQueryString = "?strMessage=" +strMessage
	if (intIcon > 0)
	{
		strQueryString += "&intIcon=" + intIcon
	}
	fnOpenModalPopup(strBasePath + '/applications/globalPopup/alertbox.aspx' + strQueryString, strAction,350,150,0)
}

function fnGetWinOptions(intWidth, intHeight, btResizable, btScrollable)
{
	var offsetWidth
	var offsetHeight 
	if (document.all)
	{
		offsetWidth = window.top.screenLeft + (window.top.document.body.offsetWidth/2)
		offsetHeight = window.top.screenTop + (window.top.document.body.offsetHeight/2)
	}
	else
	{
		if (self.top.screenX > screen.top.left && screen.top.left> 0)
		{
			offsetWidth = screen.top.left/2
			offsetHeight = 300
		}
		else
		{
			offsetWidth = self.top.screenX + (window.top.document.body.offsetWidth/2)
			offsetHeight = self.top.screenY + (window.top.document.body.offsetHeight/2)
		}
	}
	
	var intLeft = offsetWidth - (intWidth / 2)
	var intTop = offsetHeight - (intHeight / 2)
	
	
	var strWinOptions = ""
	strWinOptions += "channelmode=no,"
	strWinOptions += "directories=no,"
	strWinOptions += "fullscreen=no,"
	strWinOptions += "location=no,"
	strWinOptions += "menubar=no," 
	if (btResizable == 1)
		strWinOptions += "resizable=yes,"
	else
		strWinOptions += "resizable=no,"
	if (btScrollable == 1)
		strWinOptions += "scrollbars=yes,"
	else
		strWinOptions += "scrollbars=no,"
	
	
	strWinOptions += "status=no,"
	strWinOptions += "toolbar=no,"
	strWinOptions += "left=" + intLeft + ","
	strWinOptions += "top=" + intTop + ","
	strWinOptions += "width=" + intWidth + ","
	strWinOptions += "height=" + intHeight
	return strWinOptions
}           
	

function fnCheckModalDialogFocus()
{  
	var documentDisableLayer = document.getElementById("documentDisableLayer")
	if (currentModalDialog && !currentModalDialog.closed) 
	{
		
		currentModalDialog.focus()  
		if(documentDisableLayer)
		{
			if (documentDisableLayer.style.display == 'none')
			{ 
				documentDisableLayer.style.width = document.body.clientWidth;
				documentDisableLayer.style.height = document.body.clientHeight;
				documentDisableLayer.style.display= 'block'
				savedKeyDown = document.onkeydown
				document.onkeydown = fnWindowFocus
				fnHideSelectElements()
			}           
		}
		
		
		
		return false; 
	}
	else
	{
		if(documentDisableLayer)
		{
			if (documentDisableLayer.style.display == 'block')
			{
				documentDisableLayer.style.display= 'none'
				documentDisableLayer.style.width = 10;
				documentDisableLayer.style.height = 10;
				document.onkeydown = savedKeyDown
				if (currentModalCloseAction != null)
				{
					eval(currentModalCloseAction)
					currentModalCloseAction = null
				} 
				fnRestoreSelectElements()
			} 
		}
		
		
		
		return true
	}
}       

function popupFramework(url)
{
	
	//newwindow=window.open('','Login','height=350,width=450,resizable=no,top=150,left=150');
	//newwindow.close()
	newwindow=window.open(url,'IdiosFramework','height=600,width=800,resizable=yes,scrollbars=no');
	if (window.focus) {newwindow.focus()}
	return false;
}

function fnEval(strAction)
{
	eval(strAction)
}


