//initialisierungen

selectedIdLists=new Array();
componentEntryIdLists=new Array();
componentTables=new Array();
tableEntryIdLists=new Array();



//functions


function update_ta_submit() {
	document.getElementById('savebutton').disabled = true;
	for(var i = 0; i < nrRichtextEditors; i ++) {
		if(document.getElementById('ta_editable_'+i).style.display == 'none') {
			document.getElementById('ta_editable_'+i).value = document.getElementById('if_editable_'+i).contentWindow.document.body.innerHTML;
		}
	}
	//document.formular.submit();
	return true;
}


function showhide_html(id, checked) {
	if(checked == true) {
		update_ta(id);
		document.getElementById('if_editable_'+id).style.display = 'none';
		document.getElementById('ta_editable_'+id).style.display = '';
		document.getElementById('toolbar_'+id).style.display = 'none';
	} else {
		update_if(id);
		document.getElementById('if_editable_'+id).style.display = '';
		document.getElementById('ta_editable_'+id).style.display = 'none';
		document.getElementById('toolbar_'+id).style.display = '';
	}
}

function update_ta(id) {
	document.getElementById('ta_editable_'+id).value = document.getElementById('if_editable_'+id).contentWindow.document.body.innerHTML;
	//window.setTimeout("document.getElementById('ta_editable_'+id).value = document.getElementById('if_editable_'+id).contentWindow.document.body.innerHTML", 150);
	/*
	for(var i = 0; i < nrRichtextEditors; i ++) {
		window.setTimeout("document.getElementById('ta_editable_'+i).value = document.getElementById('if_editable_'+i).contentWindow.document.body.innerHTML", 150);
	}
	*/
}
function update_if(id) {
	document.getElementById('if_editable_'+id).contentWindow.document.body.innerHTML = document.getElementById('ta_editable_'+id).value;
}



function mceOnChangeHandler(inst) {
	var element,i,id="",fieldname="";
	
	var str=inst.formTargetElementId;
	var pos=str.indexOf("_");
	var tablename=str.substr(0,pos);
	var rest=str.substr(pos+1);
	for (i=0;i<rest.length;i++){
		if(rest[i]>="0" && rest[i]<="9"){
			id+=rest[i];
		} else {
			fieldname+=rest[i];
		}
	}
	selectId(tablename,id,"");
}

function initialize(){
	self.focus();

	/*
	for (var i=0;i<nrRichtextEditors;i++){		
		window.setTimeout("initializeRichtextEditor("+i+")", 100);
	}
	*/
}

function initRichtextEditorFirefox(id) {
	// Iframe einblenden, Textarea ausblenden
	document.getElementById('if_editable_'+id).style.display = '';
	document.getElementById('ta_editable_'+id).style.display = 'none';
	
	// Designmode Aktiverung auf onfocus setzen 
	document.getElementById('if_editable_'+id).contentWindow.onfocus = function() {
		this.document.designMode = 'On';
		this.document.execCommand("styleWithCSS", false, false);
		selectId(this.document.tablename,this.document.entryId,"");
	 } 

	var tablename_entryId=document.getElementById('if_editable_'+id).name;
	var parts=tablename_entryId.split('_');
	
	document.getElementById('if_editable_'+id).contentWindow.document.tablename=parts[0];
	document.getElementById('if_editable_'+id).contentWindow.document.entryId=parts[1];

	// Toolbars einblenden
	document.getElementById('toolbar_'+id).style.display = '';
	
	// HTML-toggle einblenden
	document.getElementById('showhidehtml_'+id).style.display = '';
	
	// Text aus der Textarea kopieren
	window.setTimeout("update_if("+id+")", 250);
}
function initRichtextEditorMSIE(id) {
	// Iframe einblenden, Textarea ausblenden
	document.getElementById('if_editable_'+id).style.display = '';
	document.getElementById('ta_editable_'+id).style.display = 'none';
	
	document.getElementById('if_editable_'+id).contentWindow.document.designMode = 'On';

	// Toolbars einblenden
	document.getElementById('toolbar_'+id).style.display = '';
	
	// HTML-toggle einblenden
	document.getElementById('showhidehtml_'+id).style.display = '';
	
	// Text aus der Textarea kopieren
	window.setTimeout("update_if("+id+")", 250);
}

	
function treemenuon(id,parentid,position,event){
	var menu;
	menu=document.getElementById('treemenu');
	menu.style.visibility='visible';
	menu.style.left=event.screenX+document.body.scrollLeft-10;; 
	menu.style.top=event.screenY+document.body.scrollTop-150; 
	treemenu_clickedid=id;
	treemenu_parentid=parentid;
	treemenu_position=position;
}

function treemenuoff(id,event){
	var menu;
	menu=document.getElementById('treemenu');
	menu.style.visibility='hidden';
}



