giftpack = false;

window.addEvent('domready', function() {
	
	/* SET UP FORM FROM COOKIES */
	
			
		// slide 3
		
			if (val1 = notNull(Cookie.read('billing_donation'))) {$('form[billing][donation]').value = val1;}
			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;}
			
		
		
		// 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;}
				
	
	
	// check the recurring inputs
	
	checkrecur ();
	
	/* PREV/NEXT BUTTONS */
	
		
		$$('a.gotopayment').addEvents({
			mousedown: function(event) {
				var slideOK = checkSlide('1');
				setTimeout((function(){				
					if (slideOK) {				
						eek = processSlide('1');
						var bob = setTimeout('document.forms[\'giveusyamoney\'].submit();','100');
					}
				}),100);
			}
		});
	
});


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

/* 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'
				};
			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]'];
					}
				}
			}
				
			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) {
		
			// short term cookie it
			Cookie.write('billing_donation',$('form[billing][donation]').value,{ 'path' : '/' });
					
			// 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' : '/' });
			
			
			// 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' : '/' });
				
		
	}



/* 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;
			$('form[giftaid][country]').value = $('form[billing][country]').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;
		
	}
	


// check the recurring checkbox
			
	function checkrecur () {
		
		return;
	
		/*
		var thisForm = document.forms['giveusyamoney'];
		if (thisForm.elements['form[billing][repeat]'].value) {
			thisForm.elements['form[billing][repeatonlyyear]'].disabled = false;
			$('checkonlyyear').removeClass('disabled');
		} else {
			thisForm.elements['form[billing][repeatonlyyear]'].disabled = true;
			thisForm.elements['form[billing][repeatonlyyear]'].checked = false;
			$('checkonlyyear').addClass('disabled');
		}
		*/
	}
	
	
	function checkdonate () {
	
		var thisForm = document.forms['giveusyamoney'];
		
		
		var currentDonate = thisForm.elements['donate_select'].value;
		if (currentDonate == '') {
			$('donate_other').setStyle('display','block');
		} else {
			$('donate_other').setStyle('display','none');
		}
		thisForm.elements['form[billing][donation]'].value = currentDonate;
		
		
		
	}
