/*
4/2/2008
2/20/2008
7/26/07
1/1/07
11/10/06
10/23/06
v 7/7/06
v 1/20/06
v 9/26/05
v 11/27/04
v 10/23/04
v 10/9/04
v 6/25/04
*/

// 9/26/05 quick browser tests
var ns4 = (document.layers) ? true : false;
var ie4 = (document.all && !document.getElementById) ? true : false;
var ie5 = (document.all && document.getElementById) ? true : false;
var ns6 = (!document.all && document.getElementById) ? true : false;

	function ConfirmDelete1()
	{
	/*******************************************************************************
	v 12/29/03	
	Will once request user to confirm deletion of an item.
	Usually called by click event of a delete button.
	*******************************************************************************/
	 if (confirm("Are you sure you want to delete this item?"))
		return true;
	else
		return false;
	}

	function ConfirmDelete2()
	{
	/*******************************************************************************
	v 12/26/03	
	Will twice request user to confirm deletion of an item.
	Usually called by click event of a delete button.
	*******************************************************************************/
	 if (confirm("Are you sure you want to delete this item?"))
		{
		if (confirm("Are you REALLY sure ?"))
			return true;
		else
			return false;
		}
	else
		return false;
	}
	
	function ConfirmDeleteX(iTimes, sDescrip, sMsgX)
	{
	/*******************************************************************************
	v 1/20/06
	Will request X # times user to confirm deletion of an item.
	Usually called by click event of a delete button.
	*******************************************************************************/
	var iCnt;
	var sMsg1;
	var sMsg2;
	var sX;
	
	sX="";
	if(iTimes == null || iTimes == "" || iTimes < 1)
		iTimes=1;
	
	if (sDescrip == null || sDescrip == "")
		sDescrip = "item";
		
	sMsg1="Are you";
	sMsg2=" sure you want to delete this " + sDescrip + "?";
	
	if (sMsgX != null && sMsgX != "")
		sMsg2 += "\n" + sMsgX;
	
	for(iCnt=0; iCnt<iTimes; iCnt++)
	{
		if(iCnt > 0)
		{
			if(sX != "")
				sX += ",";
			sX = sX + " REALLY";
		}
		if (!confirm(sMsg1 + sX + sMsg2))
			return false;
	}
	return true;
	}

	function DoFocus(strFocus)
	{
	/*******************************************************************************
	10/23/06
	v 10/23/04 passing control name by parameter so hidden textbox is not needed
	retaining it for backward compatibility
	v 12/26/03	
	This will set focus to the control whose ID is specified by the value of the 
	hidden text box txtSetFocus.
	Useful when events on the server side dictate which control should receive focus.
	Usually called by the page load event.
	*******************************************************************************/
		//var strFocus;
		var varFocus;
		
		if (strFocus == null || strFocus == "")	//10/23/06
		{
			varFocus = document.getElementById("txtSetFocus");
			if (varFocus != null)
			{
				strFocus = varFocus.value;
			}
		}
		if (strFocus != null && strFocus != "")	//10/23/06
		{
		   varFocus = document.getElementById(strFocus);
			if (strFocus != "" && varFocus != null)
			{
			   varFocus.focus();
			   //10/23/06
			   if (varFocus.type != null && varFocus.type.indexOf("select")!= 0)
				{
				   varFocus.select();
				}
			}
		}
	}

function DisableControl(strControl)
{
	var txtEnable;
	
	txtEnable = document.getElementById(strControl);
	if (txtEnable != null)
	txtEnable.disabled = "disabled";
	return true;
}

function handleError() 
{
	/*******************************************************************************
	v 6/25/04	
	Block errors
	*******************************************************************************/
	return true;
}

function ClearFields()
{
	/*******************************************************************************
	v 10/9/04
	Reset controls contained in array 'arClearFields' to value contained in the array
	var arClearFields =  new Array('control name1','reset value1',
	'control name2','reset value2','control name3','reset value 3');
	*******************************************************************************/
	var i;
	var ctl;
		for (i = 0; i < arClearFields.length; i+=2)
		{
			ctl= document.getElementById(arClearFields[i]);
			if (ctl != null)
			ctl.value = arClearFields[i+1];
		}
}

	/*******************************************************************************
function CancelEnterKey()
   {
   if (window.event.keyCode == 13)  // checks whether the SHIFT key 
          // is pressed
   {
    window.event.cancelBubble = true;
    window.event.returnValue = false;
   }
   }
	*******************************************************************************/