function richtextchanged(eventobj){

	this.body.innerHTML="<p>xxx</p>";
	
	//element2=document.getElementById('firefoxedit1');
	//element2.contentWindow.document.body.innerHTML="<p>"+this+"</p>";

	//document.form1[formFieldName].value=innerHtml;
	//selectId(table,entryId,document.form1[formFieldName]);

	//alert('xxx');
}



function doAction(elementname,action){
  	if (action == "createlink") {
    		var szURL = prompt("Enter a URL:", "http://");
    		if ((szURL != null) && (szURL != "")) {
      		document.getElementById(elementname).contentWindow.document.execCommand("CreateLink",false,szURL);
    		}
	} else {
		document.getElementById('if_editable_'+elementname).contentWindow.document.execCommand(action, false, null);
		document.getElementById('if_editable_'+elementname).contentWindow.focus();
	}
}

function hideElements(elementId,className){
  elements = getElementsByClassName(elementId,className);
  for(i=0;i<elements.length;i++) elements[i].style.display = 'none';
}
function showElements(elementId,className){
  elements = getElementsByClassName(elementId,className);
  for(i=0;i<elements.length;i++) elements[i].style.display = '';
}
function getElementsByClassName(elementId,className){
  var ret_obj,all_obj,j;
  ret_obj=new Array();
  j=0;
  all_obj=document.getElementById(elementId).getElementsByTagName("*");
    for(i=0;i<all_obj.length;i++){
      if(all_obj[i].className==className){
          ret_obj[j]=all_obj[i];
          j++;
      }
    }
    return ret_obj;
}


function selectId(table,entryId,myElement){
  var formField;
  formField=document.form1.elements[table+"__selected"+entryId];
  if (formField) formField.checked=true;
  //alert(table+"__selected"+entryId);
}

function unselectId(table,entryId,myElement){
  document.form1.elements[table+"__selected"+entryId].checked=false;
}

function selectAllIds(componentHandleList,myElement){
  var tableEntryIds,entryIds,table,entryNr;
  tableEntryIds=getTableEntryIds(componentHandleList);
  for (table in tableEntryIds){
    entryIds=tableEntryIds[table];
    for (entryNr in entryIds){
      document.form1.elements[table+"__selected"+entryIds[entryNr]].checked=true;
    }
  }
}
function setRowEditable(componentHandleList,rowName){
  var tableEntryIds,entryIds,table,entryNr;
  tableEntryIds=getTableEntryIds(componentHandleList);
  for (table in tableEntryIds){
    entryIds=tableEntryIds[table];
    for (entryNr in entryIds){
      document.all["edit__"+table+"_"+rowName+entryIds[entryNr]].style.display='';
      document.all["view__"+table+"_"+rowName+entryIds[entryNr]].style.display='none';
    }
  }
}


function unselectAllIds(componentHandleList,myElement){
  var tableEntryIds,entryIds,table,entryNr;
  tableEntryIds=getTableEntryIds(componentHandleList);
  for (table in tableEntryIds){
    entryIds=tableEntryIds[table];
    for (entryNr in entryIds){
      document.form1.elements[table+"__selected"+entryIds[entryNr]].checked=false;
    }
  }
}

function invertIdSelection(componentHandleList,myElement){
  var tableEntryIds,entryIds,table,entryNr;
  tableEntryIds=getTableEntryIds(componentHandleList);
  for (table in tableEntryIds){
    entryIds=tableEntryIds[table];
    for (entryNr in entryIds){
      document.form1.elements[table+"__selected"+entryIds[entryNr]].checked=!document.form1.elements[table+"__selected"+entryIds[entryNr]].checked;
    }

  }
}

function getTableEntryIds(componentHandleList){
  var componentHandleNr,tableList,componentHandle,tableEntryIdLists,tableEntryIds;
  //alert(componentHandleList);
  componentHandles=componentHandleList.split(",");
  tableList="";
  tableEntryIdLists=new Array();
  tableEntryIds=new Array();
  for (componentHandleNr in componentHandles){
    componentHandle=componentHandles[componentHandleNr];
    table=componentTables[componentHandle];

    if (tableList.indexOf(table)==-1){
      tableList+=table+",";
      tableEntryIdLists[table]="";
      tableEntryIds[table]=new Array();
    }
//    alert ("component:"+componentHandle+"table:"+table+"componentEntryIdList:"+componentEntryIdLists[componentHandle]);
    tableEntryIdLists[table]+=componentEntryIdLists[componentHandle];
    tableEntryIdLists[table]+=",";

    tableEntryIds[table]=tableEntryIds[table].concat(componentEntryIdLists[componentHandle].split(","));
  }
/*
  tableEntryIds=new Array();
  for (table in tableEntryIdLists){
    tableEntryIdList=tableEntryIdLists[table];
    tableEntryIdList=tableEntryIdList.substring(0,tableEntryIdList.length-1);
    alert("tableEntryIds von "+table+":"+tableEntryIdList);
    //tableEntryIds[table]=tableEntryIdList.split(",")
    if (!tableEntryIds[table]) tableEntryIds[table]=new Array();
    tableEntryIds[table]=tableEntryIds[table].concat(tableEntryIdList);
  }
*/
  return tableEntryIds;
}


