window.addEvent('domready', function() {


	/* DISABLE THE TABBING BETWEEN FIELDS - IT BREAKS THE SLIDING */

	document.onkeypress = function(event) {
		var keyCode = (window.event) ? window.event.which : event.keyCode;
		if (keyCode == 13) {
			return false;
		}
	}
	
	function KeyTest() {
		if (event.keyCode==9) {
			return false;
		}
	}
	$$('input.notab').addEvents({
		keydown: function(event) {
			return KeyTest()
		}
	})
	$$('select.notab').addEvents({
		keydown: function(event) {
			return KeyTest()
		}
	})
	
	/* SET UP FORM FROM COOKIES */ 
	
		// slide 2
		
			if (val1 = notNull(Cookie.read('friend1_email'))) {$('form[friend1][email]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_name_first'))) {$('form[friend1][name_first]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_name_last'))) {$('form[friend1][name_last]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_address1'))) {$('form[friend1][address1]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_address2'))) {$('form[friend1][address2]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_town'))) {$('form[friend1][town]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_county'))) {$('form[friend1][county]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_county_uk'))) {
				selectMenu('form[friend1][county_uk]',val1);
			}
			if (val1 = notNull(Cookie.read('friend1_county_usa'))) {
				selectMenu('form[friend1][county_usa]',val1);
			}
			if (val1 = notNull(Cookie.read('friend1_postcode'))) {$('form[friend1][postcode]').value = val1;}
			if (val1 = notNull(Cookie.read('friend1_country'))) {
				selectMenu('form[friend1][country]',val1);
			}
			checkCounty($('form[friend1][country]'));
			if (val1 = notNull(Cookie.read('friend1_tel'))) {$('form[friend1][tel]').value = val1;}
			
		// 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) {
				if (billing_who=='adoptee' || renewal_who=='friend2') {
					billing_who = 'friend1';
				}
				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'))) {$('form[billing][county]').value = val1;}
				if (val1 = notNull(Cookie.read('billing_county_uk'))) {
					selectMenu('form[billing][county_uk]',val1);
				}
				if (val1 = notNull(Cookie.read('billing_county_usa'))) {
					selectMenu('form[billing][county_usa]',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);
				}
				checkCounty($('form[billing][country]'));
				if (val1 = notNull(Cookie.read('billing_tel'))) {$('form[billing][tel]').value = val1;}
			
			} else {
				$$('.BillingName').set('disabled',true);
			}
			
			// shipping
			setShipping();
		
		// slide 4
		
			var renewal_who = notNull(Cookie.read('renewal_who'));
			if (renewal_who) {
				if (renewal_who=='adoptee' || renewal_who=='friend2') {
					renewal_who = 'friend1';
				}
				choosePersonSwitch('renewal',renewal_who);
			}
			
		// slide 5
		
			var delivery_who = notNull(Cookie.read('delivery_who'));
			if (delivery_who) {
				if (delivery_who=='adoptee' || delivery_who=='none' || delivery_who=='friend2') {
					delivery_who = 'friend1';
				}
				choosePersonSwitch('delivery',delivery_who);
			}
			if (delivery_who=='other') {
			
				if (val1 = notNull(Cookie.read('delivery_email'))) {$('form[delivery][email]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_name_first'))) {$('form[delivery][name_first]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_name_last'))) {$('form[delivery][name_last]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_address1'))) {$('form[delivery][address1]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_address2'))) {$('form[delivery][address2]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_town'))) {$('form[delivery][town]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_county'))) {$('form[delivery][county]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_county_uk'))) {
					selectMenu('form[delivery][county_uk]',val1);
				}
				if (val1 = notNull(Cookie.read('delivery_county_usa'))) {
					selectMenu('form[delivery][county_usa]',val1);
				}
				if (val1 = notNull(Cookie.read('delivery_postcode'))) {$('form[delivery][postcode]').value = val1;}
				if (val1 = notNull(Cookie.read('delivery_country'))) {
					selectMenu('form[delivery][country]',val1);
				}
				checkCounty($('form[delivery][country]'));
				if (val1 = notNull(Cookie.read('delivery_tel'))) {$('form[delivery][tel]').value = val1;}
			}
		
		// 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;}
			if (val1 = notNull(Cookie.read('giftaid_country'))) {$('form[giftaid][country]').value = val1;}
				
	
	/* PREV/NEXT BUTTONS */
	
		$$('a.prev').addEvents({
			mousedown: function(event) {
				var old_margin = $('slide_track').getStyle('margin-left');
				old_margin = old_margin.replace('px','');
				var slide = ((0-parseInt(old_margin))/500)+1;
				new_margin = parseInt(old_margin)+500;
				if (new_margin>0) {
					new_margin = 0;
				}
				//alert(old_margin+' : '+new_margin);
				eek = slider(old_margin,new_margin);
				eek = processSlide(slide);
			}
		});
		
		$$('a.next').addEvents({
			mousedown: function(event) {
				var old_margin = $('slide_track').getStyle('margin-left');
				old_margin = old_margin.replace('px','');
				var slide = ((0-parseInt(old_margin))/500)+1;
				
				// if the slide checks out, go to next stage
				var slideOK = checkSlide(slide);
				if (slideOK) {
					new_margin = parseInt(old_margin)-500;
					//alert(old_margin+' : '+new_margin);
					eek = slider(old_margin,new_margin);
					eek = processSlide(slide);
				} else {
					alert('Sorry, you might need to check all sections of the form are completed');
				}
			}
		});
		
		$$('a.gotopayment').addEvents({
			mousedown: function(event) {
				var slideOK = checkSlide('6');
				setTimeout((function(){				
					if (slideOK) {
						var bob = setTimeout('document.forms[\'giveusyamoney\'].submit();','100');
					} else {
						alert('Sorry, you might need to check all sections of the form are completed');
					}
				}),100);
				
			}
		});
	
	/* STEP 1,2,3 BUTTONS */
	
		var stepsCont = $$('.steps');
		var stepslength = stepsCont.length;
		
		for (i=0;i<stepslength;i++) {
			stepsLinks = stepsCont[i].getChildren('a');
			stepsLinks.addEvents({
				mousedown: function(event) {
					event.stop();
					var old_margin = $('slide_track').getStyle('margin-left');
					old_margin = old_margin.replace('px','');
					var thisSlide = this.getProperty('href');
					thisSlide = thisSlide.replace('#slide','');
					new_margin = 0-(thisSlide-1)*500
					slider(old_margin,new_margin);
				}
			})
		};
	
});


