/****************************************************************************************
 * If multiple buttons exist, the button that should handle the enter key press needs 
 * to be specified in the onKeyDown of a textbox in that form.
 * Example:
 *   <asp:TextBox id="TextBoxID" runat="server" OnKeyDown="KeyDownHandler(btnSubmit)" />
****************************************************************************************/  
function KeyDownHandler(buttonName)
{	
	if (event.keyCode == 13)
	{
		event.returnValue = false;
		event.cancel = true;
		buttonName.click();
	}
}

function IsKeyEnter(buttonName)
{
	document.forms[0].elements[14].name;
	return;	
}
// Browser detection and layer object creation from DHTMLCentral.com need copyright message below.
/****************************************************************************   
DHTML library from DHTMLCentral.com
*   Copyright (C) 2001 Thomas Brattli 2001
*   This script was released at DHTMLCentral.com
*   Visit for more great scripts!
*   This may be used and changed freely as long as this msg is intact!
*   We will also appreciate any links you could give us.
*
*   Made by Thomas Brattli 2001
***************************************************************************/
function lib_bwcheck()
{ 
  this.ver = navigator.appVersion;
  this.agent = navigator.userAgent;
  this.dom = document.getElementById?1:0;
  this.opera5 = this.agent.indexOf("Opera 5")>-1;
  this.ie5 = (this.ver.indexOf("MSIE 5")>-1 && this.dom && !this.opera5)?1:0; 
  this.ie6 = (this.ver.indexOf("MSIE 6")>-1 && this.dom && !this.opera5)?1:0;
  this.ie7 = (this.ver.indexOf("MSIE 7")>-1 && this.dom && !this.opera5)?1:0;
  this.ie4 = (document.all && !this.dom && !this.opera5)?1:0;
  this.ie = this.ie4||this.ie5||this.ie6;
  this.mac = this.agent.indexOf("Mac")>-1;
  this.ns6 = (this.dom && parseInt(this.ver) >= 5) ?1:0; 
  this.ns4 = (document.layers && !this.dom)?1:0;
  this.bw = (this.ie7 || this.ie6||this.ie5||this.ie4||this.ns4||this.ns6||this.opera5);
  this.maximized = true;
  this.minimized = false;
  return this;
}

bw = new lib_bwcheck();

//* Layer object creation.
function lib_obj(obj, nest)
{ 
  if(!bw.bw) 
		return;
	
  nest=(!nest) ? "":'document.'+nest+'.';
  this.evnt=bw.dom? document.getElementById(obj):
    bw.ie4?document.all[obj]:bw.ns4?eval(nest+"document.layers." +obj):0;	
  if(!this.evnt) 
	return lib_message('The layer does not exist ('+obj+')' 
    +'- \nIf your using Netscape please check the nesting of your tags!');
 
  this.css=bw.dom||bw.ie4 ? this.evnt.style : this.evnt;
  this.writeIt = lib_writeIt; 
  
  this.ref=bw.dom||bw.ie4 ? document:this.css.document;
  
  this.x=parseInt(this.css.left)||this.css.pixelLeft||this.evnt.offsetLeft||0;
  this.y=parseInt(this.css.top)||this.css.pixelTop||this.evnt.offsetTop||0
  this.w=this.evnt.offsetWidth||this.css.clip.width||
    this.ref.width||this.css.pixelWidth||0; 
  this.h=this.evnt.offsetHeight||this.css.clip.height||
    this.ref.height||this.css.pixelHeight||0
  this.c=0 //Clip values
  if((bw.dom || bw.ie4) && this.css.clip) {
  this.c=this.css.clip; this.c=this.c.slice(5,this.c.length-1); 
  this.c=this.c.split(' ');
  for(var i=0;i<4;i++){this.c[i]=parseInt(this.c[i])}
  }
  this.ct=this.css.clip.top||this.c[0]||0; 
  this.cr=this.css.clip.right||this.c[1]||this.w||0
  this.cb=this.css.clip.bottom||this.c[2]||this.h||0; 
  this.cl=this.css.clip.left||this.c[3]||0
  this.obj = obj + "Object"; eval(this.obj + "=this");  
  return this
}

function lib_writeIt(text,startHTML,endHTML)
{
	if(bw.ns4)
	{
		if(!startHTML)
		{
			startHTML=""; endHTML=""
		}
		this.ref.open("text/html"); 
		this.ref.write(startHTML+text+endHTML); 
		this.ref.close()
	}
	else 
		this.evnt.innerHTML=text
}
	
if (bw.ns4)
	document.captureEvents(Event.MOUSEMOVE);

var mX = 0;
var mY = 0;	