function CatchEnter(strControl)
{
	/*******************************************************************************
	4/2/2008
	v 2/20/2008	strControl can be either a control or the id of a control
	v 10/23/04
	Capture the Enter key and cancel it's default action
	strControl can be control itself or the uniqueID of a control
	
	If strcontrol is supplied, click that object instead
	*******************************************************************************/
	var ctl;
	if(event.keyCode == 13)
	{
		if (strControl !=null)
		{
			if(strControl.type != null)	// 2/20/2008 
			{
				ctl = strControl;
			}
			else
			{
				ctl= document.getElementById(strControl);
			}
			if (ctl != null)
			{
				if (ctl.type == 'textarea')	//	4/2/2008
				{
					event.cancelBubble = true;
					return true;
				}
				else
				{
					ctl.click();
					return true;
				}
			}
		}
		else
		   return false;
	}
	else
		return true;
}

function ClientTimeZoneOffset(strControl)
	/*******************************************************************************
	v 11/27/04
	If strcontrol is supplied, insert the client time zone offset from UTC time in minutes.
	*******************************************************************************/
{
	if (strControl !=null)
	{
		ctl= document.getElementById(strControl);
		if (ctl != null)
		{
			var datNow = new Date();
			strControl.value = datNow.getTimezoneOffset();
		}
	}
}

	/*******************************************************************************
	v 9/26/05
	show or hide an object.
	object still takes space on page if hidden
	*******************************************************************************/
