
if (!window.A4J) { window.A4J= {};}

if(!A4J.findForm){

function _JSFFormSubmit(linkId,formName,target,parameters){
	var form = document.getElementById(formName);
	if(form){
		/*var hiddenField = form.elements[formName+":_idcl"];
		if(hiddenField){
			hiddenField.value=linkId;
	    } else {
			var input = document.createElement("input");
			input.type="hidden";
			input.name=formName+":_idcl";
			input.value=linkId;
			form.appendChild(input);
		}*/
		
		var paramNames = [];
		
		if(target){
			form.target=target;
		}
		if(parameters){
			for(var param in parameters){
				paramNames.push(param);
				if(form.elements[param]){
					form.elements[param].value = parameters[param];
				} else {
					var input = document.createElement("input");
					input.type="hidden";
					input.name=param;
					input.value=parameters[param];
					form.appendChild(input);
				}
			}
		}
		form.submit();
		_clearJSFFormParameters(formName,target,paramNames);
		
	} else {
		alert("Form "+formName+" not found in document");
	}
	return false;
};

function _clearJSFFormParameters(formName,target,fields){
	var form = document.getElementById(formName);
	if(form){
		if(target){
			form.target = target;
		} else {
			form.target = '';
		}
		if(fields){
		 for(var i=0;i<fields.length;i++){
			var field = form.elements[fields[i]];
			if(field){
				form.removeChild(field);
			}
		 }
		}
	}
}

function clearFormHiddenParams(formName,target,fields){
	_clearJSFFormParameters(formName,target,fields);
}


  A4J.findForm = function(element){
	var parent = element;
	do{
		parent = parent.parentNode;
	} while (parent && parent.nodeName.toLowerCase() != 'form');
	if(!parent){
		parent = {reset:function(){}, submit:function(){}};
	}
	return parent;
  }

  A4J._formInput = null;

// setup form to handle 'onclick' events, to detect input element.
  A4J.setupForm = function(id){
  		var element = window.document.getElementById(id);
	    var name = "click";
        if (element.addEventListener) {
             element.addEventListener(name, A4J._observer, false);
        } else if (element.attachEvent) {
             element.attachEvent('on' + name, A4J._observer);
        }
        // TODO - handle array of all attached forms and remove listeners
        // on ajax updates, to avoid memory leaks.
  }

  A4J._observer = function(evt){
  	var src = evt.target||evt.srcElement;
  	if( src && src.nodeName.toUpperCase() == 'INPUT' && src.type.toUpperCase() == 'SUBMIT'){
  		A4J._formInput = src;
  	} else {
  		A4J._formInput = null;
  	}
  }
}