$('head').append('<style type="text/css">form#donate fieldset { display:none; } form#donate fieldset:first-child { display:block; } </style>');
	 
String.prototype.countWords=function()
{
	var str=this;
	var matches=str.match(/[^\s]+/g);
	return (matches)?matches.length:0;
}

String.prototype.isValidEmail=function()
{
	return Boolean(this.match(/^[a-z0-9_\-\.+%]+@[a-z0-9\-\.]+$/i));
}

$(document).ready(function()
	{
		maxDedicationWords=20;
		backButtonLabel='Back';
		continueButtonLabel='Continue';

		var $fields=$("#donate:not('.supporter-signup-form, .standard-donation-form, .corporate-donation-form') fieldset");
	
		$('.stepButtons .continue',$fields).remove();
		$('.stepButtons .back',$fields).remove();

		$fields.children('.stepButtons').prepend('<input class="continue form_submit" type="button" value="'+continueButtonLabel+'"/> ');
		$fields.children('.stepButtons').prepend('<input class="back" type="button" value="'+backButtonLabel+'"/> ');
		
		

		$validationSteps=
		[
			/* Validation method for step 1 */
			function(i)
			{
				$errorContainers.eq(i).html('');

				var validates=true;

				if(!$('#email').val().isValidEmail())
				{
					validates=false;
					$errorContainers.eq(i).html('Please enter a valid email address');					
				}


				if(!(
					$('#name').val().length>0 &&
					$('#address').val().length>0 &&
					$('#telephone').val().length>0
				))
				{
					validates=false;
					$errorContainers.eq(i).html('Please complete each field');
				}

				return (validates);
			},
			/* Validation method for step 2 */
			function(i)
			{
				$errorContainers.eq(i).html('');
				var dedicationMessageWordCount=($('#dedication_message').val()).countWords();
				var validates=true;

				switch($('[name=greeting_card]:checked').val())
				{
					case 'post':
						if($('#dedication_address').val().length==0)
						{
							validates=false;
							$errorContainers.eq(i).html('Please complete the address to post to dedication message to.');
						}

						break;
					case 'email':
						if(!$('#dedication_email').val().isValidEmail())
						{
							validates=false;
							$errorContainers.eq(i).html('Please enter a valid email address');
						}
						break;
					case 'none':
						break;
				}

				
				if(!(
					$('#dedication_name').val().length>0 &&
					dedicationMessageWordCount>0
				))
				{
					$errorContainers.eq(i).html('Please complete each field');
				}


				if(dedicationMessageWordCount>maxDedicationWords)
				{
					validates=false;
					$errorContainers.eq(i).html('The dedication message must contain less than '+maxDedicationWords+' words');
				}

				return validates;
			},
			/* Validation method for step 3 */
			function(i)
			{
				$errorContainers.eq(i).html('');
				var validates=$('[name=woodland]:checked',$fields).length>0;
				if(!validates)
				{
					$errorContainers.eq(i).html('Please select a woodland');
				}
				$('#mindonation').val($("input[name='woodland']:checked").val() == 'Homeopathy Wood' ? "12.5" : "35");
				return validates;
			},
			/* Validation method for step 4 */
			function(i)
			{
				$errorContainers.eq(i).html('');
				return true;
			}
		];

		$errorContainers=$('.error',$fields);
		$stepContinueButtons=$('.stepButtons .continue',$fields);
		$stepBackButton=$('.stepButtons .back',$fields);

		$fields.eq(0).before('<ol id="donateSteps"></ol>');
		var $donateSteps=$('#donateSteps');
		var $legends=$fields.children('legend');
		$legends.each(function(i)
		{
			var text=$(this).html();
			$donateSteps.append('<li></li>').children(':last-child').append(text);
		})
		$donateSteps.children().eq(0).addClass('current');

		$fields.children('legend').hide();

		//On click continue
		$stepContinueButtons.click(function()
		{ 
			var $buttons=$stepContinueButtons;
			var $field=$(this).parent().parent();
			var i=$buttons.index(this);
			if(!$validationSteps[i](i))return;
			i++
			$field.hide();
			$fields.eq(i).show();
			$donateSteps.children().removeClass('current');
			$donateSteps.children().eq(i).addClass('current');
		});

		//On click back
		$stepBackButton.click(function()
		{ 
			var $buttons=$stepBackButton;
			var $field=$(this).parent().parent();
			var i=$buttons.index(this);
			//if(!$validationSteps[i]())return;
			i--;
			$field.hide();
			$fields.eq(i).show();
			$donateSteps.children().removeClass('current');
			$donateSteps.children().eq(i).addClass('current');
		});

		$('#dedication_message').keyup(
			function()
			{
				var words=String($(this).val()).countWords();
				$('#dedication_validation_message').eq(0).html((maxDedicationWords-words)+' words left');
				//'moo: '+40-parseInt(countWords($(this).val()))
			}
		)

		$fields.hide();
		$fields.eq(0).show();
		$stepBackButton.eq(0).hide();
		$stepContinueButtons.eq($stepContinueButtons.length-1).remove();

		$('.greeting_card_info',$fields).css({display:'none'});
		$('[name=greeting_card]',$fields).click(
			function()
			{
				$('.greeting_card_info',$fields).slideUp();
				$(this).parent().children('.greeting_card_info').slideDown();
			}
		);
		
	});

		
