

// needed data
var printOutVar = new Array();
var orgMainTableTop;
var orgMastheadheight;
var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
/** trim hilfsfuntkion

*/


function trim(s) {
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  while (s.substring(s.length-1,s.length) == '\n') {
    s = s.substring(0,s.length-1);
  }
  return s;
}


/** move the given id to the current mouse position */
// function moveDiv(id)
//  {	var test=event.clientY;
// 	alert("blup");
// 	alert(test);
// }


function doStart() {

if (success) {
	alert(success);
}
if (!login) {
	$$("navBar").style.visibility="hidden";
	$$("helperboxes").style.visibility="hidden";
	$$("pages").style.visibility="hidden";
}
}
 /** hilfunktion, damit getElemenbyId für ie und firefox funzt */
function getElementById(ElementId)
{
   if (document.documentElement)
   {
      return document.getElementById(ElementId);
   }
   else
   {
      return document.all[ElementId];
   }
}

function placeFocus() {
if (document.forms.length > 0) {
var field = document.forms[0];
for (i = 0; i < field.length; i++) {
if ((field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type.toString().charAt(0) == "s")) {
document.forms[0].elements[i].focus();
break;
         }
      }
   }
}
//  End -->

function resetHelp() {
	getElementById("helptext").innerHTML="";
}

function hideLayer(layerId) {
layer=getElementById(layerId);
layer.style.visibility='hidden';
// layer.style.width="0px";
// layer.style.height="0px";
// getElementById('Maintable').style.top='3em';
if (layerId=="addingLayer") {
		$("#receiptForm").replaceContent("Betrag:");
		$("#receiptForm").create("input",{id:"receiptAmount",type: 'text'},true);
		$("#receiptForm").addContent("&euro;&nbsp;&nbsp; Name, Vorname des Spenders:");
		$("#receiptForm").create("input",{id:"receiptName",type:'text'},true);
		$("#receiptForm").create("input",{id:"receiptSbt",type:'submit',value:"Eintragen"},true);
	
}
ungreyMaintable();
}

function showLayer(layerid) {
layer=getElementById(layerid);

layer.style.visibility='visible';
// getElementById('focus_'+layerid);
greyMaintable();
// alert(navigator.appName);
if (layerid=="promisedLayer" && navigator.appName=="Netscape") {
// alert(layerid);

layer.style.position='fixed';
layer.style.top="25em";
layer.style.left="20em";
layer.style.width="30em";
// layer.style.height="130px";
getElementById('Maintable').style.zIndex="1";
layer.style.zIndex="9";
 } else if(layerid=="promisedLayer") {
layer.style.position='absolute';
layer.style.top=event.clientY + document.body.scrollTop-100;
layer.style.left="5px";
layer.style.width='99%';
layer.style.height='10em';
// alert(event.clientX + document.body.scrollLeft);
// alert(event.clientY + document.body.scrollTop);
layer.style.zIndex="9";
getElementById('Maintable').style.zIndex="1";
} else if (IE6) {
 
	layer.style.position='absolute';
	layer.style.top='100px';
	layer.style.left="5px";
	layer.style.width='99%';
	layer.style.height='11em';
 } else {

// showBetrag();
	layer.style.position='fixed';
	layer.style.top="11.5em";
	layer.style.left='10%';
	layer.style.width='80%';
	// layer.style.height='10em';
	layer.style.zIndex="9";
// try{
// getElementById('Maintable').style.zIndex="1";
// } catch(e) { }
// getElementById('Maintable').style.top='10em';

}

}
function greyMaintable() {
try {
getElementById('Maintable').style.backgroundColor="#D1D1D1";
getElementById('Maintable').style.color="#9E9E9E";
}
catch(e) { }
}

function ungreyMaintable() {
try {
getElementById('Maintable').style.backgroundColor="";
getElementById('Maintable').style.color="black";
}
catch(e) { }
}

function disableRow(ID) {
// alert(ID);
try {
row=getElementById("Row_"+ID);

row.className="disabled";
//Iterating through the columns array, changing the onclicks and striking the text
for (var i=0;i<columns.length;i++) {
// 	alert(i);
// 	alert(columns[i]);
// 	alert(ID+"_"+columns[i]);
	div=getElementById("S"+ID+"_"+columns[i]);
// 	alert(getElementById(ID+"_"+columns[i]).onClick);
	div.onClick="return false;";
// 	alert(div.onClick);

}
} catch(e) { }
// gives me the rownumber for the deletion later on..
// alert(row.rowIndex);
}


