var mutils = new MiscUtils();
EventUtil.addEventHandler(window, 'load', onWindowLoad);
var allCheckboxes;
var satbandCheckboxes;
var atmosCheckboxes;
var zenithCheckboxes;
var colmoisCheckboxes;
var skintempCheckboxes;
var satbandDefault = 'S01';
var atmosDefault = 1;
var zenithDefault = 0;
var colmoisDefault = 1;
var skintempDefault = 0;
var satbandDefaultIndex;
var atmosDefaultIndex;
var zenithDefaultIndex;
var colmoisDefaultIndex;
var skintempDefaultIndex;
var nChecked = 0;
var message;

/////////////////////// FUNCTIONS: BEGIN ////////////////////////////////
//--------------------------------------------------------------
function onWindowLoad() {

  if (document.getElementById("wfForm")) {
    allCheckboxes = getAllCheckboxes();
    for (var i=0; i < allCheckboxes.length; i++) {
      EventUtil.addEventHandler(allCheckboxes[i], 'change', enforceRules);
      EventUtil.addEventHandler(allCheckboxes[i].parentNode, 'mouseover', checkRules);
      EventUtil.addEventHandler(allCheckboxes[i].parentNode, 'mouseout', checkRules);
    }
    var containers = mutils.getElementsByClassName('checkboxContainer');
    for (var i=0; i < containers.length; i++) {
      EventUtil.addEventHandler(containers[i], 'mouseout', enableAllCheckboxes2);
    }

    EventUtil.addEventHandler(document.getElementById("go"), 'click', submitForm);
    EventUtil.addEventHandler(document.getElementById("reset"), 'click', resetForm);

    // determine array index of default checkbox for zenith
    for (var i=0; i < satbandCheckboxes.length; i++) {
      if (satbandCheckboxes[i].value == satbandDefault) {
        satbandDefaultIndex = i;
        break;
      }
    }

    for (var i=0; i < atmosCheckboxes.length; i++) {
      if (atmosCheckboxes[i].value == atmosDefault) {
        atmosDefaultIndex = i;
        break;
      }
    }

    for (var i=0; i < zenithCheckboxes.length; i++) {
      if (zenithCheckboxes[i].value == zenithDefault) {
        zenithDefaultIndex = i;
        break;
      }
    }

    // determine array index of default checkbox for colmois
    for (var i=0; i < colmoisCheckboxes.length; i++) {
      if (colmoisCheckboxes[i].value == colmoisDefault) {
        colmoisDefaultIndex = i;
        break;
      }
    }

    // determine array index of default checkbox for skintemp
    for (var i=0; i < skintempCheckboxes.length; i++) {
      if (skintempCheckboxes[i].value == skintempDefault) {
        skintempDefaultIndex = i;
        break;
      }
    }
  }

  message = document.getElementById("message");

  updateForm();
}

//--------------------------------------------------------------
function openWindow(url){
  window.open(url, 'newWindow','width=750,height=450,location=yes,menubar=yes,resizable=yes,scrollbars=yes,toolbar=no');
}

//--------------------------------------------------------------
function resetForm() {
  clearAll('satband');
  clearAll('atmos');
  clearAll('zenith');
  clearAll('colmois');
  clearAll('skintemp');

  satbandCheckboxes[satbandDefaultIndex].checked = true;
  atmosCheckboxes[atmosDefaultIndex].checked = true;
  zenithCheckboxes[zenithDefaultIndex].checked = true;
  colmoisCheckboxes[colmoisDefaultIndex].checked = true;
  skintempCheckboxes[skintempDefaultIndex].checked = true;

  document.getElementById('satbandValues').value = '';
  document.getElementById('atmosValues').value = '';
  document.getElementById('zenithValues').value = '';
  document.getElementById('colmoisValues').value = '';
  document.getElementById('skintempValues').value = '';

  enforceRules();
}

//--------------------------------------------------------------
function selectAll(param) {
  eval('var checkboxes = ' + param + 'Checkboxes');
  
  for (var i=0; i < checkboxes.length; i++) {
    if (checkboxes[i].getAttribute('disabled') != 'disabled') {
      checkboxes[i].checked = true;
    }
  }

  enforceRules();
}

//--------------------------------------------------------------
function clearAll(param) {
  eval('var checkboxes = ' + param + 'Checkboxes');
  
  for (var i=0; i < checkboxes.length; i++) {
    checkboxes[i].checked = false;
  }

  enforceRules();
}

