// written by zacharias@antivegan.info

function includeJS(thefile){
  var myscript=document.createElement('script');
  myscript.src=thefile;
  myscript.type='text/javascript';
  myscript.defer=true;
  myhead=document.getElementsByTagName('head')[ 0 ];
  if(myhead){
    myhead.appendChild(myscript);
  }
}

function showtextlimit(thetext,thetoken,thelimit){
  /*
    textcounter for input-box,
  */
  var mytext = document.getElementById(thetext);
  var mycounter = document.getElementById(thetoken);
  var mycount;
  mycount = thelimit - mytext.value.length;
  if (mycount < 0) {
    mycounter.innerHTML = '<b>'+mycount+'</b>';
  }else{
    mycounter.innerHTML = mycount;
  }
}

function togglecollapse(the) {
  var my=document.getElementById(the);
  //alert(the);
  if(my.style.display=="none"){
    my.style.display="";
  }else{
    my.style.display="none";
  }
}

function tryout(){
  var myid="";
  var my;
  var next;
  if (document.getElementsByClassName !== undefined) {
    var myarray=document.getElementsByClassName("collapser");
    for(var i=0;i<myarray.length;i+=1) {
      myid="collapsed_"+i;
      my=myarray[i];
      //my.style.backgroundColor='#e0ffe0';
      //my.style.textDecoration='underline';
      next=my.nextSibling;
      // go to next paragraph
      while(next.nodeType != myarray[i].nodeType) {
        next=next.nextSibling;
      }
      //
      // I want these style-changes only when Javascript is active!
      next.style.display="none";
      next.style.marginTop="-12px";
      my.style.color="#800000";
      my.onmouseover=my.style.cursor='pointer';
      //
      // connect to the toggling-function
      next.id=myid;
      my.onclick=function(x) {
        return function(){
          togglecollapse(x);
        };
      }(next.id);
    }
  }
}

function dothisonload(){
  /*
    This function to be called on load of body
  */
  var myouter=document.getElementById('c_write_box');
  var myinner=document.getElementById('commenttext');
  if(myouter !== null && myinner !== null){
    if(myouter.offsetWidth > 60){
      myinner.style.width = (myouter.offsetWidth-30)+"px";
    }
  }
  tryout();
}


//function hidesecondinput(thefirst,thesecond){
//  /*
//    as long as there is no input in thefirst, thesecond remains hidden
//  */
//  if(document.getElementById(thefirst).value.length === 0){
//    hideme(thesecond);
//  }else{
//    document.getElementById(thesecond).style.display='';
//  }
//}
//
//
//function dothisafterload(){
//  var a="blep"; /**/
//}
