relation = new Array();
/* initAll onload() */
function initAll() {
destination = 'content';
isJason = true;
selected = null;
/*
doAjaxRequest('index.php?ajax=1&com=init','content',true);
*/
}
function initJoin() {
destination = 'content';
isJason = true;
selected = null;
/*doAjaxRequest('index.php?ajax=1&com=newjoin','content',true);*/
}
function doAjaxRequest(url,id , jason) {
destination = id;
isJason = jason;
http_request = false;
if (window.XMLHttpRequest) { // Mozilla, Safari,...
   http_request = new XMLHttpRequest();
   if (http_request.overrideMimeType) {
      http_request.overrideMimeType('text/plain');
      }
   }
else if (window.ActiveXObject) { // IE
          try {
              http_request = new ActiveXObject("Msxml2.XMLHTTP");
              } catch (e) {
                try {
                   http_request = new ActiveXObject("Microsoft.XMLHTTP");
              } catch (e) {}
              }
   }

if (!http_request) {
   alert('Ende :( Kann keine XMLHTTP-Instanz erzeugen');
   return false;
   }
http_request.onreadystatechange = handleAjaxRequest;
http_request.open('GET', url, true);
http_request.send(null);

}
function handleAjaxRequest() {
if (http_request.readyState == 4) {
   if (http_request.status == 200) {
      if (isJason)
         {
         jasonImport(destination,'');
         }
      else
         {
         ajaxOut(destination,'');
         }
      } else {
             writeHtml('logs','e');
       }
    }

}
function writeHtml(bereich,temp)
{
document.getElementById(bereich).style.visibility = "hidden";
document.getElementById(bereich).innerHTML = temp;
document.getElementById(bereich).style.visibility = "visible";
}

function ajaxOut(id,temp)
{
document.getElementById(id).style.visibility = "hidden";
document.getElementById(id).innerHTML = http_request.responseText;
document.getElementById(id).style.visibility = "visible";
}

function jasonImport(id,temp)
{
var html = '';
var test = '';
request = eval('(' + http_request.responseText + ')');


if (request.chords_on) {
   var chord_html = '';
   var $c_counter = 0;
   for (var chord in request.guitar_chords)
      {
      if (request.guitar_chords[chord].grundton >= 0){
	     $c_counter++;
         neue_tabulatur(request.guitar_chords[chord].id);
         chord_html = chord_html + request.guitar_chords[chord].grundton; 
         griff[griff.length] = new init_set();
         griff[griff.length-1].id = request.guitar_chords[chord].id;
         griff[griff.length-1].tabmode = request.guitar_chords[chord].tabmode;
         griff[griff.length-1].grundton = request.guitar_chords[chord].grundton;
         griff[griff.length-1].modus = request.guitar_chords[chord].modus;
         griff[griff.length-1].zusatz = request.guitar_chords[chord].zusatz;
         griff[griff.length-1].griffart = request.guitar_chords[chord].griffart;
         for (var counter = 0 ; counter < 6 ; counter++) {
             var myBar = request.guitar_chords[chord].bars[counter];
             griff[griff.length-1].bars[griff[griff.length-1].bars.length] = request.guitar_chords[chord].bars[counter];
             var myIndice = request.guitar_chords[chord].indices[counter];
	         griff[griff.length-1].indices[griff[griff.length-1].indices.length] = request.guitar_chords[chord].indices[counter];
             griffbrett[dieser][myIndice].setfinger(myBar);
	         /*alert(dieser + '<br>' + myBar  + '<br>' +  myIndice);*/
	         }
      }
   }
   /*writeHtml('menue',chord_html);*/
   change_tab(griff.length-1);
   }
else {
   /*alert(0);*/
   }
for (var element in request.div_list)
{
test = test + ' ' + element;
   if (document.getElementById(element))
      {
      writeHtml(element,request.div_list[element]);
      }
    else
      {
      var newDiv = document.createElement("div");
      document.getElementById("mybody").appendChild(newDiv);
      document.getElementById("mybody").lastChild.setAttribute('id',element);
      writeHtml(element,request.div_list[element]);
      }
}
for (var element in request.object_list.relation)
{
relation[relation.length] = element;
}

showRelation();
/* Chat Control */
document.title = request.chat_on;
if (request.chat_on) { 
   var refresh = window.setTimeout("refreshChat()", 30000);
   document.chat.chatinput.focus();
   document.onkeyup = handelKeys;
   }
else {
   document.onkeyup = noKeys;
   }
}
function refreshChat() {
document.title = "refresh";
doAjaxRequest('index.php?ajax=1&com=start_chat&text=&command=refresh_text','inhalt',true);
}
/* Relation Control */
function showRelation()
{
var string = '';
for (var element in relation)
   {
   writeHtml('menue',element);
   }

}
function showpos(mouseevent)
{
var temp = '';
if (!mouseevent) {
    mouseevent = window.event;
    }
var elem = (mouseevent.target) ? mouseevent.target : mouseevent.srcElement;
try
    {
    
    var temp = elem.getAttribute('id');
    selected = temp;
    writeHtml('test_script',temp);
    var text = document.getElementById(temp).style;

    }
catch(e)
    {
    if (!mouseevent) {
        mouseevent = window.event;
        }
    }
}