/** function that takes a click to a X and blurs out the cell if the User answers a question accordingly */
// works with a differnet style, and then calling a ajax-function-bundle (delete)
function askDelete(SID) {
newName="S"+SID+"_Name";
//alert(SID)alert(newName)
// alert("earlytest")
Name2=getElementById(newName).innerHTML;
// alert(getElementById("1_name"));
// alert(Name2.innerHTML)
// alert("test")
if (Name2=="&nbsp;&nbsp;") {
	Name2="";
}
Firstname2=getElementById("S"+SID+"_Firstname").innerHTML;
if (Firstname2=="&nbsp;&nbsp;") {
	Firstname2="";
}
if (confirm("Moechten sie "+Firstname2+" "+Name2+" wirklich loeschen?\n WARNUNG: Dies kann nicht rueckgaengig gemacht werden!")) {
disableRow(SID);
do_delete(SID);
} else {
// alert("nichts passiert!");
}


}
/** unchecks all set marks for the "print" colum 
**** I Shoudl prob do this one and checkedAll via the table iterator! */
function uncheckPrint() {

	CheckList=document.getElementsByTagName("input");
	for (var i=0;i<CheckList.length;i++) { // looping through all input elements this page has..
		if (CheckList[i].type=="checkbox" && CheckList[i].id.match(/_printBox/)!="") {
			CheckList[i].checked=false;
		}
		
	}
}

/** function that selects all visible print checkboxes and checks them */
/** edited to fit the new way. When it's only select to select all visible, exactly that is done. Otherwise, a special way is executed! */
function checkAll(checkedVar) {
// 	alert(checkedVar.checked);
if (checkedVar.checked) {
	getElementById('Maintable').style.backgroundColor="#D1D1D1";
	getElementById('Maintable').style.color="#9E9E9E";
// 	alert(checkedVar.id);
	
// 	getElementById(checkedVar.id).disabled=true;
	//if (checkedVar.checked && confirm("Wenn Sie alle Laeufer auswaehlen wollen (egal ob gerade sichbar oder nicht), klicken Sie bitte auf Okay. \n Wenn Sie auf Abbrechen Klicken, werden nur die gerade sichtbaren Laeufer beruecksichtigt!") == true) {
	printOutVar['all']=true;
//	showLayer("printLayer");
//	} else {
// 	alert(" i just got called..");
	
// 	CheckList=document.getElementsByTagName("input");
// 	alert("test");
// 	alert(CheckList.length)
// 	for (var i=0;i<CheckList.length;i++) { // looping through all input elements this page has..
// 		if (CheckList[i].type=="checkbox" && CheckList[i].id.match(/_print/)) {
// 			CheckList[i].checked=checkedVar.checked;
// 			alert(CheckList[i].id);
// 		}
		
// 	}
showLayer("printLayer");
//	}
// 	checkedVar.disabled=false;
	
// 	var n=theForm.elements.length;
// 		for (var i=0;i<n;i++){
// 			if (theForm.elements[i].className.indexOf(cName) !=-1){
// 			if (allNo_stat.checked) {
// 				theForm.elements[i].checked = true;
// 			} else {
// 				theForm.elements[i].checked = false;
// 			}
// 			}
// 		}
}
}

function getKeyCode(event) {
   event = event || window.event;
   return event.keyCode;
}

function nextElement(event) {
	var charCode = getKeyCode(event);
	if (charCode<41&& charCode >36 ) { // pfeiltasten!
// 	document.write("TAB!");
		var field,newColumn;
		if (!currentField) { // benutz der erste Feld
			field=getElementById(firstField);
		} else {
			// finding the next field
// 			alert(charCode);
			if (charCode==39 ) { // rechts
				
// 				alert(currentField.replace(/(\d*)_.*/,"$1_"+columns[currentColumn+1]));
				// 				alert(field.id);
				newColumn=currentColumn+1;
			} else if (charCode==37) {
				newColumn=currentColumn-1;
			}
			field=getElementById(currentField.replace(/(\d*)_.*/,"$1_"+columns[newColumn]));
// 			alert(currentField.replace(/(\d*)_.*/,"activeForm$1"));
// 			var old=getElementById(currentField.replace(/(\d*)_.*/,"activeForm$1"));
// 			alert (old.id);
			eval("do_"+funcs[currentColumn]+"('"+currentField+"input')");

		}
// 		alert(field.type);
// 		field=getElementById("5_Name");
		if (field.type) {
			
			field.focus();
		} else {
// 			alert('blah!!');
			editCell(funcs[newColumn],field.id);
		}
	}
}

function disabelHelp() {
	if (helpEn) {
		helpEn=false;
		getElementById('help').style.visibility="hidden";
		getElementById('helptext').innerHTML="";
		
		$$("helphead").replaceContent("");
		$$('feature').style.top=orgMainTableTop;
		$$('masthead').style.height=orgMastheadheight;
		$$('helpCall').replaceContent("<a href=\"#\" onClick=\"disabelHelp()\">Hilfe?</a>");
// 		$('helpclose').replaceContent("Hilfe anzeigen");
	} else {
		helpEn=true;
		$$("helphead").replaceContent("Hilfe");
		//'getElementById('help').style.border='2px solid #00bfff';
	$$('helpCall').replaceContent("");
		orgMainTableTop=$$('feature').style.top;
		$$('feature').style.top ='17em';
		orgMastheadheight=$$('masthead').style.height;
		$$('masthead').style.height='17em';
		
// 		getElementById('helpclose').innerHTML="X";
		$$('help').style.visibility="visible";
	}
// 	alert(helpEn);
}

