var subFormIds = new Array();
var subFormSubmitIds = new Array();
var activeSubForm="";
var postToSubPage = false;

///////////////////////////////////////////////
/// SUBFORM STUFF
///////////////////////////////////////////////
function registerSubForm(formID, submitID) {
	var subForm = document.getElementById(formID);

	subFormIds.push(formID);

	if (subForm.addEventListener) {
		//add submit control ID to our associative array
		subFormSubmitIds[formID] = submitID;

		//register event
		subForm.addEventListener("keypress", domSubFormKeyPress, true);
	}
	else
	{
	  subForm.onkeypress = function (e) { nonDomCompliantSubFormKeyPress(e,submitID,formID); };
	  subForm.onclick = function (e) { nonDomCompliantSubFormClick(e,formID); };
	}
}

function domSubFormKeyPress(theEvent) {
		if (theEvent.keyCode == 13) {

		if (theEvent.currentTarget.nodeName.toLowerCase() == "textarea")
			return;
			
		// HACK: don't steal event from rich text edit
		if (theEvent.currentTarget.className == 'textedit')
			return;
			
		// get the control that should do the submit
		var submitCt = document.getElementById(subFormSubmitIds[theEvent.currentTarget.id]);
		
		if (submitCt) {
			
			//the control was found, stop propagation and prevent the default action
			theEvent.preventDefault();
			theEvent.stopPropagation();
			
			//set the subformid for validation
			activeSubForm = theEvent.currentTarget.id;
			
			//invoke click if present
			if (submitCt.click) {
				submitCt.click();
			} else {
				var executeHref = true;
				if (submitCt.onclick) {
					executeHref = submitCt.onclick();
					if (isUndefined(executeHref)) executeHref = true;
				}
				//else check for href of hyperlinks
				if (submitCt.href && executeHref) {
					if (submitCt.href.substr(0,11).toLowerCase() == "javascript:") {
						code = decodeURI(submitCt.href.replace(/javascript:/, ''));
					} else {
  						code = "window.location.href='" + submitCt.href + "'";
					}
					eval(code);
				}
			}
		}
	}	
}

// This function handles the keypress for IE
function nonDomCompliantSubFormKeyPress(e, submitCtrlId, subFormId) {
	if (e == null) e = window.event;
	
	if (e.keyCode == 13) {
		var submitCt = document.getElementById(submitCtrlId);
		if (e.srcElement.nodeName.toLowerCase() == 'textarea')
			return;
			
		// HACK: don't steal event from rich text edit
		if (e.srcElement.className == 'textedit')
			return;

		if (submitCt) {
			//prevent event propagation
			e.returnValue = false;
			e.cancelBubble = true;
			
			//set the subformid for validation
			activeSubForm = subFormId;

			
			submitCt.click();
		} else {
			alert("submit ctrl not found, srcElement is " + e.srcElement.id + ", submitCtrlId should be " + submitCtrlId);
		}
	}
}

// This is used to track the subform in which the user invokes a link. Since the code for
// validation in linkbutton controls is contained in the href attribute, we can't infere
// the subform from the source element of the event
function nonDomCompliantSubFormClick(e,subFormId) {
	activeSubForm = subFormId;
}
