 /*
 **************************************************************************
 Dieses javascipt steuert den Cursor und die Feldaktivierung bei der Eingabe von Werten im Beginner-Suchformular
 
 Folgende Funktionen sind implementiert:
 
 1.   Anklicken einer onoff-checkbox
 --
 1.a. Angefangene Zeilen gibt es nicht (kein Sprung von einer anderen Zeile), das Input-Feld der gleichen Zeile IST leer:
      -> Die Checkbox wird 'checked'.
	  -> Der Rahmen des Input-Feldes wird 2px-Green.
	  -> Der Cursor wird in das Input-Feld gesetzt
	  -> Es erscheint eine showiTxt-Message mit allgemeinen Beschreibungen zur Eigenschaft (Zeile)
	  Realisierung:
	  Einzelschritt: via function acivateRow(), Part A, activeID=propID (kennzeichnet angefangene 'gleiche' Zeile)
 --
 1.b. Angefangene Zeilen gibt es nicht, Sprung vom Input-Feld der gleichen Zeile, welches IST NICHT leer:
      -> Die Checkbox wird 'checked'.
	  -> Der Rahmen des Input-Feldes wird 2px-Green.
	  -> Der Cursor wird in das Input-Feld gesetzt
	  -> Es erscheint eine showiTxt-Message mit allgemeinen Beschreibungen zur Eigenschaft (Zeile)
	  Realisierung: 
	  Schritt 1: function leaveValueField(), Part C: 
	             Der Inhalt des Input-Feldes wird an transValue übergeben, dann wird es gelöscht, 
				 ebenso wird sich die laufende propID in transID gemerkt.
	  Schritt 2: function activateRow(), Part A:
	             Der Inhalt der transValue-Variablen wird an das Input-Feld übergeben, 
				 aber nur dann wenn die angeklickte checkbox in der gleichen Zeile liegt wie die zu transID zugehörige.
				 Rest wie 1a.
 --
 1.c. Das onoff-Feld ist bereits angeklickt, das Input-Feld IST NICHT/IST leer (Inhalt fehlerbehaftet oder -frei). 
      Wichtig: Der Cursor befindet sich in diesem Feld. Es soll die gesamte Zeile deaktiviert werden:
      -> Die Checkbox wird 'unchecked'.
	  -> Der Rahmen des Input-Feldes wird (von Grren, Red, oder Standard) auf Standard rückgesetzt.
	  -> Bei vorhandener ShowiTxt-Meldung wird diese gelöscht.
	  -> Der Inhalt des Input-Feldes wird gelöscht.
      Realisierung:
	  Schritt 1: function leaveValueField(), Part B: 
	             Der Inhalt des Input-Feldes wird auf seine Syntax überprüft (gegebenfalls Fehlermeldung + roter Rahmen ausgegeben)
	  Schritt 2: function activateRow(), Part B:
	             Wenn showiStatus=true wird Meldung gelöscht.
				 Checkbox wird unchecked.
 --
 1.d. Das onoff-Feld ist bereits angeklickt, das Input-Feld IST NICHT leer (Inhalt fehlerfrei). Der Cursor befindet sich NICHT in diesem Feld. 
      Es soll die gesamte Zeile deaktiviert werden:
      -> Die Checkbox wird 'unchecked'.
	  -> Input-value wird gelöscht.
	  -> Cursor ist nicht mehr focussiert.
      Realisierung:
	  Schritt 1: function leaveValueField(), Part D: 
	             input-value wird gelöscht
	  Schritt 2: function activateRow(), Weder Part A, B, oder C. Läuft durch.
 --            				 
 1.e. Angefangene Zeilen gibt es, Sprung vom Input-Feld einer anderen Zeile, welche IST NICHT leer:
      -> Die Checkbox wird 'checked'.
	  -> Der Rahmen des Input-Feldes wird 2px-Green.
	  -> Der Cursor wird in das Input-Feld gesetzt
	  -> Es erscheint eine showiTxt-Message mit allgemeinen Beschreibungen zur Eigenschaft (Zeile)
	  -> Der Inhalt des ursprünglichen Input-Feldes (andere Zeile) wird gelöscht.
	  Realisierung: 
	  Schritt 1: function leaveValueField(), Part C: 
	             Der Inhalt des Input-Feldes wird an transValue übergeben, dann wird es gelöscht, 
				 ebenso wird sich die laufende propID in transID gemerkt.
				 Dabei ist die Merkaktion hier ohne Belang.
	  Schritt 2: function activateRow(), Part A:
	             Normale Zeilenaktivierung, ohne dass ein transValue übergeben wird (dieser wurde gelöscht, 
				 weil die gemerkte transID nicht gleich der aktuellen propID ist)
 ==
 
 2.    Verlassen eines Input-Feldes mit NICHT CHECKED onoff-checkbox durch Klick irgendwohin (nur keine Checkbox)
       -> Value des Input-Feldes wird gelöscht
	   Realisierung: 
	   Schritt 1: function leaveValueField(), Part C: 
	             Der Inhalt des Input-Feldes wird an transValue übergeben, dann wird es gelöscht, 
				 ebenso wird sich die laufende propID in transID gemerkt.
				 Dabei ist die Merkaktion hier ohne Belang.
 --
 3.    Verlassen eines Input-Feldes mit CHECKED onoff-checkbox durch Klick irgendwohin (nur keine Checkbox)
 3.1.  fehlerbehafteter Inhalt
       -> Der Rahmen des Input-Feldes wird 2px-Red.
	   -> Der Cursor wird zurück in das Input-Feld gesetzt
	   -> Es erscheint eine showiTxt-Message mit einer Fehlerbeschreibung
	   Realisierung:
	   Schritt 1: function leaveValueField(), Part B: 
	              Der Inhalt des Input-Feldes wird auf seine Syntax überprüft (unterschiedlich für die einzelnen Eigenschaftstypen bzw. Gruppen).
				  Es wird ein roter Rahmen gesetzt und eine Fehlermeldung ausgegeben.
 --
 3.2.  fehlerfreier Inhalt
       -> Der Rahmen des Input-Feldes wird auf Standard zurückgesetzt.
	   -> Der Cursor wird gelöst und wandert dorthin, wohin der Nutzer klickt.
	   Realisierung:
	   Schritt 1: function leaveValueField(), Part B: 
	              Der Inhalt des Input-Feldes wird auf seine Syntax überprüft. 
				  Da Fehlerfreiheit festgestellt wurde, werden keine weiteren Aktionen durchgeführt.
 
**************************************************************************
*/
  
 
 var ie = document.all ? true : false;
 var opera = (window.opera || navigator.userAgent.indexOf("Opera") != -1) ? true : false;
 
 // property ID of the (incomplete!) row which is activated at the moment 
 //(cursor is focused in an input field of this row)
 var activeID = 0;
 

 // value transfer
 var transValue = "";
 var transID = "";
 
 var helpID_ie =0;
 var timeID_ie =0;
 var missingSisterOption = 0;
 
 // status of the showi-message window (is shown=true)
 var showiStatus = false;
 
 

