/* ### WIN OPEN ### */
function showPic (filename, headline) {  
  window.open (filename, headline, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=600, height=600, left=10, top=10')
}

/* ### PRINT ### */
function print_view (query) {
  window.open ('print.php?'+query, 'Druckansicht', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width=568, height=800, left=5, top=5');
}

/* ### OPEN CHAT WINDOW ### */
function openChat (filename) {  
  window.open ('content/chat/index.php', 'Chat', 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=990, height=650, left=5, top=5')
}

/* ### PRELOAD IMAGES ### */
function preLoadImages () {
  document.Preload = new Array();
  if (document.images) {
    for (var i=0; i<preLoadImages.arguments.length; i++) {
      document.Preload[i] = new Image();
      document.Preload[i].src = preLoadImages.arguments[i];
    }
  }
}

/* ### CHECK ACCESS ### */
function checkAccess (zustand, sendForm) {
  if (zustand.checked == true)
    sendForm.disabled = false;
  else
    sendForm.disabled = true;
}

/* ALERT INFO */
function alertInfo (page, title, target) {
 	var antwort = confirm(title);
	if (antwort == true) {
    if (target == '_blank')
      newWin = window.open(page, "Bestätigung", "left=100,top=100,width=100,height=100,scrollbars=no");
    else
 		  location.href = page;
  }
}

/* CHECK CONTENT */
function checkInhalt () {
  var data = document.forms['form'];   
  if (data.elements['title'].value == '') {
    alert('Bitte geben Sie einen Titel ein!');
    data.elements['title'].focus();
    return false;
  }
  for (i=1; i<=20; i++) {
    if (((data.elements['link'+i+'_name'].value == '') || (data.elements['link'+i+'_type'].value == '') || (data.elements['link'+i+'_link'].value == ''))) {
      alert('Bitte geben Sie einen vollständigen Link ein!');
      data.elements['link'+i+'_name'].focus();
      return false;
    }
  }
}

/* REMAINING CHARS */
function RemainingChars (maxLength, field, lengthField) {
  var temp = document.getElementById(field);
  len = temp.value.length;
  if (len > maxLength) {
    temp.value = temp.value.substring(0,maxLength);
    left = 0;
  }
  else
    left = maxLength - len;
  myLenField = document.getElementById(lengthField);
  myLenField.value=left;
}

/* ### DIV-LAYER ### */
function getDIV (id) {
  if      (document.layers)         return document.layers[id];         // NC 4
  else if (document.all)            return document.all[id];            // IE 4
  else if (document.getElementById) return document.getElementById(id); // DOM
  else                              return null;        
}
// --> FOR CSS ACCESS
function getDIVStyle (id) {
  if      (document.layers)         return document.layers[id];              
  else if (document.all)            return document.all[id].style;           
  else if (document.getElementById) return document.getElementById(id).style;
  else                              return null;        
}
var BGC = document.layers? 'bgColor' : 'backgroundColor';

function setBgColor (id, fieldvalue) {
  var BGC = document.layers? 'bgColor' : 'backgroundColor';
  getDIVStyle(id)[BGC] = fieldvalue;
}

/* NUMBER_FORMAT */
function number_format (number, decimals, dec_point, thousands_sep) {
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf("e");
  if (eindex > -1) {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  if (decimals != null) {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0) {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "") {
  	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  return sign + integer + fractional + exponent;
}

/* BGW-CODE */
function bgw (beginnTag, endTag, formindex) {
  var input = document.getElementById(formindex);
  input.focus();
  /* IE */
  if (typeof document.selection != 'undefined') {
    var range = document.selection.createRange();
    var insert = range.text;
    range.text = beginnTag + insert + endTag;
    range = document.selection.createRange();
    if (insert.length == 0)
      range.move('character', -endTag.length);
    else
      range.moveStart('character', beginnTag.length + insert.length + endTag.length);   
    range.select();
  }
  /* NEW BROWSER */
  else if (typeof input.selectionStart != 'undefined') {
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insert = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + beginnTag + insert + endTag + input.value.substr(end);
    var position;
    if (insert.length == 0)
      position = start + beginnTag.length;
    else
      position = start + beginnTag.length + insert.length + endTag.length;
    input.selectionStart = position;
    input.selectionEnd = position;
  }
  /* OTHER BROWSER */
  else {
    var position;
    var re = new RegExp('^[0-9]{0,3}$');
    while (!re.test(position))
      position = prompt("Einfügen (0.." + input.value.length + "):", "0");
    if (position > input.value.length)
      position = input.value.length;
    var insert = prompt("Bitte geben Sie den Code ein:");
    input.value = input.value.substr(0, pos) + beginnTag + insert + endTag + input.value.substr(pos);
  }
}

/* AUSWAHLFELDER */
function bgwSelect (formtype, vari) {
  if (vari == '') {
    document.forms[0].reset();
    document.forms[0].elements[formtype].blur();
    return;
  }
  else {
    if (formtype == 'style')
      document.forms[0].value = bgw('[STYLE='+ vari +']', '[/STYLE]');
    if (formtype == 'font')
      document.forms[0].value = bgw('[FONT='+ vari +']', '[/FONT]');
    if (formtype == 'color')
      document.forms[0].value = bgw('[COLOR='+ vari +']', '[/COLOR]');
    if (formtype == 'header')
      document.forms[0].value = bgw('['+ vari +']', '[/'+ vari +']');
    if (formtype == 'size')
      document.forms[0].value = bgw('[SIZE='+ vari +']', '[/SIZE]');
    if (formtype == 'bgcolor')
      document.forms[0].value = bgw('[BGCOLOR='+ vari +']', '[/BGCOLOR]');
    if (formtype == 'lh')
      document.forms[0].value = bgw('[LH='+ vari +']', '[/LH]');
    //document.forms['form'].reset();
    document.forms[0].elements[formtype].blur();
  }
}

/* FORM-HREF */
function goForm (x) {
 if (x == '') {
   document.forms['kunde'].reset();
   document.forms['kunde'].elements[0].blur();
   return;
 }
 else {
   location.href = x;
   document.forms['kunde'].reset();
   document.forms['kunde'].elements[0].blur();
 }
}
/* SEND FORM */
function submitForm (x) {
  document.getElementById(x).submit();
}

/* TEXTAREA VERGRÖSSERN */
function textarea (boxid, link1) { 
  var t = document.getElementById(boxid); 
  if (link1.innerHTML == 'vergrößern') { 
    t.style.height = t.offsetHeight + 200 + "px"; 
    link1.innerHTML = 'verkleinern'; 
  } 
  else { 
    t.style.height = t.offsetHeight - 200 + "px"; 
    link1.innerHTML = 'vergrößern'; 
  } 
} 

/* POPUP AUFRUFEN */
function getColor (filename) {
  var sInput = '';
  var setting = 'dialogHeight: 300px; dialogWidth: 350px; dialogTop: 180px; dialogLeft: 200px; resizable: no; status: no; scrollbar: auto';
  var rValue = showModalDialog(filename, sInput, setting);
  document.forms['form'].value = bgw('[COLOR='+ rValue +']', '[/COLOR]');
}

/* FARBE SETZEN */
function setColor (color) {
  window.returnValue = color;
  window.close();
}

/* POPUP AUFRUFEN */
function getAmount (filename, title) {
  var setting = 'toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=250, height=200, left=50, top=50';
  window.open(filename, title, setting);
}
/* BETRAG SETZEN */
function setAmount(fieldname, amount, typ, mwst) {
  mwst = parseInt(mwst);
  mwst_dez = 100 + mwst;
  mwst_hex = (mwst / 100) + 1;
  if (typ == 1) {
    if (mwst == 0)
      resval = amount;
    else
      resval = (amount / mwst_dez) * 100;
  }
  else if (typ == 2)
    resval = amount * mwst_hex;
  else
    resval = amount;
  resval = number_format(resval, 2, ".", "");
  window.opener.document.getElementById(fieldname).value = resval; 
  window.close(); 
}
/* USER_ID SETZEN */
function setUID(fieldname, user_id) {
  window.opener.document.getElementById(fieldname).value = user_id; 
  window.close(); 
}
/* ### FORMAT NUMBER ### */
function formatNumber (typ) {
  amount = document.getElementById(typ).value;
  amount = amount.replace(',', '.');
  document.getElementById(typ).value = number_format(amount, 2, ".", "");
}

/* POPUP AUFRUFEN */
function getTable (filename) {
  var sInput = '';
  var setting = 'dialogHeight: 300px; dialogWidth: 350px; dialogTop: 180px; dialogLeft: 200px; resizable: no; status: no; scrollbar: auto';
  var rValue = showModalDialog(filename, sInput, setting);
  
  document.forms['form'].value = bgw('[TABLE]\n', '');
  for (i=0; i<rValue[1]; i++) {
    document.forms['form'].value = bgw('[TR]\n', '');
    for (j=0; j<rValue[0]; j++)
      document.forms['form'].value = bgw('[TD][/TD]\n', '');
    document.forms['form'].value = bgw('[/TR]\n', '');
  }  
  document.forms['form'].value = bgw('[/TABLE]\n', '');
}

/* TABLE SETZEN */
function setTable (cols, rows) {
  window.returnValue = [cols, rows];
  window.close();
}

/* COPY 2 CLIPBOARD */
function copy2clipboard (fieldname) {
  window.clipboardData.setData('text', getDIV(fieldname).value);
}

/* SHOW / HIDE DIV */
function showDIV (fieldname) {
  if (document.getElementById(fieldname).style.display == 'none')
    document.getElementById(fieldname).style.display = 'block';
  else
    document.getElementById(fieldname).style.display = 'none';
}

/* ### CHECK HTML ### */

/* ### CHECK FORM ### */
function checkEmptyField (fieldname, title, func) {
  if (typeof title == 'undefined')
    title = 'Eingabe';
  if (getDIV(fieldname).value == '') {
    alert(title+': Bitte gib eine Nachricht ein!');
    return false;
  }
  else {
    if (typeof func != 'undefined' && typeof func == 'function')
      func();
    else
      return true;
  }
}
