
/* Function: CheckValidate(txt, rfvC)
 * Purpose: En/Disable a required field validator
 * Parameter: txt = Textbox, rfvC = RequiredFieldValidator for Confirm
 * Note: In order to optomize validation, the Users page has conditional requirements
 * for validation on the password and password confrim edit boxes. If a user is being added,
 * the fields are required, but if they are only updating then they are not. A problem arises
 * when a user DOES edit the password, because the RequiredFieldValidators are turned off. So 
 * a password that is being editted is not checked for confirmation if the Confirm edit box is 
 * blank. So on the KeyUp event of the Passord edit box we check to see if the value of the
 * edit box has a value. If it does, then we turn on the required field validator for confirmation.
 */
function CheckValidate(txt, rfvC)
{	
	if (document.getElementById(txt).value == '')
		ValidatorEnable(document.getElementById(rfvC), false);
	else
		ValidatorEnable(document.getElementById(rfvC), true);
}

/* Calls the pressed() function when a key is pressed
 */
document.onkeypress=pressed; // IE4+ & NS4+ 

/* Function: pressed(e)
 * Purpose: This function evaluates key presses and either saves or adds depending on the state if the grid
 * Parameters: e - KeyPressed Event
 */
function pressed(e) 
{ 
    var code;

    if(!e) 
		var e = window.event;

    if(e.keyCode)
		code = e.keyCode;  //IE & NS
    else if(e.which)
		code = e.which;    //Firefox

	var Val = code;
	f = document.forms[0];

	//Did the user press the Enter key?
	if (Val == 13)
	{
		//Only use this on pages where it exists! 
		if(f.ElEdit)
		{
			/* If there is a value in the Edit, we are editing and do nothing. If there isn't, then we know
			 * that we are not editing and must be trying to add.
			 */
			if (f.ElEdit.value == '')
			{
				if (document.getElementById('lnkAddItem') != null)
				{
					document.getElementById('lnkAddItem').click();
				}
			}	
		}
	}
}

/* Function: EnableTextBox(chkArg, txtArg)
 * Purpose: Enables the URL TextBox when the CheckBox is checked.
 *          Disables the URL TextBox of unchecked.
 * Parameters: chkArg - CheckBox
 *			   txtArg - TextBox
 */
function EnableTextBox(chkArg, txtArg)
{
	chkBox = document.getElementById(chkArg);
	txtBox = document.getElementById(txtArg);
	
	if (chkBox.checked)
	{
		txtBox.disabled = false;
		txtBox.focus();
	}
	else
	{
		txtBox.disabled = true;
		txtBox.value = '';
	}
}

/* Function: OpenCalendar
 * Purpose: Opens the calendar control. Positions Calendar below the text box.
 */
function OpenCalendar(tbID)
{

	/* In order to get the absolute position of an element on the screen,
	 * we need to get the x and y offsets for each element in which the
	 * element we are nested in resides.
	 */
	var offsetTrail = document.getElementById(tbID);
	var lx = 0;
	var ly = 0;
	while (offsetTrail)
	{
		lx += offsetTrail.offsetLeft;
		ly += offsetTrail.offsetTop;
		offsetTrail = offsetTrail.offsetParent;
	}
	//This code does the same thing as above, but was harder to read so only kept for reference
	//var el = document.getElementById(tbID);
	//for (var lx=0,ly=0;el!=null; lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	
	lx = (screen.availWidth / 2);
	ly = (screen.availHeight / 2);
	
	//Now we adjust the top of the window to the botom of the text box.
	//half the height of the window
	//ly += 135;
	//alert(lx);
	//alert(ly);
	
	window.open('/controls/Calendar.aspx?control='+tbID, 'Calendar', 'Height=265, Width=260, left='+lx+'px, top='+ly+'px, resizeable=0, status=0, scroll=0');
}

/* Function: KeyPress
 * Purpose: Can be applied to a link so that the Enter Key clikcs the link.
 */
function KeyPress(link)
{
    result = (event.keyCode == 13)
    if (result)
        link.click()
    event.returnValue = !result
}

/* Function: ConfirmDelete
 * Purpose: Applied to Delete buttons in a DataGrid to give the user chance to cancel a deletion.
 */
function ConfirmDelete()
{
	return confirm("Are you sure you want to delete this record?")
}

/* Function: NoCheck
 * Purpose: Keep a checkbox from being checked!
 */
function NoCheck()
{
    elem = event.srcElement
    elem.checked = !elem.checked
}

/* Function: GetSetVerticalScrollPos
 * Purpose: Save the scroll position of the current form
 * Parameter: mode: 0 = restore, 1 = save
 * Note: Requires a hidden input field on the form...
 *       <input id="VerticalScrollPos" type="hidden" name="VerticalScrollPos" runat="server">
 */
function GetSetVerticalScrollPos(mode)
{
	f = document.forms[0]
	pos = 0;
		
	if (window.pageYOffset)
	{
		pos = window.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
	{
		pos = document.documentElement.scrollTop;
	}
	else if (document.body)
	{
		pos = document.body.scrollTop;
	}
	
	if (mode == 0 && f.VerticalScrollPos != "")
		scrollTo(0, f.VerticalScrollPos.value)
	else if (mode == 1)
		f.VerticalScrollPos.value = pos;
		
	setTimeout("GetSetVerticalScrollPos(1)", 500)
	
	externalLinks();
}


/* function: externalLinks()
 * purpose: JavaScript Document for opening external links in XHTML document
 */
function externalLinks() 
{
    if (!document.getElementsByTagName) 
        return;
    
    var anchors = document.getElementsByTagName("a");
 
    for (var i=0; i<anchors.length; i++) 
    {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