function ShowToolTip(heading, txt, e, pushLeft, pushTop)
{
	// Window height & width.
	var screenWidth = bw.ns4 ? (window.innerWidth) : (document.body.clientWidth);
	var screenHeight = bw.ns4 ? (window.innerHeight) : (document.body.clientHeight);	
	
	// Get Mouse Coordinates
	mX = 0;
	mY = 0;	
	
	if (! e)
		var e = window.event;	
	
	if (e.pageX || e.pageY)
	{
		mX = e.pageX;
		mY = e.pageY;
	}
	else if (e.clientX || e.clientY)
	{
		mX = e.clientX + document.body.scrollLeft;
		mY = e.clientY + document.body.scrollTop;
	}		
	
	if (mX + LToolTipWidth + 75 >= screenWidth) // Don't let the tooltip layer cause a horizontal scrollbar.
		LToolTip.x = (screenWidth - 10) - LToolTipWidth;
	else
		LToolTip.x = mX + 75;		
		
	if (pushLeft) // Could be a select menu in the way. Allow extra x spacing.
		LToolTip.x += pushLeft;		
		
	LToolTip.css.left = LToolTip.x + 'px';	
	
	LToolTip.y = mY;
	if (pushTop) // Could be a select menu in the way. Allow extra y spacing.
		LToolTip.y -= pushTop;		
		
	LToolTip.css.top = LToolTip.y + 'px'; // Allow space for alt text.	
	LToolTip.css.visibility = "visible";	
	
	var strText =	'<table class="TableBordered" width="100%" height="100%" cellpadding="2" cellspacing="0">';
	strText +=		'  <tr>';
	strText +=		'    <th align="left" background="/PMIS/images/trHeader.gif" style="padding-left:30px;color:#FFFFFF; height:20px;">' + heading + '</th>';
	
	strText +=		'  </tr>';
	strText +=		'  <tr>';
	strText +=		'    <td align="left" valign="top" height="95%" style="padding-right:25px;">' + txt + '</td>';
	strText +=		'  </tr>';
	strText +=		'</table>';
	
	LToolTip.writeIt(strText);	
}

function HideToolTip()
{	
	LToolTip.css.visibility = "hidden";		
}

function ShowLayer(layerName)
{	
	eval(layerName + '.css.visibility = "visible"');	
}

function HideLayer(layerName)
{
	eval(layerName + '.css.visibility = "hidden"');
}

/***************************************************
 * Checks all checkboxes on a form.
 *		Usage: onClick="return CheckAll(this.form)"
***************************************************/
function CheckAll(form)
{
	if (confirm("Are you sure you want to check every checkbox on this page?"))
	{
		for (i = 0; i < form.elements.length; i++)
		{
			if (form.elements[i].type == "checkbox")
				form.elements[i].checked = true;
		}
	}	
	return false;
}

/***************************************************
 * Un-checks all checkboxes on a form.
 *		Usage: onClick="return UnCheckAll(this.form)"
***************************************************/
function UnCheckAll(form)
{
	if (confirm("Are you sure you want to un-check every checkbox on this page?"))
	{
		for (i = 0; i < form.elements.length; i++)
		{
			if (form.elements[i].type == "checkbox")
				form.elements[i].checked = false;
		}
	}		
	return false;
}

// Netscape resize fix
function WM_netscapeCssFix()
{
  if (document.WM.WM_netscapeCssFix.initWindowWidth != window.innerWidth ||
        document.WM.WM_netscapeCssFix.initWindowHeight != window.innerHeight) 
  {
     document.location = document.location;
  }
}

function WM_netscapeCssFixCheckIn() 
{
  if ((navigator.appName == 'Netscape') && (parseInt(navigator.appVersion) == 4)) 
  {
    if (typeof document.WM == 'undefined')
    {
       document.WM = new Object;
    }
    if (typeof document.WM.WM_scaleFont == 'undefined')
    {
       document.WM.WM_netscapeCssFix = new Object;
       document.WM.WM_netscapeCssFix.initWindowWidth = window.innerWidth;
       document.WM.WM_netscapeCssFix.initWindowHeight = window.innerHeight;
    }
    window.onresize = WM_netscapeCssFix;
  }
}

WM_netscapeCssFixCheckIn();

function ValidateNumeric(object)
{
	var re = /,/g;
	if (object.value != "" && (object.value.replace(re, "") != parseFloat(object.value.replace(re, ""))))
	{
		alert("Please enter a valid numeric amount!");
		object.focus();
	}
}

function ValidateDate(object)
{
	if (object.value != "")
	{
		if (! IsDate(object))
		{
			alert("Please enter a valid date!");
			object.focus();
		}
	}
}

// Formats a number ex:50000 returns as 50,000.00
function FormatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + dblValue + '.' + strCents);
}
  