// activate (set click und activeID=propID), or deactivate/release (unclick und set activeID=0) property row
function activateRow(propID, propName, messageText, tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green) {
	
		
	//if(helpID_ie)
	//alert('helpID_ie is here');
	
	if(!tableWidth) tableWidth = '585';
	
	var num = '';
	
	// delete transValue, if click is targeted to another row not coming from
	if(transID != propID && transValue)
	transValue = "";
	
	if(propName) 
	var dx = ': '; 
	else 
	var dx = '';
	

	//if(helpID_ie)
	//alert('document.getElementsByName("bg_onoff_"+propID)[0].checked='+document.getElementsByName("bg_onoff_"+propID)[0].checked+' activeID='+activeID+' propID='+propID);
	
	// [A] 
	// an unactivated field was just clicked, and no other row is activated,
	if((document.getElementsByName("bg_onoff_"+propID)[0].checked && activeID == 0) ||
	   (helpID_ie && (timeID_ie == 1 || activeID != propID))) {

		// check deadline optine 
		if(propID != 5 && propID != 6 && !checkDeadlineOption(propID, tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green)) {
			document.getElementsByName("bg_onoff_"+propID)[0].checked = false;
			return false;
		}
		
		
		// add Warning, if someone checked area-options, both!
		if(document.getElementsByName("bg_onoff_1").length)
		if(propID == propNameAreaID && 
		   document.getElementsByName("bg_onoff_1")[0].checked) 
		dx = dx + Syntax[8];
		
		if(document.getElementsByName("bg_onoff_"+propNameAreaID).length)
		if(propID == 1 && 
		   document.getElementsByName("bg_onoff_"+propNameAreaID)[0].checked) 
		dx = dx + Syntax[9];
				
		if(helpID_ie)
		document.getElementsByName("bg_onoff_"+propID)[0].checked = true;
		
		// close showi message window, deactivate row (set activeID =0, reset border color), 
		// if showi-message window exists
		if(showiStatus)
		closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 1);
		
		// in each case show fundamental property information
		
		showiTxt(propName+dx+messageText, propID+100000, messageText.length, tableWidth, helplevel, '0', 'rgb(255,255,221)', 'rgb(196,227,180)');
		
		// set showi-message-window status true
		showiStatus = true;
		
		// set property ID for active row
		activeID = propID;
		
		
		//777
		// move cursor/focus on input-select field
		focusOnValueField(propID, elBorderActivateStyle_green);
		
		// transfer value from input field, 
		// which was deleted by function leaveValueField() previously at the earlier step
		if(transValue) {
			document.beginnerSearchForm["bg_value_"+propID].value = transValue;
			transValue ="";
		}
		

		
		//alert('A');
	
	}
	
	// [B]
	// user clicked the CHECKED onoff-field, while its (the same) row is already active
	// :: unchecking onoff-field user has just done
	// -> close showi message window if it exits
	// -> delete value from input-select value field
	else if(activeID && activeID == propID) {
				
		if(helpID_ie)
		document.getElementsByName("bg_onoff_"+propID)[0].checked = false;
		
		// close showi message window, deactivate row (set activeID =0, reset border color), 
		// if showi-message window exists
		if(showiStatus)
		closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 1);

		// make sure that no value is put/selected
		document.beginnerSearchForm["bg_value_"+propID].value = "";
			
		//alert('B');
	}
	
	// [C]
	// user clicked some onoff-field, while another row is already active
	// -> unclick this onoff-field
	else if(activeID && activeID != propID) {
	
		// declick foreign onoff-field, all other things leave untouched
		switchCheckingOnoffField(propID);
		
		//alert('C');
	}
	
	// [D] 
	// user uncheckes an full-row
	// -> delete value of the same row as onoff-field
	else if(!document.getElementsByName("bg_onoff_"+propID)[0].checked &&
			 document.beginnerSearchForm["bg_value_"+propID].value) {

		document.beginnerSearchForm["bg_value_"+propID].value ="";
		
		//alert('D');
	}
	
	
	if(helpID_ie) {
	helpID_ie =0;
	timeID_ie=0;
	}
	
	
		
	//alert('showiStatus='+showiStatus+' activeID='+activeID+' propID='+propID+' num='+num);		
		
} // end of function



