// When adding more checkboxes and text inputs make sure that the checkbox
// is named "name" and the text input is named "name_amount". 
var relevant_fields = ["general_use",
                       "university_scholarship_fund",
                       "marigot_hospital_fund",
                       "marigot_primary_school_fund",
                       "enterprise_development_fund",
                       "sports_development_fund",
                       "heroes_park_construction_fund"]

// If the user unchecks a checkbox the relevant text input's value is and
// the total is updated to reflect the changes.
function emptyField(current_checkbox){
	var current_checkbox = current_checkbox;
	var current_field = document.getElementsByName(current_checkbox.name + "_amount")[0];

	if(current_checkbox.checked == false){current_field.value = ""; current_field.className = "inputstyle";}
	
	updateTotal();
}

// Loop through all of the relevant checkboxes and text inputs, remove any
// beginning and ending spaces, and then append event listeners as needed.
function eventHandler(){
	if(!document.getElementsByName) return false;
	for(var i = 0; i < relevant_fields.length; i++){
		var current_checkbox = document.getElementsByName(relevant_fields[i])[0];
		var current_field = document.getElementsByName(relevant_fields[i] + "_amount")[0];
		
		current_checkbox.onclick = function(){
			emptyField(this);
		}
		
		current_field.onchange = function(){
			this.value = this.value.replace(/^\s+/, "");
			this.value = this.value.replace(/\s+$/, "");
			
			validate(this);
			updateTotal();
		}
	}
}

// Loops through all of the text inputs, converts their values to floating
// point integers and adds them up for the total.
function updateTotal(){
	var total = document.getElementsByName("total_donation")[0];
	var new_value = parseFloat(0);
	
	for(var i = 0; i < relevant_fields.length; i++){
		var current_value = document.getElementsByName(relevant_fields[i] + "_amount")[0].value;
		
		if(current_value != ""){
			current_value = current_value.replace(/[$,]/g, "");
			new_value += parseFloat(current_value);
		}
	}
	
	total.value = "$" + Math.round(new_value * 100) / 100;
}

// If an invalid value is submitted the relevant text input is highlighted,
// the value is reset, the relevant checkbox is unchecked, and the user
// is alerted. If an invalid value was set, and then corrected, the 
// highlight is removed from the relevant text input and the relevant
// checkbox is then checked. Otherwise, a valid value was submitted and the
// relevant checkbox is checked.
function validate(current_field){
	var current_field = current_field;
	var current_checkbox = document.getElementsByName(current_field.name.replace("_amount", ""))[0];
	
	if(current_field.value == "" || current_field.value.match(/[^$,.0-9]/) != null){
		current_field.className = "error";
		current_field.value = "";
		current_checkbox.checked = false;
		alert("Please enter a valid value for the highlighted fields.");
	}else{
		current_field.className = "inputstyle";
		current_checkbox.checked = true;
	}
}

window.onload = eventHandler;