// JavaScript Document

/***********************************************/
/** dietzhilfe.com - Internetportal			  **/
/** category.js								  **/
/** Version: 1.0							  **/
/** Date: 04.12.2008						  **/
/** Copyright (c) 2008 Dr. Dietz Hilfen GmbH  **/
/** Author: Martin Soisch			    	  **/
/** email: soisch@dietzhilfe.com			  **/
/***********************************************/
if(window.location.host=='dietzhilfe.local'){
	
	//var fullPath = "http://"+window.location.host+""+((window.location.pathname).substr(0, (window.location.pathname).lastIndexOf("dietzhilfe/")+11));
	var fullPath = "http://dietzhilfe.local";
	//alert(fullPath);
} else if(window.location.host=='dietzhead.local'){
	var fullPath = "http://dietzhead.local";
} else if(window.location.host=='dietzdev.local'){
	var fullPath = "http://dietzdev.local";
} else if(window.location.host=='stage.dietzhilfe.com'){
	var fullPath = "http://stage.dietzhilfe.com";
} else if(window.location.host=='olddietzhilfe.dietzhilfe.com'){
    var fullPath = "http://olddietzhilfe.dietzhilfe.com";
} else {
	//alert(window.location.host);
	var fullPath = "http://www.dietzhilfe.com";
}

// --- Kategorieebene wechseln ---
function change2Cat(catid, parCatid){
	var cat = document.getElementById('catid_'+catid);
	var parameter = document.location.search;
	var parms = parameter.toQueryParams();
	if(parms['pid']!=null){
		parCatid = parms['pid']+"-"+catid;
	} else {
		parCatid = catid;
	}
	window.location.href="index.php?cat=public&subcat=products&site=cat_products&pid="+parCatid+"&id="+catid;
	//openCatlist(catid);
}

// --- Kategorie hinzuf�gen ---
function addCat() {
	
	 //var ni = document.getElementById('catfieldsDiv');
  var catsTable = document.getElementById('catsTable');
   var catsBody = document.getElementById('catsBody');
  var lastRowClass = catsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellMid";
  
  var newCol3 = document.createElement('td');
  newCol3.className = "gCellMid";
  
  var newCol4 = document.createElement('td');
  newCol4.className = "gCellRight";
  newCol4.width = "16";
  
  var divIdName = 'kategorie_'+(num);
  var strValue = "";

  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  newCol1.colSpan = "1";
  newCol1.innerHTML = "\n<input type=\"text\" class=\"normalfont\" id=\"lkategorie_"+newID+"\" name=\"0\" size=\"50\" maxlength=\"255\" value=\"\" />";

  newCol2.innerHTML = "";
  
  newCol3.innerHTML = "\n<input class=\"normalfont\" name=\"0\" type=\"checkbox\" id=\"lkategorieV_"+newID+"\" /><a class=\"normalfont\">sichtbar</a>";
  
  newCol4.innerHTML = "<a href=\"#\" onclick=\"removeCat("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  
  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  newRow.appendChild(newCol3);
  newRow.appendChild(newCol4);
  
  catsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
// ----------------------------------------

// --- Kategorie sichtbar/unsichtbar machen ---
function setCatHandicap(catid, handicap){
	var hdc;
	if(handicap){
		hdc=1;
	} else {
		hdc=0;
	}
	send2server("action=setCatHandicapped&catid="+catid+"&handicap="+hdc);
}
// ----------------------------------------


// --- Kategorie sichtbar/unsichtbar machen ---
function setCatVisible(catid, visible){
	var vis;
	if(visible){
		vis=1;
	} else {
		vis=0;
	}
	send2server("action=setCatVisiblet&catid="+catid+"&visible="+vis);
}
// ----------------------------------------

// --- Kategorie sperren/entsperren ---
function setCatLock(catid, locked){
	var lock;
	if(locked){
		lock=1;
	} else {
		lock=0;
	}
	send2server("action=setCatLocked&catid="+catid+"&locked="+lock);
}
// ----------------------------------------

// --- Kategorie entfernen ---
function removeCat(fieldId) {
  var d = document.getElementById('catsBody');
  var olddiv = document.getElementById(('kategorie_'+fieldId));
  d.removeChild(olddiv);
}
// ----------------------------------------

// --- Kategorie l�schen ---
function delCat(id_kategorie, pos){
	if(confirmDelete()){
		var serial = "";
		var feldfenster = document.forms[0];
		var anzahl = feldfenster.elements.length;
		
		serial = "id="+id_kategorie;
	
		send2server("action=delCat&"+serial); 
		removeCat(pos);
	}
}
// ----------------------------------------

// --- Kategorien speichern ---
function saveCats() {
    var serial;
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var serial="";
	
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text" || feldfenster.elements[i].type=="checkbox"){
    		if(feldfenster.elements[i].type=="checkbox"){
    			bez = feldfenster.elements[i].id;
    			inh = feldfenster.elements[i].checked;
    		} else {
		        bez = feldfenster.elements[i].id;
		        inh = encodeURIComponent(feldfenster.elements[i].value);
		        id = feldfenster.elements[i].name;
    		}
	        serial += bez+"="+inh+"&id_"+id+"="+id+"&";   
    	}
    }
   // alert(serial);
    sa=send2server("action=saveCats&"+serial);
    alert("gespeichert!");
    window.location.href="index.php?cat=admin&subcat=kategorie&site=category_edit";
}
// ----------------------------------------