// Date Validation - Returns Boolean
//	Pass a form object to the function.
function IsDate(object)
{
	var dateString = new String(object.value);
	var blnValidDate = true;
	var separator;
	var arSeparatorPositions = new Array();
	var month;
	var day;
	var year;
	var blnLeapYear = false;	
	var dateobj = new Date();
	var currentYear = dateobj.getYear();	

	// Determine Separator type (dash or slash)
	if (dateString.indexOf('/') != -1)
		separator = '/'
	else if (dateString.indexOf('-') != -1)
		separator = '-';
	else
	{
		blnValidDate = false;
	}
	
	// Get the positions of the separators
	for (i = 0; i < dateString.length; i++)
	{
		if (dateString.charAt(i) == separator)
			arSeparatorPositions[arSeparatorPositions.length] = i;
	}

	if (arSeparatorPositions.length != 2)
	{
		blnValidDate = false;
	}

	month = dateString.substring(0, arSeparatorPositions[0]);
	day = dateString.substring((arSeparatorPositions[0] + 1), arSeparatorPositions[1]);
	
	// Year could be two digits or four
	var tmpYear = dateString.substring((arSeparatorPositions[1] + 1), dateString.length);

	if (tmpYear.length == 4 && parseInt(tmpYear.charAt(0), 10) != 0)
		year = tmpYear;

	// Two digit year, decide which century prefix to give it
	else if (tmpYear.length == 2)
	{
		var thisCentury = new String(currentYear);
		thisCentury = thisCentury.substring(0, 2);
		thisCentury += "00";

		var prevCentury = new String(currentYear - 100);
		prevCentury = prevCentury.substring(0, 2);
		prevCentury += "00";	

		var cutOff = currentYear + 20;

		var formattedYear;

		if (cutOff - parseInt(tmpYear, 10) < thisCentury)
			formattedYear = parseInt(prevCentury, 10) + parseInt(tmpYear, 10);
		else
			formattedYear = parseInt(thisCentury, 10) + parseInt(tmpYear, 10);
			
		year = formattedYear
	}
	
	else
	{
		blnValidDate = false;
	}	
	
	// Make sure month, day, and year are valid integers	
	if (parseInt(month, 10) == month && parseInt(day, 10) == day && parseInt(year, 10) == year)
	{
		if (month < 1 || month > 12 || day < 1 || day > 31)			
		{
			blnValidDate = false;
		}
	}	
	else
	{
		blnValidDate = false;
	}
	
	// Check for months that only have 30 days
	if (parseInt(month, 10) == 4 && parseInt(day, 10) == 31) // April
		blnValidDate = false;
		
	if (parseInt(month, 10) == 6 && parseInt(day, 10) == 31) // June
		blnValidDate = false;
		
	if (parseInt(month, 10) == 9 && parseInt(day, 10) == 31) // September
		blnValidDate = false;
		
	if (parseInt(month, 10) == 11 && parseInt(day, 10) == 31) // November
		blnValidDate = false;
			
	
	// Check for leap year
	if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
	{
		blnLeapYear = true;
	}

	// Check for valid February day
	if (month == 2)
	{
		if ((blnLeapYear && day > 29) || (! blnLeapYear && day > 28))
		{
			blnValidDate = false;
		}
	}
	
	if (blnValidDate)
		object.value = parseInt(month, 10) + '/' + parseInt(day, 10) + '/' + parseInt(year, 10);

	return blnValidDate;
}

function RolloverTabs(formObj, imageUrl, bottomImageUrl)
{
	formObj.src = imageUrl;	
	document.forms[0].imgRolloverBottom.src = bottomImageUrl;
}

function Rollover(formObj, imageUrl)
{
	formObj.src = imageUrl;
}

// catch all errors...
function stopError()
{
	return true;
}

function RemoveTextBoxValue(formObj, defaultText)
{
	if (formObj.value == defaultText)
	  formObj.value = '';
}

function ReplaceTextBoxValue(formObj, txt)
{
	if (formObj.value == '' || formObj.value == txt)
		formObj.value = txt;
}
//window.onerror=stopError; // Disable javascript errors. Enable for production.


	
/***********************************************************
   *	Parsing The Querystring with Javascript
   *	By Peter A. Bromberg, Ph.D.
************************************************************/
function PageQuery(q)
{
	if(q.length > 1)
	{
		this.q = q.substring(1, q.length);
	}
	else
	{
		this.q = null;
	}
	
	this.keyValuePairs = new Array();
	
	if(q)
	{
		for(var i=0; i < this.q.split("&").length; i++) {
		this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
			{
				return unescape(this.keyValuePairs[j].split("=")[1]);
			}
		}
		return false;
	}
	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++)
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	this.getLength = function() { return this.keyValuePairs.length; } 
}
