String.prototype.unescapeHTML = function() {
  dummie = document.createElement('span');
  dummie.innerHTML = this;
  return dummie.firstChild.data;
}

function formCheck(aForm) {
   var theAction = aForm.Action.value;
   var DeleteMessage = String("Sind Sie sicher, dass Sie diese Meldung l&ouml;schen wollen?").unescapeHTML();
   if(theAction == "delete") return confirm(DeleteMessage);
   return check_match(aForm);
}

function check_match(aForm) {
   var check = true;
   for(i=1;i<6;i++) {     // alert(i);


      check = check_satz(aForm,i);
      if(check == false) break;
      // maxSet = get_match_result(aForm,i);  // alert(maxSet);
      // if(maxSet == 3)
      return true;

      // else get_match_result(aForm);
      // alert(maxSet); //

   }
   return check;
}

function check_satz(aForm,aSet) { // alert(aSet);
   var aField1 = aForm.elements["Satz" + aSet + "_Team1"];
   var aField2 = aForm.elements["Satz" + aSet + "_Team2"];

   /* if(aField1.value == "" || aField1.value == "") {
      var errorMessage = String("Satz " + aSet +": Werte d&uuml;rfen nicht leer sein!").unescapeHTML();
      alert(errorMessage);
      return false;
   }  */
   if(aField1.value == 0 && aField2.value == 0) {
      var errorMessage = String("Satz " + aSet +": Werte d&uuml;rfen nicht beide 0 sein!").unescapeHTML();
      alert(errorMessage);
      return false;
   }
   if(parseInt(aField1.value) < 25 && parseInt(aField2.value) < 25 && aSet != 5) {
      var errorMessage = String("Satz " + aSet +": Ein Satz ist mit mind. 25 Punkten gewonnen!").unescapeHTML();
      alert(errorMessage);
      return false;
   }
   if(Math.abs(parseInt(aField1.value) - parseInt(aField2.value)) < 2) {
      var errorMessage = String("Satz " + aSet +": Ein Satz muss mit mind. 2 Punkten Differenz gewonnen werden!").unescapeHTML();
      alert(errorMessage);
      return false;
   }

   return true;
}

function get_match_result(aForm,aSet) {
   // return aSet;
   var tmp1 = 0;
   var tmp2 = 0;
   for(x=1;x<=aSet;x++) {
      var aField1 = aForm.elements["Satz" + x + "_Team1"];
      var aField2 = aForm.elements["Satz" + x + "_Team2"];

      if(aField1.value != aField2.value) {
         if(parseInt(aField1.value) > parseInt(aField2.value)) tmp1++;
         if(parseInt(aField1.value) < parseInt(aField2.value)) tmp2++;
      }
      aForm.elements["Match1"].value = tmp1;
      aForm.elements["Match2"].value = tmp2;
   }
   return Math.max(tmp1,tmp2);
}

function get_new_match_result(aForm,aSet) {
   // return aSet;
   var tmp1 = 0;
   var tmp2 = 0;
   for(x=1;x<=aSet;x++) {
      var aField1 = aForm.elements["resultat[Satz" + x + "_Team1]"];
      var aField2 = aForm.elements["resultat[Satz" + x + "_Team2]"];

      if(aField1.value != aField2.value) {
         if(parseInt(aField1.value) > parseInt(aField2.value)) tmp1++;
         if(parseInt(aField1.value) < parseInt(aField2.value)) tmp2++;
      }
      aForm.elements["resultat[Match1]"].value = tmp1;
      aForm.elements["resultat[Match2]"].value = tmp2;
   }
   return Math.max(tmp1,tmp2);
}

function switchDayStatus(aButton) {
   var aCell = aButton.parentNode;
   if(aButton.checked == true) {
      aCell.className = (aCell.className == "Ferien")?"Ferien":"klndr_no";
   }
   else {
      aCell.className = (aCell.className == "Ferien")?"Ferien":"klndr";
   }
}

