function keySort(pDropDown, caseSensitive)
{
	var pControl = __GetObject(pDropDown);
	// check the keypressBuffer attribute is defined on the dropdownlist
	var undefined;
	if (pControl.keypressBuffer == undefined)
	{
		pControl.keypressBuffer = '';
	}

	// get the key that was pressed
	var key = String.fromCharCode(window.event.keyCode);
	pControl.keypressBuffer += key;
	if (!caseSensitive)
	{
		// convert buffer to lowercase
		pControl.keypressBuffer = pControl.keypressBuffer.toLowerCase();
	}

	// find if it is the start of any of the options
	var optionsLength = pControl.options.length;
	for (var n=0; n < optionsLength; n++)
	{
		var optionText = pControl.options[n].text;
		if (!caseSensitive)
		{
			optionText = optionText.toLowerCase();
		}
		if (optionText.indexOf(pControl.keypressBuffer,0) == 0)
		{
			pControl.selectedIndex = n;
			return false; // cancel the default behavior since
						// we have selected our own value
		}
	}
	// reset initial key to be inline with default behavior
	pControl.keypressBuffer = key;
	return true; // give default behavior
}

/*
	GetKeyPressed
	returns the last key code pressed 
*/
function GetKeyCodePressed()
{
	return event.keyCode;
}

/*
	allowNumericOnly
	Allow only 0-9 to be entered in the control
*/
function allowNumericOnly(pItem, bAllowDecimal)
{
	var pControl = __GetObject(pItem);
	var keyCode;
	keyCode = event.keyCode;
	if (keyCode == 46)
	{
		if (bAllowDecimal == true)
		{
			if (pControl.value.indexOf(".") > 0)
			{
				return false;
			}
		}
		else
		{
			return false;
		}
	}

	if ( (keyCode==8)  //backspace
		|| (keyCode==0)  //tab
		|| (keyCode==13)) //return key
	{
		return true;
	}
	if ((keyCode < 48 || keyCode > 57) && (keyCode != 46))
	{
		// Allow only integers and decimals if set
		return false;
	}
	return true;
}

/*
	allowNumericOnlyTrapEnterKey
	Allow only 0-9 to be entered in the control and trap the enter key
*/

function allowNumericOnlyTrapEnterKey(pControl, bAllowDecimal, pBtn)
{ 
	return (GetKeyCodePressed() != 13 && 
	        allowNumericOnly(pControl, bAllowDecimal)) ||
	       (GetKeyCodePressed() == 13 && 
	        TrapEnterKey(pBtn));
}

/*
	allowMaxTextLength
	Suppress characters after a certain length has been entered in the specified control
*/
function allowMaxTextLength(pItem, iMax)
{
	var pControl = __GetObject(pItem);
	var keyCode
	keyCode = event.keyCode
	if ( (keyCode==8)  //backspace
		|| (keyCode==0)  //tab
		|| (keyCode==13)) //return key
	{
		return true;
	}
	if (pControl.value.length >= iMax)
	{
		return false;
	}
	return true;
}

/*
	allowUpperCaseOnly
	Change any lowercase characters to uppercase	
*/
function allowUpperCaseOnly(pItem)
{
	var pControl = __GetObject(pItem);
	var keyCode
	keyCode = event.keyCode
	if ( (keyCode==8)  //backspace
		|| (keyCode==0)  //tab
		|| (keyCode==13)
		|| (keyCode==32)) //return key
	{
		return true;
	}
	if ((keyCode >= 48) && (keyCode <= 57))
	{
		return true;
	}
	if ((keyCode >= 65) && (keyCode <= 90))
	{
		return true;
	}
	if ((keyCode >= 97) && (keyCode <= 122))
	{
		event.keyCode = keyCode - 32;
		return true;
	}
	return false;
}

/*
	allowUpperCaseOnly
	Change any lowercase characters to uppercase and trap the enter key
*/
function allowUpperCaseOnlyTrapEnterKey(pControl, pBtn)
{ 
	return (GetKeyCodePressed() != 13 && 
	        allowUpperCaseOnly(pControl)) ||
	       (GetKeyCodePressed() == 13 && 
	        TrapEnterKey(pBtn));
}