// check input-syntax of value (input/select)-field, eg. is empty (input/select)?, or is of wrong format (input only)?
function leaveValueField(field, propID, propName, tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green, elBorderActivateStyle_red) {
	
	
	if(!tableWidth) tableWidth = '585';
	
	//alert('onoffChecked='+document.getElementsByName("bg_onoff_"+propID)[0].checked+' activeID='+activeID);
	

	// [A]
	// error: trial to leave an empty field
	if(!field && (activeID == propID || document.getElementsByName("bg_onoff_"+propID)[0].checked)) {
						
		
			//	alert('aa');
			
			
			// 1. close showi message window, deactivate row (set activeID =0, reset border color), 
			// if showi-message window exists
			if(showiStatus)
			closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 0);
			
			
			// 2. show new message
			var text = propName+': '+Syntax[0];
			showiTxt(text, propID+100000, text.length, tableWidth, helplevel, '0', 'rgb(255,255,221)', 'rgb(196,227,180)');
			

			
			// 3. focus
			if(ie && !opera && document.beginnerSearchForm["bg_value_"+propID].type != "text") {
			
				helpID_ie++;
				if(helpID_ie <2) {
				focusOnValueField(propID, elBorderActivateStyle_red);
				timeID_ie=0;
				}
				else {
				document.getElementsByName("bg_onoff_"+propID)[0].checked = false;
				if(showiStatus)
				closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 0);
				setTimeout("timeID_ie=1", 200);
				}
				
			}
			else
				focusOnValueField(propID, elBorderActivateStyle_red);
			
	}
	