// --- Popup-Fenster mit Kategorien �ffnen ---
function openCatlist(parCatId){
	catlistWindow = window.open(fullPath+'/sites/admin/kategorie/catlist.php?id='+parCatId, "Kategorien", "width=400, height=900, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
}

// --- Kategorie einer anderen unterordnen ---
function addChCat2parCat(parCatId, catid, cat){
	var targetDiv = opener.document.getElementById('categories');
	var divValue = "";
	var targetDivHTML = targetDiv.innerHTML;
	var serial = "";
	var numCats = targetDiv.childNodes.length;
	var newdiv = opener.document.createElement('div');
	var haveInsert;
	var ebene = catid;
	var parameter = opener.document.location.search;
	var parms = parameter.toQueryParams();
	var notAllowed = false;
	
	if(parms['pid']!=null){
		ebene = parms['pid']+"-"+catid;
	}
	//alert(ebene);
	newdiv.style.clear = "both";
	sa = send2server("action=getChCats&catid="+catid+"&parCatId="+parCatId);
	if(sa){
		felder = sa.split('&');
		var strNewDiv = "";
		//strNewDiv += "\n<div style=\"clear:both\">";
		strNewDiv += "\n<div style=\"float:left\">";
		strNewDiv += "<ul class=\"catlist\"><a href=\"#\" id=\"catid_"+catid+"\" name=\""+catid+"\" onclick=\"setParCat("+catid+", "+parCatId+");\">"+unescape(cat)+"</a>";
		strNewDiv += "\n<a href=\"#\" onclick=\"delCatRel("+catid+", "+parCatId+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>\n";
		
		for(i=0; i<felder.length; i++){
			idBegin = felder[i].indexOf("_")+1;
			idEnds = felder[i].indexOf("=");
			idLength = idEnds-idBegin;
			newlabel = felder[i].substr(felder[i].indexOf("=")+1);
			fieldID = felder[i].substr(idBegin, idLength);
			if(fieldID==parCatId || fieldID==catid || parCatId==catid){
				notAllowed = true;
			}
			if(newlabel!=""){
				strNewDiv += "\n<li>"+decodeURIComponent(newlabel)+"</li>";
			}
		}
		strNewDiv += "\n</ul>";
		strNewDiv += "\n</div>";
		strNewDiv += "\n</div>";
		
		if(notAllowed){
			alert("Diese Kategorie beinhaltet Kind-Kategorien, die in diesem Zweig bereits enthalten ist!!");
		} else {
			var newText = opener.document.createTextNode(strNewDiv);
			newdiv.innerHTML = strNewDiv;
			var strTest = "";
			var arrTest = targetDiv.getElementsByTagName("ul").innerHTML;
			if(targetDiv.getElementsByTagName("ul").length>0){
				haveInsert = false;
				for(i=0; i<targetDiv.getElementsByTagName("ul").length; i++){
					if(encodeURIComponent(targetDiv.getElementsByTagName("ul")[i].firstChild.innerHTML)>encodeURIComponent(cat)){
						targetDiv.insertBefore(newdiv, targetDiv.getElementsByTagName("ul")[i].parentNode.parentNode);
						haveInsert=true;
						break;
					}
				}
				if(!haveInsert){
					targetDiv.appendChild(newdiv);
				}
			} else {
				targetDiv.appendChild(newdiv);
			}
			
			serial = "catid="+catid+"&parCatId="+parCatId+"&ebene="+ebene+"&";
			//alert(serial);
			sa=send2server("action=saveCatRel&"+serial);
		}
		//alert(fullPath);
		//alert(sa);
	    //opener.window.history.go(0);
	    //opener.window.location.reload();
	    //alert(catid);
	    opener.window.document.getElementById("catid_"+catid).focus();
	} else {
		alert("Kategorie ist auf dieser Ebene bereits vorhanden!");
	}
}

// --- Seite f�r neue Kategorienzuordnung laden ---
function setParCat(catid, parCatid){
	var cat = document.getElementById('catid_'+catid);
	var parameter = document.location.search;
	var parms = parameter.toQueryParams();
	if(parms['pid']!=null){
		parCatid = parms['pid']+"-"+catid;
	} else {
		parCatid = catid;
	}
	window.location.href="index.php?cat=admin&subcat=kategorie&site=category_assign&pid="+parCatid+"&id="+catid;
	//openCatlist(catid);
}

// --- Kategorienzuordnung l�schen ---
function delCatRel(catid, parCatId){
	antwort = confirm("M"+String.fromCharCode(246)+"chten Sie die Verkn"+String.fromCharCode(252)+"pfung wirklich l"+String.fromCharCode(246)+"schen?");
	if(antwort==true){
		var serial = "";
		
		serial = "catid="+catid+"&parCatId="+parCatId+"&";
		send2server("action=delCatRel&"+serial); 
		window.history.go(0);
	}
}
// -----------------------------------

// --- Eingabefeld f�r Kategorie hinzuf�gen ---
function addCatfield() {
  //var ni = document.getElementById('catfieldsDiv');
  var catfieldsTable = document.getElementById('catfieldsTable');
   var catfieldsBody = document.getElementById('catfieldsBody');
  var lastRowClass = catfieldsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  //var newdiv = document.createElement('div');
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellRight";
  var divIdName = 'kategorie_'+(num);
  var strValue = "";
  
  //newdiv.id = divIdName;
  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  /*
  strValue += "\n<label><input class=\"textinput\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"50\" maxlength=\"255\" \/></label>";
  strValue += "\n<a href=\"#\" onclick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  */
  
  	//strValue += "\n<td class=\"gCellLeft\">";
  	newCol1.colSpan = "2";
	newCol1.innerHTML = "\n<input fieldtype=\"1\" class=\"normalfont\" name=\""+newID+"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"70\" maxlength=\"255\" />";
	//strValue += "\n</td>";
	
	//strValue += "\n<td class=\"gCellRight\">";
	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	//strValue += "\n</td>";
  //newdiv.innerHTML = strValue;
  //newRow.innerHTML = strValue;
  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  //ni.appendChild(newdiv);
  catfieldsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
// ----------------------------------------

// --- Auswahlfeld f�r Kategorie hinzuf�gen ---
function addCatfieldCombo() {
  //var ni = document.getElementById('catfieldsDiv');
  var catfieldsTable = document.getElementById('catfieldsTable');
  var catfieldsBody = document.getElementById('catfieldsBody');
  var lastRowClass = catfieldsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  //var newdiv = document.createElement('div');
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellRight";
  var divIdName = 'kategorie_'+(num);
  var strValue = "";
  var strCol1 = "";
  //newdiv.id = divIdName;
  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  /*
  strValue += "\n<label><input class=\"textinput\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"50\" maxlength=\"255\" \/></label>";
  strValue += "\n<a href=\"#\" onclick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  */
  
  	strCol1 += "\n<input class=\"normalfont\" fieldtype=\"2\" name=\""+newID+"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"70\" maxlength=\"255\" />";
  	strCol1 += "\n<select id=\"selCFEw_"+newID+"\">";
  	strCol1 += "\n<option value=\"zero\">--- Bitte ausw&auml;hlen ---</option>";
  	strCol1 += "\n</select>";
  	strCol1 += "\n<a class=\"midfont\" href=\"\" onclick=\"openAuswahlfelderEdit("+newID+", "+num+", 'new'); return false;\" title=\"Auswahlwerte bearbeiten\">edit</a>";
  	newCol1.colSpan = "2";
	newCol1.innerHTML = strCol1;
	//strValue += "\n</td>";
	
	//strValue += "\n<td class=\"gCellRight\">";
	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	//strValue += "\n</td>";
  //newdiv.innerHTML = strValue;
  //newRow.innerHTML = strValue;
  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  //ni.appendChild(newdiv);
  catfieldsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
// ----------------------------------------

// --- Memofeld f�r Kategorie hinzuf�gen ---
function addMemofield() {
  //var ni = document.getElementById('catfieldsDiv');
  var catfieldsTable = document.getElementById('catfieldsTable');
   var catfieldsBody = document.getElementById('catfieldsBody');
  var lastRowClass = catfieldsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  //var newdiv = document.createElement('div');
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellRight";
  var divIdName = 'kategorie_'+(num);
  var strValue = "";
  
  //newdiv.id = divIdName;
  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  /*
  strValue += "\n<label><input class=\"textinput\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"50\" maxlength=\"255\" \/></label>";
  strValue += "\n<a href=\"#\" onclick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  */
  
  	//strValue += "\n<td class=\"gCellLeft\">";
  	newCol1.colSpan = "2";
	newCol1.innerHTML = "\n<textarea fieldtype=\"4\" class=\"normalfont\" name=\""+newID+"\" id=\"lkategorie_"+newID+"\" cols=\"60\" rows=\"2\"></textarea>";
	//strValue += "\n</td>";
	
	//strValue += "\n<td class=\"gCellRight\">";
	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	//strValue += "\n</td>";
  //newdiv.innerHTML = strValue;
  //newRow.innerHTML = strValue;
  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  //ni.appendChild(newdiv);
  catfieldsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
// ----------------------------------------

// --- PopUp-Fenster mit Auswahlfeldern f�r Auswahlbox �ffnen ---
function openAuswahlfelderEdit(fieldid, pos, newfield){
	if(newfield=="new"){
		var choise=confirm("Um Auswahlfelder zur Auswahlbox hinzuzuf"+String.fromCharCode(252)+"gen, muss diese erst gespeichert werden.\nWollen Sie jetzt speichern?");
		if(choise){
			//alert("funcid="+funcid+" pos="+pos);
			var feldfenster = document.forms[0];
			var pf=document.getElementById('lkategorie_'+fieldid);
			if(pf.value==""){
				alert("Sie m"+String.fromCharCode(252)+"ssen dem Feld nat"+String.fromCharCode(252)+"rlich irgendeine Bezeichnung geben!");
			} else {
				var serial = "pf_2="+pf.value+"&";
				sa=send2server("action=saveCatfield&"+serial);
				//alert(sa);
				fieldid = sa;
				if(sa==""){
					alert("Speichern war NICHT erfolgreich!");
				} else {
					pf.id = "lkategorie_"+fieldid;
					pf.name = fieldid;
					modwindow = window.open(fullPath+'/sites/admin/kategorie/catfieldComboEwEdit.php?fieldid='+fieldid+'&pos='+pos, "Auswahlfelder", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
				}
			}
		}
	} else {
		modwindow = window.open(fullPath+'/sites/admin/kategorie/catfieldComboEwEdit.php?fieldid='+fieldid+'&pos='+pos, "Auswahlfelder", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
	}
	
}

// --- Auswahl-Eingabewert f�r Eingabefeld hinzuf�gen ---
function addKsfEW() {
  //var ni = document.getElementById('catfieldsDiv');
  var catfieldsEWsBody = document.getElementById('catfieldsEWsBody');
  if(catfieldsEWsBody.lastChild){
	  var lastRowClass = catfieldsEWsBody.lastChild.className;
	  if(lastRowClass=="gRowOdd"){
	  	var newRowClass = "gRowEven";
	  } else {
	  	var newRowClass = "gRowOdd";
	  }
  } else {
  	var newRowClass = "gRowEven";
  }
  
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  //var newdiv = document.createElement('div');
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellRight";
  var divIdName = 'ksfew_'+(num);
  var strValue = "";
  
  //newdiv.id = divIdName;
  newRow.id = divIdName;
  newRow.className = newRowClass;

  	//strValue += "\n<td class=\"gCellLeft\">";
  	newCol1.colSpan = "2";
	newCol1.innerHTML = "\n<input class=\"normalfont\" name=\"\" type=\"text\" id=\"lKsfEw_"+newID+"\" size=\"70\" maxlength=\"255\" />";
	//strValue += "\n</td>";
	
	newCol2.colSpan = "1";
	newCol2.width = "16";
	//strValue += "\n<td class=\"gCellRight\">";
	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeKsfEW("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"../../../images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	//strValue += "\n</td>";

  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  //ni.appendChild(newdiv);
  catfieldsEWsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lKsfEw_"+newID).focus();
}
// ----------------------------------------

// --- Auswahl-Eingabewert f�r Eingabefeld entfernen ---
function removeKsfEW(fieldId) {
  var d = document.getElementById('catfieldsEWsBody');
  var olddiv = document.getElementById(('ksfew_'+fieldId));
  d.removeChild(olddiv);
}
// ----------------------------------------

// --- Auswahl-Eingabewert f�r Eingabefeld l�schen ---
function delKsfEW(fieldId, pos){
	if(confirmDelete()){
		var serial = "";		
		serial = "fieldId="+fieldId;
	
		send2server("action=delKsfEW&"+serial); 
		removeKsfEW(pos);
	}
}
// ----------------------------------------

// --- Auswahl-Eingabewerte f�r Eingabefeld speichern ---
function saveKsfEW(catfieldid, pos) {
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var sourceSelect = opener.document.getElementById('selCFEw_'+catfieldid);
    var strSourceSelect = "";
    var emptyField = false;
    var serial="";
	
	strSourceSelect += "\n<option value=\"zero\">--- Bitte ausw&auml;hlen ---</option>";
	serial += "catfieldid="+catfieldid+"&";
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text"){
	        bez = feldfenster.elements[i].id;
	        inh = encodeURIComponent(feldfenster.elements[i].value);
	        if(inh=="") emptyField=true;
	        
	        id = feldfenster.elements[i].name;
	        
	        strSourceSelect += "\n<option value=\""+id+"\">"+feldfenster.elements[i].value+"</option>";
	        serial += bez+"="+inh+"&id_"+id+"="+id+"&";
	        //serial += bez+"="+inh+"&id_"+id+"="+id+"\n";   
    	}
    }
    if(emptyField){
    	alert("Es sind keine leeren Felder gestattet!");
    } else {
	    //alert(serial);
	    sa=send2server("action=saveKsfEW&"+serial);
	    sourceSelect.innerHTML = strSourceSelect;
	    window.close();
    }
}
// ----------------------------------------

// --- Auswahlfeld einfach/mehrfach - Auswahl ---
function setMC(catfieldid, marked){
	var mark;
	if(marked){
		mark=1;
	} else {
		mark=0;
	}
	send2server("action=setMC&catfieldid="+catfieldid+"&mark="+mark);
}
// ----------------------------------------------

// --- Eingabefeld f�r Kategorie entfernen ---
function removeCatfield(fieldId) {
  //var d = document.getElementById('catfieldsDiv');
  //var d = document.getElementById('catfieldsTable');
  var d = document.getElementById('catfieldsBody');
  var olddiv = document.getElementById(('kategorie_'+fieldId));
  d.removeChild(olddiv);
}
// ----------------------------------------

// --- Eingabefeld f�r Kategorie l�schen ---
function delCatfield(catfieldid, pos){
	if(confirmDelete()){
		/*
		var serial = "";
		var feldfenster = document.forms[0];
		var anzahl = feldfenster.elements.length;
		var sourceSelect = opener.document.getElementById('selCFEw_'+catfieldid);
    	var strSourceSelect = "";
    	
    	for(i=0; i<anzahl; i++) {
	    	if(feldfenster.elements[i].type=="text"){
		        bez = feldfenster.elements[i].id;
		        inh = encodeURIComponent(feldfenster.elements[i].value);
		        if(inh=="") emptyField=true;
		        
		        id = feldfenster.elements[i].name;
		        
		        strSourceSelect += "\n<option value=\""+id+"\">"+feldfenster.elements[i].value+"</option>";
	    	}
	    }
    	*/
		serial = "id="+catfieldid;
	
		send2server("action=delCatfield&"+serial);
		//sourceSelect.innerHTML = strSourceSelect;
		removeCatfield(pos);
	}
}
// ----------------------------------------

// --- Eingabefeld f�r Kategorien speichern ---
function saveCatfields() {
    var serial;
    var feldfenster = document.forms['formCatfield'];
    var anzahl = feldfenster.elements.length;
    var serial="";
	//alert(document.getElementById('lkategorie_851').tagName);
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text"){
    		//alert(document.forms[0].name);
	        bez = feldfenster.elements[i].id;
	        inh = encodeURIComponent(feldfenster.elements[i].value);
	        feldtyp = feldfenster.elements[i].getAttribute("fieldtype");
	        id = feldfenster.elements[i].name;

	        serial += bez+"="+inh+"&id_"+id+"="+id+"&type_"+id+"="+feldtyp+"&";
	        //serial += bez+"="+inh+"&id_"+id+"="+id+"\n";   
    	} else if(feldfenster.elements[i].tagName=="TEXTAREA"){
    		bez = feldfenster.elements[i].id;
	        inh = encodeURIComponent(feldfenster.elements[i].value);
	        feldtyp = feldfenster.elements[i].getAttribute("fieldtype");
	        id = feldfenster.elements[i].name;
	        
	        serial += bez+"="+inh+"&id_"+id+"="+id+"&type_"+id+"="+feldtyp+"&";
    	}
    }
   // alert(serial);
    sa=send2server("action=saveCatfields&"+serial);
    //window.location.href="index.php?cat=admin&site=index_admin";
    window.location.href="index.php?cat=admin&subcat=kategorie&site=catfield_edit";
}
// ----------------------------------------

// --- Standardeingabefeld sperren/entsperren ---
function lockKsef(catfieldid, mode) {
	var catfieldlocktd = $('catfieldlocktd'+catfieldid);
	var catfield = $("lkategorie_"+catfieldid);
	var tdcontent = "";
	var serial;
	serial = "catfieldid="+catfieldid+"&mode="+mode;
	
	if(mode==1){
		catfield.disabled = "disabled";
		tdcontent += "\n<tr>";
		tdcontent += "\n<td width=\"100\">";
		tdcontent += "\n<a class=\"midfont\" style=\"color:red;\">gesperrt</a>";
		tdcontent += "\n<br />";
		tdcontent += "\n<a class=\"smallfont\" href=\"\" onclick=\"lockKsef("+catfieldid+", 0); return false;\">entsperren</a>";
		tdcontent += "\n</td>";
		tdcontent += "\n</tr>";
	} else {
		catfield.disabled = "";
		tdcontent += "\n<tr>";
		tdcontent += "\n<td width=\"100\">";
		tdcontent += "\n<a class=\"midfont\" href=\"\" onclick=\"lockKsef("+catfieldid+", 1); return false;\">sperren</a>";
		tdcontent += "\n</td>";
		tdcontent += "\n</tr>";
	}
	
	catfieldlocktd.innerHTML = tdcontent;
	send2server("action=lockCatfield&"+serial);
}
// -------------------------------------------

// --- Eingabefeldgruppe hinzuf�gen ---
function addCatfieldGroup() {
  //var ni = document.getElementById('catfuncsDiv');
  var catfieldGroupsTable = document.getElementById('catfieldGroupsTable');
  var catfieldGroupsBody = document.getElementById('catfieldGroupsBody');
  var lastRowClass = catfieldGroupsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;

  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellMid";
  newCol2.align = "right";
  var newCol3 = document.createElement('td');
  newCol3.className = "gCellRight";
  newCol3.align = "right";
  var divIdName = 'group_'+(num);
  var strValue = "";
  
  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  
	newCol1.innerHTML = "\n<input class=\"normalfont\" name=\"\" type=\"text\" id=\"lgroup_"+newID+"\" size=\"100%\" maxlength=\"255\" />";
	newCol1.innerHTML += "\n<br />";
	newCol1.innerHTML += "\n<div id=\"fields"+num+"\">";
	newCol1.innerHTML += "\n</div>";

	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeCatfieldGroup("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	//newCol3.innerHTML = "\n<a href=\"#\" class=\"midfont\" style=\"text-decoration:underline;\" onClick=\"openEGFPopUp("+newID+", "+num+", 'new'); return false;\">EG-Felder</a>";
	newCol3.innerHTML = "\n&nbsp;";

  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  newRow.appendChild(newCol3);

  catfieldGroupsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lgroup_"+newID).focus();
}
// ----------------------------------------

// --- PopUp-Fenster mit Modulen �ffnen ---
function delCatfieldGroup(fieldgroupID, pos){
	var choise=confirm("Wollen Sie die Gruppe wirklich l"+String.fromCharCode(246)+"schen?\nDie ihr zugeordneten Eingabefelder werden dann gruppenlos sein");
	if(choise){
		var serial = "fieldgroupID"+"&";
		sa=send2server("action=delCatfieldGroup&"+serial);
	}
}
// ----------------------------------------


// --- PopUp-Fenster mit Modulen �ffnen ---
function openEGFPopUp(fieldgroupID, pos, newfield){
	if(newfield=="new"){
		var choise=confirm("Um Eingabefelder zur Gruppen hinzuzuf"+String.fromCharCode(252)+"gen, muss diese erst gespeichert werden.\nWollen Sie jetzt speichern?");
		if(choise){
			//alert("funcid="+funcid+" pos="+pos);
			var feldfenster = document.forms[0];
			var pf=document.getElementById('lkategorie_'+fieldgroupID);
			var serial = "pf="+pf.value+"&";
			sa=send2server("action=saveCatfieldGroups&"+serial);
			//alert(sa);
			fieldgroupID = sa;
			pf.name = fieldgroupID;
			modwindow = window.open(fullPath+'/sites/admin/kategorie/fl4fgroups.php?fgid='+fieldgroupID+'&id='+pos, "Eingabefelder", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
		}
	} else {
		modwindow = window.open(fullPath+'/sites/admin/kategorie/fl4fgroups.php?fgid='+fieldgroupID+'&id='+pos, "Eingabefelder", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
	}
	
}


// --- Eingabefeld einer Kategorie zuordnen ---
function addField2FG(catfieldid, catfield, feldtyp, strOptVal, pos, fieldgroupID){
	//var serial = "fgid="+$fieldgroupID+"&fid="+catfieldid;
	var sourceDoc = opener.document;
	var catfieldsBody = opener.document.getElementById('fields'+pos);
  //var ni = opener.document.getElementById('catfieldsDiv');
	var numi = opener.document.getElementById('theValue');
	var num = (opener.document.getElementById('theValue').value -1)+ 2;

	
	if(catfieldsBody.lastChild){
		var lastRowClass = catfieldsBody.lastChild.className;
		if(lastRowClass=="gRowOdd"){
			var newRowClass = "gRowEven";
		} else {
			var newRowClass = "gRowOdd";
		}
	} else {
		var newRowClass = "gRowEven";
	}
	
	var newRow = sourceDoc.createElement('tr');
	newRow.id = "catfield_"+ catfieldid;
	//newRow.className = newRowClass;
	newRow.style.cursor = "pointer";

	var newCol1 = sourceDoc.createElement('td');
	var strCol1 = "";
	newCol1.className = "gCellLeft";
	newCol1.align = "left";
	strCol1 += "\n<a class=\"smallfont\">"+catfield+"</a>";
	newCol1.innerHTML = strCol1;
	
	var newCol2 = sourceDoc.createElement('td');
	var strCol2 = "";
	newCol2.className = "gCellMid";
	newCol2.align = "left";
	if(feldtyp==1){
		strCol2 += "\n<input id=\"lcatfield_"+catfieldid+"\" name=\""+catfieldid+"\" type=\"text\" class=\"smallfont\" size=\"50\" disabled />";
	} else {
		strCol2 += "\n<select class=\"smallfont\" id=\"lcatfield_"+catfieldid+"\" name=\""+catfieldid+"\">";
		strCol2 += "\n<option value=\"zero\">--- Bitte ausw&auml;hlen ---</option>";
		strCol2 += strOptVal;
		strCol2 += "\n</select>";
	}
	newCol2.innerHTML = strCol2;
	
		
	var newCol4 = sourceDoc.createElement('td');
	var strCol4 = "";
	newCol4.className = "gCellRight";
	newCol4.align = "right";
	strCol4 += "\n<a href=\"#\" onclick=\"delCfGrpRel("+fieldgroupID+","+catfieldid+","+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
 	newCol4.innerHTML = strCol4;
 	
	newRow.appendChild(newCol1);
  	newRow.appendChild(newCol2);
  
  	newRow.appendChild(newCol4);
  	
  	catfieldsBody.appendChild(newRow);
  	
  	saveCfGrpRel(fieldgroupID, catfieldid);
}
// ----------------------------------------

// -- Gruppenzuordnung des Eingabefeldes speichern --
function saveCfGrpRel(fieldgroupID, catfieldid) {
	var serial = "fgid="+fieldgroupID+"&fid="+catfieldid;
	sa=send2server("action=saveCfGrpRel&"+serial);
}
// ----------------------------------------

// -- Gruppenzuordnung des Eingabefeldes l�schen --
function delCfGrpRel(fieldgroupID, catfieldid, pos) {
	
	saveCfGrpRel(0, catfieldid);
	
	var catfieldsBody = document.getElementById('fields'+pos);
	var oldRow = document.getElementById(('catfield_'+catfieldid));
	
	catfieldsBody.removeChild(oldRow);
}
// ----------------------------------------

// --- Produktfunktionen f�r Kategorien speichern ---
function saveCatfieldGroups() {
    var serial;
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var serial="";
	
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text"){
	        bez = feldfenster.elements[i].id;
	        inh = encodeURIComponent(feldfenster.elements[i].value);
	        id = feldfenster.elements[i].name;
	        
	        serial += bez+"="+inh+"&id_"+id+"="+id+"&";   
    	}

    }
    //alert(serial);
    sa=send2server("action=saveCatfieldGroups&"+serial);
    //window.location.href="index.php?cat=admin&site=index_admin";
    window.location.href="index.php?cat=admin&subcat=kategorie&site=catfieldgroups";
}
// ----------------------------------------

// --- Popup-Fenster mit Eingabefeldern �ffnen ---
function openCatfieldlist(catfieldid){
	catlistWindow = window.open(fullPath+'/sites/admin/kategorie/catfieldlist.php?id='+catfieldid, "Kategorien", "width=800, height=900, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
}
// ----------------------------------------

// --- Eingabefeld einer Kategorie zuordnen ---
function addField2Cat(catfieldid, catfield, feldtyp, strOptVal, mand, inheritFields){
	if(inheritFields){
		var sourceDoc = document;
		var catfieldsBody = document.getElementById('catfieldsBody');
	  //var ni = document.getElementById('catfieldsDiv');
		var numi = document.getElementById('theValue');
		var num = (document.getElementById('theValue').value -1)+ 2;
	} else {
		var sourceDoc = opener.document;
		var catfieldsBody = opener.document.getElementById('catfieldsBody');
	  //var ni = opener.document.getElementById('catfieldsDiv');
		var numi = opener.document.getElementById('theValue');
		var num = (opener.document.getElementById('theValue').value -1)+ 2;
	}
	
	if(catfieldsBody.lastChild){
		var lastRowClass = catfieldsBody.lastChild.className;
		if(lastRowClass=="gRowOdd"){
			var newRowClass = "gRowEven";
		} else {
			var newRowClass = "gRowOdd";
		}
	} else {
		var newRowClass = "gRowEven";
	}
	
	var newRow = sourceDoc.createElement('tr');
	newRow.id = "catfield_"+ catfieldid;
	newRow.className = newRowClass;
	newRow.style.cursor = "pointer";

	var newCol1 = sourceDoc.createElement('td');
	var strCol1 = "";
	newCol1.className = "gCellLeft";
	newCol1.align = "left";
	strCol1 += "\n<a class=\"normalfont\">"+catfield+"</a>";
	newCol1.innerHTML = strCol1;
	
	var newCol2 = sourceDoc.createElement('td');
	var strCol2 = "";
	newCol2.className = "gCellMid";
	newCol2.align = "left";
	if(feldtyp==1){
		strCol2 += "\n<input id=\"lcatfield_"+catfieldid+"\" name=\""+catfieldid+"\" type=\"text\" class=\"normalfont\" size=\"50\" disabled />";
	} else if(feldtyp==4){
		strCol2 += "\n<textarea id=\"lcatfield_"+catfieldid+"\" name=\""+catfieldid+"\" class=\"normalfont\" cols=\"60\" rows=\"2\" disabled></textarea>";
	} else {
		strCol2 += "\n<select id=\"lcatfield_"+catfieldid+"\" name=\""+catfieldid+"\">";
		strCol2 += "\n<option value=\"zero\">--- Bitte ausw&auml;hlen ---</option>";
		strCol2 += strOptVal;
		strCol2 += "\n</select>";
	}
	newCol2.innerHTML = strCol2;
	
	var newCol3 = sourceDoc.createElement('td');
	var strCol3 = "";
	newCol3.className = "gCellMid";
	newCol3.align = "left";
	strCol3 += "\n<input type=\"checkbox\" id=\"mdfield_"+catfieldid+"\" style=\"vertical-align:middle;\" "+(mand==1?"checked":"")+"/><a style=\"margin-right:15px;\">Pflichtfeld</a>";
	newCol3.innerHTML = strCol3;
	
	var newCol4 = sourceDoc.createElement('td');
	var strCol4 = "";
	newCol4.className = "gCellRight";
	newCol4.align = "right";
	strCol4 += "\n<a href=\"#\" onclick=\"removeCatfieldRel("+catfieldid+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
 	newCol4.innerHTML = strCol4;
 	
	newRow.appendChild(newCol1);
  	newRow.appendChild(newCol2);
  	newRow.appendChild(newCol3);
  	newRow.appendChild(newCol4);
  	
  	catfieldsBody.appendChild(newRow);
}
// ----------------------------------------

// --- Produktfunktionen von Elternkategorie erben ---
function inheritFields2Cat(catid){
	mand = 0;
	sa=send2server("action=inheritFields2Cat&catid="+catid);
	
	if(sa){
		arrPf = sa.split('&');
		
		for(i=0; i<arrPf.length-1; i++){
			idBegin = arrPf[i].indexOf("_")+1;
			idEnds = arrPf[i].indexOf(">");
			mandBegin = arrPf[i].indexOf(">")+1;
			mandEnds = arrPf[i].indexOf(">>");
			feldtypBegin = arrPf[i].indexOf(">>")+2;
			feldtypEnds = arrPf[i].indexOf("=");
			idLength = idEnds-idBegin;
			pf = arrPf[i].substr(arrPf[i].indexOf("=")+1);
			pfid = arrPf[i].substr(idBegin, idLength);
			
			var strOptVal = send2server("action=getKsfEw&fieldID="+pfid);
			mand = parseInt(arrPf[i].substr(arrPf[i].indexOf(">")+1,1));
			feldtyp = parseInt(arrPf[i].substr(arrPf[i].indexOf(">>")+2,1));
			addField2Cat(pfid, pf, feldtyp, strOptVal, mand, true);
			//alert(pfid, pf, feldtyp, strOptVal, mand, true);
		}
	} else {
		alert("Keine Felder von der direkten Elternkategorie zu erben.");
		document.getElementById('inheritButton').disabled = "disabled";
	}
}
// ----------------------------------------

// --- Seite f�r neue Eingabefeldzuordnung laden ---
function setParCat4Fields(catid, parCatid){
	var cat = document.getElementById('catid_'+catid);
	window.location.href="index.php?cat=admin&subcat=kategorie&site=catfield_assignlist&id="+catid;
	//openCatlist(catid);
}
// ----------------------------------------

// --- Eingabefeldzuordnung entfernen ---
function removeCatfieldRel(fieldId) {
	var catfieldsBody = document.getElementById('catfieldsBody');
	var oldRow = document.getElementById(('catfield_'+fieldId));
	catfieldsBody.removeChild(oldRow);
}
// ----------------------------------------

// --- Eingabefeldzuordnung l�schen ---
function delCatfieldRel(catid, catfieldid){
	antwort = confirm("M"+String.fromCharCode(246)+"chten Sie die Verkn"+String.fromCharCode(252)+"pfung wirklich l"+String.fromCharCode(246)+"schen?");
	if(antwort==true){
		var serial = "";
		
		serial = "catid="+catid+"&catfieldid="+catfieldid+"&";
		send2server("action=delCatfieldRel&"+serial); 
		window.history.go(0);
	}
}
// ----------------------------------------

// --- Alle Eingabefeldzuordnungen l�schen ---
function delAllCatfieldRels(catid){
	x = confirm("M"+String.fromCharCode(246)+"chten Sie wirklich ALLE Verkn"+String.fromCharCode(252)+"pfungen l"+String.fromCharCode(246)+"schen?");
	if(x){
		antwort = confirm("WIRKLICH???");
		if(antwort==true){
			var serial = "";
			
			serial = "catid="+catid+"&";
			send2server("action=delAllCatfieldRels&"+serial); 
			window.history.go(0);
		}
	}
}
// ----------------------------------------

// --- Eingabefeldzuordnungen speichern ---
function saveCatfieldRels(catid) {
    var serial;
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var serial="";
	serial= "catid="+catid+"&";

    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text" || feldfenster.elements[i].tagName=="SELECT" || feldfenster.elements[i].tagName=="TEXTAREA"){
	        bez = feldfenster.elements[i].id;
	        inh = feldfenster.elements[i].value;
	        id = feldfenster.elements[i].name;
	        if(document.getElementById('mdfield_'+id).checked){
	        	mdfield = 1;
	        } else {
	        	mdfield = 0;
	        }
	        serial += "catfieldid_"+id+"="+mdfield+"&";
	        //alert(mdfield);  
	         
    	}
    	//alert(feldfenster.elements[i].tagName);
    }
   //alert(serial);
    sa=send2server("action=saveCatfieldRels&"+serial);
   // alert(sa);
    //window.location.href="index.php?cat=admin&site=index_admin";
    window.location.href="index.php?cat=admin&subcat=kategorie&site=catfield_assign&id="+catid;
}
// ----------------------------------------

// --- Produktfunktion f�r Kategorie hinzuf�gen ---
/*
function addCatfunc() {
  var ni = document.getElementById('catfuncsDiv');
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  var newdiv = document.createElement('div');
  var divIdName = 'kategorie_'+(num);
  var strValue = "";
  
  newdiv.id = divIdName;
  
  strValue += "\n<label><input class=\"textinput\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"50\" maxlength=\"255\" \/></label>";
  strValue += "\n<a href=\"#\" onclick=\"removeCatfunc("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  strValue += "\n<a href=\"#\" onClick=\"openModulPopUp("+newID+", "+num+", 'new'); return false;\">Module</a>\n";
  strValue += "<div id=\"mods"+num+"\">\n";
  newdiv.innerHTML = strValue;
  ni.appendChild(newdiv);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
*/
// ----------------------------------------

// --- Eingabefeld f�r Kategorie hinzuf�gen ---
function addCatfunc() {
  //var ni = document.getElementById('catfuncsDiv');
  var catfuncsTable = document.getElementById('catfuncsTable');
  var catfuncsBody = document.getElementById('catfuncsBody');
  var lastRowClass = catfuncsBody.lastChild.className;
  if(lastRowClass=="gRowOdd"){
  	var newRowClass = "gRowEven";
  } else {
  	var newRowClass = "gRowOdd";
  }
  var numi = document.getElementById('theValue');
  var num = (document.getElementById('theValue').value -1)+ 2;
  var newIDi = document.getElementById('newID');
  var newID = document.getElementById('newID').value;
  numi.value = num;
  //var newdiv = document.createElement('div');
  var newRow = document.createElement('tr');
  var newCol1 = document.createElement('td');
  newCol1.className = "gCellLeft";
  var newCol2 = document.createElement('td');
  newCol2.className = "gCellRight";
  var newCol3 = document.createElement('td');
  newCol3.className = "gCellRight";
  var divIdName = 'kategorie_'+(num);
  var strValue = "";
  
  //newdiv.id = divIdName;
  newRow.id = divIdName;
  newRow.className = newRowClass;
  
  /*
  strValue += "\n<label><input class=\"textinput\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"50\" maxlength=\"255\" \/></label>";
  strValue += "\n<a href=\"#\" onclick=\"removeCatfield("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
  */
  
  	//strValue += "\n<td class=\"gCellLeft\">";
	newCol1.innerHTML = "\n<input class=\"normalfont\" name=\"\" type=\"text\" id=\"lkategorie_"+newID+"\" size=\"100%\" maxlength=\"255\" />";
	newCol1.innerHTML += "\n<br />";
	newCol1.innerHTML += "\n<div id=\"mods"+num+"\">";
	//strValue += "\n</td>";
	
	//strValue += "\n<td class=\"gCellRight\">";
	newCol2.innerHTML = "\n<a href=\"#\" onClick=\"removeCatfunc("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	newCol3.innerHTML = "\n<a href=\"#\" onClick=\"openModulPopUp("+newID+", "+num+", 'new'); return false;\">Module</a>";
	//strValue += "\n</td>";
  //newdiv.innerHTML = strValue;
  //newRow.innerHTML = strValue;
  newRow.appendChild(newCol1);
  newRow.appendChild(newCol2);
  newRow.appendChild(newCol3);
  //ni.appendChild(newdiv);
  catfuncsBody.appendChild(newRow);
  newIDi.value = parseInt(newID)+1;
  document.getElementById("lkategorie_"+newID).focus();
}
// ----------------------------------------

// --- PopUp-Fenster mit Modulen �ffnen ---
function openModulPopUp(funcid, pos, newfield){
	if(newfield=="new"){
		var choise=confirm("Um Module zur Produktfunktion hinzuzuf"+String.fromCharCode(252)+"gen, muss diese erst gespeichert werden.\nWollen Sie jetzt speichern?");
		if(choise){
			//alert("funcid="+funcid+" pos="+pos);
			var feldfenster = document.forms[0];
			var pf=document.getElementById('lkategorie_'+funcid);
			var serial = "pf="+pf.value+"&";
			sa=send2server("action=saveCatfunc&"+serial);
			//alert(sa);
			funcid = sa;
			pf.name = funcid;
			modwindow = window.open(fullPath+'/sites/admin/modul/ml4func.php?funcid='+funcid+'&id='+pos, "Module", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
		}
	} else {
		modwindow = window.open(fullPath+'/sites/admin/modul/ml4func.php?funcid='+funcid+'&id='+pos, "Module", "width=900, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
	}
	
}


// --- Modul zur Produktfunktion hinzuf�gen ---
function insert_Mod_Zeile(funcid, modid, pos, werte, comms){
	var modDiv = opener.document.getElementById('mods'+pos);
	var newdiv =document.createElement('div');
	var strValue = "";
	var serial = "";
	
	newdiv.id = "modid_"+pos+"_"+modid;
	
	strValue += "\n<a href=\"#\" class=\"liste\" onmouseover=\"TagToTip('comm_"+pos+"_"+modid+"')\" onclick=\"delPFModRel("+pos+","+modid+","+funcid+"); return false;\">- "+unescape(werte)+"</a>\n";
	strValue += "<span style=\"display: none;\" id=\"comm_"+pos+"_"+modid+"\">"+comms+"</span>\n";
	strValue += "<input type=\"hidden\" id=\"lmodid"+modid+"\" name=\""+funcid+"\" />";
	newdiv.innerHTML = strValue;
	modDiv.appendChild(newdiv);
	
	serial += "fid="+funcid+"&mid="+modid+"&";
	sa=send2server("action=saveFuncMod&"+serial);
	//alert(sa);
}
// -------------------------------------------

// --- Beziehung zwischen Produktfunktion und Modul wird gel�scht ---
function delPFModRel(pos, modid, funcid){
	var choise=confirm("Wollen Sie diese Beziehung wirklich l"+String.fromCharCode(246)+"schen?");
	if(choise){
		var pDivId = "mods"+pos;
		var cDivId = "modid_"+pos+"_"+modid;
		var str = "";
		
		var d = $(pDivId);
		var olddiv = $(cDivId);
		var serial = "";
		d.removeChild(olddiv);
		
		serial += "fid="+funcid+"&mid="+modid+"&";
		send2server("action=delFuncMod&"+serial);
	}
	//location.reload();
}
// -------------------------------------------

// --- Funktion f�r k�rperlich beeintr�chtigte Personen setzen ---
function setFuncHandicapped(funcid, handcap){
		var hc;
		if(handcap){
			hc=1;
		} else {
			hc=0;
		}
		var serial = "fid="+funcid+"&hc="+hc+"&";
		//alert(serial);
		send2server("action=setFuncHandicapped&"+serial);
}
// -------------------------------------------


// --- Produktfunktion f�r Kategorie entfernen ---
function removeCatfunc(fieldId) {
  //var d = document.getElementById('catfuncsDiv');
  var d = document.getElementById('catfuncsBody');
  var olddiv = document.getElementById(('kategorie_'+fieldId));
  d.removeChild(olddiv);
}
// ----------------------------------------

// --- Produktfunktion f�r Kategorie l�schen ---
function delCatfunc(id_kategorie, pos){
	if(confirmDelete()){
		var serial = "";
		var feldfenster = document.forms[0];
		var anzahl = feldfenster.elements.length;
		
		serial = "id="+id_kategorie;
	
		send2server("action=delCatfunc&"+serial); 
		removeCatfunc(pos);
	}
}
// ----------------------------------------

// --- Reihenfolge der Module innerhalb einer Produktfunktion speichern ---
function saveModposInFunc(){
	var pfid = document.getElementById('pfid').value;
  	var rows = document.getElementsByTagName('tr');
  	var odd=true;
  	var modid;
  	var serial = "pfid="+pfid+"&";
  	for(i=0; i<rows.length; i++){
  		
  		if((rows[i].id).indexOf("modulRow_")!=-1){
  			modid = rows[i].getAttribute('modid');
  			if(odd){
  				rows[i].style.backgroundColor = "white";
  				odd=false;
  			} else {
  				rows[i].style.backgroundColor = "#D0D1D3";
  				odd=true;
  			}
  			serial += "modid_"+i+"="+modid+"&";
  		}
  	}
  	sa=send2server("action=saveModposInFunc&"+serial); 
  }
// ----------------------------------------
 
// --- Produktfunktionen f�r Kategorien speichern ---
function saveCatfuncs() {
    var serial;
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var serial="";
	
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="text"){
	        bez = feldfenster.elements[i].id;
	        inh = encodeURIComponent(feldfenster.elements[i].value);
	        id = feldfenster.elements[i].name;
	        
	        serial += bez+"="+inh+"&id_"+id+"="+id+"&";   
    	}

    }
    //alert(serial);
    sa=send2server("action=saveCatfuncs&"+serial);
    //window.location.href="index.php?cat=admin&site=index_admin";
    window.location.href="index.php?cat=admin&subcat=kategorie&site=catfunc_edit";
}
// ----------------------------------------

// --- Popup-Fenster mit Produktfunktionen �ffnen ---
function openCatfunclist(catfuncid){
	catlistWindow = window.open(fullPath+'/sites/admin/kategorie/catfunclist.php?id='+catfuncid, "Kategorien", "width=600, height=400, left=100, top=200, resizable=yes, scrollbars=yes, dependent=yes");
}
// ----------------------------------------

// --- Produktfunktion einer Kategorie zuordnen ---
function addFunc2Cat(catfuncid, catfunc, bond, inheritFuncs){
	if(inheritFuncs){
		var catfuncsTable = document.getElementById('catfuncsTable');
		var catfuncsBody = document.getElementById('catfuncsBody');
		
		var numi = document.getElementById('theValue');
		var num = (document.getElementById('theValue').value -1)+ 2;
		var newIDi = document.getElementById('newID');
		var newID = document.getElementById('newID').value;
		numi.value = num;
		//var newdiv = document.createElement('div');
		var newRow = document.createElement('tr');
		var newCol1 = document.createElement('td');
		var newCol2 = document.createElement('td');
		var newCol3 = document.createElement('td');
		var newCol4 = document.createElement('td');
	} else {
		var catfuncsTable = opener.document.getElementById('catfuncsTable');
		var catfuncsBody = opener.document.getElementById('catfuncsBody');
		
		
		
		var numi = opener.document.getElementById('theValue');
		var num = (opener.document.getElementById('theValue').value -1)+ 2;
		var newIDi = opener.document.getElementById('newID');
		var newID = opener.document.getElementById('newID').value;
		numi.value = num;
		//var newdiv = opener.document.createElement('div');
		var newRow = opener.document.createElement('tr');
		var newCol1 = opener.document.createElement('td');
		var newCol2 = opener.document.createElement('td');
		var newCol3 = opener.document.createElement('td');
		var newCol4 = opener.document.createElement('td');
	}
	
	if(catfuncsBody.lastChild){
		var lastRowClass = catfuncsBody.lastChild.className;
		if(lastRowClass=="gRowOdd"){
			var newRowClass = "gRowEven";
		} else {
			var newRowClass = "gRowOdd";
		}
	} else {
		var newRowClass = "gRowOdd";
	}
	
	var divIdName = 'catfunc_'+(num);
	
	newRow.id = divIdName;
	newRow.className = newRowClass;
	
	newCol1.className = "gCellLeft";
	newCol2.className = "gCellMid";
	newCol2.width = "100";
	newCol3.className = "gCellMid";
	newCol3.width = "230";
	newCol4.className = "gCellRight";
	newCol4.width = "16";
	
	var strCol1 = "";
	var strCol2 = "";
	var strCol3 = "";
	var strCol4 = "";

	strCol1 = "\n<a class=\"normalfont\">"+catfunc+"</a>";
	
	strCol2 = "\n<input id=\"lcatfunc_"+catfuncid+"\" name=\""+catfuncid+"\" type=\"radio\" align=\"right\" disabled=\"true\" /><a class=\"normalfont\">ja</a>";
	strCol2 +="\n<input type=\"radio\" align=\"right\" disabled=\"true\" /><a class=\"normalfont\">nein</a>";
	
	strCol3 = "\n<select id=\"bond_"+catfuncid+"\" name=\"bond_"+catfuncid+"\">";
	strCol3 +="\n<option value=\"1\" "+(bond==1?"selected":"")+">vom Benutzer auszuw&auml;hlen</option>";
	strCol3 +="\n<option value=\"2\" "+(bond==2?"selected":"")+">Vorgabe/Benutzer kann &auml;ndern</option>";
	strCol3 +="\n<option value=\"3\" "+(bond==3?"selected":"")+">Vorgabe/nicht sichtbar</option>";
	strCol3 +="\n</select>";
	
	strCol4 = "\n<a href=\"#\" onclick=\"onclick=\"removeCatfuncRel("+num+"); return false;\"><img class=\"icon\" height=\"16\" width=\"16\" src=\"images/icons/b_drop.png\" alt=\"L&ouml;schen\" title=\"L&ouml;schen\" /></a>";
	
	newCol1.innerHTML = strCol1;
	newCol2.innerHTML = strCol2;
	newCol3.innerHTML = strCol3;
	newCol4.innerHTML = strCol4;

	newRow.appendChild(newCol1);
	newRow.appendChild(newCol2);
	newRow.appendChild(newCol3);
	newRow.appendChild(newCol4);

	catfuncsBody.appendChild(newRow);
	newIDi.value = parseInt(newID)+1;
	//Position.includeScrollOffsets = true;
	//Sortable.create(catfuncsBody,{tag:'tr', ghosting:true,constraint:'vertical'});
}
// ----------------------------------------

// --- Produktfunktionen von Elternkategorie erben ---
function inheritFunc2Cat(catid){
	sa=send2server("action=inheritFunc2Cat&catid="+catid);

	if(sa){
		arrPf = sa.split('&');
		
		for(i=0; i<arrPf.length-1; i++){
			idBegin = arrPf[i].indexOf("_")+1;
			idEnds = arrPf[i].indexOf("=");
			idLength = idEnds-idBegin;
			pfBegin = arrPf[i].indexOf("=")+1;
			pfEnds = arrPf[i].lastIndexOf("_");
			pfLength = pfEnds-pfBegin;
			pf = arrPf[i].substr(pfBegin, pfLength);
			pfid = arrPf[i].substr(idBegin, idLength);
			bond = arrPf[i].substr(arrPf[i].lastIndexOf("_")+1);
			
			//alert("pfid="+pfid+", pf="+pf+", bond="+bond+"\n");
			addFunc2Cat(pfid, pf, bond, true);
		}
	} else {
		alert("Keine Funktionen von der direkten Elternkategorie zu erben.");
		document.getElementById('inheritButton').disabled = "disabled";
	}
}
// ----------------------------------------


// --- Seite f�r neue Produktfunktionszuordnung laden ---
function setParCat4Funcs(catid, parCatid){
	var cat = document.getElementById('catid_'+catid);
	window.location.href="index.php?cat=admin&subcat=kategorie&site=catfunc_assignlist&id="+catid;
	//openCatlist(catid);
}
// ----------------------------------------

// --- Produktfunktionszuordnung entfernen ---
function removeCatfuncRel(fieldId) {
  var d = document.getElementById('catfuncsDiv');
  var olddiv = document.getElementById(('catfunc_'+fieldId));
  d.removeChild(olddiv);
}
// ----------------------------------------

// --- Produktfunktionszuordnung l�schen ---
function delCatfuncRel(catid, catfuncid){
	antwort = confirm("M"+String.fromCharCode(246)+"chten Sie die Verkn"+String.fromCharCode(252)+"pfung wirklich l"+String.fromCharCode(246)+"schen?");
	if(antwort==true){
		var serial = "";
		
		serial = "catid="+catid+"&catfuncid="+catfuncid+"&";
		send2server("action=delCatfuncRel&"+serial); 
		window.history.go(0);
	}
}
// ----------------------------------------

// --- Alle Funktionszuordnungen l�schen ---
function delAllCatfuncRels(catid){
	x = confirm("M"+String.fromCharCode(246)+"chten Sie wirklich ALLE Verkn"+String.fromCharCode(252)+"pfungen l"+String.fromCharCode(246)+"schen?");
	if(x){
		antwort = confirm("WIRKLICH???");
		if(antwort==true){
			var serial = "";
			
			serial = "catid="+catid+"&";
			send2server("action=delAllCatfuncRels&"+serial); 
			window.history.go(0);
		}
	}
}
// ----------------------------------------

// --- Produktfunktionszuordnungen speichern ---
function saveCatfuncRels(catid) {
    var serial;
    var feldfenster = document.forms[0];
    var anzahl = feldfenster.elements.length;
    var bond = 0;
    var serial="";
	var extraInfo = encodeURIComponent($('extraInfo').value);
	
	serial= "catid="+catid+"&";
	serial += "extraInfo="+extraInfo+"&";
	
    for(i=0; i<anzahl; i++) {
    	if(feldfenster.elements[i].type=="radio" && feldfenster.elements[i].id!=""){
	        bez = feldfenster.elements[i].id;
	        inh = feldfenster.elements[i].value;
	        id = feldfenster.elements[i].name;
	        bond = document.getElementById('bond_'+id).value;
	        
	        serial += "catfuncid_"+id+"="+bond+"&";   
    	}
    }
    //alert(serial);
    sa=send2server("action=saveCatfuncRels&"+serial);
    //alert(sa);
    //window.location.href="index.php?cat=admin&site=index_admin";
    window.location.href="index.php?cat=admin&subcat=kategorie&site=catfunc_assign&id="+catid;
}
// ----------------------------------------