/*
	GotoNextControl
	When enter is pressed, focus will move to a specific control
*/	
function GotoNextControl(nextcontrol)
{
	var pControl = __GetObject(nextcontrol);
    if (pControl)
    {
        if (event.keyCode == 13)
        {
            pControl.focus();
            event.keyCode = 0;
            //return false;
        }
    }
    return true;
}

function getJSObject(objID)
{
    var myObj;

    if(document.getElementById)
    {
        myObj = document.getElementById(objID);
    }
    else if (document.layers)
    {
        myObj = document.layers[objID];
    }
    else if (document.all)
    {
        myObj = document.all(objID);
    }

    return(myObj);
}

function GetDate(CtrlName)
{
    ChildWindow = window.open('Calendar.aspx?Date=' + document.forms[0].elements[CtrlName].value + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=160,height=160,top=200,left=200,toolbars=no,scrollbars=no,status=no,resizable=nos");
}

function GetDateSubApp(CtrlName)
{
    ChildWindow = window.open('..\\common\\Calendar.aspx?Date=' + document.forms[0].elements[CtrlName].value + '&CtrlName=' + CtrlName, "PopUpCalendar", "width=160,height=160,top=200,left=200,toolbars=no,scrollbars=no,status=no,resizable=no");
}

/*
	TrapEnterKey
	When an enter key is pressed, fire the click event of a specified button
*/	
function TrapEnterKey(btn)
{
	var pButton = null;
	if (btn != null)
	{
		pButton = __GetObject(btn);
	}
	
    if (document.all)
    {
        if (event.keyCode == 13)
        {
            event.returnValue = false;
			event.cancel = true;
			if (pButton != null)
			{
				pButton.click();
			}
		}
	}
}

/* 
	__GetObject
	Used by methods requiring an object as a parameter.  this allows the page to pass the object as it's string name,
	or by the actuall object by uing getJSObject() on the page.
*/
function __GetObject(pObject)
{
	var pRetVal;
	var t = typeof(pObject);
	if (t == "string")
	{
		pRetVal = getJSObject(pObject); 
	}
	else
	{
		pRetVal = pObject;
	}
	return pRetVal;
}

/* 
	CopyDropDownValueToEditBox
	Copies the value of the selected item in a drop down box to a text box
*/
function CopyDropDownValueToEditBox(pFrom, pTo)
{
	var pcboFrom = __GetObject(pFrom);
	var pedtTo = __GetObject(pTo);
	
	if (pcboFrom != null && pedtTo != null)
	{
		pedtTo.value = pcboFrom.options[pcboFrom.selectedIndex].value;
	}
    return false;
}

/*
	CopyDropDownTextToEditBox
	Copies the text value of the selected item in a combo box to a text box
*/
function CopyDropDownTextToEditBox(pFrom, pTo)
{
	var pcboFrom = __GetObject(pFrom);
	var pedtTo = __GetObject(pTo);
	
	if (pcboFrom != null && pedtTo != null)
	{
		pedtTo.value = pcboFrom.options[pcboFrom.selectedIndex].text;
	}
    return false;
}

function CopyDropDownTextToMultiEditBox(pFrom, pPhone, pEmail, pMnem)
{
	var pcboFrom = __GetObject(pFrom);
	var pedtPhone = __GetObject(pPhone);
	var pedtEmail = __GetObject(pEmail);
	var pedtMnem = __GetObject(pMnem);
	
	if (pcboFrom != null)
	{
		pedtPhone.value = pcboFrom.options[pcboFrom.selectedIndex].value.substring(0, pcboFrom.options[pcboFrom.selectedIndex].value.indexOf(';'));
		pedtEmail.value = pcboFrom.options[pcboFrom.selectedIndex].value.substring(pcboFrom.options[pcboFrom.selectedIndex].value.indexOf(';')+1,pcboFrom.options[pcboFrom.selectedIndex].value.indexOf('#'));
		pedtMnem.value = pcboFrom.options[pcboFrom.selectedIndex].value.substring(pcboFrom.options[pcboFrom.selectedIndex].value.indexOf('#')+1);
	}	
    return false;
}

/* 
	TextBoxIsFilled
	Returns true of a text box has any text
*/
function TextBoxIsFilled(pTextBox)
{
	var bRetVal = false;
	var pedtBox = __GetObject(pTextBox);
	if (pedtBox != null)
	{
		bRetVal = pedtBox.value.length > 0;
	}
	return bRetVal;
}

/* 
	Check
	Sets the checked state of a checkbox or radio button
*/
function Check(pCheckbox, bCheck)
{
	var pchk = __GetObject(pCheckbox);
	if (pchk != null)
	{
		pchk.checked = bCheck;
	}
	return false;
}

function GetChecked(pCheckbox)
{
    var bChecked = false;
	var pchk = __GetObject(pCheckbox);
	if (pchk != null)
	{
		bChecked = pchk.checked;
	}
	return bChecked;
}

/*
	GetText
	Returns the text property of a control
*/
function GetText(pcnt)
{
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		return pControl.value;
	}
	else
	{
		return null;
	}
}

