var SelectedDateField;
var newPosX;
var newPosY;

var responseHandler = new Array();
responseHandler["createPlacedPopup"] = createPlacedPopup;
responseHandler["createPopup"] = createPopup;
responseHandler["createMenu"] = createMenu;
responseHandler["setStatusLine"] = setStatusLine;
responseHandler["replaceContent"] = replaceContent;
responseHandler["setRecordData"] = setRecordData;
responseHandler["insertRow"] = insertRow;

function changeContent(target1,target2) {
     document.getElementById(target1).innerHTML = document.getElementById(target2).innerHTML;


}

// Position eines Records (Zeile) in einer Tabelle
function getPosition(aElem) {
   theTBody = aElem.parentNode.parentNode.parentNode;
   if(theTBody.getElementsByTagName("TR").length > 1)  {
      for(x=0;x<theTBody.getElementsByTagName("TR").length;x++) {
         if(aElem.parentNode.parentNode == theTBody.getElementsByTagName("TR")[x]) aRow = x;
      }
   }
   else aRow = 0;
   return aRow;
}

// Variablen ersetzen in Input, Select, Textarea (in einem Form)
function setFormData(block) {

}

function getFormFields(aForm) {

}

// Variablen ersetzen in Input, Select, Textarea (in einem Record)
// Start ---
function setRecordData(block) {
   var aTableID = block.getAttribute("tableID");
   var aTable = document.getElementById(aTableID);
   var aRowNo = parseInt(block.getAttribute("rowNo"));
   var aRow = aTable.getElementsByTagName("TBODY")[0].getElementsByTagName("TR")[aRowNo];
   var dataFields = getRecordFields(aRow);

   var data = block.getElementsByTagName("data");
   for(d=0;d<data.length;d++) {
      fKey = data[d].getAttribute("field");
      dataFields[fKey].value = data[d].firstChild.nodeValue;
   }
}

function getRecordFields(aRow) {
   var dataFields = new Array();
   theCells = aRow.getElementsByTagName("TD");
   for(c=0;c<theCells.length;c++) {
      elems = theCells[c].childNodes;
      for(e=0;e<elems.length;e++) {
         if(elems[e].tagName == "INPUT" || elems[e].tagName == "SELECT" || elems[e].tagName == "TEXTAREA")
         dataFields[elems[e].name] = elems[e];
      }
   }
   return dataFields;
}
// --- Ende

// f&uuml;gt eine neue Zeile in einer Tabelle an einer bestimmten Zelle ein
// Start ---
function insertRow(block) {
   var aTableID = block.getAttribute("tableID");
   var aTable = document.getElementById(aTableID);
   var aRowNo = parseInt(block.getAttribute("rowNo"));
   var aRow = aTable.getElementsByTagName("TBODY")[0].getElementsByTagName("TR")[aRowNo];

   newRow = document.createElement("TR");
   theCells = block.getElementsByTagName("cell");
   for(c=0;c<theCells.length;c++) {
      aCell = document.createElement("TD");
      aTextNode = theCells[c].firstChild.nodeValue;
      aCell.innerHTML = aTextNode;
      newRow.appendChild(aCell);
   }
   if(aRow != null)  {
      aTable.getElementsByTagName("TBODY")[0].insertBefore(newRow,aRow);
   }
   else aTable.getElementsByTagName("TBODY")[0].appendChild(newRow);
}
// --- Ende

// entfernt eine Zeile aus einer Tabelle
function removePosition(aElem) {
   theCell = getCell(aElem);
   theRow  = getRow(theCell);
   theRow.parentNode.removeChild(theRow);
}

function getCell(aElem) {
   if(aElem.parentNode.tagName != "TD") {
      getCell(aElem.parentNode);
   }
   else {
      SelectedElem = aElem.parentNode;
   }
   return SelectedElem;
}

function getRow(aElem) {
   if(aElem.parentNode.tagName != "TR") {
      getRow(aElem.parentNode);
   }
   else {
      SelectedElem = aElem.parentNode;
   }
   return SelectedElem;
}

function getTopMenu(menuItem,menuID,menuPath) {
   // return;
   var SessionID = getSessionID();
   var pos = new getItemPosition(menuItem);
   var url =  "x.action.php?sid=" + SessionID;
   url += "&action=common.menu.top";
   url += "&parentID=" + menuID;
   url += "&posX=" + parseInt(pos.x - 10);
   url += "&posY=" + (parseInt(pos.y) + 20);
   // menusToShow = menuPath.split(",");
   // alert(url);
   XGetRequest(url);
   // return true;
}


function doKalender(TheField,aView,aDate) {
   if(aView == null) aView = "day";
   if(aDate == null) aDate = "date=" + TheField.value;
   var SessionID = getSessionID();
   var posX = 250;
   var posY = 250;
   if(TheField != '') SelectedDateField = TheField;
   var url = "x.action.php?sid=" + SessionID + "&action=common.kalender.show&view=" + aView +"&" + aDate;
   url += "&posX=" + posX;
   url += "&posY=" + posY;
   xGet(url);
}

