window.addEvent('domready', function() {
/* Checks for zero values on the form.
 * Sets containing divs with label 'hide' to hidden.
 * Good when CSS is not available.
 */
	var zeroDenom = $$('div[class^=hide]');
	
		zeroDenom.each(function(zeroed, i){
			zeroed.setStyles('display: none;');
		});
/* Variable initialization section
 * TODO: Clean up variables, make code nicer
 */
	var givingForm = $('gift_form');
	var givingFormInputs = $$('form#gift_form input');
	var otherDenom = $$('input[id^=gift_other_]');
	var otherDenomValue = $$('input[id^=gift_value_]');
	var totalBox = $('gift_total');
	var regDollar = /(^[0-9]+$|^[0-9]+\.[0-9]{1,2}$|^[0-9]+\.$)/;
	var regDollarInput = /(^[0-9]$|^\.$)/;
	var funds = givingForm.getElements('input[name^=FUND]');
	var fundnames = new Array;
	var errorMsg = new Array;
	var goodNum = new Array;
/* Goes through and caches all text-field fundnames (ones user can input into)
 * BUG: Back and Forward need to work properly (instead of caching old values on "back" or "forward," values should reset
 */
	funds.each(function(other, i){
		if(other.getProperty('type') == 'text') {
			fundnames[i] = other.value;
			other.addEvents({
				'focus' : function() {
					if(other.value==fundnames[i]){
							other.value = '';
					}
				},
				'blur' : function() {
					if(other.value == '') {
						other.value = fundnames[i];	
					}
				}
			});
		}
	});
/* Custom Denomination Field Validator
 * Checks whether custom giving amount is a valid number,
 * Sets radio to "checked"
 * Returns former value OR null if input is incorrect
 */
	otherDenomValue.each(function(other, i){
		if(other.value != 0.00) {
			otherDenom[i].setProperty('value', other.value);
		}
		other.addEvents({
			'focus' : function() {
				other.fireEvent('changeval');
			},
			'keypress' : function(e) {
				var event = new Event(e);
				var keynum;
				if(window.event) {
					keynum = e.keyCode;	
				}
				else if (e.which) {
					keynum = e.which;	
				}
					keyChar = String.fromCharCode(keynum);
					other.fireEvent('changeval');
			},
			'click' : function(e) {
				other.fireEvent('changeval');
			},
			'keyup' : function(e) {
				other.fireEvent('changeval');
			},
			'changeval' : function() {
					if(!other.value.test(regDollar)) {
						if(goodNum[i]&&other.value!=""){
							other.value = goodNum[i];
							otherDenom[i].setProperty('checked', 'checked');
						}
						else{
							other.value='';	
						}
					}
					if(other.value!=""){
						goodNum[i]= other.value;
						otherDenom[i].setProperty('value', other.value);
						otherDenom[i].setProperty('checked', 'checked');
					}
					else {
						otherDenom[i].setProperty('value', 0.00);
					}
			}
		});
	});
/* Giving Form Functionality
 * Updates total and checks for blank fields
 */
	givingForm.addEvents({
			'click' : function() {
				givingForm.fireEvent('updateTotal');
			},
			'keyup' : function () {
				givingForm.fireEvent('updateTotal');
			},
			'submit' : function (e) {
				var event = new Event(e);
				var fundamounts = new Array;
				var checkvalues = givingForm.getElements('input[name^=GIFT_AMOUNT]');
				checkvalues.each(function(other, i) {
					var test = other.getValue();
					if(test){
						fundamounts.push(test);
					}
				});				 
				funds.each(function(other, i){
					if(other.getProperty('type') == 'text') {
						if((fundnames[i]==other.value||other.value=='')&&(fundamounts[i]!=0)) {
							if(errorMsg[i] == undefined) {
							errorMsg[i] = new Element('div').injectAfter(other).addClass('error').setProperty('title', 'giving_error' +i).setText("Please insert all values");
							}
							event.stop();
						}
					}
				});
			},
			'updateTotal' : function () {
				totalBox.value = 0.00;
				givingFormInputs.each(function(add, i){
					if((add.getProperty('type') == 'radio')&&(add.checked)&&(!isNaN(add.value))) {
						var totalBoxtype = parseFloat(totalBox.value) + parseFloat(add.value);
						totalBoxtype = parseFloat(totalBoxtype).toFixed(2);
						totalBox.value = totalBoxtype;
					}
				});
			}
	});
});