//	else if(activeID != 0 && document.beginnerSearchForm["bg_value_"+activeID].value) {
//	else if(field && document.getElementsByName("bg_onoff_"+propID)[0].checked) {
	
	// [B]
	// go to some other field, but not onoff of the same row
	// -> proof syntax of input value
	else if(field && document.getElementsByName("bg_onoff_"+propID)[0].checked) {
	
			//	alert('bb');
			
			if(ie)
			helpID_ie =0;
			
			var chkZ = 0;
			
			if(missingSisterOption) //
			missingSisterOption = 0;
			
			// check input-syntax
			
			// amount(2), price(3), 
			if(propID == 2 || 
			   propID == 3 || 
			   propID == 208 || 
			   propID == 216 ||
			   propID == 217 ||
			   propID == 218 ||
			   propID == 219 ||
			   propID == 223 ||
			   propID == 224 ||
			   propID == 256 ||
			   propID == 259) { // 0002-11
							
					for(i=0;i<field.length;++i) {
   					if(field.charAt(i) < "0" || field.charAt(i) > "9")
					chkZ = 1;
					}
					if(field.length > 8)
					chkZ = 2;

			} // 0002-12
			
			// shipment: check deadline: is it present?
			else if(propID == 5)
			
					checkDeadlineOption(propID, tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green);
								
			
			else if(propID == 6) {
				
					// weekday only
					if(field.match(/^(mo|di|mi|do|fr|montag|dienstag|mittwoch|donnerstag|freitag)$/i))
					chkZ = 3;
					// no MM-TT-YY date
					else if(field.match(/-/i))
					chkZ = 4;
					// forbidden characters
					else if(!field.match(/^[a-z0-9.: ]+$/i))
					chkZ = 5;
					// missing time
					else if(!field.match(/(([0-2]?[0-9])[.:]([0-5][0-9])[ ]?(Uhr)?|([0-2]?[0-9])[ ]?Uhr[ ]?([0-5][0-9])?)\b/i))
					chkZ = 6;
					
					
			
			}
			
			else if(propID == 264) {
				
					// 
					if(!field.match(/^([5-9]|[5-9][0-9]|100)$/i))
					chkZ = 13;
					
			}
			
			else if(propID == propNameAreaID) {
				
					// 
					if(field.match(/^[0][0-9 ]+x[1-9 ][0-9 ]+$/) || field.match(/^[1-9][0-9 ]+x[0][0-9 ]+$/))
					chkZ = 10;
					
					else if(!field.match(/^[1-9][0-9 ]+x[1-9 ][0-9 ]+$/))
					chkZ = 11;
					
					
			
			}
			
			else {
				
					// 
					if(!field.match(/^[ a-z0-9._:-äöüéè]+$/i))
					chkZ = 12;
			
			}
			
			
			
			
			// errerous
			if(chkZ >0) {

					
					// close showi message window, deactivate row (set activeID =0, reset border color), 
					// if showi-message window exists
					if(showiStatus)
					closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 0);
			
					var text = propName+':|'+Syntax[chkZ];
					showiTxt(text, propID+100000, text.length, tableWidth, helplevel, '0', 'rgb(255,255,221)', 'rgb(196,227,180)');
					
					focusOnValueField(propID, elBorderActivateStyle_red);
					
					
					
   			}
			// set signal, that row is 'released' now
						
			// none-errerous
			
						
			// a little bit crucial: 
			// avoids message deleting if user put an input error and clicks on a non-empty value field of an active row
			else if(activeID == propID)  {			
					
					// close showi message window, deactivate row (set activeID =0, reset border color), 
					// if showi-message window exists
					if(showiStatus)
					closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 1); // 777
			}
			
			
			// delete second area-option (jump to activateRow(), section [B]
			if(document.getElementsByName("bg_onoff_1").length)
			if(propID == propNameAreaID && 
			   document.getElementsByName("bg_onoff_1")[0].checked) {
				
				document.beginnerSearchForm["bg_value_1"].value = "";
				document.getElementsByName("bg_onoff_1")[0].checked = false;
			
			}
			
			if(document.getElementsByName("bg_onoff_"+propNameAreaID).length)
			if(propID == 1 && 
			   document.getElementsByName("bg_onoff_"+propNameAreaID)[0].checked)  {
				
				document.beginnerSearchForm["bg_value_"+propNameAreaID].value = "";
				document.getElementsByName("bg_onoff_"+propNameAreaID)[0].checked = false;
			}
			
			
			
	}
	