/* 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 */
	


	// is the extra county input box needed?
	
	function countyOption (formObject,togglewhich) {
	
		var orig = formObject.id;
		
		var countyField = orig.replace('][','||');
		var countyField = countyField.replace('country','county');
		var countyField = countyField.replace('||','][');
		
		// the "UK" field
		var countyUkField = orig.replace('][','||');
		var countyUkField = countyUkField.replace('country','county_uk');
		var countyUkField = countyUkField.replace('||','][');
		
		// the "USA" field
		var countyUsaField = orig.replace('][','||');
		var countyUsaField = countyUsaField.replace('country','county_usa');
		var countyUsaField = countyUsaField.replace('||','][');
		
		// the "n/a" field
		var countyNaField = orig.replace('][','||');
		var countyNaField = countyNaField.replace('country','county_na');
		var countyNaField = countyNaField.replace('||','][');
		
		//console.log(extraid0);
		//console.log(extraid1);
		//console.log(extraid2);
		
		if (togglewhich=='GBR') {
			$(countyField).setStyle('display','none');
			$(countyUkField).setStyle('display','block');
			$(countyUsaField).setStyle('display','none');
			$(countyNaField).setStyle('display','none');
			/*if (!window.ie) { // weird bug where IE6/7 won't focus on the item because it thinks it's invisible - maybe?
				$(countyField).focus();
			}*/
			
		} else if (togglewhich=='USA') {
			$(countyField).setStyle('display','none');
			$(countyUkField).setStyle('display','none');
			$(countyUsaField).setStyle('display','block');
			$(countyNaField).setStyle('display','none');
			if (!window.ie) { // weird bug where IE6/7 won't focus on the item because it thinks it's invisible - maybe?
				$(countyField).focus();
			}
			
		} else {
		
			// is this country is in the list of countries that has no county
			if (nocounties != 'undefined') {
				var length = nocounties.length;
				for(var i = 0; i < length; i++) {
					if(nocounties[i] == togglewhich) {					
						$(countyField).setStyle('display','none');
						$(countyUkField).setStyle('display','none');
						$(countyUsaField).setStyle('display','none');
						$(countyNaField).setStyle('display','block');
						return;
					}
				}
			}	
			
			$(countyField).setStyle('display','block');
			$(countyUkField).setStyle('display','none');
			$(countyUsaField).setStyle('display','none');
			$(countyNaField).setStyle('display','none');
		
		}
	
	}
	
	
	function checkCounty(formObject) {
		
		var orig = formObject.id;
		var countyUkField = orig.replace('][','||');
		countyUkField = countyUkField.replace('country','county_uk');
		countyUkField = countyUkField.replace('||','][');
		var countyUsaField = orig.replace('][','||');
		countyUsaField = countyUsaField.replace('country','county_usa');
		countyUsaField = countyUsaField.replace('||','][');
		var countyField = orig.replace('][','||');
		countyField = countyField.replace('country','county');
		countyField = countyField.replace('||','][');
		
		// open the correct county menu
		if (formObject.value=='GBR') {
			countyOption (formObject,formObject.value);
			
			// reset the county based on the menu
			$(countyField).value = $(countyUkField).value;
		
		} else if (formObject.value=='USA') {
			countyOption (formObject,formObject.value);
			
			// reset the county based on the menu
			$(countyField).value = $(countyUsaField).value;
			
		} else {
			$(countyField).value = '';
			countyOption (formObject,formObject.value);
		}
	
	}
	
	function setCounty(role,formObject) {
	
		$('form[' + role + '][county]').value = formObject.value;
		//alert($('form[' + role + '][county]').value);
	
	}
	
			

