/////////////////////////////////////////////////////////////////////////////
// Button.js
//		Java script functions used by the Button.cs class.

/////////////////////////////////////////////////////////////////////////////
//	Helper function used to find a child element matching the specified
//	tag name and containing the attribute attrName.
function _lpButtonSearchChildElements(obj, tagName, attrName)
{
	var	children = obj.getElementsByTagName(tagName);
	for (var i=0; i < children.length; i++)
	{
		if (children[i] && children[i].getAttribute(attrName))
			return children[i];
	}
	return null;
}

/////////////////////////////////////////////////////////////////////////////
//	Helper function used to find the form associated with the element.
function _lpButtonSearchForm(obj)
{
	while (obj.parentNode)
	{
		if (obj.parentNode.tagName == "form" || obj.parentNode.tagName == "FORM")
			return obj.parentNode;
		obj = obj.parentNode;
	}
}

/////////////////////////////////////////////////////////////////////////////
//	Event function that watches for the enter key to accept submit buttons.
function _lpButtonOnKeyDown(e)
{
	// Target object is srcElement for IE and target for Netscape, Firefox and Safari
	var	target = e.target || e.srcElement;
	// Key code is keyCode IE and which for Netscape, Firefox and Safari
	var keyCode = e.keyCode || e.which;

	// Check for enter key everywhere except textarea objects
	if (keyCode == 13 && target.nodeName != "TEXTAREA")
	{
		// Search for the closest 'submit' button
		var	obj = target;
		var objForm = null;
		while (obj.parentNode)
		{
			if (obj.nodeName == "DIV" || obj.nodeName == "TABLE" || obj.nodeName == "FORM" || obj.nodeName == "BODY")
			{
				// Keep track of the encapsulating form
				if (obj.nodeName == "FORM")
					objForm = obj;			
			
				// Look for a graphic 'submit' buttons
				var	found = _lpButtonSearchChildElements(obj, "A", "lpDoSubmit");
				if (!found)
					found = _lpButtonSearchChildElements(obj, "INPUT", "lpDoSubmit");
				if (found)
				{
					if (!objForm)				
						objForm = _lpButtonSearchForm(obj);
					if (objForm)
					{
						// If the form's action is not a postback to our page then ignore the keypress
						// This allows standard browser processing for secondary forms within our pages
						if (objForm.action && (objForm.action.indexOf("http") == 0 || objForm.action.indexOf("HTTP") == 0) && objForm.action != window.location)
							return true;
					}
				
					// Netscape, FireFox and Safari do not support click on an a tag so we will
					// try click first then resort to redirecting the href if needed
					if (found.click)
						found.click();
					else if (found.href)
					{
						if (found.href != "javascript:;")
							document.location = found.href;
					}
					break;
				}				
			}
			obj = obj.parentNode;
		}
	
		// Stop event processing in Netscape, Firefox and Safari
		if (e.preventDefault)
		{
			e.preventDefault();
			e.stopPropagation();
		}
		return false;	// Stop event processing in IE	
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////
//	Used to capture key events and watch for enter key to accept submit buttons.
function _lpButtonCaptureEnterKey()
{
	// Test for addEventListener (Netscape, Firefox and Safari)
	if (document.addEventListener)
		document.addEventListener("keypress", _lpButtonOnKeyDown, false); 
	// Test for attachEvent (IE)
	else if (document.attachEvent)
		document.attachEvent("onkeydown", _lpButtonOnKeyDown); 
}

/////////////////////////////////////////////////////////////////////////////
//	Used to present confirmation messages prior to postback.
function _lpButtonPostback(button, validate, confirmation, secondconfirmation)
{
	var obj = document.getElementById(button.replace("$", "_"));
	if (obj.type != 'submit')
	{
		if (confirmation.length > 0 && !confirm(confirmation))
			return;
		if (secondconfirmation.length > 0 && !confirm(secondconfirmation))
			return;
	}
	if (validate && typeof(Page_ClientValidate) == 'function')
		Page_ClientValidate();
	if (obj.type != 'submit')
		__doPostBack(button,'click');
}

/////////////////////////////////////////////////////////////////////////////
// Used to replace the source for our <img> when using image buttons
function _lpButtonImageSwap(button, image, ignoredisabled)
{
	var obj = document.getElementById(button);
	if (!obj || obj.tagName != 'A')	return;
	if (obj.disabled && !ignoredisabled) return;
	if (obj.hasChildNodes)
	{
		var tags = obj.getElementsByTagName('img');
		if (tags && tags.length == 1) tags[0].src = image;
		else if (tags && tags.length == 2)
		{
			// Must be a 3 piece background image
			tags[0].src = image;
			tags[1].src = image.replace("_l", "_r");
			tags = obj.getElementsByTagName('td');
			if (tags && tags.length == 3)
			{
				tags[1].style.background = 'url(' + image.replace("_l", "_m") + ')';
				tags[1].style.backgroundRepeat = "repeat-x";
			}		
		}
	}
}

/////////////////////////////////////////////////////////////////////////////
// Used to modify our <a> tag when it becomes disabled if we are using image buttons
function _lpButtonImagePropertyChange(button, image)
{
	var obj = document.getElementById(button);
	if (!obj || obj.tagName != 'A')	return;
	if (event.propertyName == 'disabled')
	{
		if (obj.disabled)
		{
			if (typeof(obj.myoldhref) == 'undefined')
				obj.myoldhref = obj.getAttribute('href');
			obj.removeAttribute('href');
			if (typeof(obj.myoldimage) == 'undefined')
			{
				var tags = obj.getElementsByTagName('img');
				if (tags && tags.length > 0) obj.myoldimage = tags[0].src;
			}
			_lpButtonImageSwap(button, image, true);
		}
		else
		{
			if (typeof(obj.myoldhref) != 'undefined')
				obj.setAttribute('href', obj.myoldhref);
			if (typeof(obj.myoldimage) != 'undefined')
				_lpButtonImageSwap(button, obj.myoldimage, false);
		}
	}
}

