/* define variables for "if n4 (Netscape 4), if IE (IE 4.x), 
and if n6 (if Netscape 6/W3C-DOM compliant)" */
var n4, ie, n6;

n4 = false;
ie = false;
n6 = false;

// detecting browser support for certain key objects/methods 
if (document.layers) {
  n4 = true;
}
else if (document.all) {
  ie = true;
}
else if (document.getElementById) 
{
  n6 = true;
}

// declare some variables for the window dimensions
var windowwidth, windowheight;
var centerhorizontalizontal, centervertical;

//Get dimensions of the window
function GetDimensions() 
{
	if(n4 || n6){
	  windowwidth=window.innerWidth;
	  windowheight=window.innerHeight;
	}
	else if (ie) {
		windowwidth=screen.availWidth;
		windowheight=screen.availHeight;
	}

	centerhorizontal = windowwidth/2;
	centervertical = windowheight/2;
}

function ShowLookup(idBoxName, textBoxName, TableName, DisplayField, KeyField, Caption, ShowAddButton, SelectedItem, PageSize, ShowIDColumn, Distinct, Filter)
{
	var params;
  var now = new Date();
  var theTime = now.getTime();
  
  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_uc/LookupPage.aspx?TN=" + TableName + "&C=" + Caption + "&t=" + theTime;
  if (DisplayField != null && DisplayField != '')
    thePath += "&DF=" + DisplayField;
  if (KeyField != null && KeyField != '')
    thePath +=  "&KF=" + KeyField 
  if (ShowAddButton != null && ShowAddButton != '')
    thePath +=  "&Add=" + ShowAddButton
  if (SelectedItem != null && SelectedItem != '')
		thePath += "&SI=" + SelectedItem;
	if (ShowIDColumn != null && ShowIDColumn != '')
		thePath += "&SIC=" + ShowIDColumn;
	if (PageSize != null && PageSize != '')
		thePath += "&PS=" + PageSize;
	if (Distinct != null && Distinct != '')
		thePath += "&D=" + Distinct;	
	if (Filter != null && Filter != '')
		thePath += '&F=' + Filter;

	var myObject = new Object();
	myObject.ID = "";
	myObject.Desc = "";
	
	  // get screen size
  GetDimensions();
  var dialogBoxHeight = windowheight / 100 * 75;
  var dialogBoxWidth = windowwidth / 100 * 60;
	params = window.showModalDialog(thePath, myObject, "dialogHeight: " + dialogBoxHeight + "px; dialogWidth: " + dialogBoxWidth + "px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");

	if (params == null)
		return;

	if (params == '')
		return;

  try
  {    
    // this field does not exist on list screen so this has to be wrapped in a try block  
	  aspnetForm.elements[idBoxName].value = myObject.ID;		
	}
	catch (err) {
	  // nothing to do just ignore
	}

  aspnetForm.elements[textBoxName].value = myObject.Desc;
}

// used by datgrid to show current row
var lastGridRowColorUsed;

function HighlightRow(row, highlight)
{
  if (highlight)
	{
		lastGridRowColorUsed = row.style.backgroundColor;
		row.style.backgroundColor = rowSelectedColor;
	}
	else
	{
	  if (row!=curSelectedRow)
	  {
	    row.style.backgroundColor = lastGridRowColorUsed;      
	  }
	}
}	      

var curSelectedRow=null;

function SelectedRow(row)
{
  if(curSelectedRow)
  {
    curSelectedRow.style.backgroundColor=lastGridRowColorUsed;
  }
  curSelectedRow=row;
  curSelectedRow.style.backgroundColor="lightskyblue";       
}
 
// used by calendar to show current day
var lastCalendarColorUsed;

function HighlightCalendarDay(cell, highlight)
{
  if (highlight)
	{
		lastCalendarColorUsed = cell.style.backgroundColor;
		cell.style.backgroundColor = rowSelectedColor;
	}
	else
	{
	  cell.style.backgroundColor = lastCalendarColorUsed;      
	}
}     

function ShowAddDialog(ScreenName)
{
	var params;
  var now = new Date();
  var theTime = now.getTime();
  
  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_inc/AddDialog.aspx?ScreenName=" + ScreenName + "&t=" + theTime;
	var myObject = new Object();
	myObject.ID = "";	
	
	  // get screen size
  GetDimensions();
  var dialogBoxHeight = windowheight / 100 * 75;
  var dialogBoxWidth = windowwidth / 100 * 60;
	params = window.showModalDialog(thePath, myObject, "dialogHeight: " + dialogBoxHeight + "px; dialogWidth: " + dialogBoxWidth + "px; dialogLeft: 100px; dialogTop: 100px; edge: Raised; center: No; help: No; resizable: Yes; status: No;");

	if (params == null)
		return;

	if (params == '')
		return;

  try
  {    
    // this field does not exist on list screen so this has to be wrapped in a try block  
	  document.frmLookup.elements["Lookup1_txtKeywords"].value = myObject.ID;		
	  document.frmLookup.submit();
	}
	catch (err) {
	  // nothing to do just ignore
	}
}