function show(sw,obj) {
	if (sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'visible';
	if (!sw && (ie4 || ie5) ) document.all[obj].style.visibility = 'hidden';
	if (sw && ns4) document.layers[obj].visibility = 'visible';
	if (!sw && ns4) document.layers[obj].visibility = 'hidden';
}

	/*******************************************************************************
	v 9/26/05
	Change display style of an object.
	If 'none', object takes no space on page
	*******************************************************************************/
function DisplayNew(sDisplay,obj) {
	if ((ie4 || ie5) ) document.all[obj].style.display = sDisplay;
	if (ns4) document.layers[obj].display = sDisplay;
}

function TabOnEnter()
{
	/*******************************************************************************
	v 1/16/06
	Capture the Enter key and replace action with tab key
	*******************************************************************************/
	if(event.keyCode == 13)
	{
		event.keyCode = 9;
	}
}

	function ConfirmX(iTimes, sMsg)
	{

/*******************************************************************************
	v 7/7/06
	Will request X # times user to confirm.
	Adding a 'Reconfirm' after first time.
	Usually called by click event of a button.
	*******************************************************************************/
	var iCnt;

	if(iTimes == null || iTimes == "" || iTimes < 1)
		iTimes=1;
	
	if (sMsg == null || sMsg == "")
		sMsg = "Please confirm.";
		
	for(iCnt=0; iCnt<iTimes; iCnt++)
	{
		if(iCnt == 1)
		{
			sMsg += "\n Please re-confirm!";
		}
		if (!confirm(sMsg))
			return false;
	}
	return true;
	}

function numCheck(eventObj, obj, decimals, allowNeg)
{
var keyCode
var decPos
var curPos

	// Check For Browser Type
	if (document.all)
	{ 
		keyCode=eventObj.keyCode
	}
	else
	{
		keyCode=eventObj.which
	}

var str=obj.value

	decPos=str.indexOf(".")

	if(keyCode==46)
	{ 
		if (decimals == 0 || decPos>=0)
		{
			return false
		}
	}
	
	curPos=caretPos(obj)
	
	if (decPos > 0) 
	{
		if (curPos - decPos > decimals + 1)
		{
			return false
		}
	}
	

//added minus
	if(keyCode==45)
	{ 
		if (allowNeg==false || str.indexOf("-")>=0)	
		{
			return false
		}
	}

	if((keyCode<48 || keyCode >58) && (keyCode != 46) && (keyCode != 45))
	{ // Allow only integers and decimal points 
		return false
	}
	return true
}

function caretPos(txtBox)
{
	if (txtBox.selectionStart)
	{
		return obj.selectionStart;
	}
	var i=txtBox.value.length+1;
	if (txtBox.createTextRange)
	{
		theCaret = document.selection.createRange().duplicate();
		while (theCaret.parentElement()==txtBox && theCaret.move("character",1)==1) --i;
		return i;
	}
	else return -1;
}

function lenCheck(obj, chars)
{
/***************************************************
v 1/16/06
limit input of text area to specified length 
call from onkeypress event
****************************************************/
var str=obj.value;

	if(str.length >= chars)
	{ 
			return false;
	}
	else
		return true;
}

function lenFix(obj, chars)
{
/***************************************************
v 1/16/06
limit text area to specified length 
needed in case text is pasted into the control
call from onBlur event
****************************************************/
var str=obj.value;

	if(str.length > chars)
	{ 
			obj.value = str.slice(0,chars);
	}
}

function UpdateControlValues(ctL, ctlVal, arCopyControls)
{
/*******************************************************************************
	v 10/27/06
	Update values of control(s) with values of other control(s) 
	if value of a criteria control (ctL) equals the specified value (ctlVal).
	Clientid's of control(s) to copy from are in the even # indexes of array 'arCopyControls'.
	Clientid's of the control(s) to copy to are the odd # indexes. 
	ctL must be a control with a checked property or the clientid of a control with a checked property
	If ctlVal is null, "" or matches the checked property of ctL,
	the values are copied.
	*******************************************************************************/
var i;
var ctl1;
var ctl2;
var opt;
	
	if (typeof(ctL)=="string") 
	{
		ctL = document.getElementById(ctL);
		if (ctL == null)
			return;
	}
	if(ctlVal != "" && ctlVal != null && ctL.checked != ctlVal)
	{
		return;
	}
	for (i = 0; i < arCopyControls.length; i+=2)
	{
		ctl1= document.getElementById(arCopyControls[i]);
		if (ctl1 != null)
		{
			ctl2=document.getElementById(arCopyControls[i +1]);
			if (ctl2 != null && ctl1.type !=null)
			{
				switch(ctl1.type)
				{
					case "checkbox":
					{
						ctl2.checked = ctl1.checked;
						break;
					}
					case "radio":
					{
						ctl2.checked = ctl1.checked;
						break;
					}
					case "select-multiple":
					{
						for (opt=0;opt<ctl1.options.length;opt++) 
						{
							ctl2.options[opt].selected = ctl1.options[opt].selected;
						}
						break;
					}
					default:	// "text", "select-one", or anything else
					{
						ctl2.value = ctl1.value;
						break;
					}
				}
			}
		}
	}
}

	function ValueFromMinToMax(ctlId, MinVal, MaxVal)
	{
	/*******************************************************************************
	1/1/07
	*******************************************************************************/
		//var strFocus;
		var ctL;
		var Val;
		
		ctL = document.getElementById(ctlId);
		if (ctL != null)
		{
			Val = ctL.value;
			if (Val == null || Val == "")
			{
			   return MinVal;
			}
			else if (Val < MinVal)
			{ 
			   return MinVal; 
			}
			else if (Val > MaxVal)
			{ 
			   return MaxVal; 
			}
			else
			{
			   return Val;
			}
		}
		else
		{
		   return MinVal;
		}
	}

function UpdateControlsFromArray(ctL, ctlVal, arCopyControls)
{
/*******************************************************************************
	v 7/26/07
	Update values of control(s) with values contained in the even elwments of an array 
	if value of a criteria control (ctL) equals the specified value (ctlVal).
	Values to copy from are in the even # indexes of array 'arCopyControls'.
	Clientid's of the control(s) to copy to are the odd # indexes. 
	ctL must be a control with a checked property or the clientid of a control with a checked property
	If ctlVal is null, "" or matches the checked property of ctL,
	the values are copied.
	*******************************************************************************/
var i;
var ctl2;
var opt;
	
	if (typeof(ctL)=="string") 
	{
		ctL = document.getElementById(ctL);
		if (ctL == null)
			return;
	}
	if(ctlVal != "" && ctlVal != null && ctL.checked != ctlVal)
	{
		return;
	}
	for (i = 0; i < arCopyControls.length; i+=2)
	{
		ctl2=document.getElementById(arCopyControls[i +1]);
		if (ctl2 != null)
		{
			switch(ctl2.type)
			{
				case "checkbox":
				{
					ctl2.checked = arCopyControls[i];
					break;
				}
				case "radio":
				{
					ctl2.checked = arCopyControls[i];
					break;
				}
				case "select-multiple":
				{
/* Not implemented for listboxes
					for (opt=0;opt<ctl1.options.length;opt++) 
					{
						ctl2.options[opt].selected = ctl1.options[opt].selected;
					}
*/
					break;
				}
				default:	// "text", "select-one", or anything else
				{
					ctl2.value = arCopyControls[i];
					break;
				}
			}
		}
	}
}

window.onerror = handleError;