function SetDatum(Date) {
   SelectedDateField.value = Date;
   removePopup('KalenderContainer');
}


function replaceContent(block) {
   var Key = block.getAttribute("target");
   var NewInhalt = block.getElementsByTagName("content")[0].firstChild.nodeValue;

   if(document.getElementById(Key)) {
      document.getElementById(Key).innerHTML = NewInhalt;
   }
}

function createPopup(block) {
   var Container = document.createElement("DIV");
   removePopup(block.getAttribute("target"));
   Container.id = block.getAttribute("target");   // alert(); return;
   Container.style.position = "absolute";
   goodGuy = (document.all)?"yes":"no";
   if(goodGuy == "yes") Inhalt = block.getElementsByTagName("content")[0].firstChild.nodeValue;
   if(goodGuy == "no") Inhalt = block.getElementsByTagName("content")[0].childNodes[1].nodeValue;
   Container.innerHTML = Inhalt;      // menuID + ": </br>" +
   document.body.appendChild(Container);
}

function createPlacedPopup(block) {
   var Container = document.createElement("DIV");
   removePopup(block.getAttribute("target"));
   Container.id = block.getAttribute("target");   // alert(); return;
   Container.style.position = "absolute";
   Container.style.left = block.getAttribute("posX") + "px";
   Container.style.top = block.getAttribute("posY") + "px";
   Container.style.zindex = "200";
   Inhalt = block.getElementsByTagName("content")[0].firstChild.nodeValue;
   Container.innerHTML = Inhalt;      // menuID + ": </br>" +
   document.body.appendChild(Container);
}


function createMenu(block) {
   // alert("xx");
   var goodbrowser;
   if(document.all) goodbrowser = true;
   else goodbrowser = false;
   var Container = document.createElement("DIV");
   removePopup(block.getAttribute("target"));
   Container.id = block.getAttribute("target");   // alert(); return;
   Container.style.position = "absolute";
   var srcItemID = block.getAttribute("srcItem");
   var srcItem = document.getElementById(srcItemID);
   var newPosition = getItemPosition(srcItem);
   Container.style.left = parseInt(newPosition.x) + "px";
   Container.style.top = parseInt(parseInt(newPosition.y) + 20) + "px";
   Container.style.width = "140px";
   if(goodbrowser) Inhalt = block.getElementsByTagName("content")[0].childNodes[0].nodeValue;    // alert(Inhalt);
   else Inhalt = block.getElementsByTagName("content")[0].childNodes[1].nodeValue;    // alert(Inhalt);
   Container.innerHTML = Inhalt;      // menuID + ": </br>" +
   document.body.appendChild(Container);
}


String.prototype.unescapeHTML = function() {
  dummie = document.createElement('span');
  dummie.innerHTML = this;
  return dummie.firstChild.data;
}

function getItemPosition(aItem) {
   this.x = 0;
   this.y = 0;

   var tNode = aItem;
   menuWidth = aItem.parentNode.offsetWidth;
   menuHeight = aItem.parentNode.offsetHeight;

   while (tNode.nodeName != "BODY") {
      this.x += tNode.offsetLeft;
      this.y += tNode.offsetTop;
      tNode = tNode.offsetParent;
   }
   return this;
}

function OpenSelection(TheFeld) {
   var SessionID = getSessionID();
   var url = "x.action.php?sid=" + SessionID

   if(TheFeld.name == "AdrRange") {     // alert(TheFeld.name);
       url += "&action=common.address.range&adrRange=" + TheFeld.value;
       xGet(url);
   }

   if(TheFeld.name == "P_AdrRange") {     // alert(TheFeld.name);
       url += "&action=common.address.range&adrRange=" + TheFeld.value;
       url += "&person=1";
       xGet(url);
   }

   if(TheFeld.name == "AdrGroup") {     // alert(TheFeld.name);
      var adrRange = document.forms["Entity"].elements["AdrRange"].value;
      url += "&action=common.address.group&adrGroup=" + TheFeld.value;
      url += "&adrRange=" + adrRange;
      xGet(url);
   }
   if(TheFeld.name == "AdrType") {    // alert(TheFeld);
      url += "&action=common.address.type&adrType=" + TheFeld.value;
      xGet(url);
   }

   if(TheFeld.name == "P_AdrGroup") {     // alert(TheFeld.name);
      var adrRange = document.forms["Entity"].elements["P_AdrRange"].value;
      url += "&action=common.address.group&adrGroup=" + TheFeld.value;
      url += "&adrRange=" + adrRange;
      url += "&person=1";
      xGet(url);
   }
   if(TheFeld.name == "P_AdrType") {    // alert(TheFeld);
      url += "&action=common.address.type&adrType=" + TheFeld.value;
      url += "&person=1";
      xGet(url);
   }


}

function switchItem(Key,Field,aForm,aTarget) {
   if(Field.checked == true) addItem(aForm,aTarget,Key);
   if(Field.checked == false) removeItem(aForm,aTarget,Key);
}

