/*
 * *****************************************************************
 * Name:		taxman_ws.js
 * Description:	This javascript handles the sending of the taxman
 *				form content to the php script (taxman_ws.php)
 *				on the server, and displays the results in the 
 *				taxman_results div.
 *
 * Requires:	MooTools 1.2.1
 * 
 * Revision History:
 * -----------------
 * Version 1.0, 20090215, Initial build.
 * 
 * *****************************************************************
 */

// Setup the event for the calculate button
window.addEvent('domready', function() {

	$('taxman_calculator').addEvent('submit', function(e) {
		// Prevent the default submit event firing and loading the new page
		e.stop();

		// Check for value in gross wage
		var taxman_grosswage = $('taxman_grosswage');
		if (taxman_grosswage.value == '0'  || taxman_grosswage.value.length < 1){
			alert("Please enter a gross wage");
			return(false);
		} else {
			if (!taxman_IsNumeric(taxman_grosswage.value)){
				alert("Please enter a numeric value for the gross wage");
				reutnr(false);
			}
		}

		// Check for numeric extra
		var taxman_extra = $('taxman_extra');
		if (taxman_extra.length != 0 && !taxman_IsNumeric(taxman_extra.value)){
			alert("Please enter a numeric value for Other allowances/deducations");
			return(false);
		}
		
		// Check for numeric extra
		var taxman_pension = $('taxman_pension');
		if (taxman_pension.length != 0 && !taxman_IsNumeric(taxman_pension.value)){
			alert("Please enter a numeric value for pension (pecentage are allowed)");
			return(false);
		}
		
		// Empty the results div and show the ajax loading indicator
		var taxman_divElement = $('taxman_results').empty().addClass('ajax-loading');
		
		// Set the options of the form's request handler
		this.set('send', {onComplete: function(responseHTML) {
			taxman_divElement.removeClass('ajax-loading');
			taxman_divElement.set('html', responseHTML);
		}});
		
		// Send the form data to the taxman_ws.php web service
		this.send();
	}); 
});

//  check for valid numeric strings	
function taxman_IsNumeric(taxman_string){
	var taxman_validChars = "0123456789.-,%";
	var taxman_char;
	var taxman_result = true;

	for (i = 0; i < taxman_string.length && taxman_result == true; i++){
		taxman_char = taxman_string.charAt(i);
		if (taxman_validChars.indexOf(taxman_char) == -1){
        	taxman_result = false;
		}
	}
	   	
	return taxman_result;
}
