
// ControlKeyPress(field, e, btnId)
//	Method intercepts key stroke for forms and directs <enter>
//	to a specific button.
function ControlKeyPress(field, e, btnId)
{
	var keycode;
	var btn;

	if (!window.event && !e)	// just exit here
		return true;

	if (window.event)	// ie-type event capable
		keycode = window.event.keyCode;
	else
		keycode = e.which;

	if (keycode == 13)
	{
		if (btnId)
			return PerformClick(btnId);
	}

	return true;
}

// ControlKeyPressExt(field, e, btnAcceptId, btnCancel)
//	Method intercepts key stroke for forms and directs <enter> and <esc>
//	to a specific buttons.
function ControlKeyPressExt(field, e, btnAcceptId, btnCancelId)
{
	var keycode;
	var btn;
	var shift = false;

	if (!window.event && !e)	// just exit here
		return true;

	if (window.event)	// ie-type event capable
	{
		keycode = window.event.keyCode;
		shift = window.event.shiftKey;
	}
	else
	{
		keycode = e.which;
		shift = false;//TODO: GET SHIFT FOR THIS?
	}

	if (keycode == 13)
	{
		if (btnAcceptId)
			return PerformClick(btnAcceptId);
	}
	else if ((keycode == 27) && shift)
	{
		if (btnCancelId)
			return PerformClick(btnCancelId);
	}

	return true;
}

// PerformClick(btnId)
//	Clicks a specified button.
function PerformClick(btnId)
{
	var btn;

	// this is how we check for newer browser that follows DOM guidelines...
	if (document.getElementById && btnId)
	{
		btn = document.getElementById(btnId);

		if (btn && btn.click)
		{
			btn.click();
			return false;
		}
	}
	else
	{
		// if we care about ns4, then change
		// this else clause to handle it.
	}

	return true;
}

// GetDaysInMonth(month, year)
function GetDaysInMonth(month, year)
{
	if (isNaN(month))
		return 31;

	month = parseInt(month);

	if (isNaN(year))
	{
		var today = new Date();
		year = parseInt(today.getFullYear());
	}

	if (month == 2)
		return ((year % 4 == 0) && ((year % 100 != 0) || (year % 400 == 0))) ? 29 : 28;
	else 
		return ((month == 4) || (month == 6) || (month == 9) || (month == 11)) ? 30 : 31;
}

// SetControlFocus(controlId)
//	Sets focus to the control with specified control ID.
function SetControlFocus(controlId)
{
	var control;

	if (document.getElementById)
	{
		control = document.getElementById(controlId);

		if (control && control.focus)
		{
			control.focus();
		}
	}

	return true;
}

// attempts to scroll the specified control into view (at bottom of the page).
function ScrollToControl(controlId)
{
	var control;

	if (document.getElementById)
	{
		control = document.getElementById(controlId);

		if (control.scrollIntoView)
		{
			control.scrollIntoView(false);

			if (control && control.focus)
				control.focus();
		}
	}
}

// on validation failure, attempts to set focus on the first invalid control
function ValidatorFocus()
{
	if (document.getElementById)
	{
		for (var index = 0; index < Page_Validators.length; index++)
		{
			if (!Page_Validators[index].isvalid)
			{
				if (Page_Validators[index].controltovalidate)
					ScrollToControl(Page_Validators[index].controltovalidate);
				else if (Page_Validators[index].robscontroltovalidate)
					ScrollToControl(Page_Validators[index].robscontroltovalidate);
				break;
			}
		}
	}
}

// custom validation method for street addresses in the Profile_Address.ascx control.
function ValidateStreetAddress(src, args)
{
	var valid = false;
	var lines = eval(src.id + "_lines");

	src.robscontroltovalidate = eval(src.id + "_focusControl");

	if (lines.length > 0)
	{
		for (var i = 1; i < lines.length; i++)
		{
			for (var j = i; j > 0; j--)
			{
				if ((lines[j - 1].value.length == 0) && (lines[j].value.length > 0))
				{
					lines[j - 1].value = lines[j].value;
					lines[j].value = "";
				}
			}
		}

		valid = (lines[0].value.length > 0);
	}

	args.IsValid = valid;
}

// toggle the display of an item on the page
function toggleDisplay(id)
{
	if (document.getElementById)
	{
		var control = document.getElementById(id);
		if (control && control.style)
		{
			var display = false;

			if (control.currentStyle)
				display = (control.currentStyle.display == 'none');
			else
				display  = (control.style.display == 'none');
				
			control.style.display = display ? '' : 'none';
		}
	}
}
