
var Input = {

  // Update the cell from the current value
  // of the checkbox inside it
  updateCellFromCheckbox: function(cell) {
    inputs = cell.getElementsByTagName("input");
    for ( i = 0; i < inputs.length; ++i ) {
      if ( inputs[i].getAttribute('type')=='checkbox') {
	if (inputs[i].checked) {
	    //cell.style.background = "url(\"images/checked.png\") no-repeat top left";
	  cell.style.backgroundColor = "#0f0";
	} else {
	    //cell.style.background = "url(\"images/possible.png\") no-repeat top left";//"none";
	  cell.style.background = "none";
	  cell.style.backgroundColor = "#fff";
	}
      }
    }
  },

 
 initialize: function() {
    var cells = document.getElementsByTagName("td");
    for(var i = 0; i < cells.length; i++) {
      if(cells[i].className.match("selectable-day")) {
	cells[i].onmouseup = Input.handle;
	Input.updateCellFromCheckbox(cells[i]);
	
	// Set checkbox display to none if javascript
	// is enabled
	inputs = cells[i].getElementsByTagName("input");
	for ( j = 0; j < inputs.length; ++j ) {
	  if ( inputs[j].getAttribute('type')=='checkbox') {
	      inputs[j].style.display = "none";
	  }
	}
      }
    }
  },

  
 // Toggle the state of the checkbox inside
 // the cell
 handle: function() {
    inputs = this.getElementsByTagName("input");
    for ( i = 0; i < inputs.length; ++i ) {
      if ( inputs[i].getAttribute('type')=='checkbox') {
	inputs[i].checked = !inputs[i].checked;
	Input.updateCellFromCheckbox(this);
      }
    }
  }

}

window.onload = Input.initialize;


