
function displayProSearch(id){
  if (!id){
    id = 'profisuche';
  }
  if (!document.getElementById(id)){
    return;
  }
  if (document.getElementById(id).style.display == 'block'){
    document.getElementById(id).style.display = 'none';
  } else {
    document.getElementById(id).style.display = 'block';
    document.location = '#' + id;
  }
}

function displayProSearchHelp(id){
  if (!id){
    id = 'profisuchehilfe';
  }
  if (!document.getElementById(id)){
    return;
  }
  if (document.getElementById(id).style.display == 'block'){
    document.getElementById(id).style.display = 'none';
  } else {
    document.getElementById(id).style.display = 'block';
  }
}


// Calendar

function openCalendarPopup(buttonID, cgiID, datetype){
  if (!ElementPopup.getInstance() || (ElementPopup.getInstance() && ElementPopup.getInstance().getTargetElementID() != buttonID)){
    if (datetype == 'datetime'){
      var callback = new Function("date", "setCalendarDateTime('" + cgiID + "', date)");
    } else {
      var callback = new Function("date", "setCalendarDate('" + cgiID + "', date)");
    }
    dialogContext = {callback : callback, multiple : false};
    var currentDate = false;
    if (!currentDate){
      currentDate = new Date();
    } else {
      currentDate = Util.getDateFromDateTime(currentDate);
    }
    var calendar = Calendar.createInstance();
    calendar.setDate(currentDate);
    calendar.setBundesland(10);

    var popupProperties = ElementProps.createElementProps(buttonID, '');
    var popup = new ElementPopup.createInstance();
    popup.setID("PopupContainer");
    popup.setTargetElementID(buttonID);
    popup.setX((popupProperties.positionTop()) + 'px');
    popup.setY((popupProperties.positionLeft()+23) + 'px');
    popup.setHTML(calendar.getHTML());
    popup.open();
  } else {
    ElementPopup.getInstance().close();
  }
}

function setCalendarDate(cgiID, date){
  if (document.getElementById(cgiID)){
    //document.getElementById(cgiID).value = formatDateToSystemDateFrontend(date);
    document.getElementById(cgiID).value = date;
  }
  if (document.getElementById(cgiID + '_')){
    document.getElementById(cgiID + '_').value = date;
  }
  ElementPopup.getInstance().close();
}

function setCalendarDateTime(cgiID, date){
  if (document.getElementById(cgiID)){
    document.getElementById(cgiID).value = formatDateToSystemDateFrontend(date) + ' 00:00:00.0';
  }
  if (document.getElementById(cgiID + '_')){
    document.getElementById(cgiID + '_').value = date;
  }
  ElementPopup.getInstance().close();
}

function submitEyecatcherCalenderDay(cgiID, date){
  if (document.getElementById('Form_' + cgiID)){
    if (document.getElementById('Document.Idx.Datum_' + cgiID)){
      document.getElementById('Document.Idx.Datum_' + cgiID).value = date;
    }
    document.getElementById('Form_' + cgiID).submit();
  }
}

function submitEyecatcherCalenderMonth(cgiID, dateFrom, dateTo){
  if (document.getElementById('Form_' + cgiID)){
    if (document.getElementById('Document.Idx.Datum-Von_' + cgiID)){
      document.getElementById('Document.Idx.Datum-Von_' + cgiID).value = dateFrom;
    }
    if (document.getElementById('Document.Idx.Datum-Bis_' + cgiID)){
      document.getElementById('Document.Idx.Datum-Bis_' + cgiID).value = dateTo;
    }
    document.getElementById('Form_' + cgiID).submit();
  }
}

function setDateInCMSField(cgiID, date){
  if (document.getElementById(cgiID)){
    if (date){
      document.getElementById(cgiID).value = formatDateToSystemDateFrontend(date);
    } else {
      document.getElementById(cgiID).value = '';
    }
  }
}

function formatDateToSystemDateFrontend(date){
  var str = '';
  if (date){
    var tempdate = date.split(/\./);
    for (var i=parseFloat(tempdate.length -1); i>=0; i--){
      if (tempdate[i].toString().length == 1){
        str += '0' + tempdate[i];
      } else {
        str += tempdate[i];
      }
      if (i >= 1){
        str += '-';
      }
    }
  }
  return str;
}

