// initialize
$(document).ready(function() 
{	
	$("#submit-donation").click( onSubmitClick );
	$("#currency-amount-input").keyup( onAmountChange );
	$("#currency-amount-input").click( onAmountClick );
	$("#mutableAmount").click( onMutableAmountClick );
	$("#phone1").keyup( onPhoneChange );
	$("#phone2").keyup( onPhoneChange );
	$("#phone3").keyup( onPhoneChange );
});


/** ============= PROPERTIES ============== */

var _failedFormElements;

/** ============= EVENT HANDLERS ============== */

function onSubmitClick( evt )
{
	evt.preventDefault();
	formIsValid() ? submitForm() : failForm();
	evt.stop();
}

function onAmountChange( evt ) 
{
	var value = $(this).val();
	$(this).val( value.replace( /[^0-9\.]/g, '' ) );
//	if ( $(this).val() == '' ) $(this).val( '50.00' );
	if( $( "input[@name='_AMOUNT']:checked").attr('id') == 'mutableAmount' ) $( "input[@name='_AMOUNT']:checked").val( $(this).val() );	
}

function onAmountClick( evt )
{
	this.form.AMOUNT[4].checked=true;
	$( "input[@name='_AMOUNT']:checked" ).val( $(this).val() );	
}

function onMutableAmountClick ( evt ) {
	$( '#customAmount' ).val( '50.00' );
}

function onPhoneChange( evt ) 
{
	var value = $(this).val();
	$(this).val( value.replace( /[^0-9]/g, '' ) );	
}


/** ============== FORM VALIDATION ============== */

function formIsValid()
{
	prepareForm();
	
	var isValid = false;
	_failedFormElements = [];

	// check required fields
	if( $( "select[name='_DESCRIPTION']").val() == 'Other' )
	{ 
	isValid = !formElementIsEmpty( $("input[name='USER4']") );
	}
	else
	{ 
	isValid = !formElementIsEmpty( $( "select[name='_DESCRIPTION']") );
	}
	isValid = !formElementIsEmpty( $( "input[@name='AMOUNT']:checked") );
	isValid = !formElementIsEmpty( $( "input[name='NAME']") );
	isValid = !formElementIsEmpty( $( "input[name='ADDRESS']") );
	isValid = !formElementIsEmpty( $( "input[name='CITY']") );
	isValid = !formElementIsEmpty( $( "select[name='STATE']") );
	isValid = !formElementIsEmpty( $( "input[name='ZIP']") );
	isValid = !formElementIsEmpty( $( "input[name='PHONE']") );
	isValid = !formElementIsEmpty( $( "input[name='EMAIL']") );	
		
//	alert('Form is valid: ' + ( _failedFormElements.length == 0 ) );
		
	return ( _failedFormElements.length == 0 );
}

function prepareForm() 
{	
	// required, complex
	$( "input[name='PHONE']").val( '('+$( "input[name='phone1']").val() + ') ' + $( "input[name='phone2']").val() + ' ' + $( "input[name='phone3']").val() );
	$( "input[name='ADDRESS']").val( $( "input[name='address1']").val() + ' ' + $( "input[name='address2']").val() );
	
	// optional, complex
	var inHonorOfName = $("input[name='Custom_112']").val().split(' ').join('');
	if( inHonorOfName != '') $( "input[name='USER1']").val( $("input[name='Custom_112']").val() + '|' + $( "input[name='Custom_114_st1']").val() + '|' + $( "input[name='Custom_114_st2']").val()  + '|' + $( "input[name='Custom_114_city']").val()  + '|' + $( "select[name='Custom_114_state']").val()  + '|' + $( "input[name='Custom_114_zip']").val() );
	
	var inMemoryOfName = $( "input[name='Custom_116']").val().split(' ').join('');
	if( inMemoryOfName != '') $( "input[name='USER2']").val( $( "input[name='Custom_116']").val() + '|' + $( "input[name='Custom_119_st1']").val() + '|' + $( "input[name='Custom_119_st2']").val()  + '|' + $( "input[name='Custom_119_city']").val()  + '|' + $( "select[name='Custom_119_state']").val()  + '|' + $( "input[name='Custom_119_zip']").val() );
}

function formElementIsEmpty( element ) 
{
	// remove spaces
	var val = $(element).val().split(' ').join('');
//	alert( 'checking: '+$(element).attr('name')+'\n\nvalue given: ---]'+val +'[---' );//\n\nelement type of: '+typeof element );
	if( val == '' || val == 'undefined' || val == '()' )
	{
		_failedFormElements.push( element );
		return true;
	}
	else return false;
}

/**
 *  once the form is validated, finalize the form data
 */
function completeForm() 
{
	// consolidate fund name/paypal purchase description
	if( $( "select[name='_DESCRIPTION']").val() == 'Other' )
	{ 
		$( "input[name='DESCRIPTION']").val( $("input[name='USER4']").val() );	
	}
	else
	{ 
		$( "input[name='DESCRIPTION']").val( $( "select[name='_DESCRIPTION']").val() );
	}
	
	// assign AMOUNT value from _AMOUNT radio buttons
	$( "input[name='AMOUNT']").val( $( "input[@name='_AMOUNT']:checked" ).val() );
	
//	alert('Final form values:\n\nDESCRIPTION: ' + $( "input[name='DESCRIPTION']").val() + '\nAMOUNT: ' + $( "input[name='AMOUNT']").val() );
}


/** =============== FORM SUBMIT/FAILURE ================ */

function failForm () {

	var failMessage = 'In order to complete your donation, please enter the following information:\n\n';
	
	$.each( _failedFormElements, function(n,val)
	{
		failMessage += '- '+$(val).attr('failmsg')+'\n';
	});
	
	alert( failMessage );
}

function submitForm()
{
	completeForm();
	
	var f = $('#donation-form');
	f.submit();	
}	
