var aoSubmitButtons = new Array();

/**
 * Exampel for elements:
 * 	<h1 class="autoFormDiv_Class" autoForm="text" id="Name" label="Name">bla blub</h1>
 * 	<br autoform="show" class="autoFormDiv_Class clearBoth" style="display:none"/>
 *  <span class="autoFormDiv_Class" autoForm="hide">lalala</span>
 * 	<p id="autoFormDiv" autoFormClass="">
 * 		<a href="/city.php?..." autoForm="text" id="PostalCode" completionTable="Position" completionRow="PostalCode">55294</a>
 * 		<span autoForm="hide">Mainz</span>
 *	</p>
 * 	<a href="#" onclick='autoForm('ChangeCity...', $('autoFormDiv'), )'>ändern</a>
 */
function autoForm( sHandlerName, oDiv, oLink, asAdditionalRequestVars) {
 	hide(oLink);

	var aoAutoFormElements =  getAutoFormElements( oDiv );

	oSubmitButton = document.createElement('button');
	oSubmitButton.setAttribute("alt", "Speichern");
	oSubmitButton.innerHTML = "Speichern";
	
	oSubmitButton.onclick = submit;
	
	oSubmitButton.className = oLink.getAttribute('autoFormClass');
	
	addAfterElement(oSubmitButton, oLink);
	
	oCancelButton = document.createElement('button');
	oCancelButton.setAttribute("alt", "abbrechen");
	oCancelButton.innerHTML = "abbrechen";
	oCancelButton.onclick = showAgain;
	
	oCancelButton.className = oLink.getAttribute('autoFormClass');
	addAfterElement(oCancelButton, oSubmitButton);
	
	var aoInputElements = addInputs( aoAutoFormElements );
	
	var aoButtons = [oSubmitButton, oCancelButton];
	oSubmitButton.aoButtons =  aoButtons;
	oCancelButton.aoButtons =  aoButtons;
	
	oSubmitButton.aoInputElements =  aoInputElements;
	oCancelButton.aoInputElements =  aoInputElements;
	
	oSubmitButton.aoAutoFormElements =  aoAutoFormElements;
	oCancelButton.aoAutoFormElements =  aoAutoFormElements;
	
	oSubmitButton.oLink =  oLink;
	oCancelButton.oLink =  oLink;
	
	oSubmitButton.showAgain = showAgain;
	oSubmitButton.asAdditionalRequestVars = asAdditionalRequestVars;
	oSubmitButton.sHandlerName = sHandlerName;
	oSubmitButton.sDivID = oDiv.id;
}

function closeAutoForm(sID){
		aoSubmitButtons[sID].showAgain();
}

function submit(){
	var asValues = new Array();

	for(i in this.aoInputElements) {
		oElement = this.aoInputElements[i];
		oOriginalElement = oElement.oOriginalElement;

		if (oElement.oCommaSeparatedValuesElement) oElement = oElement.oCommaSeparatedValuesElement; 
		if (oElement.oDataElement) {
			asValues[oElement.oDataElement.id] = oElement.oDataElement.value;
		}
		sName = oElement.name ? oElement.name : oElement.id;
		
		switch(oElement.type) {
			case "select-one":
				if(oOriginalElement){
					oOriginalElement.innerHTML = oElement.options[oElement.selectedIndex].value;
				}
				asValues[sName.replace(/^AutoForm_/, '')] = encodeURIComponent(oElement.options[oElement.selectedIndex].value);
				break;
			default:
				if(oOriginalElement){
					oOriginalElement.innerHTML = oElement.value.replace(/<[^>]*>/g, '').replace(/^\s*\b/, '').replace(/\b\s*$/, '').replace(/\n/g, '<br/>');
				}
				asValues[sName.replace(/^AutoForm_/, '')] = encodeURIComponent(oElement.value);
				break;
		}
	}
	var bSActionIncluded = false;
	for (var sKey in this.asAdditionalRequestVars) {
	    asValues[sKey] = this.asAdditionalRequestVars[sKey];
	    if(sKey == "sAction"){
	    	bSActionIncluded = true;
	    }
	}
	
	asValues['autoFormID'] = this.sDivID;
	if(!bSActionIncluded){
		asValues['sAction'] = "submit";
	}
	callHandler(this.sHandlerName, asValues);	
	aoSubmitButtons[this.sDivID] = this;
}

function showAgain(){
	for (i in this.aoInputElements) {
		oElement = this.aoInputElements[i];
		if (oElement.oAutoCompleter) {
			oElement.oAutoCompleter.remove();
		}
		removeElement(oElement);
	}
	
	for (i in this.aoAutoFormElements) {
		oElement = this.aoAutoFormElements[i];
		//show(oElement);
		if (oElement.sOldDisplay) oElement.style.display = oElement.sOldDisplay;
		else oElement.style.display = '';
	}
	
	for (i in this.aoButtons) {
		oElement = this.aoButtons[i];
		hide(oElement);
	}
	show(this.oLink);
}