// [C]clicks on a checked onoff-field, while input-field of the same row is not empty!
	else if (field && !document.getElementsByName("bg_onoff_"+propID)[0].checked) {
		
		//alert('cc');
		
		transID = propID;
		transValue = document.beginnerSearchForm["bg_value_"+propID].value;
		document.beginnerSearchForm["bg_value_"+propID].value = "";
	}
	

	
/* we cannot differentiate, thats why, we try to solve the problem at activeRow() ... 10208, 17:35
// [C]
	// user put some data into value field, and jumps to an unchecked onoff-field, trying to check it,
	// while targeted onoff-field may be belong (a) to the same or (b) to another row
	// -> do not .. 
	else 
		document.beginnerSearchForm["bg_value_"+propID].value = "";
*/

	
} // end of function



// ---

// helper function
function switchCheckingOnoffField(propID) {

		if(ie  || opera) {
		
			if(document.getElementsByName("bg_onoff_"+propID)[0].checked)
			document.getElementsByName("bg_onoff_"+propID)[0].checked = "";
			else
			document.getElementsByName("bg_onoff_"+propID)[0].checked = true;
		}
		else
		document.getElementsByName("bg_onoff_"+propID)[0].click();

} // end of function



// helper function
function focusOnValueField(propID, elBorderActivateStyle) {
	
	var name = "bg_value_"+propID;
	

	if(ie && !opera)
	document.all[name].style.border = elBorderActivateStyle;
	else
	document.beginnerSearchForm[name].style.border = elBorderActivateStyle;
	
	

	if(ie || opera) {			
		
		document.beginnerSearchForm[name].focus();
	}
	
	// firefox only
	else { 
		setTimeout("document.beginnerSearchForm['"+name+"'].focus();",1);
	}
	



		
} // end of function


	


// helper function
function closeShowiTxt_emptyValue(propID, elBorderStandardStyle, deactivateRow) {
	
	// close showiTxt-window
	showiTxt('', propID+100000, '','','none', '', 'rgb(255,255,221)', 'rgb(196,227,180)');
	
	if(deactivateRow) {
		
		var name = "bg_value_"+propID;
	
		if(ie && !opera)
		document.all[name].style.border = elBorderStandardStyle;
		else
		document.beginnerSearchForm[name].style.border = elBorderStandardStyle;
	
		activeID = 0;
	}
		
} // end of function

// helper function
function changeOption(no, propID, displayValue, valueValue) {

  document.beginnerSearchForm["bg_value_"+propID].options[no] = null;
  var NewOption = new Option(displayValue, valueValue);
  document.beginnerSearchForm["bg_value_"+propID].options[no] = NewOption;
  document.beginnerSearchForm["bg_value_"+propID].options[no].selected = true;
}

// helper function
function checkDeadlineOption(propID, tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green) {
	
		// shipment: check deadline: is it present?
		if(document.getElementsByName("bg_onoff_5")[0].checked && document.beginnerSearchForm["bg_value_5"].value &&
		  !document.beginnerSearchForm["bg_value_6"].value) {
				
					// if showi-message window exists
					if(showiStatus)
					closeShowiTxt_emptyValue(propID, elBorderStandardStyle, 1);
					
			
					// simulate click onto checkbox of deadline-option ...
					document.getElementsByName("bg_onoff_6")[0].checked = true;
					
					
					// ... resulting in a standard activateRow-Call
					activateRow(6, '', Syntax[7], tableWidth, helplevel, elBorderStandardStyle, elBorderActivateStyle_green);
					
					return false;
			
		}
		
		return true;

}

// ===========

function showSearchProfileSelectElement(messageText, tableWidth, helplevel) {

	if(ie && ! opera)
	document.all.searchProfileForm["spSelEl"].style.removeAttribute("width", false);
	else
	document.getElementById("spSelEl").style.width = "";

	document.searchProfileForm["spSelEl"].style.visibility = "visible";
	document.getElementById("spHref").firstChild.nodeValue ="";
//	alert(document.getElementById("iTarg_spHref").firstChild.class);
//	document.getElementById("iTarg_spHref").firstChild.style.textDecoration ="none";
	showiTxt(messageText, "spHref", messageText.length, tableWidth, helplevel, '0', 'rgb(255,255,221)', 'rgb(196,227,180)');
	showiStatus = true;
	document.searchProfileForm["spSelEl"].focus();
} // end of js-function

function closeSearchProfileShowiTxt () {

	if(showiStatus)
	showiTxt('', "spHref", '', '', 'none', '', 'rgb(255,255,221)', 'rgb(196,227,180)');
	
}