giftpack = false;

window.addEvent('domready', function() {
	
	/* DISABLE THE TABBING BETWEEN FIELDS - IT BREAKS THE SLIDING */

	document.onkeypress = function(event) {
		if (event.keyCode==13) {
			return false;
		}
	}
	
	/* SET UP FORM FROM COOKIES */
	
			
		// slide 3
		
			if (val1 = notNull(Cookie.read('billing_donation'))) {$('form[billing][donation]').value = val1;}
			var billing_who = notNull(Cookie.read('billing_who'));
			if (billing_who) {
				choosePersonSwitch('billing',billing_who);
			}
			if (billing_who=='other') {
						
				if (val1 = notNull(Cookie.read('billing_email'))) {$('form[billing][email]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_name_first'))) {$('form[billing][name_first]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_name_last'))) {$('form[billing][name_last]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_address1'))) {$('form[billing][address1]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_address2'))) {$('form[billing][address2]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_town'))) {$('form[billing][town]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_county'))) {
					selectMenu('form[billing][county]',val1);
				}
				if (val1=='OTHER') {
					countyOtherToggle('billing','on');
				}
				if (val1 = notNull(Cookie.read('billing_county_extra'))) {$('form[billing][county_extra]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_postcode'))) {$('form[billing][postcode]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_country'))) {
					selectMenu('form[billing][country]',val1);
				}
				if (val1 = notNull(Cookie.read('billing_tel'))) {$('form[billing][tel]').value = val1;}
			
			} else {
				$$('.BillingName').set('disabled',true);
			}
		
		
		// slide 6
		
			var giftaid_yes = notNull(Cookie.read('giftaid_yes'));
			if (giftaid_yes) {
				blocking('giftaid','on');
				$('form[giftaid][yes]').checked = true;
			}
			
			if (val1 = notNull(Cookie.read('giftaid_name_first'))) {$('form[giftaid][name_first]').value = val1;}
			if (val1 = notNull(Cookie.read('giftaid_name_last'))) {$('form[giftaid][name_last]').value = val1;}
			if (val1 = notNull(Cookie.read('giftaid_address1'))) {$('form[giftaid][address1]').value = val1;}
			if (val1 = notNull(Cookie.read('giftaid_address2'))) {$('form[giftaid][address2]').value = val1;}
			if (val1 = notNull(Cookie.read('giftaid_town'))) {$('form[giftaid][town]').value = val1;}
			if (val1 = notNull(Cookie.read('giftaid_county'))) {
				selectMenu('form[giftaid][county]',val1);
			}
			if (val1 = notNull(Cookie.read('giftaid_postcode'))) {$('form[giftaid][postcode]').value = val1;}
				
	
	/* PREV/NEXT BUTTONS */
	
		
		$$('a.gotopayment').addEvents({
			mousedown: function(event) {
				var slideOK = checkSlide('1');
				if (slideOK) {
					document.forms['giveusyamoney'].submit();
				}
			}
		});
	
});


/* EASY SELECT */


	function selectMenu(selName,selValue) {
	
		// check the select option is correct
		if (document.forms['giveusyamoney'].elements[selName].value != selValue) {
			var thisForm = document.forms['giveusyamoney'];
			options = document.forms['giveusyamoney'].elements[selName].options;
			optionsLength = options.length;
			for (i=0;i<optionsLength;i++) {
				if (thisForm.elements[selName].options[i].value == selValue) {
					thisForm.elements[selName].options[i].selected = true;
				} else {
					thisForm.elements[selName].options[i].selected = false;
				}
			}
		}
		
	}


/* SORT OUT "COUNTY EXTRA" INPUT */

	function countyOtherCheck(slideForm) {
	
		var thisSelection = $('form['+slideForm+'][county]').value;
		
		if (thisSelection=='OTHER') {
			countyOtherToggle(slideForm,'on');
		} else {
			countyOtherToggle(slideForm,'off');
		}
	
	}

	function countyOtherToggle(slideForm,onoff) {
	
		if (onoff=='on') {
			$('form['+slideForm+'][county_extra]').setStyle('display','inline');
		} else {
			$('form['+slideForm+'][county_extra]').value = '';
			$('form['+slideForm+'][county_extra]').setStyle('display','none');
		}
	
	}
	