function removeItem(aForm,aFieldName,aID) {
   var aField = document.forms[aForm].elements[aFieldName];
   var aFieldItems = aField.value.split(",");

   var tmp = new Array();
   for(i=0;i<aFieldItems.length;i++) {
      if(aFieldItems[i] != aID) tmp.push(aFieldItems[i]);
   }
   aField.value = tmp.join(",");
}

function addItem(aForm,aFieldName,aID) {    // alert(aForm);
   var aField = document.forms[aForm].elements[aFieldName];
   var aFieldItems = aField.value.split(",");

   var tmp = new Array();
   for(i=0;i<aFieldItems.length;i++) {
      if(aFieldItems[i] == aID) return;
      else {
         if(aFieldItems[i] != '') tmp.push(aFieldItems[i]);
      }
   }
   tmp.push(aID);
   aField.value = tmp.join(",");
}

function getSessionID(){
   var s = window.location.search.substring(1).split('&');
   if(!s.length) return;
   for(var i  = 0; i < s.length; i++) {
      var parts = s[i].split('=');
      if(parts[0] == "sid") return parts[1];
   }
}

function sessionSave(Key,Wert){     // alert(url); return;
   Session = getSessionID();
   url = "x.action.php?sid=" + Session +"&action=common.session.save&key=" + Key +"&wert=" + Wert;
   xGet(url);
}

function ToggleSection(xSection,aButton) {
   if(document.getElementById(xSection).style.display == "block") {
      document.getElementById(xSection).style.display = "none";
      aButton.src = "css/img/area.show.jpg";
      sessionSave(xSection,"hide");
   }
   else {
      document.getElementById(xSection).style.display = "block";
      aButton.src = "css/img/area.hide.jpg";
      sessionSave(xSection,"show");
   }

}

function ToggleActivitySection(Section,Button) {
   if(document.getElementById(Section).style.display == "block") {
      document.getElementById(Section).style.display = "none";
      AT = document.getElementById("ActivityTable");
      RowsToHide = AT.getElementsByTagName("TR");
      for(x=2;x<RowsToHide.length;x=x+2) {
         RowsToHide[x].style.display = "none";
      }
      Button.src = "css/img/area.show.jpg";
      sessionSave(Section,"hide");
   }
   else {
      document.getElementById(Section).style.display = "block";
      Button.src = "css/img/area.hide.jpg";
      AT = document.getElementById("ActivityTable");
      RowsToShow = AT.getElementsByTagName("TR");
      for(x=2;x<RowsToShow.length;x=x+2) {
         RowsToShow[x].style.display = "block";
      }
      sessionSave(Section,"show");
   }
}

function performHelp(aField){
   var PosX = ((window.event.screenX - 40) > 0)?(window.event.screenX - 40):40;
   var PosY = ((window.event.screenY - 40) > 0)?(window.event.screenY - 40):40;

   var SessionID = getSessionID();
   var url = "x.action.php?&sid=" + SessionID + "&action=common.help.show";
   url += "&fld=" + aField;
   url += "&posX=" + PosX;
   url += "&posY=" + PosY;
   xGet(url);
}



function doAutoSuggest(TheValue,TheUser) {
   newPosX = ((window.event.screenX - 40) > 0)?(window.event.screenX - 40):40;
   newPosY = ((window.event.screenY - 40) > 0)?(window.event.screenY - 40):40;
   url = "autosuggest.php?match=" + TheValue + "&user=" + TheUser;
   performAutoSuggest(url);
}

function ChangeSelectBox(aField,aTarget) {      // alert(TheFeld);
   var SessionID = getSessionID();
   var url = "x.action.php?sid=" + SessionID;
   switch(aTarget) {
      case "ProvinceCode":
         url += "&action=common.provinceCode.select";
         url += "&countryCode=" + aField.value;
      break;
   }
   xGet(url);
}

function closePopup(ThePopup) {
   document.getElementById(ThePopup).style.display = "none";
}

function openPopup(ThePopup) {
   document.getElementById(ThePopup).style.display = "block";
}

function removePopup(ThePopup) {
   if(document.getElementById(ThePopup)) document.body.removeChild(document.getElementById(ThePopup));
}

function setStatusLine(block) {
   // alert(block.firstChild.nodeValue);
   displayStatusLine(block.firstChild.nodeValue);
}

function displayStatusLine(Text) {
   var targetID = "StatusLine";
   var target = document.getElementById(targetID);
   target.innerHTML = Text;
}

function MoveLeft(Zelle) {
   Zeile = Zelle.parentNode;
   Zeile.insertBefore(Zelle,Zelle.previousSibling);
}

function MoveRight(Zelle) {
   Zeile = Zelle.parentNode;
   ToMove = Zelle.nextSibling;
   Zeile.insertBefore(ToMove,Zelle);
}

function MoveUp(Zeile) {
   Table = Zeile.parentNode;
   if(Zeile.previousSibling != Table.firstChild) Table.insertBefore(Zeile,Zeile.previousSibling);
}

function MoveDown(Zeile) {
   Table = Zeile.parentNode;
   ToMove = Zeile.nextSibling;
   if(ToMove != null) Table.insertBefore(ToMove,Zeile);
}
