////
/// debug system
//
var a_d__first=0; //for a_d(*) internal use only
var a_d__last=0;  //for a_d(*) internal use only
var a_d__MAXLENGTH=10;  //for a_d(*) configuration / change after including script
//for debugging output
//debug_id a single letter as a debugging keyword
//output the debugging output
function a_d(debug_id,output) {
  if (! document.getElementById('a_debug')) {return false;}
  var out = document.getElementById('a_debug');
  line=document.createElement('div');
  line.appendChild(document.createTextNode(debug_id+':'+output));
  line.className='a_debug_line';
  line.id='a_d__'+a_d__last;
  out.appendChild(line);
  a_d__last++;
  if (a_d__last-a_d__first>a_d__MAXLENGTH) {
    out.removeChild(document.getElementById('a_d__'+a_d__first));
    a_d__first++;
  }
}

////
/// output in a box
//
//output $output$ in a tag with id $out_id$
function a_out(out_id,output) {
  if (! document.getElementById(out_id)) {
    tag=document.createElement('div');
    tag.className='a_out_tag';
    tag.appendChild(document.createTextNode('-'));
    tag.id=out_id;
    document.getElementsByTagName('BODY')[0].appendChild(tag);
  }
  var out = document.getElementById(out_id);
  //out.firstChild.nodeValue=output;
   //out.firstChild.innerHTML=output;
  out.innerHTML=output;
}

// output $output$ as a new line in a tag with id $out_id$
var a_outln__first=0;
var a_outln__last=0;
var a_outln__MAXLENGTH=10;
function a_outln(tag_id,output/*,max_line_cn*/) {
  if (arguments.length>2) {maxlen=arguments[2];} else {maxlen=a_outln__MAXLENGTH;}
  if (! document.getElementById(tag_id)) {
    a_d('w','a_outln: could not found id='+tag_id+'. create it');
    tag=document.createElement('div');
    tag.className='a_outln_box';
    tag.id=tag_id;
    document.getElementsByTagName('BODY')[0].appendChild(tag);
  }
  var tag = document.getElementById(tag_id);
  line=document.createElement('div');
  line.appendChild(document.createTextNode(output));
  line.className='a_outln_line';
  line.id='a_outln__'+a_outln__last;
  tag.appendChild(line);
  a_outln__last++;
  while (a_outln__last-a_outln__first>maxlen) {
    tag.removeChild(document.getElementById('a_outln__'+a_outln__first));
    a_outln__first++;
  }
}

////
/// cookies
//
function a_cookie_long(coo,val) {
  var t = new Date();
  var lange = t.getTime() + (99 * 365 * 24 * 60 * 60 * 1000);
  t.setTime(lange);
  document.cookie = coo+"="+val+"; expires=" + t.toGMTString();
  a_d('c','set 99-year-cookie: '+coo+'='+val);
}
function a_cookie_session(coo,val) {
  document.cookie = coo+'='+encodeURI(val)+';';
  a_d('c','set sessioncookie: '+coo+'='+encodeURI(val));
}
function a_cookie_get(coo) {
  if (document.cookie) {
    all_coo=document.cookie;
    expr = new RegExp(coo+'=([^;]+)','g');
    res = expr.exec(all_coo);
    if (res) {
      val=res[1];
      a_d('c','find cookie: '+coo+'='+val);
      return decodeURI(val);
    } else {
      a_d('c','could not find cookie: '+coo);
    }
  }
  return '';
}
function a_cookie_delete(coo) {
  //var t = new Date();
  //var lange = t.getTime() + (99 * 365 * 24 * 60 * 60 * 1000);
  //t.setTime(lange);
  document.cookie = coo+"=; expires=0";
  a_d('c','delete cookie: '+coo);
}

////
/// open and close elements by id
//
//open close or toggle tags by id
// 'v' 'c' 't' follow by ids
// numers are complieted with 'a_'.nr
function a_v() {
  state='o'; cookie_=false;
  arg=0; while (arg<arguments.length) {
    id=arguments[arg];
    cookie_=(arguments[arg].substr(0,2)=='S_');
    //if (cookie_) {id=arguments[arg];}
    //else        {id=arguments[arg];}
    if ((id=='o') || (id=='c') || (id=='t')) {
      state=id;
    } else if (document.getElementById(id)) {
      tag=document.getElementById(id);
      if (state=='t') {
        if (tag.style.display=='none') {state='o';}
        else                           {state='c';}
      }
      if (state=='o') {
        tag.style.display=a_v__display[id];
        a_d('x','o '+id);
      } else {
        tag.style.display='none';
        a_d('x','c '+id);
      }
      if (cookie_) {
        a_cookie_session(id,state);
      }
    }
    arg++;
  }
}