function storeRichtextarea(table,entryId,formFieldName,innerHtml){

  document.form1[formFieldName].value=innerHtml;
  selectId(table,entryId,document.form1[formFieldName]);
}


function setDateTime(table,formFieldName,myElement,entryId){
  myForm=document.form1;
  dateString=getDateString(myForm,formFieldName);
  timeString=getTimeString(myForm,formFieldName);
  myForm.elements[formFieldName].value=dateString+" "+timeString;
  selectId(table,entryId,myElement);
}
function setDateOnly(table,formFieldName,myElement,entryId){
  myForm=document.form1;
  dateString=getDateString(myForm,formFieldName);
  myForm.elements[formFieldName].value=dateString;
  selectId(table,entryId,myElement);
}
function setTimeOnly(table,formFieldName,myElement,entryId){
  myForm=document.form1;
  timeString=getTimeString(myForm,formFieldName);
  myForm.elements[formFieldName].value=timeString;
  selectId(table,entryId,myElement);
}


function getDateString(myForm,formFieldName){
  dayElement=myForm.elements[formFieldName+"_day"];
  monthElement=myForm.elements[formFieldName+"_month"];
  yearElement=myForm.elements[formFieldName+"_year"];
  dateString=dayElement.options[dayElement.selectedIndex].value+'.'
    +monthElement.options[monthElement.selectedIndex].value+'.'
    +yearElement.options[yearElement.selectedIndex].value;
  return dateString;
}

function getTimeString(myForm,formFieldName){
  hourElement=myForm.elements[formFieldName+"_hour"];
  minuteElement=myForm.elements[formFieldName+"_minute"];
  timeString=hourElement.options[hourElement.selectedIndex].value+":"
    +minuteElement.options[minuteElement.selectedIndex].value;
  return timeString;
}

function insertDeveloperDefinedTag(openTag, closeTag)
{
   //----- Creates Developer Defined Tag -----
   var cursor = document.selection.createRange();
   document.execCommand("cut",false,null);

   var content = clipboardData.getData("Text");

   var newTag = openTag+content+closeTag;
   cursor.pasteHTML(newTag);
}

function insertHyperlink(nr) {
	if(navigator.userAgent.toLowerCase().indexOf('msie')>0){
	  	var cursor=document.getElementById('if_editable_'+nr).contentWindow.document.selection.createRange();
	  	var link="";
	
	  	link=window.showModalDialog("linkWindow.php", cursor,
	    "dialogHeight:225px;dialogWidth:395px;status:no;help:no;");
	
	  	if (link!="" && link!=null) {
	    	link=link.replace("///", "//");
	     	cursor.pasteHTML(link);
	  	}
	  }
	  else {
	  	document.getElementById('if_editable_'+nr).contentWindow.open('linkWindow.php', 'Hyperlink erstellen', 'dependent=1, modal=1, width=395, height=225');
	  }
}

/* Öffnet das Kalender-Fenster. Paremeter sind Formularname und
   Eingabefeldname.
*/
function openCalendar(form, input, table, entryId) {
  var datum=eval("document."+form+"."+input+".value");
  var url="calendar.php?form="+form+"&input="+input+"&datum="+datum+"&changedField="+table+"__selected"+entryId;
  var params="height=310,width=330";
  var win=window.open(url, "calendarWindow", params);
  win.focus();
}
function openClock(form, input, table, entryId) {
  var datum=eval("document."+form+"."+input+".value");
  var url="time.php?form="+form+"&input="+input+"&datum="+datum+"&changedField="+table+"__selected"+entryId;
  var params="height=50,width=180";
  var win=window.open(url, "calendarWindow", params);
  win.focus();
}

/* popupFormType */
function openPopupFormTypeWindow(formType, table, tableField, form, inputValue, inputName, entryId) {
	var url="popupFormType.php";
	var params=new Array();
	var options="width=435,height=250,sizeable=yes,resizable=yes,scrollbars=yes,dependent=yes";

	var initValue=eval("document."+form+"."+inputValue+".value");

	formType=formType.replace("popup", "");

	params.push("openerForm="+form);
	params.push("openerValueInput="+inputValue);
	params.push("openerNameInput="+inputName);
	params.push("table="+table);
	params.push("tableField="+tableField);
	params.push("initValue="+initValue);
	params.push("formType="+formType);
	params.push("changedField="+table+"__selected"+entryId);

	var win=window.open(url+"?"+params.join("&"), "popupFormType", options);
	win.focus();

}


