function load_1()
{
  // accept only numbers
  $("td input").keypress(function(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

    return true;
  });
  
  calc_totals();

  $("td input").keyup(function() {
    calc_totals();
  });
  
  
}

function load_2(skip_validation)
{
  events(); 
  calc_totals2();
  $("#help_p3_4").tooltip({ 'class': 'pretty fancy', title: 'Tipo de Horário', text: 'Assinale as duas opções somente se a acção de formação abrangeu ambos os períodos.' });
  $("#help_p3_6").tooltip({ 'class': 'pretty fancy', title: 'Nº de Participantes', text: 'Os participantes são contados tantas vezes quanto o nº de acções que tenham participado.' });
  
    //  $("#next").click(validate_p3);
  
  $("form").submit(function() {
    if (!validate_p3())
      return false;
    
    $("tr.line").each(function() {
      var lab = $(this).find('input.lab:checked').val();
      var poslab = $(this).find('input.poslab:checked').val();

      var val = "none";
      if (lab) val = "lab";
      if (poslab) val = "poslab";
      if (lab && poslab) val = "both";

      $(this).find('input.none').attr('checked', "checked").val(val);

    });
    return true;
  });
  
}

function validate_p3() {
  
  if (window.admin === true)
    return true;
    
  $(".required, .num").each(function() {
    $(this).css('border', "");
  });
  
  $(".label_lab, .label_poslab").css('color', "black");
  
  var validado = true;
  $(".required, .num").each(function() {
    if ($(this).val() == "")
    {
      validado = false;
      $(this).css('border', "1px solid red");
    }
  });
  
  $("tr.line").each(function() {
    var lab = $(this).find('input.lab:checked').val();
    var poslab = $(this).find('input.poslab:checked').val();
      
    if (!lab && !poslab)
    {
      validado = false;
      $(this).find('.label_lab, .label_poslab').css('color', "red");
    }
  });



    
  if (!validado)
  {
    $("#error").show();
    return false;  
  }
  
      

  return true;
}


function load_3()
{
    // accept only numbers
  $("td input").keypress(function(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    
    if (charCode != 46 && charCode > 31 && (charCode < 48 || charCode > 57))
      return false;
    
    return true;
  });
  calc_totals3();

  $("input.num").keyup(function() {
    calc_totals3();
  });
  
  
}



function add_line()
{
  var l = $('tr.line:first').clone(true);
  l.find('input[type=text]').val('');
  l.find('input[type=checkbox]').each(function() {
    $(this).attr("checked", false);
  });
  l.find('td.total').html('');
  l.find('select').each(function() {
    $(this).attr("selected", false);
  });
  l.find('textarea').html('');
  $('#footer').before(l);
}

function rem_line(el)
{
  if ($('tr.line').length > 1)
    $(el).parent('tr').remove();
}

function events()
{
  $("input.num").keypress(function(evt) {
    var charCode = (evt.which) ? evt.which : event.keyCode
    
    if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

    return true;
  });
  
  $("input.num").keyup(function() {
    calc_totals2();
  });
  
/*  $("input.p3_4").keyup(function() {
    var p3_6 = $(this).parents('tr').find('input.p3_6');
    if (parseInt(p3_6.val()) < parseInt($(this).val()))
      p3_6.val($(this).val());
  });
  
  $("input.p3_6").blur(function() {
    var p3_4 = $(this).parents('tr').find('input.p3_4');
    if (parseInt($(this).val()) < parseInt(p3_4.val()))
      $(this).val(p3_4.val());
  });*/
  
}

var prefix="€ "
var wd
function parseelement(thisone)
{
  var thisone = new String(thisone);
  if (thisone.charAt(0)=="$")
    return
    
    wd="w"
    var tempnum=thisone
    for (i=0;i<tempnum.length;i++){
      if (tempnum.charAt(i)=="."){
        wd="d"
        break
      }
    }
    if (wd=="w")
    return prefix+tempnum+".00"
    else{
      if (tempnum.charAt(tempnum.length-2)=="."){
        return prefix+tempnum+"0"
      }
      else{
        tempnum=Math.round(tempnum*100)/100
        return prefix+tempnum
      }
    }
}

function calc_totals3()
{
  var total = 0;
  $('input.p4_3_1').each(function() {
    var val = parseFloat($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('input.p4_3_2').each(function() {
    var val = parseFloat($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('td.p4_3').text(parseelement(total));
  
  var total2 = 0;
  $('input.p4_2').each(function() {
    var val = parseFloat($(this).val());

    if (!isNaN(val))
      total2 += val;
  });
  $('td.p4_1').text(parseelement(total+total2));
}

function calc_totals2()
{
  $('tr.line').each(function() {
    var total = 0;
    
    var val = $(this).find('input.p3_3').val();
    var val = parseInt(val);

    if (!isNaN(val))
      total += val;
    else
      val = 0;
      
    var val = $(this).find('input.p3_6').val();
    var val = parseInt(val);
    
    if (!isNaN(val))
      total = total*val;
    else
      total = 0;
      
    $(this).find('td.total').text(total);
  });
  
  var total = 0;
  $('input.p3_3').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('td.total_p3_3').text(total);
  
  var total = 0;
  $('input.p3_4').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('td.total_p3_4').text(total);
  
  var total = 0;
  $('input.p3_6').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('td.total_p3_6').text(total);

  var total = 0;
  $('td.total').each(function() {
    var val = parseInt($(this).text());
    if (!isNaN(val))
      total += val;
  });
  $('td.total_p3_7').text(total);
}


function calc_totals()
{
      $('tr.line').each(function() {
      var line_total = 0;
      var total = 0;
      $(this).find('input.col1').each(function() {
        var val = parseInt($(this).val());

        if (!isNaN(val))
          total += val;
        });
      $(this).find('td.total_col1').text(total);

      line_total += total;
      var total = 0;
      $(this).find('input.col2').each(function() {
        var val = parseInt($(this).val());

        if (!isNaN(val))
          total += val;
        });
      $(this).find('td.total_col2').text(total);

      line_total += total;
      var total = 0;
      $(this).find('input.col3').each(function() {
        var val = parseInt($(this).val());

        if (!isNaN(val))
          total += val;
        });
      $(this).find('td.total_col3').text(total);
      line_total += total;

      $(this).find('td.total_col').text(line_total);
    });

  
  var total = 0;
  $('td.total_col').each(function() {
    var val = parseInt($(this).text());
  
    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col1').text(total);
  
  // COL 1
  var total = 0;
  $('td.total_col1').each(function() {
    var val = parseInt($(this).text());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col2').text(total);
  
  var total = 0;
  $('td.h input.col1').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col3').text(total);
  
  var total = 0;
  $('td.m input.col1').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col4').text(total);
 
  // COL 2 
  var total = 0;
  $('td.total_col2').each(function() {
    var val = parseInt($(this).text());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col5').text(total);

  var total = 0;
  $('td.h input.col2').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col6').text(total);

  var total = 0;
  $('td.m input.col2').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col7').text(total);
  
  // COL 3 
  var total = 0;
  $('td.total_col3').each(function() {
    var val = parseInt($(this).text());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col8').text(total);

  var total = 0;
  $('td.h input.col3').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col9').text(total);

  var total = 0;
  $('td.m input.col3').each(function() {
    var val = parseInt($(this).val());

    if (!isNaN(val))
      total += val;
  });
  $('tr.total td.col10').text(total);
}