var CHECKBOX_CHECKED = "checkbox_checked";
var CHECKBOX_CHECKED_HOVER = "checkbox_checked_hover";
var CHECKBOX_UNCHECKED = "checkbox_unchecked";
var CHECKBOX_UNCHECKED_HOVER = "checkbox_unchecked_hover";

var checkboxCollection = new Array();

function CCheckbox_Converter()
{
   var inputs = document.getElementsByTagName("input");
   this.convertCount = 0;

   this.get = Checkboxes_Get;
   this.convert = Checkboxes_Convert;
   
   var checkboxes = this.get(inputs);
   this.convert(checkboxes);
}

function Checkboxes_Get(inputs)
{
   var checkboxes = new Array();

   for(var i = 0; i < inputs.length; i++)
      if(inputs[i].type.toLowerCase() == "checkbox")
         if(inputs[i].className.toLowerCase() == "ccheckbox")
            checkboxes.push(inputs[i]);

   return checkboxes;
}

function Checkboxes_Convert(checkboxes)
{
   for(var i = 0; i < checkboxes.length; i++)
   {
      var checkbox = new CCheckbox(checkboxes[i].id, checkboxes[i].name, checkboxes[i].value);
      checkbox.checked = checkboxes[i].checked;
      checkbox.disabled = checkboxes[i].disabled;
      checkbox.visible = checkboxes[i].visible;
      checkbox.backgroundColor = checkboxes[i].style.backgroundColor;
            
      checkbox.draw(checkboxes[i].parentNode, checkboxes[i]);
      this.convertCount++;
   }
}

function CCheckbox(id, name, value)
{
   this.id = "";
   this.name = "";
   this.value = "";
   this.backgroundColor = "";
   this.checked = false;
   this.disabled = false;
   this.visible = true;
   this.self = null;
   this.label = null;
   
   this.check = Checkbox_Check;
   this.uncheck = Checkbox_Uncheck;
   this.disable = Checkbox_Disable;
   this.enable = Checkbox_Enable;
   this.hide = Checkbox_Hide;
   this.show = Checkbox_Show;
   this.draw = Checkbox_Draw;
   
   this.id = id;
   this.name = name;
   this.value = value;
}

function Checkbox_Draw(parent, input)
{
   //Check Values
   if(this.id == null || this.id == undefined)
      this.id = "";
      
   if(this.name == null || this.name == undefined)
      this.name = "";
      
   if(this.value == null || this.value == undefined)
      this.value = "";
      
   if(this.value == null || this.value == undefined)
      this.backgroundColor = "";

   if(this.checked == null || this.checked == undefined || this.checked == "")
      this.checked = false;
      
   if(this.disabled == null || this.disabled == undefined || this.disabled == "")
      this.disabled = false;
      
   if(this.visible == null || this.visible == undefined || this.visible == "")
      this.visible = true;
   
   //Create Container
   var div = document.createElement("div");
   
   if(this.backgroundColor != "")
      div.style.backgroundColor = this.backgroundColor;
   
   if(this.checked)
      div.className = CHECKBOX_CHECKED;
   else
      div.className = CHECKBOX_UNCHECKED;

   div.onclick = function ()
   {
      if (this != null)
      {
         var input = this.nextSibling;

         for (var i = 0; i < checkboxCollection.length; i++)
         {
            if (checkboxCollection[i].self == this)
            {
               if (input != null && checkboxCollection[i].self != null)
               {
                  if (checkboxCollection[i].disabled)
                     break;
                  if (checkboxCollection[i].checked)
                  {
                     this.className = CHECKBOX_UNCHECKED_HOVER;
                     checkboxCollection[i].checked = false;
                  }
                  else
                  {
                     this.className = CHECKBOX_CHECKED_HOVER;
                     checkboxCollection[i].checked = true;
                  }

                  input.click();
               }

               break;
            }
         }
      }
   };
   
   div.onmouseover = function()
   {
      if(this != null)
      {
         for(var i = 0; i < checkboxCollection.length; i++)
         {  
            if(checkboxCollection[i].self == this)
            {
               if(checkboxCollection[i].checked)
                  this.className = CHECKBOX_CHECKED_HOVER;
               else
                  this.className = CHECKBOX_UNCHECKED_HOVER;
            }
         }
      }
   };
   
   div.onmouseout = function()
   {
      if(this != null)
      {
         for(var i = 0; i < checkboxCollection.length; i++)
         {
            if(checkboxCollection[i].self == this)
            {
               if(checkboxCollection[i].checked)
                  this.className = CHECKBOX_CHECKED;
               else
                  this.className = CHECKBOX_UNCHECKED;
            }
         }
      }
   };
   
   //Find Label
   var label = null;
   var labels = document.getElementsByTagName("label");
   
   for(var i = 0; i < labels.length; i++)
   {
      if(labels[i].htmlFor != null && labels[i].htmlFor != undefined && labels[i].htmlFor != "")
      {
         if(labels[i].htmlFor == this.id)
            label = labels[i];
      }
   }
   
   if(label != null)
   {
      label.onclick = function ()
      {
         if (this != null)
         {
            for (var i = 0; i < checkboxCollection.length; i++)
            {
               if (checkboxCollection[i].id == this.htmlFor)
               {
                  var input = document.getElementById(this.htmlFor);
                  if (checkboxCollection[i].disabled)
                     break;
                  if (checkboxCollection[i].checked)
                  {
                     checkboxCollection[i].checked = false;
                     input.previousSibling.className = CHECKBOX_UNCHECKED;
                  }
                  else
                  {
                     checkboxCollection[i].checked = true;
                     input.previousSibling.className = CHECKBOX_CHECKED;
                  }

                  break;
               }
            }
         }
      };
      
      this.label = label;
   }
   
   //Draw/Add
   this.self = div;
   checkboxCollection.push(this);
   parent.insertBefore(this.self, input);
}

function Checkbox_Check()
{
   var input = this.self.nextSibling;
   
   if(input != null && this.self != null)
   {
      this.checked = true;
      this.self.className = CHECKBOX_CHECKED;
      input.checked = true;
   }
}

function Checkbox_Uncheck()
{
   var input = this.self.nextSibling;
   
   if(input != null && this.self != null)
   {
      this.checked = false;
      this.self.className = CHECKBOX_UNCHECKED;
      input.checked = false;
   }
}

function Checkbox_Hide()
{
   var input = this.self.nextSibling;
   
   if(input != null && this.self != null)
   {
      this.visible = false;
      this.self.style.display = "none";
      input.style.display = "none";
   }
}

function Checkbox_Show()
{
   var input = this.self.nextSibling;
   
   if(input != null && this.self != null)
   {
      this.visible = true;
      this.self.style.display = "block";
      input.style.display = "inline";
   }
}

/* Nicht Implementiert */
function Checkbox_Disable()
{ alert("Not Implemented"); }

function Checkbox_Enable()
{ alert("Not Implemented"); }