//--------------------------------------------------------------
function submitForm() {

/*
  var nSubmit = 0;

  // if too many are checked
  if (nChecked > 25) {
    nSubmit = parseInt(document.getElementById('nSubmit').value);
    nSubmit++;
    document.getElementById('nSubmit').value = nSubmit;
  } 
*/

//  if (nSubmit > 1 || nChecked <=25)  document.hiddenForm.submit();
  document.hiddenForm.submit();
}

//--------------------------------------------------------------
function updateForm() {
  var n = 0;
  
  var checkedValues = new Array();
  for (var i=0; i < satbandCheckboxes.length; i++) {
    if (satbandCheckboxes[i].checked) {
      checkedValues.push(satbandCheckboxes[i].value);
      n++;
    }
  }
  document.getElementById('satbandValues').value = checkedValues.join('|');
  nChecked = n;

  n = 0;
  checkedValues = new Array();
  for (var i=0; i < atmosCheckboxes.length; i++) {
    if (atmosCheckboxes[i].checked) {
      checkedValues.push(atmosCheckboxes[i].value);
      n++;
    }
  }
  document.getElementById('atmosValues').value = checkedValues.join('|');
  nChecked = nChecked * n;

  n = 0; 
  checkedValues = new Array();
  for (var i=0; i < zenithCheckboxes.length; i++) {
    if (zenithCheckboxes[i].checked) {
      checkedValues.push(zenithCheckboxes[i].value);
      n++;
    }
  }
  document.getElementById('zenithValues').value = checkedValues.join('|');
  nChecked = nChecked * n;  

  n = 0;
  checkedValues = new Array();
  for (var i=0; i < colmoisCheckboxes.length; i++) {
    if (colmoisCheckboxes[i].checked) {
      checkedValues.push(colmoisCheckboxes[i].value);
      n++;
    }
  }
  document.getElementById('colmoisValues').value = checkedValues.join('|');
  nChecked = nChecked * n;

  n = 0;
  checkedValues = new Array();
  for (var i=0; i < skintempCheckboxes.length; i++) {
    if (skintempCheckboxes[i].checked) {
      checkedValues.push(skintempCheckboxes[i].value);
      n++;
    }
  }
  document.getElementById('skintempValues').value = checkedValues.join('|');
  nChecked = nChecked * n;

  if (nChecked > 25) {
    message.style.backgroundColor = 'white';
    message.style.padding = '5px';
    message.innerHTML = 'You have selected ' + nChecked + ' images - this might crash your browser.  Click the Go button to view your requested images anyway';  
  } else {
    message.style.backgroundColor = 'transparent';
    message.style.padding = '0';
    message.innerHTML = ' ';  
  }

  document.getElementById("imageCountMessage").innerHTML = 'Number of possible images selected: ' + nChecked + ' (some images may be unavailable)';
}

//--------------------------------------------------------------
function getAllCheckboxes() {

  satbandCheckboxes = mutils.getElementsByName('satband');
  atmosCheckboxes = mutils.getElementsByName('atmos');
  zenithCheckboxes = mutils.getElementsByName('zenith');
  colmoisCheckboxes = mutils.getElementsByName('colmois');
  skintempCheckboxes = mutils.getElementsByName('skintemp');

  var cBoxes = satbandCheckboxes;
  cBoxes = cBoxes.concat( atmosCheckboxes );
  cBoxes = cBoxes.concat( zenithCheckboxes );
  cBoxes = cBoxes.concat( colmoisCheckboxes );
  cBoxes = cBoxes.concat( skintempCheckboxes );
  
  return cBoxes;
}

//--------------------------------------------------------------
function nonDefaultChecked(param) {
  // check to see if a non-default value is checked and return true if this is the case, otherwise return false
  if (param == 'zenith') {
    for (var i=0; i < zenithCheckboxes.length; i++) {
      if (zenithCheckboxes[i].checked && i != zenithDefaultIndex) return true;
    }
  } else if (param == 'colmois') {
    for (var i=0; i < colmoisCheckboxes.length; i++) {
      if (colmoisCheckboxes[i].checked && i != colmoisDefaultIndex) return true;
    }
  } else if (param == 'skintemp') {
    for (var i=0; i < skintempCheckboxes.length; i++) {
      if (skintempCheckboxes[i].checked && i != skintempDefaultIndex) return true;
    }
  }

  return false;
  
}

