/*------------------------------------------------------------------------------

  cms5.net Javascript Headfile
    by Martin Walz

------------------------------------------------------------------------------*/

$(document).ready( function() {

  $.datepicker.setDefaults($.datepicker.regional['de']);
  $.datepicker.setDefaults({ autoSize: true, showWeek: true, altFormat: 'yy-mm-dd', showOn: 'focus', showAnim: 'fold'});
  
  $(".nav_head").click(function () { $(this).next(".nav_container").toggle("blind", {}, 500); });
  $("#info_span").click(function () { $(this).toggle("blind", {}, 500); });
  $("#info_link").click(function () { $("#info_span").toggle("blind", {}, 500); });
  $("#print_r_vars_link").click(function () { $("#print_r_vars").toggle("fold", {}, 500); });
  $("#print_r_cms_link").click(function () { $("#print_r_cms").toggle("fold", {}, 500); });
  $("#print_r_vars").mouseleave(function () { $(this).toggle("fold", {}, 500); });
  $("#print_r_cms").mouseleave(function () { $(this).toggle("fold", {}, 500); });
  if($.browser.msie) {
  } else {
  }

  $('.content_frame tr').mouseenter(function () { $(this).children().addClass("td_highlight") });
  $('.content_frame tr').mouseleave(function () { $(this).children().removeClass("td_highlight") });
  $('.cms_dialog').dialog({ autoOpen: true, modal: true, show: 'fold', hide: 'fold', buttons: { "Ok": function() { $(this).dialog("close"); } } });
  $('.nav_head:parent b').css("background-color", "#ffff00");
  $('th[align="center"]').css("text-align", "center"); // da sonst Positionierung durch css überschrieben

  $('form').validate();
  
  $(".table2csv caption").append(" <a href=\"javascript:void(0)\"><img src=\"./images/icons/w_xls.gif\" align=\"right\" alt=\"\"/></a>");
  $(".table2csv caption a").click(function () {
	              $(this).append("<form action=\"./export_csv.php\" method =\"post\" id=\"tmp_form\"><input type=\"hidden\" name=\"csv_data\" id=\"csv_data\" /></form>");
	              $("#csv_data").val($(this).parent().parent().table2CSV());
	              $("#csv_data").parent().submit();
	              $("#tmp_form").remove(); });

  $("#waitbox").click( function() { $("#waitbox").hide(); $("#waitbox_bg").hide(); });
  $('#waitbox_bg').hide();
  $('#waitbox').hide();
});

$(document).unload( function() {
  $("#waitbox").show();
  $("#waitbox_bg").show();
});


var wait_to_load = 'wird geladen<br><img src=\"images/waitbox.gif\" border=\"0\" alt=\"\">';
/*------------------------------------------------------------------------------

  Alte Funktionen zur Kompatibilität noch dabei

------------------------------------------------------------------------------*/
function OpenSitemap(init) {  //reloads the window if Nav4 resized
  NewWin = window.open('pages/sitemap.php','Sitemap','scrollbars=yes,resizable=yes,width=400,height=500')
  NewWin.focus();
}

