function setCookie(c_name,value,expiredays) {
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+expiredays);
  document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

function ClickIntoForm(id, standardvalue) {
  var input = document.getElementById( id );
  if (input.value == standardvalue) {
    input.value = '';
    input.style.color = '#000000';
  }
}

function LeaveForm(id, standardvalue) {
  var input = document.getElementById( id );
  if (input.value == "" ) {
    input.value = standardvalue;
    input.style.color = '#999999';
  }
}

function textCounter(field,cntfield,length) { 
  if (field.value.length > length) {
    field.value = field.value.substring(0, length); 
  } else {
    cntfield.value = length - field.value.length; 
  }
}

function submitQuickSearch() {
  s = document.menu_quicksearch;
  if( s.searchtxt.value == "Suche in Interpret/Titel" ) {
    s.searchtxt.value = "";
  }
  if( s.uploader.value == "Suche nach Uploader" ) {
    s.uploader.value = "";
  }
  s.submit();
}

function submitEnter( myfield,e ) {
  var keycode;
  if (window.event) keycode = window.event.keyCode;
  else if (e) keycode = e.which;
  else return true;

  if (keycode == 13) {
    submitQuickSearch();
    return false;
  }
  else
    return true;
}

function confirmGoto( question, url ) {
  if( confirm( question ) ) {
  window.location.href = url
  }
}

function insert(aTag, eTag, form, element) {
  var input = document.forms[form].elements[element];
  if( input.disabled ) return false;
  input.focus();

  /* für Internet Explorer */
  if(typeof document.selection != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    /* Anpassen der Cursorposition */
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();

  /* für neuere auf Gecko basierende Browser */
  } else if(typeof input.selectionStart != 'undefined') {
    /* Einfügen des Formatierungscodes */
    var start = input.selectionStart;
    var end = input.selectionEnd;
    var insText = input.value.substring(start, end);
    input.value = input.value.substr(0, start) + aTag + insText + eTag + input.value.substr(end);
    /* Anpassen der Cursorposition */
    var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    input.selectionStart = pos;
    input.selectionEnd = pos;

  /* für die übrigen Browser */
  } else {
    /* Abfrage der Einfügeposition */
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    /* Einfügen des Formatierungscodes */
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
  }
}

function selectCheckboxes(field,type) {
  if(type == 1) {                      // check all
    for(i = 0; i < field.length; i++) {
      field[i].checked = true;
    }
  } else if(type == 2) {               // deselect all
    for(i = 0; i < field.length; i++) {
      field[i].checked = false;
    }
  } else if(type == 3) {               // inverse selection
    for(i = 0; i < field.length; i++) {
      if( field[i].checked ) {
        field[i].checked = false;
      } else {
        field[i].checked = true;
      }
    }
  } else {
    return false;
  }
}


function openQueryLog( user_id ) {
  window.open("/?action=querylog&user_id=" + user_id ,"","toolbars=no,scrollbars=yes,statusbars=no,menubars=no,resizable=yes,width=700,height=600,left=10,top=10");
}

function toggleDisplay(id) {
  if( document.getElementById(id).style.display == "inline" ) {
    document.getElementById(id).style.display = "none";
  } else {
    document.getElementById(id).style.display = "inline";
  }
}


/* -----------------------------------------------------------------------------------------
  unused ~:-/
 -----------------------------------------------------------------------------------------*/

function HelpPopup(dateiname) {
  window.open("<? echo $correct_server_url ?>include/" + dateiname,"","width=400,height=500,left=250,top=250");
}

function OpenUserLog(user_id) {
  window.open("http://3dl.am/index.php?action=userlog&user_id=" + user_id,"","toolbars=no,scrollbars=yes,statusbars=no,menubars=no,resizable=yes,width=1000,height=750,left=100,top=100");
}

function ShowMirror(id) {
  if(document.getElementById("mirror_" + id).style.display == "inline") {
    document.getElementById("mirror_" + id).style.display = "none";
  } else {
    document.getElementById("mirror_" + id).style.display = "inline";
  }
}

function Tooltip(id,hide) {
  if(hide == 1) {
    document.getElementById("tooltip_" + id).style.display = "block";
  } else {
    document.getElementById("tooltip_" + id).style.display = "none";
  }
}

function closePMBenachrichtigung() {
  document.getElementById("pm_benachrichtigung").style.display = "none";
}

function ChangeHoster(hoster) {
  document.detailsuche.hoster.value = hoster;
}