function CloseAddDialog(ID)
{
  try 
  {    
    var eObject = window.dialogArguments;
    eObject.ID = ID;
    window.returnValue = "OK";      		
    window.close();
  }
  catch (error)
  {
  }
}

function CloseScreen()
{
  window.returnValue = "";
  window.close();
}

function ViewSource() {
  window.location = "view-source:" + window.location.href 
}

function ShowHelp(HelpFile)
{
  var now = new Date();
  var theTime = now.getTime();
  
  // appending the time ensures that browser does not cache the popup dialog
	// kludge to get around folder hierarchy issue
  if (HelpFile.toLowerCase() == 'default.htm')
  {
  	var thePath = "help/helpframe.aspx?helpfile=" + HelpFile + "&t=" + theTime;
  }
  else
  {
		var thePath = "../help/helpframe.aspx?helpfile=" + HelpFile + "&t=" + theTime;
	}

  // get screen size
  GetDimensions();
  var Height = windowheight / 100 * 75;
  var Width = windowwidth / 100 * 80;
	var helpWindow = window.open(thePath, null, "height=" + Height + "px, width=" + Width + "px, left=100, top=100, directories=no,menubar=no,toolbar=no,scrollbars=yes,resizable=no");
	helpWindow.focus();
}

function showChildGrid(gridName)
{
	try {			    
		eval(gridName + ".style.display='block';");
		document.getElementById(gridName).focus();
	}
	catch (error)
	{
	}
}

function ClearLookupFields(idBoxName, textBoxName)
{
	try
	{
		document.getElementById(textBoxName).value = "";
		document.getElementById(idBoxName).value = "";		
	}
	catch (err) 
	{
	  // nothing to do just ignore
	}
}

function ClearDateField(textBoxName)
{
	try
	{
		document.getElementById(textBoxName).value = "";
	}
	catch (err) 
	{
	  // nothing to do just ignore
	}
}

function ShowHideSelectBoxes(visibleflag) {
	ShowHideElements(visibleflag,document.getElementsByTagName('select'));
}

function ShowHideElements(visibleflag,elements) {
	for (var i=0;i<elements.length;i++) elements[i].style.visibility=visibleflag;
}

