
function calculate_pasi_score(){
    var elementGroups = [];
    // Construct the pasi elementGroups array we need in calculateFields(elementGroups)
    // for calculation and rendering.
    $( "#pasi_form tr td input" ).each(function(){
        var rows = $(this).attr('name').match(/^([a-z])[0-9]*/);
        if(typeof(this) != 'undefined') elementGroups[rows[1]] = new Array(1, 2, 3 ,4);
    });
    $( "#pasi_form tr td input" ).each(function(){
        var rows = $(this).attr('name').match(/^([a-z])([0-9])*/);
        if(typeof(this) != 'undefined') elementGroups[rows[1]][rows[2]] = this;
    });
    calculateFields(elementGroups);
}
	
function print_pasi_score(){
		
    for (i = 0; i <= 2; i++)
        url_param += document.head_form.elements[i].value + ':';
	
    win = popup(document.URL + '&print=true' + url_param, 'pasi', 500, 600, 'scrollbars=yes');
		
}

function calculateFields(elementGroups){
    var arealFactor = [0.1, 0.2, 0.3, 0.4];
    var grandTotal = 0;
    for (colNum=1; colNum <= 5; colNum++){
        var sum = 0;
        var rowNum = 1;
        var total = 0;
        for (var row in elementGroups) {
            var colVal = parseInt($(elementGroups[row][colNum]).val());
            colVal = (!isNaN(colVal)) ? colVal : 0;
            switch (rowNum) {
                case 1:
                case 2:
                case 3:
                    // Add first three rows.
                    sum = sum + colVal;
                    break;
                case 4:
                    // Write out sum of previous 3 columns.
                    $(elementGroups[row][colNum]).val(sum);
                    break;
                case 5:
                    // Multiply total with areal factor.
                    sum = sum * colVal * arealFactor[colNum-1];
                    total = Math.round(sum*100)/100;
                    break;
                case 6:
                    // Last row. Calculate total and grand total.
                    total = (!isNaN(total)) ? total : 0;
                    grandTotal = grandTotal + total;
                    // Show total in all columns
                    // except last column show grand total.
                    if(colNum <=4){
                        $(elementGroups[row][colNum]).val(total);
                    }else{
                        $(elementGroups[row][colNum]).val(Math.round(grandTotal*100)/100);
                    }
                    
                    break;
            }
            rowNum++;
        }
    }
}