var no_parts = document.getElementById("decision_table").rows.length - 5;
console.log('Number parts is ' + no_parts);
var current_selection = []
for(var i = 0; i < no_parts; i++){
current_selection[i] = false;
}
console.log('The current selection is ' + current_selection);
//When the window is loaded we assign the onclick functions
$(function(){
computeWeight();
$(".gurke").click(function(){
save_selection_clicks();
computeWeight();
})
});
//Computes the weight of the current selection and sets error message if capacity is exceeded
function computeWeight(){
var table = document.getElementById("decision_table");
var rows = table.rows;
var totalWeight = 0;
var totalValue = 0;
for(var i = 2; i < (rows.length - 3); i++){
var part_id = parseInt(rows[i].getElementsByTagName("TD")[0].innerHTML);
var c_weight = parseFloat(rows[i].getElementsByTagName("TD")[1].innerHTML);
var c_value = parseFloat(rows[i].getElementsByTagName("TD")[2].innerHTML);
var selected = document.getElementById('part_' + (part_id)).checked;
totalWeight += (c_weight*selected);
totalValue += (c_value*selected);
}
$('#total_weight').html(totalWeight);
$('#total_value').html(totalValue);
try {
if(totalWeight>capacity){
$('#message').html('
Your selection weighs ' + (totalWeight-capacity) + ' lb more than you have capacity! Please select fewer items!
');
document.getElementById("Next_Button").disabled = true;
}else{
$('#message').html('
');
document.getElementById("Next_Button").disabled = false;
}
}
catch(error) {
console.error(error);
// expected output: ReferenceError: nonExistentFunction is not defined
// Note - error messages will vary depending on browser
}
}
function save_selection_clicks(){
var selection_click_history = document.getElementById("id_select_clicks").value;
var complete_click_history = document.getElementById("id_click_history").value;
var table = document.getElementById("decision_table");
var rows = table.rows;
var sorted_selection = [];
var part_id_clicked = 0;
var checked_unchecked = 'na';
for(var i = 2; i < (rows.length - 3); i++){
var part_id = parseInt(rows[i].getElementsByTagName("TD")[0].innerHTML);
var selected = document.getElementById('part_' + (part_id)).checked;
sorted_selection[part_id-1] = selected;
if(sorted_selection[part_id-1] != current_selection[part_id-1]){
part_id_clicked = part_id;
if(sorted_selection[part_id-1] == true){
checked_unchecked = 'c';
}else{
checked_unchecked = 'u';
}
}
}
// console.log('Old selection: ' + current_selection);
// console.log('New selection: ' + sorted_selection);
for(var i = 0; i < sorted_selection.length; i++){
current_selection[i] = sorted_selection[i];
}
// console.log('Old selection: ' + current_selection);
// console.log('New selection: ' + sorted_selection);
// console.log('Activity: ' + part_id_clicked + " " + checked_unchecked);
selection_click_history += part_id_clicked + checked_unchecked + '_';
complete_click_history += part_id_clicked + checked_unchecked + '_';
document.getElementById("id_select_clicks").value = selection_click_history;
document.getElementById("id_click_history").value = complete_click_history;
console.log('Selection Activity Log: ' + document.getElementById("id_select_clicks").value);
console.log('Complete Activity Log: ' + document.getElementById("id_click_history").value);
}