/** assembels all the data for a printout!
 * now supporting the new object format, with printOutVar[all]=true also
*/	
function wopenPrint() {
	var SIDs;
	if (getElementById("laufzettel").selected) {
		var type="laufzettel&Rounds="+getElementById("rounds").value;
	} else {
		var type="Urkunden";
	}
		
	var SIDs="";
		

	CheckList=document.getElementsByTagName("input");
	for (var i=0;i<CheckList.length;i++) { // looping through all input elements this page has..
		if (CheckList[i].type=="checkbox" && CheckList[i].id.match(/_print/) && (CheckList[i].checked==true || printOutVar["all"]==true)) {
// will nicht, irgendwie wird nur der letzt werte genommen
// möglichkeit "alle" muss noch bearbeitet werden!
	//"+CheckList[i].id+"
			SIDs=SIDs+"&SID[]="+CheckList[i].id;
		} 
	

	
	
// 		getElementById(printOutVar[id]).disabled=false;
	}
	printOutVar["all"]=false
// 	printOurVar= new Object();
	hideLayer("printLayer");
	F1=window.open("printout.php?mode="+type+"&"+SIDs+"&.pdf", "PDF","width=500,height=300");
	uncheckPrint();
}

	/** gotoPrint shows another div, allowing for hte seleciotn 
	of an outputform
	for each additional checkd item (after the first) this item is added to the list of items to printout
	
	Now, nothing is done (besides that the layer is shown. The actuall reading out is done vie the Printout(wopen)= function
	*/
	function  gotoPrint(id) {
// 		alert(getElementById(id).checked)
		if (id.match(/_printBox/)) {		
		checkbox=getElementById(id);
		} else {
		id=id.replace(/_print/,"");
// 		alert(id);	
		id=id+"_printBox";
		checkbox=getElementById(id);	
		}
// 		alert(checkbox.id)
		if (checkbox.checked==true) {
// 		var headprint="head_print";
// alert(document.getElementById("head_print"));
// 		getElementById('head_print').innerHTML="";
// 		getElementById("printLayer").style.visibility="visible";
// 		getElementById("printLayer").style.width="400px";
// 		getElementById("printLayer").style.height="300px";

// wichtig: ab jetzt ist prointOutVar ei objekt, damit ich löschen kann. Dabei ist immer der index = der id
		showLayer("printLayer");
// 		if (id.match(/_printBox/)) {		
// 		checkbox=getElementById(id);
// 		} else {
// id=id.replace(/_print/,"");
// // 		alert(id);	
// 		id=id+"_printBox";
// 		checkbox=getElementById(id);	
// 		}
// 		
// 		if (checkbox.checked) {
// 			printOutVar[id]=id;
// 			checkbox.checked=true;
// // 			alert(checkbox.checked);
// // 			checkbox.disabled=true;
// 		} else {
// 			checkbox.checked=false;
// 			delete(printOutVar[id]);
// 		}
		
// 		alert(printOutVar);
		}
	}
	
	function supChangeSelect() {
		if (getElementById("laufzettel").selected) {
			getElementById("printLayerAddedContent").innerHTML="Wieviele Runden sollen auf dem Laufzettel sein?&nbsp;<input name='Rounds' type='text' size='2' value='30' id='rounds'>";
		} else {
			getElementById("printLayerAddedContent").innerHTML="";
		}
	}

	function showNewClass(id) {
		if ($(id).value=="neu") {
			$$('reportNew').replaceContent("Neue Klasse:");
			$$('reportNew').create("input",{type: 'text',id:'newClass'},true);
// 		if (content=="neu") {
// 			content=prompt("Name der neu anzulegenden Gruppe/Klasse","")
// 		}
		}

	}
// function that selects all currently shown lines and deletes those
function deleteAll() {
	if (confirm("Hiermit werden alle angezeigten Läufer gelöscht.\nSind Sie Sicher?") && confirm("Sollen wirklich alle gerade angezeigten Läufer gelöscht werden?")) {
		greyMaintable();
		$("td div a img").each(function () {
// 			alert(this.src);
			do_delete(this.parentNode.parentNode.parentNode.id);
			});
		
		$("td a img").each(function () {
// 			alert(this.src);
			do_delete(this.parentNode.parentNode.id);
			});
		classFilter('%');
	}
// }	function do_delete(SID) {
// // 		/*alert*/(SID);
// 		x_delete(SID,delete_cb);

	}