function ChooseDate(textBoxName,caption, lowerYearRange, upperYearRange)
{

  var params;
  var now = new Date();
  var theTime = now.getTime();  
	var currentDate = document.getElementById(textBoxName).value;

  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_uc/DatePicker.aspx?caption=" + caption + "&CurrentDate=" + currentDate + "&lowerYearRange=" + lowerYearRange + "&upperYearRange=" + upperYearRange + "&t=" + theTime;

  params = window.showModalDialog(thePath, "", "dialogHeight: 300px; dialogWidth: 220px; dialogTop: 100px; dialogLeft: 200px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
	
	if (params != null)
	{
		if (params != '')
		{	
			document.getElementById(textBoxName).value = params;
		}
	} 
}

function ShowTreePopup(idBoxName, textBoxName, TableName, DisplayField, KeyField, Caption, ParentCol, ChildCol, Filter)
{
	var params;
  var now = new Date();
  var theTime = now.getTime();
  
  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_uc/TreePopupPage.aspx?TN=" + TableName + "&C=" + Caption + "&t=" + theTime;
  if (DisplayField != null && DisplayField != '')
    thePath += "&DF=" + DisplayField;
  if (KeyField != null && KeyField != '')
    thePath +=  "&KF=" + KeyField 
 	if (ParentCol != null && ParentCol != '')
		thePath += "&PC=" + ParentCol;
	if (ChildCol != null && ChildCol != '')
		thePath += "&CC=" + ChildCol;	
	if (Filter != null && Filter != '')
		thePath += '&F=' + Filter;
	
	var myObject = new Object();
	myObject.ID = "";
	myObject.Desc = "";
	
	// set dimensions
  var dialogBoxHeight = 650;
  var dialogBoxWidth = 350;
	params = window.showModalDialog(thePath, myObject, "dialogHeight: " + dialogBoxHeight + "px; dialogWidth: " + dialogBoxWidth + "px; edge: Raised; center: Yes; help: No; resizable: Yes; status: No;");

	if (params == null)
		return;

	if (params == '')
		return;

  try
  {    
    // this field does not exist on list screen so this has to be wrapped in a try block  
	  aspnetForm.elements[idBoxName].value = myObject.ID;		
	}
	catch (err) {
	  // nothing to do just ignore
	}

  aspnetForm.elements[textBoxName].value = myObject.Desc;
}

function ShowFileUpload(TableName, WhereClause, ImageField, Caption)
{
	var params;
  var now = new Date();
  var theTime = now.getTime();
  
  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_inc/ImageUpload.aspx?Table=" + TableName + "&WhereClause=" + WhereClause + "&IField=" + ImageField + "&caption=" + Caption + "&t=" + theTime;
	
	// set dimensions
  var dialogBoxHeight = 125;
  var dialogBoxWidth = 400;
	params = window.showModalDialog(thePath, null, "dialogHeight: " + dialogBoxHeight + "px; dialogWidth: " + dialogBoxWidth + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
	if (params == null)
		return false;

	if (params == '')
		return false;
		
	return true;
}

function ShowFullSizeImage(TableName, WhereClause, ImageField, Caption)
{
	var params;
  var now = new Date();
  var theTime = now.getTime();
    	
  var dialogBoxWidth = 800;
  var dialogBoxHeight = 600;
  
  // appending the time ensures that browser does not cache the popup dialog
  var thePath = "../_inc/ShowImage.aspx?Table=" + TableName + "&WhereClause=" + WhereClause + "&IField=" + ImageField 
		+ "&PWidth=" + dialogBoxWidth + "&PHeight=" + dialogBoxHeight
		+ "&caption=" + Caption + "&t=" + theTime;
  
	window.showModalDialog(thePath, null, "dialogHeight: " + dialogBoxHeight + "px; dialogWidth: " + dialogBoxWidth + "px; edge: Raised; center: Yes; help: No; resizable: No; status: No;");
}

function HandleEscapeKey()
{
	// check to see if ESC key was pressed
	if(event.keyCode==27)
	{				
		CloseScreen();
	}					
}

function ToggleSearchCriteria()
{
  var cmdShowHide;
  var divListCriteria = document.getElementById('divListCriteria');
  if (divListCriteria != null)
  {
    cmdShowHide = document.getElementById("MasterPage_PageContent_cmdShowHide");
    
    if (divListCriteria.style.display == "block")
    {
      divListCriteria.style.display = "none";   
      cmdShowHide.value = "Show Filter";
      document.getElementById("MasterPage_PageContent_ShowHideMode").value = "Show";                  
    }
    else
    {
      divListCriteria.style.display = "block";
      cmdShowHide.value = "Hide Filter";
      document.getElementById("MasterPage_PageContent_ShowHideMode").value = "Hide";
    }
    // persist setting to cookie
    document.cookie = "ShowHide_state=" + divListCriteria.style.display;
  }
}

function SetupCriteriaDiv()
{
	try
	{
		var cmdShowHide;
		cmdShowHide = document.getElementById("MasterPage_PageContent_cmdShowHide");
		var divListCriteria = document.getElementById('divListCriteria');
		if (divListCriteria != null)
		{
			if (getCookie('ShowHide_state'))
			{
				divListCriteria.style.display = getCookie('ShowHide_state');
			}
			if (divListCriteria.style.display == "block")
			{
				cmdShowHide.value = "Hide Filter";
				document.getElementById("MasterPage_PageContent_ShowHideMode").value = "Hide";                  
			}
			else
			{
				cmdShowHide.value = "Show Filter";
				document.getElementById("MasterPage_PageContent_ShowHideMode").value = "Show";
			}
		}
	}
	catch (err) {
	  // nothing to do just ignore
	}
}

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else
		begin += 2;
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
		end = dc.length;
	return unescape(dc.substring(begin + prefix.length, end));
}

function ShowResourceViewPopup(Title, Query, ID) 
{
	//debugger;
	var params;
	var now = new Date();
	var theTime = now.getTime();
	var thePath = '../CatResources/CatResourceViewContainer.aspx?Title=' + Title + "&PageUrl=CatResourceView.aspx&Query=ResourceID&Param=" + ID + "&t=\" + theTime;";
	window.showModalDialog(thePath, "", "dialogHeight:650px; dialogWidth: 800px; center:Yes; help:No; resizable: Yes; status: No;");
}

function ShowPrintFrame(ViewURL)
{
  var now = new Date();
  var theTime = now.getTime();
  var thePath = ViewURL + "&PrintMode=1&t=" + theTime;

  // get screen size
  GetDimensions();
  var Height = windowheight / 100 * 75;
  var Width = windowwidth / 100 * 80;
  var printframeWindow = window.open(thePath, null, "height=" + Height + "px, width=" + Width + "px, left=100, top=100, directories=no,menubar=no,toolbar=no,scrollbars=yes,resizable=no");
  printframeWindow.focus();
}


