/* Common scripts shared across most forms */

var target;			// The form control currently with the focus

// =================================================
// Useful functions for forms

// Returns a reference to a hidden field. RegisterHiddenField() in ASP.NET does not set the id, so only works in IE
function getHiddenField(fieldName)
{
	var ctl;
	
	if (document.all)
	{
		ctl = document.getElementById(fieldName);
	}
	else 
	{
	  var elements = document.getElementsByName(fieldName);
		if (elements && elements.length > 0)	{	ctl = elements[0];	}
	}
	
	return ctl;
}

// Select and highlight the given field
function setFocus(c)
{
	try
  { 
    c.focus(); 
    c.select(); 
  }
	catch(e) { }
}

// Returns the currently pressed key
function getKeyCode(e)
{
	//Get the ASCII key that was pressed
	keyCode = 0;
	try
  { 
		if (!e) { var e = window.event; }
		if (e.target)					{ target = e.target; }
		else if (e.srcElement) { target = e.srcElement; }
	  
		if (e.keyCode)		{ keyCode = e.keyCode; }
		else if (e.which)	{ keyCode = e.which; }
		else							{ keyCode = 0; }
  }
	catch(e) { }

	return keyCode;
}

// Checks to see if a key press should click a default button or perform any other functionality
function processKey(e, defaultId, cancelId, filterType)
{
	nKeyCode = getKeyCode(e);
  
  if (nKeyCode == 13 && defaultId != "" && target.type != "textarea")
	{
		//Pressing enter on any control except a text box should always click the default button
		
		//Clear the key press to stop it firing again
		if (e.preventDefault) { e.preventDefault(); }
		else									{ e.keyCode = 0; }
		
		//Click the default button for this control
		__doPostBack(defaultId,'');

  }
	else if (nKeyCode == 27 && cancelId != "")
	{ 
		//Pressing cancel on any control should cancel the form

		//Clear the keypress to stop it firing again
		if (e.preventDefault) { e.preventDefault(); }
		else				{ e.keyCode = 0; }
		
		//Click the cancel button for this control
		__doPostBack(cancelId,'');

  }
  else if (filterType == 1 && nKeyCode == 32)
  {
    //Pressing Space will attempt to fill in the date automatically
    
    //Determine the current date to show (driven from the application)
		
		//Get UTC Now
		now = new Date;

		//Determine the date parts we may want to add
	  sMinute = now.getMinutes(); 
	  sHour = now.getHours() + ":"; 
	  sYear = now.getFullYear() + " "; 
	  sMonth = (now.getMonth() + 1) + "/"; 
	  sDay = (now.getDate()) + "/"; 

	  //Ensure leading zeroes are added
		if (now.getMinutes() <= 9) { sMinute = "0" + sMinute; }
		if (now.getHours() <= 9)		{ sHour = "0" + sHour; }
		if (now.getMonth() <= 9)		{ sMonth = "0" + sMonth; }
		if (now.getDate() <= 9)		{ sDay = "0" + sDay; }

    value = target.value;

		//Patterns to match for
    var sComplete = /\d+\/\d+\/\d+\s\d+:\d+/;
    var sMinutePattern1 = /\d+\/\d+\/\d+\s\d+:/;
    var sMinutePattern  = /\d+\/\d+\/\d+\s\d+/;
    var sHourPattern1 = /\d+\/\d+\/\d+\s/;
    var sHourPattern  = /\d+\/\d+\/\d+/;
    var sYearPattern1 = /\d+\/\d+/;
    var sYearPattern  = /\d+\/\d+\//;
    var sMonthPattern1 = /\d+/;
    var sMonthPattern  = /\d+\//;

		var sNewText = "";

		if (sComplete.test(value))						{ sNewText = value; }	// Nothing to do
		else if (sMinutePattern1.test(value))	{ sNewText = value + sMinute; }
		else if (sMinutePattern.test(value))	{ sNewText = value + ":" + sMinute; }
		else if (sHourPattern1.test(value))		{ sNewText = value + sHour; }
		else if (sHourPattern.test(value))		{ sNewText = value + " " + sHour; }
		else if (sYearPattern.test(value))		{ sNewText = value + sYear; }
		else if (sYearPattern1.test(value))   { sNewText = value + "/" + sYear; }
		else if (sMonthPattern.test(value))		{ sNewText = value + sMonth; }
		else if (sMonthPattern1.test(value))	{ sNewText = value + "/" + sMonth; }
		else if (value == "")									{ sNewText = sDay; }

		if (sNewText != "")		
		{
			//Set the new text 
			target.value = sNewText;

			//Cancel the old event			
			if (e.preventDefault) { e.preventDefault(); }
			else									{ e.keyCode = 0; }
		}
  }
}

// =================================================
// Misc functionality 

// Display a pop up window
function showPopup(URL, description, width, height)
{
	win = window.open("", "popupURL", "width=" + width + ", height=" + height + ", toolbar=no, location=no, status=no, resizable=yes, scrollbars=yes");
	win.document.write("<html><head><title>" + description + "</title>");
	win.document.write("<script type=\"text/javascript\">");
	win.document.write("function pageLoadScript()");
	win.document.write("{");
	win.document.write("document.location = \"" + URL + "\"");
	win.document.write("}");
	win.document.write("<\/script>");
	win.document.write("</head>");

	win.document.write("<body onload=\"pageLoadScript()\" style=\"margin: 0px;\">");
	win.document.write("</body></html>");
  win.document.close();
  win.focus();
  
  return false;
}

// Updates a control on the form with the given value - used by a popup to update the calling form
function updateField(value, text, ctlValueId, ctlTextId)
{
	// Set the value to this hidden field
	ctl = eval('document.forms["Form1"].' + ctlValueId);
	if (ctl)
	{
		ctl.value = value;
	}

	// Set the text to this control
	var ctl = document.getElementById(ctlTextId);
	if (ctl)
	{
		ctl.value = text;
	}
}

// Check / Uncheck all check boxes on the page
function selectAll(chk, prefix)
{
	var ctls = document.getElementsByTagName('input')
	var len = prefix.length;
	var isSelected = chk.checked;
	
	for(var i in ctls) {
    if (ctls[i].type == "checkbox") {
	    if (ctls[i].id.substring(0, len) == prefix) {
				ctls[i].checked = isSelected;
			}
    }
  }
}

// Makes a control visible / hidden
function showField(fieldName, visible)
{
	// Set the value to this hidden field
	ctl = document.getElementById(fieldName);
	if (ctl) {
		if (visible == 0) {
			ctl.style.visibility = "hidden";
		}
		else if (visible == -1) {
			if (ctl.style.visibility == "hidden") {
				ctl.style.visibility = "visible";
			} 
			else {
				ctl.style.visibility = "hidden";
			}
		}
		else {
			ctl.style.visibility = "visible";
		}
	}

}