function move(mouseevent) {
var temp;
if (!mouseevent) {
    mouseevent = window.event;
    }
var elem = (mouseevent.target) ? mouseevent.target : mouseevent.srcElement;
try
    {
    if (selected) {
    ywert = mouseevent.clientX  + "px";
    xwert = mouseevent.clientY + "px";
    writeHtml('test',mouseevent.clientX);

    document.getElementById(selected).style.position = "absolute";
    document.getElementById(selected).style.top = xwert;
    document.getElementById(selected).style.left = ywert;
    document.getElementById(selected).style.width = '100px';
    document.getElementById(selected).style.height = '100px';
    document.getElementById(selected).style.border = 'solid black 1px';
    }
    }
catch(e)
    {
    if (!mouseevent) {
        mouseevent = window.event;
        }
    }
}
/*Hilffunktion zum Betrachten von Objekt-Eigenschaften*/
function displayObjectInfo (obj) {
var temp = '<hr>';
    for (var i in obj)
    {
    if (isNaN(i) == true)
      {
      temp += "<br/>" + obj + "." + i + "" + obj.i;
      
      }
    else
      {
      temp += obj[i] + '<br/>';
      }
    }
return temp;
}
function release () {
selected = null;
}
/* Tasten */
function handelKeys(event)
{
document.title = 'stagstok Chat';
document.chat.chatinput.focus();
}
function noKeys()
{
return false;
}
function chatInput(string)
{
writeHtml('chat_body',string);
}
function chatSend(string)
{
doAjaxRequest('index.php?ajax=1&com=start_chat&text=' + string + '&command=new_text','inhalt',true);
document.chat.chatinput.value = '';
document.chat.chatinput.focus();
/*writeHtml('chat_body','');
writeHtml('chat_top',string);*/
return false;
}
function ajaxForm()
{
var text = document.signin.elements.text.value;
var temp = document.signin.elements.temp.value;
text = text.replace(/&/g,"<ampersand>");
doAjaxRequest('index.php?ajax=1&com=' + document.signin.elements.com.value + '&text=' + text + '&temp=' + temp,'inhalt',true);
}


function ajaxEditField(formularname)
{
com = eval('document.' + formularname + '.elements.com.value');
temp = eval('document.' + formularname + '.elements.temp.value');
field = eval('document.' + formularname + '.elements.field.value');
text = eval('document.' + formularname + '.elements.text.value');
doAjaxRequest('index.php?ajax=1&com=' + com + '&command=update&text=' + text + '&temp=' + temp + '&field=' + field,'inhalt',true);
}
function ajaxFormTextarea()
{
var text = document.ajaxTextarea.elements.text.value;
var com = document.ajaxTextarea.elements.com.value;
var command = document.ajaxTextarea.elements.command.value;
var x = document.ajaxTextarea.elements.x.value;
var id = document.ajaxTextarea.elements.temp.value;
var temp = text.replace(/&/g,"<ampersand>");


/*text = escape(text);*/
text = text.replace(/'/g,"\'");
text = text.replace(/#/g,"%23");
text = text.replace(/&/g,"%26");
var tempArray = temp.split("\n");
var send = tempArray.join("_");
doAjaxRequest('index.php?ajax=1&com=' + com + '&command=' + command + '&x=' + x + '&temp=' + id + '&text=' + text,'inhalt',true);
}
function getDateSelector(name , value) {
var temp = value.split("-");
writeHtml('date', daySelector(temp[2]) + monthSelector(temp[1]) + yearSelector(temp[0]));
}
function yearSelector(year) {
var html = '<select name="year" size="1" onchange="changeYear(this.value)">';
for (var i = 1900 ; i < 2008 ; i++) 
if (i == year) {
   html += '<option selected>' + i + '</option>';
   }
else {
   html += '<option>' + i + '</option>';
   }    
html += '</select>';
return html;
}
function changeYear(value) {
var temp = document.formular.birthday.value.split("-");
document.formular.birthday.value = value + '-' + temp[1] + '-' + temp[2];
}
function monthSelector( value ) {
var html = '<select name="month" size="1" onchange="changeMonth(this.value)">';
for (var i = 1 ; i < 12 ; i++) 
if ( i == value ) {
   html += '<option selected>' + i + '</option>';
   }
else {
   html += '<option>' + i + '</option>';
   }    
html += '</select>';
return html;
}
function changeMonth(value) {
var temp = document.formular.birthday.value.split("-");
value = addZero(value);
document.formular.birthday.value = temp[0] + '-' + value + '-' + temp[2];
}
function daySelector( value ) {
var html = '<select name="day" size="1" onchange="changeDay(this.value)">';
for (var i = 1 ; i < 31 ; i++) 
if ( i == value ) {
   html += '<option selected>' + i + '</option>';
   }
else {
   html += '<option>' + i + '</option>';
   }    
html += '</select>';
return html;
}
function changeDay(value) {
var temp = document.formular.birthday.value.split("-");
value = addZero(value);
document.formular.birthday.value = temp[0] + '-' + temp[2] + '-' + value;
}
function addZero (value) {
if (value < 10) {
   value = '0' + value;
   }
return value;
}