var num_options = 1; // The number of "Other" commitments fields
function add_commitment_option() {
	var option_num = num_options++;
	var input_total_el_name = 'commitment_total_other_' + option_num;
	var input_el_name = 'commitment_other_' + option_num;
	var li_el_name = 'commitment_' + option_num;
		
	new Insertion.Bottom('other_commitments',
		('<li id="' + li_el_name +'"><label for="' + input_el_name + '">Other</label>: <input id="' + input_total_el_name + '" name="commitment_total_other_' + option_num + '" type="text" value="0" size="6" onblur="tally_if_valid();" />&nbsp;I will&nbsp;<input id="' + input_el_name + '" name="commitment_other_' + option_num + '" type="text" value="" size="55" maxlength="200" style="" />&nbsp;<a href="#" class="small_button" title="Add a commitment." onclick="add_commitment_option(); return false;">+</a>&nbsp;<a href="#" class="small_button" title="Remove this commitment." onclick="remove_commitment_option(\'' + li_el_name +'\'); tally_if_valid(); return false;">&ndash;</a></li>')
		);
		new Effect.Highlight(input_el_name);
		$(input_total_el_name).focus();
}


function remove_commitment_option( el_id ) {
  $(el_id).parentNode.removeChild($(el_id));
}




var total_commitments = 0;
function tally_commitments() {
	var num_commitments = 0;
	var list_items = $('commitments').getElementsByTagName('input');
	var items = $A(list_items);
	items.each(function(item) {
		if (item.type == 'checkbox') {
			if (item.checked) {
				num_commitments++;
			}
		} else if (item.type == 'text') {
			num_commitments += parseInt($F(item));
		}
	});
	
	var other_items = $('other_commitments');
	if (other_items) {
		var list_items = other_items.getElementsByTagName('input');
		var items = $A(list_items);
		items.each(function(item) {
			
				if (item.name.indexOf('commitment_total_other') == 0) { // Starts with 'commitment_total_other'
					if ($F(item) != '') {
						num_commitments += parseInt($F(item));
					}
				}
			
		});
	}
	
	
	if (num_commitments != total_commitments) {
		var displayEl = $('totalCommitments');
		displayEl.innerHTML = num_commitments;
		var displayLineEl = $('totals')
		new Effect.Highlight(displayLineEl, {queue: {position:'end', scope: 'total_commitments_scope', limit:2}});
	}
	
	total_commitments = num_commitments;
}



function tally_if_valid() {
	clear_error_messages();
	if (!validate_totals()) { return; }
	tally_commitments();
}


function validate_totals() {
	var has_errors = false;
	
	var list_items = $('commitments').getElementsByTagName('input');
	var items = $A(list_items);
	items.each(function(item) {
		if (item.type == 'text') {
			if (!check_is_integer(item)) {
				item.addClassName('error');
				has_errors = true;
				has_bad_quantity = true;
			} else {
				item.removeClassName('error');
			}
		}
	});
	
	
	// others
	var list_items = $('other_commitments').getElementsByTagName('input');
	var items = $A(list_items);
	for (i = 0; i < items.length; i++) {
		var item = items[i];
			
		if (item.name.indexOf('commitment_total_other') == 0) { // Starts with 'commitment_total_other'
		
			if (check_is_integer(item)) {
				item.removeClassName('error');
			} else { // It's not an integer
				item.addClassName('error');
				has_errors = true;
			} // end check_is_integer
			
		} 
	} // end for
	
	if (has_errors) {
		add_error_message("<p>Please enter only numbers in the quantity fields (e.g., 3).</p>");
		new Effect.ScrollTo('form_errors', {offset: -24});
		return false;
	} // end if has errors
	
	return true;
}


function validate_others() {
	var has_errors = false;
	
	// others
	var list_items = $('other_commitments').getElementsByTagName('input');
	var items = $A(list_items);
	for (i = 0; i < items.length; i++) {
		var item = items[i];
			
		if (item.name.indexOf('commitment_other') == 0) { // Starts with 'commitment_total_other'
				var suffix = item.name.substr(16);
				var amt_field_name = 'commitment_total_other' + suffix;
				var amt = parseInt($F(amt_field_name));
				
				// If the amount field is not empty, check that the commitment description field is not empty.
				if ($F(amt_field_name) != null && (check_is_integer(amt_field_name) && amt > 0)) {
					if ($F(item) === '')  {
						has_errors = true;
						item.addClassName('error');
					} else {
						item.removeClassName('error');
					}
				}
			
		} // end if commitment_other
	} // end for
	
	
	if (has_errors) {
		add_error_message("<p>Please enter a description for any 'Other' commitments you want to make.</p>");
		new Effect.ScrollTo('form_errors', {offset: -24});
		return false;
	} // end if has errors
	
	return true;
}



function validate_form() {
	clear_error_messages();
	
	var has_errors = false;
	
	if (!validate_totals()) { 
		return false;
	}
	
	if (!validate_others()) {
		return false;
	}
	
	tally_commitments();
	
	if (total_commitments == 0) {
		has_errors = true;
		add_error_message("<p>You did not choose any GEM commitments.  Please choose at least one commitment before submitting the form.</p>");
	}
	
	if (has_errors) {
		new Effect.ScrollTo('form_errors', {offset: -24});
		return false;
	}
	return true;
}