function getAutoFormElements( oDiv ){
	var aoElements = new Array();
  	if(oDiv.all){
  		  	aoElements=oDiv.all;
  	}else if(oDiv.getElementsByTagName && !oDiv.all){
  		    aoElements=oDiv.getElementsByTagName("*");
  	}
	var aoFoundElements = new Array();

	for( var i = 0 ; i < aoElements.length ; i++ ) {
		if(! aoElements.item(i).getAttribute){
			continue;
		}
		var sType = aoElements.item(i).getAttribute( 'autoForm' );
		
		if( sType != null ){
			aoFoundElements[i] = aoElements.item(i);
		}
	}
	var aoClassElements = document.getElementsByClassName(oDiv.id+"_Class");
	var iStart = aoFoundElements.length+1;
	for( var i = 0 ; i < aoClassElements.length ; i++ ){
		aoFoundElements[iStart+i] = aoClassElements[i];
	}
	
	return aoFoundElements;
}

function addInputs( aoFoundElements ){
	
	var aoAddedElements = new Array();
	for (i in aoFoundElements) {
		oElement = aoFoundElements[i];
		oElement.sOldDisplay = oElement.style.display;
		oElement.style.display = 'none';
		
		sInnerHtml = oElement.innerHTML;
		
		var oInput;
		bMultiple = false;
		if(oElement.getAttribute( 'autoForm' ) == "hide"){
			continue;
		}else if(oElement.getAttribute( 'autoForm' ) == "show"){
			oElement.style.display = '';
			continue;
		}else if(oElement.getAttribute( 'autoForm' ) == "send"){
			aoAddedElements[i] = oElement;
			continue;
		}else if(oElement.getAttribute( 'autoForm' ) == "textarea"){
			oInput=document.createElement('textarea');
			oInput.innerHTML = oElement.innerHTML.replace(/<br[^>]*>/g,'\n').replace(/<[^>]*>/g, '').replace(/^\s*\b/, '').replace(/\b\s*$/, '');
		} else if (oElement.getAttribute('autoForm') == 'relation') {
			oInput = document.createElement('input');
			oInput.setAttribute('type', 'text');
			asValues = oElement.innerHTML.split(',');
			for (iIndex in asValues) {
				asValues[iIndex] = asValues[iIndex].replace(/<[^>]*>/g, '').replace(/^\s*\b/, '').replace(/\b\s*$/, '');
			}
			oInput.setAttribute('value', asValues.join(', '));
			bMultiple = true;
		} else if (oElement.getAttribute('autoForm') == 'date') {
			oInput = document.createElement('input');
			oInput.setAttribute('type', 'text');
			oInput.setAttribute("value",oElement.innerHTML.replace(/<[^>]*>/g, '').replace(/^\s*\b/, '').replace(/\b\s*$/, ''));
			oInput.onfocus = function() { this.oCalendar = new Calendar(this); }
			
		} else {
			oInput=document.createElement('input');
			oInput.setAttribute("type", oElement.getAttribute( 'autoForm' ) );
			oInput.setAttribute("value",oElement.innerHTML.replace(/<[^>]*>/g, '').replace(/^\s*\b/, '').replace(/\b\s*$/, ''));
		}
		oInput.id = 'AutoForm_' + oElement.id;
		oInput.setAttribute("name", oInput.id);
		
		if(oElement.getAttribute( 'onfocus' )){
			oInput.onfocus = function(){eval(this.oOriginalElement.getAttribute( 'onfocus' ));}
		}
		
		var oParent = oElement.parentNode;
		
		oDivElement = document.createElement('span');
		if (oElement.getAttribute('label')) {
			oLabel = document.createElement('label');
			oLabel.setAttribute("for",'AutoForm_' +oElement.id);
			oLabel.appendChild(document.createTextNode(oElement.getAttribute('label')+':'));
			oDivElement.appendChild(oLabel);
		}
		
		oDivElement.appendChild(oInput);
		oParent.insertBefore(oInput, oElement);
		
		aoAddedElements[i] = oInput;
		oInput.oOriginalElement = oElement;

		if (oElement.getAttribute('autoFormClass')) {
			oInput.className = oElement.getAttribute('autoFormClass');
		}
		
		
		var sCompletionTable = oElement.getAttribute( 'completionTable' );
		if( sCompletionTable != null && !$(oElement.id+'Proposals')){
			var sCompletionRow = oElement.getAttribute( 'completionRow' );
			oInput.oAutoCompleter = new AutoCompleter( oInput, sCompletionTable, sCompletionRow );
			oInput.oAutoCompleter.setMultiple(bMultiple);
			if (oInput.getAttribute('completionData') != null ) {
				oInput.oAutoCompleter.setDataElement(oInput.getAttribute('completionData'));
			}
		}
	
	}
	return aoAddedElements;
}