function switchWeekStatus(aButton) {
   var aTableRow = aButton.parentNode.parentNode;
   var theCells = aTableRow.cells;
   if(aButton.checked == true) {
      for(c=1;c<theCells.length;c++) {
         theCells[c].className = (theCells[c].className == "Ferien")?"Ferien":"klndr_no";
         if(theCells[c].getElementsByTagName("INPUT")[0]) theCells[c].getElementsByTagName("INPUT")[0].checked = true;
      }
   }
   else {
      for(c=1;c<theCells.length;c++) {
         theCells[c].className = (theCells[c].className == "Ferien")?"Ferien":"klndr";
         if(theCells[c].getElementsByTagName("INPUT")[0]) theCells[c].getElementsByTagName("INPUT")[0].checked = false;
      }
      restoreWeekdayStatus();
      restoreMonthStatus();
   }
}

function switchMonthStatus(aButton) {
   var aTable = aButton.parentNode.parentNode.parentNode;
   var theRows = aTable.rows;

   if(aButton.checked == true) {
      for(r=2;r<theRows.length;r++) {
         var theCells = theRows[r].cells;
         for(c=1;c<theCells.length;c++) {
            theCells[c].className = (theCells[c].className == "Ferien")?"Ferien":"klndr_no";
            if(theCells[c].getElementsByTagName("INPUT")[0]) theCells[c].getElementsByTagName("INPUT")[0].checked = true;
         }
      }
   }
   else {
      for(r=2;r<theRows.length;r++) {
         var theCells = theRows[r].cells;
         for(c=1;c<theCells.length;c++) {
            theCells[c].className = (theCells[c].className == "Ferien")?"Ferien":"klndr";
            if(theCells[c].getElementsByTagName("INPUT")[0]) theCells[c].getElementsByTagName("INPUT")[0].checked = false;
         }
      }
      restoreWeekdayStatus();
      restoreWeekStatus();
   }

}


function switchWeekdayStatus(aButton) {
   var Kalender = new Array("2009-09","2009-10","2009-11","2009-12","2010-01","2010-02","2010-03","2010-04");
   var Weekday = (aButton.value==0)?7:aButton.value;
   for(k=0;k<Kalender.length;k++) {
      var aTable = document.getElementById(Kalender[k]);
      var theRows = aTable.rows;

      if(aButton.checked == true) {
         for(r=2;r<theRows.length;r++) {
            var theCells = theRows[r].cells;
            theCells[Weekday].className = (theCells[Weekday].className == "Ferien")?"Ferien":"klndr_no";
            if(theCells[Weekday].getElementsByTagName("INPUT")[0]) theCells[Weekday].getElementsByTagName("INPUT")[0].checked = true;
         }
      }
      else {
         for(r=2;r<theRows.length;r++) {
            var theCells = theRows[r].cells;
            theCells[Weekday].className = (theCells[Weekday].className == "Ferien")?"Ferien":"klndr";
            if(theCells[Weekday].getElementsByTagName("INPUT")[0]) theCells[Weekday].getElementsByTagName("INPUT")[0].checked = false;
         }
         //
      }
   }
   restoreWeekStatus();
   restoreMonthStatus();

}

function restoreWeekdayStatus() {
   var aZone = document.getElementById("Wochentage");
   var theButtons = aZone.getElementsByTagName("INPUT");
   for(b=0;b<theButtons.length;b++) {
      if(theButtons[b].checked == true) switchWeekdayStatus(theButtons[b]);
   }

}

function restoreWeekStatus() {
   var Kalender = new Array("2009-09","2009-10","2009-11","2009-12","2010-01","2010-02","2010-03","2010-04");

   for(k=0;k<Kalender.length;k++) {
      var aTable = document.getElementById(Kalender[k]);
      var theRows = aTable.rows;
      for(r=2;r<theRows.length;r++) {
         var theCells = theRows[r].cells;
         if(theCells[0].getElementsByTagName("INPUT")[0].checked == true) switchWeekStatus(theCells[0].getElementsByTagName("INPUT")[0]);
      }
   }

}

function restoreMonthStatus() {
   var Kalender = new Array("2009-09","2009-10","2009-11","2009-12","2010-01","2010-02","2010-03","2010-04");

   for(k=0;k<Kalender.length;k++) {
      var aTable = document.getElementById(Kalender[k]);
      var theRows = aTable.rows;
      var theCell = theRows[0].cells[0];
      if(theCell.getElementsByTagName("INPUT")[0].checked == true) switchMonthStatus(theCell.getElementsByTagName("INPUT")[0]);
   }

}
