/* <![CDATA[ */

/* 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, tag) {
  var input = document.forms['form'+formindex].elements[tag];
  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['form'].reset();
    document.forms['form'].elements[formtype].blur();
    return;
  }
  else {
    if (formtype == 'style')
      document.forms['form'].value = bgw('[STYLE='+ vari +']', '[/STYLE]');
    if (formtype == 'font')
      document.forms['form'].value = bgw('[FONT='+ vari +']', '[/FONT]');
    if (formtype == 'color')
      document.forms['form'].value = bgw('[COLOR='+ vari +']', '[/COLOR]');
    if (formtype == 'header')
      document.forms['form'].value = bgw('['+ vari +']', '[/'+ vari +']');
    if (formtype == 'size')
      document.forms['form'].value = bgw('[SIZE='+ vari +']', '[/SIZE]');
    if (formtype == 'bgcolor')
      document.forms['form'].value = bgw('[BGCOLOR='+ vari +']', '[/BGCOLOR]');
    if (formtype == 'lh')
      document.forms['form'].value = bgw('[LH='+ vari +']', '[/LH]');
    //document.forms['form'].reset();
    document.forms['form'].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();
 }
}

/* 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, fieldname) {
  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'].elements[fieldname].value = rValue;
}
/* BETRAG SETZEN */
function setAmount (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;
  window.returnValue = number_format(resval, 2, ",");
  window.close();
}

/* 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();
}

/* 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;
    }
  }
}

/* ALERT INFO */
function alertInfo (page, title, target) {
 	var antwort = confirm(title);
	if (antwort == true) {
    if (target == '_blank')
      newWin = window.open(page, "Rechnung", "left=100,top=100,width=100,height=100,scrollbars=no");
    else
 		  location.href = page;
  }
}

/* ]]> */