/* SLIDE ANIMATION */
			
	function jumpToSlide(slide) {
		var old_margin = $('slide_track').getStyle('margin-left');
		old_margin = old_margin.replace('px','');
		new_margin = 0-(slide-1)*500
		slider(old_margin,new_margin);
	}
	
	function slider(old_margin,new_margin) {
		var myFx = new Fx.Tween($('slide_track'));
		myFx.start('margin-left', old_margin, new_margin);
	}
	

/* 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 2
		
		if (slide==2) {
			var fields = {
				'form[friend1][email]':			'Required Email',
				'form[friend1][name_first]':	'Required',
				'form[friend1][name_last]':		'Required',
				'form[friend1][address1]':		'Required',
				'form[friend1][town]':			'Required',
				'form[friend1][county]':		'Required',
				'form[friend1][postcode]':		'Required',
				'form[friend1][country]':		'Required'
			};
			if ($('form[friend1][country]').value=='GBR') {	
				delete fields['form[friend1][county]'];
				fields['form[friend1][county_uk]'] = 'Required';
			} else if ($('form[friend1][country]').value=='USA') {
				delete fields['form[friend1][county]'];
				fields['form[friend1][county_usa]'] = 'Required';
			// is this country is in the list of countries that has no county
			} else if (nocounties != 'undefined') {
				var needle = $('form[friend1][country]').value;
				//console.log(needle);
				var length = nocounties.length;
				for(var i = 0; i < length; i++) {
					if(nocounties[i] == needle) {					
						delete fields['form[friend1][county]'];
					}
				}
			}
			return checkNow(fields);
			
		// validate 3
		
		} else if (slide==3) {
			if ($('form[billing][who]').value=='other') {
				var fields = {
					'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',
					'form[billing][donation]':		'Numeric'
				};
				if ($('form[billing][country]').value=='GBR') {	
					delete fields['form[billing][county]'];
					fields['form[billing][county_uk]'] = 'Required';
				} else if ($('form[billing][country]').value=='USA') {
					delete fields['form[billing][county]'];
					fields['form[billing][county_usa]'] = 'Required';
				// is this country is in the list of countries that has no county
				} else if (nocounties != 'undefined') {
					var needle = $('form[billing][country]').value;
					//console.log(needle);
					var length = nocounties.length;
					for(var i = 0; i < length; i++) {
						if(nocounties[i] == needle) {					
							delete fields['form[billing][county]'];
						}
					}
				}
			} else {
				var fields = {
					'form[billing][who]':			'Required',
					'form[billing][donation]':		'Numeric'
				};
			}
			return checkNow(fields);
			
		// validate 4
		
		} else if (slide==4) {
			var fields = {
				'form[renewal][who]':			'Required',
				'form[billing][donation]':		'Numeric'
			};
			return checkNow(fields);
			
		// validate 5
		
		} else if (slide==5) {
			if ($('form[delivery][who]').value=='other') {
				var fields = {
					'form[delivery][email]':			'Required Email',
					'form[delivery][name_first]':	'Required',
					'form[delivery][name_last]':		'Required',
					'form[delivery][address1]':		'Required',
					'form[delivery][town]':			'Required',
					'form[delivery][county]':		'Required',
					'form[delivery][postcode]':		'Required',
					'form[delivery][country]':		'Required'
				};
				if ($('form[delivery][country]').value=='GBR') {	
					delete fields['form[delivery][county]'];
					fields['form[delivery][county_uk]'] = 'Required';
				} else if ($('form[delivery][country]').value=='USA') {
					delete fields['form[delivery][county]'];
					fields['form[delivery][county_usa]'] = 'Required';
				// is this country is in the list of countries that has no county
				} else if (nocounties != 'undefined') {
					var needle = $('form[delivery][country]').value;
					//console.log(needle);
					var length = nocounties.length;
					for(var i = 0; i < length; i++) {
						if(nocounties[i] == needle) {					
							delete fields['form[delivery][county]'];
						}
					}
				}
			} else {
				var fields = {
					'form[delivery][who]':			'Required'
				};
			}
			return checkNow(fields);
			
		// validate 6
		
		} else if (slide==6) {
			if ($('form[giftaid][yes]').checked==true) {
				var fields = {
					'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',
					'form[giftaid][country]':		'Required'
				};
				return checkNow(fields);
			}
			
		}
		
		return true;
	
		
	}



/* PROCESS A SLIDE */


	function processSlide(slide) {
		
		if (slide==2) {
			
			// short term cookie it
			Cookie.write('friend1_email',$('form[friend1][email]').value,{ 'path' : '/' });
			Cookie.write('friend1_name_first',$('form[friend1][name_first]').value,{ 'path' : '/' });
			Cookie.write('friend1_name_last',$('form[friend1][name_last]').value,{ 'path' : '/' });
			Cookie.write('friend1_address1',$('form[friend1][address1]').value,{ 'path' : '/' });
			Cookie.write('friend1_address2',$('form[friend1][address2]').value,{ 'path' : '/' });
			Cookie.write('friend1_town',$('form[friend1][town]').value,{ 'path' : '/' });
			Cookie.write('friend1_county',$('form[friend1][county]').value,{ 'path' : '/' });
			Cookie.write('friend1_county_uk',$('form[friend1][county_uk]').value,{ 'path' : '/' });
			Cookie.write('friend1_county_usa',$('form[friend1][county_usa]').value,{ 'path' : '/' });
			Cookie.write('friend1_postcode',$('form[friend1][postcode]').value,{ 'path' : '/' });
			Cookie.write('friend1_country',$('form[friend1][country]').value,{ 'path' : '/' });
			Cookie.write('friend1_tel',$('form[friend1][tel]').value,{ 'path' : '/' });
	
			// set up all instances of this name
			var appending = ' ' + $('form[friend1][name_first]').value;
			if ($$('.friend1Name').get('text')[0].indexOf(appending)<0) {
				$$('.friend1Name').appendText(appending);
			}	
	
			// pre fill the billing form
			if ($('form[billing][who]').value=='friend1') {
				$('form[billing][address1]').value = $('form[friend1][address1]').value;
					$('billing_address1').set('text',$('form[friend1][address1]').value);
				$('form[billing][address2]').value = $('form[friend1][address2]').value;
					$('billing_address2').set('text',$('form[friend1][address2]').value);
				$('form[billing][town]').value = $('form[friend1][town]').value;
					$('billing_town').set('text',$('form[friend1][town]').value);
				
				$('form[billing][county]').value = $('form[friend1][county]').value;
					$('billing_country').set('text',$('form[friend1][country]').value);
				selectMenu('form[billing][county_uk]',$('form[friend1][county]').value);
				selectMenu('form[billing][county_usa]',$('form[friend1][county]').value);
					$('billing_county').set('text',$('form[friend1][county]').value);
					
				$('form[billing][postcode]').value = $('form[friend1][postcode]').value;
					$('billing_postcode').set('text',$('form[friend1][postcode]').value);
				selectMenu('form[billing][country]',$('form[friend1][country]').value);
					$('billing_country').set('text',$('form[friend1][country]').value);
				checkCounty($('form[billing][country]'));
				$('form[billing][tel]').value = $('form[friend1][tel]').value;
					$('billing_tel').set('text',$('form[friend1][tel]').value);
			}
	
			// pre fill the renewal form
			if ($('form[renewal][who]').value=='friend1') {
					$('renewal_address1').set('text',$('form[friend1][address1]').value);
					$('renewal_address2').set('text',$('form[friend1][address2]').value);
					$('renewal_town').set('text',$('form[friend1][town]').value);
				
					$('renewal_country').set('text',$('form[friend1][country]').value);
					$('renewal_county').set('text',$('form[friend1][county]').value);
					
					$('renewal_postcode').set('text',$('form[friend1][postcode]').value);
					$('renewal_country').set('text',$('form[friend1][country]').value);
					$('renewal_tel').set('text',$('form[friend1][tel]').value);
			}
	
			// pre fill the delivery form
			if ($('form[delivery][who]').value=='friend1') {
				$('form[delivery][address1]').value = $('form[friend1][address1]').value;
					$('delivery_address1').set('text',$('form[friend1][address1]').value);
				$('form[delivery][address2]').value = $('form[friend1][address2]').value;
					$('delivery_address2').set('text',$('form[friend1][address2]').value);
				$('form[delivery][town]').value = $('form[friend1][town]').value;
					$('delivery_town').set('text',$('form[friend1][town]').value);
				
				$('form[delivery][county]').value = $('form[friend1][county]').value;
					$('delivery_country').set('text',$('form[friend1][country]').value);
				selectMenu('form[delivery][county_uk]',$('form[friend1][county]').value);
				selectMenu('form[delivery][county_usa]',$('form[friend1][county]').value);
					$('delivery_county').set('text',$('form[friend1][county]').value);
					
				$('form[delivery][postcode]').value = $('form[friend1][postcode]').value;
					$('delivery_postcode').set('text',$('form[friend1][postcode]').value);
				selectMenu('form[delivery][country]',$('form[friend1][country]').value);
					$('delivery_country').set('text',$('form[friend1][country]').value);
				checkCounty($('form[delivery][country]'));
				$('form[delivery][tel]').value = $('form[friend1][tel]').value;
					$('delivery_tel').set('text',$('form[friend1][tel]').value);
			}
			
		} else if (slide==3) {
		
			// short term cookie it
			var billing_who = $('form[billing][who]').value;
			Cookie.write('billing_who',billing_who,{ 'path' : '/' });
			//Cookie.write('billing_donation',$('form[billing][donation]').value,{ 'path' : '/' });
		
			// save any new billing person
			if (billing_who=='other') {
			
				// enable all subsequent options to choose the billing address
				$$('.BillingName').set('disabled',false);
			
				// short term cookie it
				Cookie.write('billing_email',$('form[billing][email]').value,{ 'path' : '/' });
				Cookie.write('billing_name_first',$('form[billing][name_first]').value,{ 'path' : '/' });
				Cookie.write('billing_name_last',$('form[billing][name_last]').value,{ 'path' : '/' });
				Cookie.write('billing_address1',$('form[billing][address1]').value,{ 'path' : '/' });
				Cookie.write('billing_address2',$('form[billing][address2]').value,{ 'path' : '/' });
				Cookie.write('billing_town',$('form[billing][town]').value,{ 'path' : '/' });
				Cookie.write('billing_county',$('form[billing][county]').value,{ 'path' : '/' });
				Cookie.write('billing_county_uk',$('form[billing][county_uk]').value,{ 'path' : '/' });
				Cookie.write('billing_county_usa',$('form[billing][county_usa]').value,{ 'path' : '/' });
				Cookie.write('billing_postcode',$('form[billing][postcode]').value,{ 'path' : '/' });
				Cookie.write('billing_country',$('form[billing][country]').value,{ 'path' : '/' });
				Cookie.write('billing_tel',$('form[billing][tel]').value,{ 'path' : '/' });
			
				// tweak the variable of who we've picked to use in further routines
				billing_who = 'billing';
				
			} else {
			
				// disable all subsequent options to choose the billing address
				$$('.BillingName').set('disabled',true);
				
				// tweak the delivery page, so that if we were deliverying to another billing address, 
				// and now it's one of the friends, it doesn't give the option of deliverying to a non-existant person
				var delivery_who = Cookie.read('delivery_who');
				if (delivery_who=='billing') {
					choosePersonSwitch('delivery','');
				}
			
			}
			
			// set up the renewal form
			var renewal_who = Cookie.read('renewal_who');
			if (!renewal_who) {
				choosePersonSwitch('renewal',billing_who);
			} else if (renewal_who=='billing' && $('form[billing][who]').value!='billing') {
				choosePersonSwitch('renewal',billing_who);
			} else if (renewal_who=='billing' && Cookie.read('renewal_address1')!=$('form[billing][address1]').value) {
				choosePersonSwitch('renewal',billing_who);
			}
			
			// set up the giftaid form (if it's different from the person who's previously marked it as giftaid)
			
			if ($('form[giftaid][name_first]').value != $('form[' + billing_who + '][name_first]').value && $('form[' + billing_who + '][country]').value=='GBR') {
				$('form[giftaid][name_first]').value = $('form[' + billing_who + '][name_first]').value;
				$('form[giftaid][name_last]').value = $('form[' + billing_who + '][name_last]').value;
				$('form[giftaid][address1]').value = $('form[' + billing_who + '][address1]').value;
				$('form[giftaid][address2]').value = $('form[' + billing_who + '][address2]').value;
				$('form[giftaid][town]').value = $('form[' + billing_who + '][town]').value;
				selectMenu('form[giftaid][county]',$('form[' + billing_who + '][county]').value);
				$('form[giftaid][postcode]').value = $('form[' + billing_who + '][postcode]').value;
				$('form[giftaid][country]').value = $('form[' + billing_who + '][country]').value;
			} else if ($('form[' + billing_who + '][country]').value!='gbr') {
				blocking('giftaid','off');
				$('form[giftaid][yes]').checked = false;
			}
			
		} else if (slide==4) {
		
			Cookie.write('renewal_who',$('form[renewal][who]').value,{ 'path' : '/' });
			
			setShipping();
			
		} else if (slide==5) {
		
			// short term cookie it
			var delivery_who = $('form[delivery][who]').value;
			Cookie.write('delivery_who',delivery_who,{ 'path' : '/' });
		
			// save any new delivery person
			if (delivery_who=='other') {
			
				Cookie.write('delivery_email',$('form[delivery][email]').value,{ 'path' : '/' });
				Cookie.write('delivery_name_first',$('form[delivery][name_first]').value,{ 'path' : '/' });
				Cookie.write('delivery_name_last',$('form[delivery][name_last]').value,{ 'path' : '/' });
				Cookie.write('delivery_address1',$('form[delivery][address1]').value,{ 'path' : '/' });
				Cookie.write('delivery_address2',$('form[delivery][address2]').value,{ 'path' : '/' });
				Cookie.write('delivery_town',$('form[delivery][town]').value,{ 'path' : '/' });
				Cookie.write('delivery_county',$('form[delivery][county]').value,{ 'path' : '/' });
				Cookie.write('delivery_county_uk',$('form[delivery][county_uk]').value,{ 'path' : '/' });
				Cookie.write('delivery_county_usa',$('form[delivery][county_usa]').value,{ 'path' : '/' });
				Cookie.write('delivery_postcode',$('form[delivery][postcode]').value,{ 'path' : '/' });
				Cookie.write('delivery_country',$('form[delivery][country]').value,{ 'path' : '/' });
				Cookie.write('delivery_tel',$('form[delivery][tel]').value,{ 'path' : '/' });
				
			}
		
		} else if (slide==6) {
		
			// short term cookie it
			
			Cookie.write('giftaid_yes',$('form[giftaid][yes]').value,{ 'path' : '/' });
			Cookie.write('giftaid_name_first',$('form[giftaid][name_first]').value,{ 'path' : '/' });
			Cookie.write('giftaid_name_last',$('form[giftaid][name_last]').value,{ 'path' : '/' });
			Cookie.write('giftaid_address1',$('form[giftaid][address1]').value,{ 'path' : '/' });
			Cookie.write('giftaid_address2',$('form[giftaid][address2]').value,{ 'path' : '/' });
			Cookie.write('giftaid_town',$('form[giftaid][town]').value,{ 'path' : '/' });
			Cookie.write('giftaid_county',$('form[giftaid][county]').value,{ 'path' : '/' });
			Cookie.write('giftaid_postcode',$('form[giftaid][postcode]').value,{ 'path' : '/' });
			Cookie.write('giftaid_country',$('form[giftaid][country]').value,{ 'path' : '/' });
				
		
		}
		
	}