//--------------------------------------------------------------
function disableCheckboxes2(checkboxes, defaultIndex) {
  for (var i=0; i < checkboxes.length; i++) {
    if (i != defaultIndex) {
//      checkboxes[i].setAttribute('disabled', 'disabled');
      checkboxes[i].parentNode.setAttribute('class', 'disabled');
    } else { // enable default index just to be on the safe side
//      checkboxes[i].removeAttribute('disabled');
      checkboxes[i].parentNode.removeAttribute('class');
    }
  }
}

//--------------------------------------------------------------
function enableCheckboxes2(checkboxes) {

  for (var i=0; i < checkboxes.length; i++) {
//    checkboxes[i].removeAttribute('disabled');
    checkboxes[i].parentNode.removeAttribute('class');
  }
}

//--------------------------------------------------------------
function enableAllCheckboxes2() {
  enableCheckboxes2(zenithCheckboxes);
  enableCheckboxes2(colmoisCheckboxes);
  enableCheckboxes2(skintempCheckboxes);
}

//--------------------------------------------------------------
function disableCheckboxes(checkboxes, defaultIndex) {

  for (var i=0; i < checkboxes.length; i++) {
    if (i != defaultIndex) {
      checkboxes[i].setAttribute('disabled', 'disabled');
      checkboxes[i].parentNode.setAttribute('class', 'disabled');
    } else { // enable default index just to be on the safe side
      checkboxes[i].removeAttribute('disabled');
      checkboxes[i].parentNode.removeAttribute('class');
    }
  }
}

//--------------------------------------------------------------
function enableCheckboxes(checkboxes) {

  for (var i=0; i < checkboxes.length; i++) {
    checkboxes[i].removeAttribute('disabled');
    checkboxes[i].parentNode.removeAttribute('class');
  }
}

//--------------------------------------------------------------
function checkRules() {
  var oEvent = EventUtil.getEvent();
  if (oEvent.target.tagName.toLowerCase() == 'label') { 
    var checkbox = oEvent.target.getElementsByTagName("input")[0];
  } else { // checkbox itself is the target
    var checkbox = oEvent.target;
  }

  // rule: only one of the following can have a non-default value at a time: zenith, colmois and skintemp
  if (checkbox.getAttribute('name') == 'zenith' && checkbox.value != zenithDefault) {
    disableCheckboxes2(skintempCheckboxes, skintempDefaultIndex);
    disableCheckboxes2(colmoisCheckboxes, colmoisDefaultIndex);
  } else if (checkbox.getAttribute('name') == 'colmois' && checkbox.value != colmoisDefault) {
    disableCheckboxes2(skintempCheckboxes, skintempDefaultIndex);
    disableCheckboxes2(zenithCheckboxes, zenithDefaultIndex);
  } else if (checkbox.getAttribute('name') == 'skintemp' && checkbox.value != skintempDefault) {
    disableCheckboxes2(zenithCheckboxes, zenithDefaultIndex);
    disableCheckboxes2(colmoisCheckboxes, colmoisDefaultIndex);
  } else {
    enableCheckboxes2(zenithCheckboxes);
    enableCheckboxes2(colmoisCheckboxes);
    enableCheckboxes2(skintempCheckboxes);
  }

}

//--------------------------------------------------------------
function enforceRules() {

  // rule: only one of the following can have a non-default value at a time: zenith, colmois and skintemp
  if ( nonDefaultChecked('zenith') ) { // loop over non-default values for other params and disable
    disableCheckboxes(skintempCheckboxes, skintempDefaultIndex);
    disableCheckboxes(colmoisCheckboxes, colmoisDefaultIndex);
  } else if ( nonDefaultChecked('colmois') ) {
    disableCheckboxes(skintempCheckboxes, skintempDefaultIndex);
    disableCheckboxes(zenithCheckboxes, zenithDefaultIndex);
  } else if ( nonDefaultChecked('skintemp') ) {
    disableCheckboxes(zenithCheckboxes, zenithDefaultIndex);
    disableCheckboxes(colmoisCheckboxes, colmoisDefaultIndex);
  } else {
    enableCheckboxes(zenithCheckboxes);
    enableCheckboxes(colmoisCheckboxes);
    enableCheckboxes(skintempCheckboxes);
  }
  
  updateForm();
}