/* CHECK A SLIDE */

		
	function checkSlide(slide) {
	
		function checkNow(fields) {
			var valNow = new validate('giveusyamoney', fields, { 
				useAjaxSubmit:false
			});
			$each(fields, function(type, el){
				if($(el) != null){
					valNow.check($(el), type);
				}
			}.bind(this));
						if (valNow.errorinform) {
				return false;
			}
			return true;
		}
		
		// validate 1
		
		if (slide==1) {
			if (parseInt($('form[billing][donation]').value)<1) {
				alert('Sorry, you can not enter less than one pound donation');
				return false;
			}
		
			var fields = {
					'form[billing][donation]':		'Required Numeric {1-99999}',
					'form[billing][email]':			'Required Email',
					'form[billing][name_first]':	'Required',
					'form[billing][name_last]':		'Required',
					'form[billing][address1]':		'Required',
					'form[billing][town]':			'Required',
					'form[billing][county]':		'Required',
					'form[billing][postcode]':		'Required',
					'form[billing][country]':		'Required'
				};
				
			var check1 = checkNow(fields);
				
			if ($('form[giftaid][yes]').checked==true) {
				var fields2 = {
					'form[giftaid][name_first]':	'Required',
					'form[giftaid][name_last]':		'Required',
					'form[giftaid][address1]':		'Required',
					'form[giftaid][town]':			'Required',
					'form[giftaid][county]':		'Required',
					'form[giftaid][postcode]':		'Required'
				};
				var check2 = checkNow(fields2);
			} else {
				check2 = true;
			}
			
			if (check1 && check2) {
				return true;
			} else {
				alert('Sorry, you might need to check all sections of the form are completed');
				return false;
			}
		
		}
		
		return true;
	
		
	}



/* PROCESS A SLIDE */


	function processSlide(slide) {
		
		if (slide==1) {
		
			// short term cookie it
			var billing_who = $('form[billing][who]').value;
			Cookie.write('billing_who',billing_who);
			Cookie.write('billing_donation',$('form[billing][donation]').value);
		
			// save any new billing person
			if (billing_who=='other') {
			
				// enable all subsequent options to choose the billing address
				$$('.BillingName').set('disabled',false);
				if (giftpack=='No') {
					$$('.BillingNameDelivery').set('disabled',true);
				}
			
				// short term cookie it
				Cookie.write('billing_email',$('form[billing][email]').value);
				Cookie.write('billing_name_first',$('form[billing][name_first]').value);
				Cookie.write('billing_name_last',$('form[billing][name_last]').value);
				Cookie.write('billing_address1',$('form[billing][address1]').value);
				Cookie.write('billing_address2',$('form[billing][address2]').value);
				Cookie.write('billing_town',$('form[billing][town]').value);
				Cookie.write('billing_county',$('form[billing][county]').value);
				Cookie.write('billing_county_extra',$('form[billing][county_extra]').value);
				Cookie.write('billing_postcode',$('form[billing][postcode]').value);
				Cookie.write('billing_country',$('form[billing][country]').value);
				Cookie.write('billing_tel',$('form[billing][tel]').value);
			
				// tweak the variable of who we've picked to use in further routines
				billing_who = 'billing';
				
			}
			
			// short term cookie it
			
			Cookie.write('giftaid_yes',$('form[giftaid][yes]').value);
			Cookie.write('giftaid_name_first',$('form[giftaid][name_first]').value);
			Cookie.write('giftaid_name_last',$('form[giftaid][name_last]').value);
			Cookie.write('giftaid_address1',$('form[giftaid][address1]').value);
			Cookie.write('giftaid_address2',$('form[giftaid][address2]').value);
			Cookie.write('giftaid_town',$('form[giftaid][town]').value);
			Cookie.write('giftaid_county',$('form[giftaid][county]').value);
			Cookie.write('giftaid_postcode',$('form[giftaid][postcode]').value);
				
		
		}
		
	}



/* BLOCKING */

	function blocking(cssid,offon) {
	
		el = $(cssid);
		
		if (!offon) {
			offon = 'toggle';
		}
	
		var currentDisplay = el.getStyle('display');
		if (currentDisplay=='none') {
			el.setStyle('display','block');
		}
	
		if (offon=='on' || (offon=='toggle' && currentDisplay=='none')) {
		
			// set up the giftaid form (if it's different from the person who's previously marked it as giftaid)
			
			$('form[giftaid][name_first]').value = $('form[billing][name_first]').value;
			$('form[giftaid][name_last]').value = $('form[billing][name_last]').value;
			$('form[giftaid][address1]').value = $('form[billing][address1]').value;
			$('form[giftaid][address2]').value = $('form[billing][address2]').value;
			$('form[giftaid][town]').value = $('form[billing][town]').value;
			selectMenu('form[giftaid][county]',$('form[billing][county]').value);
			$('form[giftaid][postcode]').value = $('form[billing][postcode]').value;
						
			var blocking1 = new Fx.Morph(el,{ 'duration':'400' });
			blocking1.start({ 'opacity':1 });
		} else {
			var blocking2 = new Fx.Morph(el,{ 'duration':'400' });
			blocking2.start({ 'opacity':0 }).chain(function() {
				el.setStyle('display','none');
			});
		}
	
	}
	
/* IE COOKIE FIX */

	function notNull(value) {
		
		if (value=='null' || value==null) {
			value='';
		}
		return value;
		
	}