/*
 * Copyright 2009 Matteo Corti
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/*
 *
 * RCS information
 * enable substitution with:
 * $ svn propset svn:keywords "Id Revision HeadURL Source Date"
 *
 * $Id: pasi.js 1057 2010-01-20 12:22:14Z corti $
 * $Revision: 1057 $
 * $HeadURL: https://matteocorti.ch/repos/personal/web/pasi.corti.li/htdocs/pasi.js $
 * $Date: 2010-01-20 13:22:14 +0100 (Wed, 20 Jan 2010) $
 *  
 */

var headElements  = [ 'cah',  'ceh',  'cih',  'cdh'  ];
var armsElements  = [ 'caul', 'ceul', 'ciul', 'cdul' ];
var trunkElements = [ 'cat',  'cet',  'cit',  'cdt'  ];
var legsElements  = [ 'call', 'cell', 'cill', 'cdll' ];

var checkedElements = new Array();

/**
 * getSelected returns the value of the selected checkbox
 * @param  checkbox  the checkbox to analyze
 * @return value     of the selected checkbox
 */
function getSelected( checkbox ) {
  
  var val = 0;
  var element = checkbox;
  var i;
  
  for (i=0;i<element.length; i++) {
    
    if (element[i].checked === true) {
      val = element[i].value;
    }
    
  }
  
  return parseInt(val, 10);
  
}

function fillDate() {

  var today = new Date();

  var year = today.getYear();

  if (year < 1000) {
    year = year + 1900;
  }
  
  var month = (today.getMonth()+1);
  if (month < 10) {
    month = "0" + month;
  }
  var day = today.getDate();
  if (day < 10) {
    day = "0" + day;
  }
  
  document.getElementById('date').innerHTML =
    year + "-" + month + "-" + day;
  
}

function isComplete() {

  var total = 0;

  for (var element in checkedElements) {
    total++;
  }

  return (total == 16);
  
}

/**
 * Colors the given element with the supplied color
 * @param element
 * @param newcolor
 */
function color( element, newcolor) {
  document.getElementById(element).style.backgroundColor=newcolor;
  checkedElements[element] = 1;
}

/**
 * Resets the colors of the form
 */
function resetForm() {

  for (var i in headElements) {
    color(headElements[i],  "#ecffb3");
  }
  for (var i in armsElements) {
    color(armsElements[i],  "#e5ffff");
  }
  for (var i in trunkElements) {    
    color(trunkElements[i], "#ffe5e5");
  }
  for (var i in legsElements) {
    color(legsElements[i],  "#ffffcc");
  }

  checkedElements = new Array();

  document.getElementById('output').innerHTML='<font size="+3">&nbsp;<\/font>';

  
}

/**
 * Computes the PASI
 */
function compute() {

  var eh = getSelected(document.PASI.eh);
  var ih = getSelected(document.PASI.ih);
  var dh = getSelected(document.PASI.dh);
  var ah = getSelected(document.PASI.ah);

  var eul = getSelected(document.PASI.eul);
  var iul = getSelected(document.PASI.iul);
  var dul = getSelected(document.PASI.dul);
  var aul = getSelected(document.PASI.aul);

  var et = getSelected(document.PASI.et);
  var it = getSelected(document.PASI.it);
  var dt = getSelected(document.PASI.dt);
  var at = getSelected(document.PASI.at);

  var ell = getSelected(document.PASI.ell);
  var ill = getSelected(document.PASI.ill);
  var dll = getSelected(document.PASI.dll);
  var all = getSelected(document.PASI.all);

  var result = (ih+eh+dh)*ah*0.1+(eul+iul+dul)*aul*0.2+(et+it+dt)*at*0.3+(ell+ill+dll)*all*0.4;

  var resultString;

  if ( isComplete() ) {
    resultString = '<font size="+3">';
  } else {
    resultString = '<font size="+3" color="#ff0000">';
  }
  
  resultString += (Math.round(result*100)/100).toFixed(1);

  if ( isComplete() ) {
    resultString += " (complete)";
  } else {
    resultString += " (not complete)";
  }

  resultString += '<\/font>';
  
  document.getElementById('output').innerHTML = resultString;

}