function submitSearchForm(elem){
  for (var i=0; i<elem.elements.length; i++){
    if (! elem.elements[i].value){
      var id = elem.elements[i].name.replace(/_$/,'');
      if (document.getElementById(id)){
        document.getElementById(id).value = '';
      }
    }
    if (Util.formatDate(elem.elements[i].value)){
      elem.elements[i].value = Util.formatDate(elem.elements[i].value);
    }
  }
  if (document.getElementById('Filter.OrderCriteria')){
    var list = document.getElementById('Filter.OrderCriteria');
    if (list.options[list.selectedIndex].value){
      list.name = 'Filter.OrderCriteria' + list.options[list.selectedIndex].value.split(/=/)[0];
      list.options[list.selectedIndex].value = list.options[list.selectedIndex].value.split(/=/)[1];
    }
    //alert(document.getElementById('Filter.OrderCriteria').name);
  }
  if (document.getElementById('Plugin.OrderCriteria')){
    var list = document.getElementById('Plugin.OrderCriteria');
    if (list.options[list.selectedIndex].value){
      list.name = 'Plugin.OrderCriteria' + list.options[list.selectedIndex].value.split(/=/)[0];
      list.options[list.selectedIndex].value = list.options[list.selectedIndex].value.split(/=/)[1];
    }
    //alert(document.getElementById('Plugin.OrderCriteria').name);
  }
  return true;
}

function display_anhang(id){
  if (document.getElementById(id)){
    if (document.getElementById(id).style.display == 'block'){
      document.getElementById(id).style.display = 'none';
    } else {
      document.getElementById(id).style.display = 'block';
    }
  }
}

function submitQuickSearch(elem) {
  if (!elem){
    return false;
  }
  if (! elem['PageView.PK'].options[elem['PageView.PK'].selectedIndex].value){
    alert('Bitte wählen Sie zuerst ein Rubrik aus!');
    return false;
  }
  if (elem['PageView.PK'].options[elem['PageView.PK'].selectedIndex].value == '18') {
    if (! elem['Document.PublicationClass']) {
      var input = document.createElement('input');
      input.type = 'hidden';
      input.name = 'Document.PublicationClass';
      input.value = 'zpt';
      elem.appendChild(input);
    }
  }
  return true;
}

//*********************************************************************************

var undefined;

//************************************************************
//************************************************************
//************************************************************
//************************************************************

function Accordeon(id) {
  this.id = undefined;
  
  this._lastIdx = undefined;
  this._setID(id);
}

//************************************************************

Accordeon.prototype._setID = function (str) {
  if (arguments.length != 1) {
    focus();
    throw new Error('Accordeon->_setID: Falsche Anzahl von Argumenten!');
  }
  if (typeof str != 'string') {                                          
    focus();
    throw new Error('Accordeon->_setID: Argument str ist nicht vom Typ String!');
  }
  this.id = str;
} 

//************************************************************

Accordeon.prototype.display = function (idx) {
  this.close();
  var cont = document.getElementById(this.id + '_CONTENT_' + idx);
  var but = document.getElementById(this.id + '_BUTTON_' + idx);
  if (cont) {
    cont.style.display = 'block';
  }
  if (but) {
    var cl = but.className;
    cl = cl.replace(/\bactive_akkordeon\b/,'');
    cl += ' active_akkordeon';
    but.className = cl;
  }
  this._lastIdx = idx;
} 

//************************************************************

Accordeon.prototype.close = function () {
  var cont = document.getElementById(this.id + '_CONTENT_' + this._lastIdx);
  var but = document.getElementById(this.id + '_BUTTON_' + this._lastIdx);
  if (cont) {
    cont.style.display = 'none';
  }
  if (but) {
    var cl = but.className;
    cl = cl.replace(/\bactive_akkordeon\b/,'');
    but.className = cl;
  }
} 

//************************************************************

Accordeon.prototype._resetActiveItems = function () {
  
} 

//************************************************************

Accordeon._increment = [];
Accordeon._autoIdxPrefix = '_Accordeon_';
Accordeon._registerInstance = {};
Accordeon._registerInstanceLength = [];

//************************************************************

Accordeon.getInstance = function(id) {
  if (arguments.length!=1) {
    focus();
    throw new Error('Accordeon.getInstance: Falsche Anzahl von Argumenten!');
  }
  if (! (Accordeon._registerInstance[id])){
    //alert(Accordeon.getInstance.caller);
    focus();
    throw new Error('Accordeon.getInstance: Es ist keine Accordeon.Instance mit id=' + id + ' registriert!');
  }
  return Accordeon._registerInstance[id];
}

//************************************************************

Accordeon.createInstance = function(id) {
  if (!arguments.length) {
    id = Accordeon._autoIdxPrefix + Accordeon._increment.length;
    Accordeon._increment.push(1);
  }
  if (! (Accordeon._registerInstance[id])){
    Accordeon._registerInstance[id] = new Accordeon(id);
    Accordeon._registerInstanceLength.push(id);
  } else {
    focus();
    throw new Error('Accordeon.createInstance: ID schon vorhanden!');
  }
  return Accordeon.getInstance(id);
}