/*
	GetComboValue
	Returns the value property of the selected item in a combo box property
*/
function GetComboValue(pcnt)
{
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		return pControl.options[pControl.selectedIndex].value;
	}
	else
	{
		return null;
	}
}

/*
	GetComboText
	Returns the text value of the selected item in a combo box control
*/

function GetComboText(pcnt)
{
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		return pControl.options[pControl.selectedIndex].text;
	}
	else
	{
		return null;
	}
}

/*
	SetHyperlink
	Sets the href property of a hyperlink control	
*/
function SetHyperlink(pcnt, strhref)
{
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		pControl.href = strhref;
	}
}

function OpenWindow(strHREF, strDefault, strTarget)
{
	if (strHREF == null || strHREF == "")
	{
		strHREF = strDefault;
	}
	window.open(strHREF, target=strTarget);
	return false;
}

/*function ShowControl(pcnt, bShow)
{
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		pControl.enabled = bShow;
		if (bShow == false)
		{
			if (pControl.className != "Hide")
			{			
				pControl.oldClassName = pControl.className;
				pControl.className = "Hide";
			}
		}
		else
		{
			if (pControl.className == "Hide")
			{
				pControl.className = pControl.oldClassName;
			}
		}
	}
	else
	{
		return null;
	}
}*/


function ShowControl(pcnt, bShow, bRetainValidatorState)
{
	debugger;
	var pControl = __GetObject(pcnt);
	if (pControl != null)
	{
		pControl.enabled = bShow;
		if (bShow == false)
		{
			if (pControl.className != "Hide")
			{			
				pControl.oldClassName = pControl.className;
				pControl.className = "Hide";
			}
		}
		else
		{
			if (pControl.className == "Hide")
			{
				pControl.className = pControl.oldClassName;
			}
		}

		if (bRetainValidatorState == false ||
			bRetainValidatorState == null)
		{
			//alert('modifying validators');
			var pItems = pControl.getElementsByTagName('SPAN');
			for (var i = 0; i < pItems.length; i++)
			{
				var val = pItems [i];
				if (typeof(val.controltovalidate) == "string")  // only do validator controls
				{
					if (bShow == false)
					{
						val.oldEnableState = val.enabled;
						ValidatorEnable(val, bShow);
					}
					else
					{
						ValidatorEnable(val, val.oldEnableState);
					}				
				}
			}
		}
	}
	else
	{
		return null;
	}
}

function SetToggleButton(pImageControl, strSource)
{
	if (pImageControl != null)
	{
		//if it's not an anchor tag
		if (typeof(pImageControl.src) != "undefined")
		{
			pImageControl.src = strSource;
		}
		else
		{ //if it is an anchor tag, get the image tag inside if there is one
			var pChildControls = pImageControl.getElementsByTagName('IMG');
			if (pChildControls.length == 1)
			{
				pImageControl = pChildControls[0];
				
				pImageControl.src = strSource;
			}
		}
	}
}


function ToggleSection(pSectionDiv, pButtonImage, bRetainValidatorState)
{
	return ToggleSectionEx(pSectionDiv, pButtonImage, bRetainValidatorState, "/common/images/ShowSection.gif", "/common/images/HideSection.gif");
}

function ToggleMenu(pSectionDiv, pButtonImage, bRetainValidatorState)
{
	return ToggleSectionEx(pSectionDiv, pButtonImage, bRetainValidatorState, "/common/images/ShowMenu.gif", "/common/images/HideMenu.gif");
}