////
/// HttpRequests
//
var a_request;
function a_onprogress(e) {
  var percentComplete = (e.position / e.totalSize);
  if (document.getElementById('a_progress_pointer')) {
    document.getElementById('a_progress_pointer').style.width=Math.round(percentComplete*300)+'px';
  }
  if (document.getElementById('a_progress_number')) {
    document.getElementById('a_progress_number').firstChild.nodeValue=Math.round(percentComplete*100)+"%";
  }
}
function a_onerror(e) {
  //alert('Error ' + e.target.status + ' occurred while receiving the document.');
}
function a_onload(e) {
  //
}
function a_onreadystatechange() {
  if(4==a_request.readyState) {
    if(200 != a_request.status) {
      alert('Fehler '+a_request.status+': '+a_request.statusText);
    } else {
      a_d('r',a_request.responseText);
    }
  }
}
function a_get_url(url) {
  try {
    if( window.XMLHttpRequest ) {
      a_request = new XMLHttpRequest();
    } else if( window.ActiveXObject ) {
      a_request = new ActiveXObject('Microsoft.XMLHTTP');
    }
    a_request.onprogress = a_onprogress;
    a_request.open('GET', url, true);
    //a_request.onload = a_onload;
    //a_request.onerror = a_onerror;
    a_request.onreadystatechange =a_onreadystatechange;
    a_request.send(null);
  } catch( e ) {
    a_d('e','a_get_url can not open('+url+') error:'+e);
  }
}

////
/// function from perent of choose_anrefile.php
//
var my_win;
function a_open_window(url,target,options) {
  if (target=='') {target='_self';}
  my_win=window.open(url,target,options);
}
var a_anredata_change_color='#F88';
var a_file,a_info;
function a_choose_anredata_receive(file,info,nr) {
  if (nr==0){
    i=0; while (i<=u_MAXPLOTS) {
      a_d('m','file='+file+' info='+info+' nr='+i);
      eval("document.plotform.f"+i+".value=file;");
      eval("document.plotform.i"+i+".value=info;");
      eval("document.plotform.i"+i+".style.backgroundColor='"+a_anredata_change_color+"';");
      i++;
    }
  } else {
    a_d('m','file='+file+' info='+info+' nr='+nr);
    eval("document.plotform.f"+nr+".value=file;");
    eval("document.plotform.i"+nr+".value=info;");
    eval("document.plotform.i"+nr+".style.backgroundColor='"+a_anredata_change_color+"';");
    document.plotform.f0.value='x';
    document.plotform.i0.value='see plot settings';
    document.plotform.i0.style.backgroundColor=a_anredata_change_color;
  }
}
function a_choose_anredata_send(f,i,nr) {
  window.opener.a_choose_anredata_receive(f,i,nr);
  window.close();
}


function a_show_anredata() {
  //my_win.close();
  a_d('m','file='+a_file);
  a_d('m','info='+a_info);
}
function a_view_data_fastBut(type,nr) {
  if (type=="ep" || type=="dp") {
    eval("document.plotform.anz.value='2';");
    eval("document.plotform.dl1.value='1';");
    eval("document.plotform.k11.selectedIndex='2';");
    a_view_data_check(document.plotform.dl1,99,"dl1");
    eval("document.plotform.dl2.value='1';");
    eval("document.plotform.k12.selectedIndex='1';");
    a_view_data_check(document.plotform.dl2,99,"dl2");
    if (type=="dp") {
      eval("document.plotform.anz.value='4';");
      eval("document.plotform.dl3.value='1';");
      eval("document.plotform.k13.selectedIndex='3';");
      a_view_data_check(document.plotform.dl3,99,"dl3");
      eval("document.plotform.dl4.value='1';");
      eval("document.plotform.k14.selectedIndex='4';");
      a_view_data_check(document.plotform.dl4,99,"dl4");
    }
    a_view_data_check(document.plotform.anz,99,"anz");
  } else {
    eval("document.plotform.dl"+nr+".value='1';");
    eval("document.plotform.k1"+nr+".selectedIndex='"+type+"';");
    eval("it=document.plotform.dl"+nr);
    a_view_data_check(it,99,"dl"+nr);
  }
}
function a_view_data_check(in_tag,def_wert,key) {
     //alert(in_tag+def_wert+in_tag.value);
  if(key=='anz') {
    f=document.plotform;
    nv=f.anz.value;
    a_d('v','anz='+nv);
    if (!isNaN(nv)) {
      n=parseInt(nv);
      i=1; while (i<u_MAXPLOTS) {
        if (i<n+1) {
          document.getElementById('tr_p_'+i).style.display = 'inline';
        } else {
          document.getElementById('tr_p_'+i).style.display = 'none';
        }
        i++;
      }
    } else {
      a_d('v',nv+' not a number');
    }
  }
  j=1; while (j<u_MAXPLOTS) {
    iskey='dl'+j;
    if(key==iskey) {
      f=document.plotform;
      nv=eval('f.'+iskey+'.value');
      a_d('v','dl'+j+'='+nv);
      if (!isNaN(nv)) {
        n=parseInt(nv);
        i=1; while (i<u_MAXGRAPHS) {
          if (i<n+1) {
            document.getElementById('tr_'+j+'_'+i).style.display = 'inline';
          } else {
            document.getElementById('tr_'+j+'_'+i).style.display = 'none';
          }
          i++;
        }
      } else {
        a_d('v',nv+' not a number');
      }
    }
    j++;
  }
  if (in_tag.value != def_wert) {
    in_tag.style.backgroundColor=a_anredata_change_color;
  } else {
    in_tag.style.backgroundColor='#fff';
  }
}

function a_include_ixi() {
  document.getElementById('ixi').innerHTML="<dev id=buh></dev><b><i>IXI</i></b>";
}
function a_include_buh() {
  if (document.getElementById('buh')) {
    document.getElementById('buh').innerHTML="<b>BUH</b>";
  }
}