function OpenSite(id) {  //reloads the window if Nav4 resized
  w = parent.mainFrame.location = 'change.php?id=' + id;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

var ObjVisible = new Array();

function Show() { //v6.0
  var i,obj,args=Show.arguments;
  for (i=0; i<(args.length); i++) { 
  $("#" + args[i]).show();
    window.clearInterval(ObjVisible[args[i]]);
  }
}

function Hide() { //v6.0
  var i,obj,args=Hide.arguments;
  for (i=0; i<(args.length); i++) { 
    $("#" + args[i]).hide();
    window.clearInterval(ObjVisible[args[i]]);
  }
}

function HideDelayed() {
  var i,obj,args=HideDelayed.arguments;
  for (i=0; i<(args.length); i++) { 
    if ((obj=MM_findObj(args[i]))!=null) {
      ObjVisible[args[i]] = window.setInterval('Hide(\'' + args[i] + '\')', 1000);
    }
  }
}

function ChangeShowHide() { //v6.0
  var i,obj,args=ChangeShowHide.arguments;
  for (i=0; i<(args.length); i++) 
    if ((obj=MM_findObj(args[i]))!=null) {
      if (obj.style) {
      obj=obj.style; 
    }
      if (obj.display == 'none') {
        obj.display='';
    } else {
        obj.display='none';
    }
  }
}

function ShowNav() { //v6.0
  var i,obj,args=ShowNav.arguments;
  for (i=0; i<(args.length); i++) 
    if ((obj=MM_findObj(args[i]))!=null) {
      if (obj.style) {
      obj=obj.style; 
    }
    obj.display='inline';
    obj.visibility ='visible';
  }
}

function HideNav() { //v6.0
  var i,obj,args=HideNav.arguments;
  for (i=0; i<(args.length); i++) 
    if ((obj=MM_findObj(args[i]))!=null) {
      if (obj.style) {
      obj=obj.style; 
    }
      obj.visibility='hidden';
  }
}

function chkFormular2() {
  var i,obj,args=chkFormular2.arguments;
  for (i=0; i<(args.length); i += 2) {
    if ((obj=MM_findObj(args[i]))!=null) {
      if (obj.value == '') {
        alert('Bitte ' + args[i + 1] + ' eingeben.');
        obj.focus();
        return false;
      }
    }
  }
}

function ChangeColor() { //v6.0
  var i,obj,args=ChangeColor.arguments;
  for (i=1; i<(args.length); i++) {
    document.getElementById(args[i]).style.background=args[0];
  }
}

function ChangeStyle() { //v6.0
  var i,obj,args=ChangeStyle.arguments;
  for (i=1; i<(args.length); i++) {
    document.getElementById(args[i]).className=args[0];
  }
}

function ChangeStyleNavHead(id) {
  if(document.getElementById(id).className == 'nav_head_hover') 
    document.getElementById(id).className = 'nav_head'
  else
    document.getElementById(id).className = 'nav_head_hover';
}

function StatusText(text){
  window.status = text;
  return true;
}

function ChangeRowStyle(row, style)
{
  var cells = null;

  if (typeof(document.getElementsByTagName) != 'undefined') {
    cells = row.getElementsByTagName('td');
  }
  else if (typeof(row.cells) != 'undefined') {
    cells = row.cells;
  }
  else {
    return false;
  }

  var rowCellsCnt  = cells.length;
  var c = null;

  for (c = 0; c < rowCellsCnt; c++) {
    cells[c].className = style;
  }

  return true;
}

function check_date(field, fieldname, field_dest)
{
  if(isFinite(field.value)) {
  var value = field.value;
    field.value = value.substr(0,2) + "." + value.substr(2,2) + "." + value.substr(4,4)
    return 0;
  } else {
    date_value = field.value;
    date_value = date_value.split(".");
    errortext = "Bitte geben Sie einen gültiges Datum im Feld " + fieldname + " ein!\n\r";
    
    if (date_value == ''){
        field_dest.value = '';
        return 0;
    }
    
    if (isNaN(date_value[0]) || date_value[0]=="" || date_value[0]==0) {
        alert(errortext);
        return 1;
    }
    if (isNaN(date_value[1]) || date_value[1]=="" || date_value[1]==0) {
        alert(errortext);
        return 1;
    }
    if (isNaN(date_value[2]) || date_value[2]=="" || date_value[2]==0) {
        alert(errortext);
        return 1;
    }

    if (date_value[0] > 31) {
        alert(errortext);
        return 1;
    }

    if (date_value[1] > 12) {
        alert(errortext);
        return 1;
    }
    if (date_value[2].length < 2) {
        alert(errortext);
        return 1;
    }
    if (date_value[2].length == 2) {date_value[2]= "20" + date_value[2];}
    if (date_value[2].length == 3) {
        alert(errortext);
        return 1;
    }
    if (date_value[2].length > 4) {
        alert(errortext);
        return 1;
    }

    field.value=date_value[0]+"."+date_value[1]+"."+date_value[2];
    if(field_dest != '') {
        field_dest.value = (date_value[2]+"-"+date_value[1]+"-"+date_value[0]);
    }
    return 0;
  }
}
function SetFileList(datei, feld, pfad) {;
  $('#'+feld).val(datei);
  $('#filelist_'+feld).dialog( "close" );
//  $('#img'+feld).src = 'pic/preview.php?file=' + pfad + text + '&size=200';
}

function OnlineHelp(text) {
  if(text != '') {
    if($("#online_help_text").html() != text) {
      $("#online_help").show();
      $("#online_help_text").html(text);
    }
  } else {
    if($("#online_help_text").html() != '') {
      $("#online_help").hide();
      $("#online_help_text").html('');
    }
  }
}

/*------------------------------------------------------------------------------

  Externe Datei in eine DIV laden

------------------------------------------------------------------------------*/
function createRequestObject() {
    var obj;
    if (window.XMLHttpRequest) { 
      obj = new XMLHttpRequest();
  
    } else if (window.ActiveXObject) { 
      obj = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return obj;
}
var http = createRequestObject();
var cms_GetExtFile = 'get.php';
var cms_GetExtWaitbox = true;

function GetExtDataOnce(label, data, param) {
  if(document.getElementById(label).innerHTML == '')
    GetExtData(label, data, param)
}


function GetExtData(label, data, param) {
  if(cms_GetExtWaitbox) {
    document.getElementById(label).innerHTML = "wird geladen<br><img src=\"images/waitbox.gif\" border=\"0\" alt=\"\">";
  }

  if(param != '')
    if(param[0] != "&")
      param = '&' + param;
  
  http.open('get', (cms_GetExtFile) + '?div=' + (label) + '&data=' + (data) + (param));
  http.onreadystatechange = GetExtDataResponse;
  http.send(null);
}

function GetExtDataResponse() {
  if(http.readyState == 4){
    var response = http.responseText;
    var update = new Array();
    if(response.indexOf('|' != -1)) {
      update = response.split('|||');
      document.getElementById(update[0]).innerHTML = update[1];
    }
  }
}

/*------------------------------------------------------------------------------

  Links bei Tabellen dynamisch einblenden

Bsp: ActivateItemContainer1('<a href="cms_user/details/%%item%%/">Details<'+'/a>|<a href="cms_user/change/%%item%%/">&auml;ndern<'+'/a>|<a href="cms_user/pw_reset/%%item%%/">Passwort<'+'/a>');

------------------------------------------------------------------------------*/

var link_code_tmp = '';

function ActivateItemContainer1(link_code) {
  link_code_tmp = link_code
  $('body').append('<div id="item_container_marker1"><img src="./images/link_marker2.gif" alt="" /></div>');
  $('body').append('<div id="item_container1"><table><tr><td id="item_container_img1"><img src="./images/link_marker2.gif" alt="" /></td><td id="item_container_inner1"></td></tr></table></div>');
  $('tr.item_container').mouseenter(function () {
    var tmp = $(this).attr('id');
    var item = tmp.replace('item_', '');
    if(item > 0) {
      $('#item_container_inner1').html(link_code_tmp.replace(/\%\%item\%\%/g, item));
      $('#item_container1').innerHeight($(this).innerHeight()+4).show();
      $('#item_container_marker1').innerHeight($(this).innerHeight()+4).show();
      $('#item_container_marker1 img').height($(this).height()+4);
      $('#item_container1 img').height($(this).height()+4);
      $('#item_container_marker1').position({ of: $( this ),  my: 'right center', at: 'left center', offset: '0 -1'});
      $('#item_container1').position({ of: $( this ),  my: 'right center', at: 'left center', offset: '0 -1'}).hide();
    }
  });
  $('tr.item_container').mouseleave(function () { $('#item_container1').hide(); $('#item_container_marker1').hide(); });
  $('#item_container_marker1').mouseenter(function () { $('#item_container1').show('slide', { direction: 'right' }, 250); });
  $('#item_container1').mouseenter(function () { $(this).show(); });
  $('#item_container1').mouseleave(function () { $(this).hide(); });
}

function ActivateItemContainer2(link_code) {
  link_code_tmp = link_code
  $('body').append('<div id="item_container_marker1"><img src="./images/link_marker2.gif" alt="" /></div>');
  $('body').append('<div id="item_container1"><table><tr><td id="item_container_img1"><img src="./images/link_marker2.gif" alt="" /></td><td id="item_container_inner1"></td></tr></table></div>');
  $('tr.item_container').mouseenter(function () {
    var tmp = $(this).attr('id');
    var item = tmp.replace('item_', '');
    if(item > 0) {
      $('#item_container_inner1').html('&nbsp;&nbsp;Links: '+link_code_tmp.replace(/\%\%item\%\%/g, item));
      $('#item_container1').innerHeight($(this).innerHeight()+4).show();
      $('#item_container_marker1').innerHeight($(this).innerHeight()+4).show();
      $('#item_container_marker1 img').height($(this).height()+4);
      $('#item_container1 img').height($(this).height()+4);
      $('#item_container1').position({ of: $( this ),  my: 'right center', at: 'right center', offset: '0 -1'}).hide();
      $('#item_container_marker1').position({ of: $( this ),  my: 'right center', at: 'right center', offset: '-1 -1'});
    }
  });
  $('tr.item_container').mouseleave(function () { $('#item_container1').hide(); $('#item_container_marker1').hide(); });
  $('#item_container_marker1').mouseenter(function () { $('#item_container1').toggle('slide', { direction: 'right' }, 250); $(this).hide();  });
  $('#item_container1').mouseenter(function () { $(this).show(); });
  $('#item_container1').mouseleave(function () { $(this).hide(); });
  $('#item_container_marker1').position({ of: $('body'),  my: 'right center', at: 'right center'});
  $('#item_container1').position({ of: $('body'),  my: 'right center', at: 'right center'});
}

function ActivateItemContainer3(link_code) {
  link_code_tmp = link_code
  $('body').append('<span id="item_container3"></span>');
  $('.content_frame tr').mouseenter(function () {
    var tmp = $(this).attr('id');
    var item = tmp.replace('item_', '');
    if(item > 0) {
      $("#item_container3").show();
      $("#item_container3").html('Links: '+link_code_tmp.replace(/\%\%item\%\%/g, item));
      $("#item_container3").position({ of: $( this ),  my: "left bottom", at: "left top"});
    }
  });
  $('.content_frame tr').mouseleave(function () { $("#item_container3").hide()});
  $('#item_container3').mouseenter(function () { $(this).show()});
  $('#item_container3').mouseleave(function () { $(this).hide()});
  $('#item_container3 tr').mouseenter(function () { });
  $('#item_container3 tr').mouseleave(function () { });
}

/*------------------------------------------------------------------------------

  Modifizierte Version von jQuery table2csv

------------------------------------------------------------------------------*/

jQuery.fn.table2CSV = function() {
    var csvData = [];
    var headerArr = [];
    var el = this;

    var tmpRow = []; // construct header avalible array

    $(el).filter(':visible').find('th').each(function() {
        if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
    });

    row2CSV(tmpRow);

    // actual data
    $(el).find('tr').each(function() {
        var tmpRow = [];
        $(this).filter(':visible').find('td').each(function() {
            if ($(this).css('display') != 'none') tmpRow[tmpRow.length] = formatData($(this).html());
        });
        row2CSV(tmpRow);
    });
    var mydata = csvData.join('\n');
    return mydata;

    function row2CSV(tmpRow) {
        var tmp = tmpRow.join('') // to remove any blank rows
        // alert(tmp);
        if (tmpRow.length > 0 && tmp != '') {
            var mystr = tmpRow.join(";");
            csvData[csvData.length] = mystr;
        }
    }
    function formatData(input) {
        // replace " with â€œ
        var regexp = new RegExp(/["]/g);
        var output = input.replace(regexp, "â€œ");
        //HTML
        var regexp = new RegExp(/\<[^\<]+\>/g);
        var output = output.replace(regexp, "");
        if (output == "") return '';
        return '"' + output + '"';
    }
};