function ToggleSectionEx(pSectionDiv, pButtonImage, bRetainValidatorState, strShowImage, strHideImage)
{
	var pSectionControl = __GetObject(pSectionDiv);
	if (pSectionControl != null)
	{
		var pImageControl = __GetObject(pButtonImage);
		if (pSectionControl.className.toLowerCase() == "hide")
		{
			ShowControl(pSectionControl,true, bRetainValidatorState);
			
			SetToggleButton(pImageControl, strHideImage);
		}
		else
		{
			ShowControl(pSectionControl,false, bRetainValidatorState);
			SetToggleButton(pImageControl, strShowImage);
		}
	}
	return false;
}

function AddParam(strParam, value)
{
	var bTest = true;
	if (typeof(value) == typeof(bTest))
	{
		if (value == true)
		{
			strParam = strParam + "=yes";
		}
		else
		{
			strParam = strParam + "=no";
		}
	}
	else
	{ 
		strParam = strParam + "=" + value;
	}
	
	return strParam;
}

function OpenWindowFeatures(strHREF, strDefault, strTarget, iWidth, iHeight, bResizable, bToolbar, bScrollbar)
{
	var strWindowFeatures = "";

	if (iWidth != null)
	{
		strWindowFeatures = AddParam("width",iWidth)
	}
	
	if (iHeight != null)
	{
		if (iWidth != null)
		{
			strWindowFeatures = strWindowFeatures + ",";
		}
		strWindowFeatures = strWindowFeatures + AddParam("height",iHeight)
	}
	
	if (iWidth != null || iHeight != null)
	{
		strWindowFeatures = strWindowFeatures + ",";
	}
	
	strWindowFeatures = strWindowFeatures +
		AddParam("resizable",bResizable) + "," + 
		AddParam("toolbar",bToolbar);

	if (bScrollbar != null)
	{
		strWindowFeatures += ("," + AddParam("scrollbars", bScrollbar));
	}
		
	if (strHREF == null || strHREF == "")
	{
		strHREF = strDefault;
	}
	
	window.open(strHREF, target=strTarget, windowFeatures=strWindowFeatures);
	return false;
}

function CreateHeaderRow(pBodyGrid, pHeaderGrid, pBodyDiv)
{
	var pBodyControl = __GetObject(pBodyGrid);
	var pHeaderControl = __GetObject(pHeaderGrid);
	if (pBodyControl != null && pHeaderControl != null)
	{
		if (pBodyControl.rows.length > 0 &&
			pHeaderControl.rows.length > 0)
		{
			var pRow = pBodyControl.rows[0];
			var pHeaderRow = pHeaderControl.rows[0];
			
			for (var j = pHeaderRow.childNodes.length - 1; j >= 0; j--) 
			{
				if (pHeaderRow.childNodes[j].nodeName == 'TD') 
				{
					pHeaderRow.removeChild(pHeaderRow.childNodes[j]);
				}
			}

			var iTotalWidth = 0;
			for (var i = 0; i < pRow.cells.length; i++)
			{
				var pCell = pBodyControl.rows[0].cells[i];
				var iWidth = pCell.clientWidth;
				var strText = pCell.innerHTML;//pCell.innerText;
				
				
				iTotalWidth += iWidth;
				
				var pNewCell = pHeaderRow.insertCell();
				pNewCell.innerHTML = strText;
				pNewCell.width = iWidth + /*pHeaderRow.borderWidth;*/1;
			}
			
	
			var pNewCell = pHeaderRow.insertCell();
				pNewCell.innerHTML = " ";
				pNewCell.width = 19;//"100%";//pBodyDiv.clientWidth - iTotalWidth;//pBodyDiv.offsetWidth; //19;



			for (var j = pRow.childNodes.length - 1; j >= 0; j--) 
			{
				if (pRow.childNodes[j].nodeName == 'TD') 
				{
					pRow.removeChild(pRow.childNodes[j]);
				}
			}
			//pHeaderRow.class = "";
			//alert(pRow.style.name);
			//pRow.style.height = 1;
			//pRow.style.table-layout = 'fixed';
			//pRow.style.display = 'block';
			//pRow.style.overflow = 'hidden';
			//pRow.style.display = 'none';
			//pRow.style.visibility = 'hidden';
			//pRow.height = 1;
		}
	}
	return false;
}