/* CHOOSE WHO TO BILL/RENEW/SEND TO */

	function choosePerson(slideForm) {
	
		var thisForm = document.forms['giveusyamoney'];
		var thisSelection = thisForm.elements['form['+slideForm+'][who]'].value;
		choosePersonSwitch(slideForm,thisSelection);
	}
	
	function choosePersonSwitch(slideForm,thisSelection) {
	
		//console.log(slideForm + ' : ' + thisSelection);
	
		// IF EMPTY OPTION - show nothing
		if (!thisSelection) {
		
			$(slideForm+'_friend').setStyle('display','none');
			$(slideForm+'_friend').setStyle('opacity','0');
			if ($(slideForm+'_other')) {
				$(slideForm+'_other').setStyle('display','none');
				$(slideForm+'_other').setStyle('opacity','0');
			}
			
		// IF OTHER OPTION - show form
		} else if (thisSelection=='other') {		
		
			$(slideForm+'_friend').setStyle('display','none');
			$(slideForm+'_friend').setStyle('opacity','0');
			blocking(slideForm+'_other','on');
			
		// IF FRIEND OPTION - show uneditable details
		} else {
		
			$(slideForm+'_email').set('text', $('form['+ thisSelection +'][email]').value );
			$(slideForm+'_name_first').set('text',$('form['+ thisSelection +'][name_first]').value );
			$(slideForm+'_name_last').set('text', $('form['+ thisSelection +'][name_last]').value );
			$(slideForm+'_address1').set('text', $('form['+ thisSelection +'][address1]').value );
			$(slideForm+'_address2').set('text', $('form['+ thisSelection +'][address2]').value );
			$(slideForm+'_town').set('text', $('form['+ thisSelection +'][town]').value );
			$(slideForm+'_county').set('text', $('form['+ thisSelection +'][county]').value );
			$(slideForm+'_postcode').set('text', $('form['+ thisSelection +'][postcode]').value );
			$(slideForm+'_country').set('text', $('form['+ thisSelection +'][country]').value );
			$(slideForm+'_tel').set('text', $('form['+ thisSelection +'][tel]').value );
			
			if ($(slideForm+'_other')) {
				$(slideForm+'_other').setStyle('display','none');
				$(slideForm+'_other').setStyle('opacity','0');
			}
			blocking(slideForm+'_friend','on');
		}
		
		// check the select option is correct
		selectMenu('form['+slideForm+'][who]',thisSelection);
		
	}
	

/* 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')) {
			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;
		
	}