function SizeHeaderRow(pBodyGrid, pHeaderGrid, pBodyDiv)
{
	var pBodyControl = __GetObject(pBodyGrid);
	var pHeaderControl = __GetObject(pHeaderGrid);
	if (pBodyControl != null && pHeaderControl != null)
	{
		if (pBodyControl.rows.length > 1)
		{
			var pRow = pBodyControl.rows[1];
			//pRow.style.display = '';
			//pRow.style.visibility = '';
			//alert(pRow.cells.length);
			for (var i = 0; i < pRow.cells.length; i++)
			{
				var pCell = pRow.cells[i];
				var iWidth = pCell.clientWidth;
				var strText = pCell.innerHTML;//pCell.innerText;
				
				pHeaderControl.rows[0].cells[i].width = iWidth + 1;
				pHeaderControl.rows[0].cells[i].innerHTML = strText;
			}
			
			//pRow.style.height = 1;
			//pRow.height = 1;
			//pRow.style.table-layout = 'fixed';
			//pRow.style.display = 'block';
			//pRow.style.overflow = 'hidden';
			//pRow.style.display = 'none';
			//pRow.style.visibility = 'hidden';
		}
	}
	return false;
}

function SyncDivXPosition(pDivMaster, pDivSlave)
{
	var pControl1 = __GetObject(pDivMaster);
	var pControl2 = __GetObject(pDivSlave);
	
	if (pControl1 != null & pControl2 != null)
	{
		pControl2.scrollLeft = pControl1.scrollLeft;
	}
	return false;
}

function SyncDivYPosition(pDivMaster, pDivSlave)
{
	var pControl1 = __GetObject(pDivMaster);
	var pControl2 = __GetObject(pDivSlave);
	
	if (pControl1 != null & pControl2 != null)
	{
		pControl2.scrollTop = pControl1.scrollTop;
	}
	return false;
}


function SaveOldValue(pControl)
{
	var pControl1 = __GetObject(pControl);
	if (pControl1 != null)
	{
		pControl1.OldValue = pControl1.value;
	}
	return false;
}


function RevertValue(pControl)
{
	var pControl1 = __GetObject(pControl);
	if (pControl1 != null)
	{
		pControl1.value = pControl1.OldValue;
	}
	return false;
}

function SetReadOnly(pControl, bReadOnly)
{
	var pControl1 = __GetObject(pControl);
	if (pControl1 != null)
	{
		pControl1.value = pControl1.OldValue;
		
		if (bReadOnly == true)
		{
			if (pControl1.className != "ReadOnly")
			{			
				pControl1.oldClassName = pControl1.className;
				pControl1.className = "ReadOnly";
				pControl1.readonly = true;
			}
		}
		else
		{
			if (pControl1.className == "ReadOnly")
			{
				pControl1.className = pControl1.oldClassName;
				pControl1.readonly = false;
			}
		}
	}
	return false;
}

function SetAllCheckBoxes(pParentControl, bCheck, pMainCheckBox)
{
	var pControl = __GetObject(pParentControl);
	var pChkMain = __GetObject(pMainCheckBox);
	if (pControl != null)
	{
		var pItems = pControl.getElementsByTagName('INPUT');
		for (var i = 0; i < pItems.length; i++)
		{
			var pChk = pItems[i];
			
			if (pMainCheckBox != null && pChk != pChkMain)
			{
				//alert(pChk.name + ' !=  ' + pChkMain.name);
				if (pChk.type.toLowerCase() == "checkbox")  // only do checkbox controls
				{
					//alert('set ' + pChk.name + ' to ' + bCheck);
					pChk.checked = bCheck;
					
				}
			}
			else
			{
				//alert(pChk.name + ' =  ' + pChkMain.name);
			}
		}
	}
	//alert('done');
	return false;
}

function IgnoreEnter()
{
	if ((event.which && event.which == 13) ||  (event.keyCode && event.keyCode == 13))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function SetVisible(pControl, pControl1, pControl2, pControl3, pControl4)
{
        
        var Label1 = document.getElementById(pControl)
        script1 += "if(document.getElementById('" + ddlStatus.ClientID + "').selectedIndex==3)";
        script1 += "textBox.style.display=''; else textBox.style.display='none';";
        this.ddlStatus.Attributes.Add("onChange", script1);
}
