$(document).ready( function() {
	//Showoff checkbox
	$('#sendMe').addClass('Disappear');
	$('#sendMeLabel').addClass('CheckReplace');		
	
	//Handler my checkbox
	if ($('#sendMe').attr('checked') == true)  $('#sendMeLabel').addClass('CheckReplaceActive');
	$('#sendMeLabel').click(checkMe);
	
	//Handler my radio
	$('#personalize li input[@name=personalize]').each(function(){
		if($(this).attr('checked') == true) {
			$('#personalize li label[@for='+this.id+']').addClass('RadioReplaceActive');
			changePreview(this.id);
		}
	});
	$('#personalize li input').addClass('Disappear');
	$('#personalize li label').addClass('RadioReplace');	
	
	$('#personalize li label').click(function(){
		unChooseAll();
		$(this).addClass('RadioReplaceActive');
		changePreview($(this).attr('for'))
	});
	
	//Handler reset button
	$('#reset').click(function(){
        unChooseAll()
		$('#personalize li input#family').attr('checked',true);
		$('#personalize li label[@for=family]').addClass('RadioReplaceActive');
		
		$('#yourNameError').removeClass('Error')
		$('#yourName').val('');
		$('#yourName').focus();
		
		$('#yourEmailError').removeClass('Error')
		$('#yourEmail').val('');
		
		$('#friendNameError').removeClass('Error')
		$('#friendName').val('');
		
		$('#friendEmailError').removeClass('Error')
		$('#friendEmail').val('');
		
		if ($('#sendMe').attr('checked') != true) {
			checkMe();
			$('#sendMe').attr('checked',true);
		}
		
		return false;
	});
	
	//Lets validate
	$('#mailfriendForm').submit(function(){
		
		if($('#yourName').val().length < 2) {
			$('#yourNameError').addClass('Error');
			$('#yourName').focus();
			return false;
		} else {
			$('#yourNameError').removeClass('Error');
		}
		
		if(! $('#yourEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#yourEmailError').addClass('Error');
			$('#yourEmail').focus();
			return false;
		} else {
			$('#yourEmailError').removeClass('Error');
		}
		
		if($('#friendName').val().length < 2) {
			$('#friendNameError').addClass('Error');
			$('#friendName').focus();
			return false;
		} else {
			$('#friendNameError').removeClass('Error');
		}
		
		if(! $('#friendEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#friendEmailError').addClass('Error');
			$('#friendEmail').focus();
			return false;
		} else {
			$('#friendEmailError').removeClass('Error');
		}
		
	});
	
	$('#yourName').bind('blur',function(){
		if($('#yourName').val().length < 2) {
			$('#yourNameError').addClass('Error');
		} else {
			$('#yourNameError').removeClass('Error');
		}								
	});
	$('#yourEmail').bind('blur',function(){
		if(! $('#yourEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#yourEmailError').addClass('Error');
		} else {
			$('#yourEmailError').removeClass('Error');
		}									
	});
	$('#friendName').bind('blur',function(){
		if($('#friendName').val().length < 2) {
			$('#friendNameError').addClass('Error');
		} else {
			$('#friendNameError').removeClass('Error');
		}	
	});
	$('#friendEmail').bind('blur',function(){
		if(! $('#friendEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
			$('#friendEmailError').addClass('Error');
		} else {
			$('#friendEmailError').removeClass('Error');
		}	
	});
	
	
});

function unChooseAll() {
	$('#personalize li label').removeClass('RadioReplaceActive');
}

function checkMe() {
	$('#sendMeLabel').toggleClass('CheckReplaceActive');
}

function changePreview(ID) {
	img = $('.EmailPreview img');
	title = $('.EmailPreview dt').next();
	desc = title.next();
	
	switch (ID) {
		case 'family':
			title.text('Christina Fry, Rick Cerett, and Chalise Jackson - Family of infant who died at 6 months of age from influenza-related complications.');
			desc.text('When infant Marques Jackson passed away from influenza-related complications, his mother Christina Fry and grandfather Rick Cerett became crusaders for yearly vaccination for parents, children, and caretakers.');
			img.attr('src',root_path + '/images/thumbs/25.jpg');
			img.attr('alt','Christina Fry, Rick Cerett, and Chalise Jackson');
		break;
		case 'adult':
			title.text('Kristi Yamaguchi - Olympic Gold Medalist and mother of two young children');
			desc.text('Kristi Yamaguchi wants to do everything she can to protect her children, but as a wife and daughter, she also knows that influenza immunization is a must for everyone in her family, even including her parents.');
			img.attr('src',root_path + '/images/thumbs/15-back.jpg');
			img.attr('alt','Kristi Yamaguchi - Olympic Gold Medalist and mother of two young children');
		break;
		case 'old':
			title.text('Ray and Stella Ross - Husband With COPD and His Wife, Household Contact');
			desc.text('Ray Ross has emphysema, a progressive chronic obstructive pulmonary disease (COPD). As a result of his emphysema, Ray is at an increased risk of developing complications from the flu. Influenza vaccination should be given annually to people with certain chronic medical conditions like COPD.');
			img.attr('src',root_path + '/images/thumbs/8.jpg');
			img.attr('alt','Ray and Stella Ross');
		break;
		case 'illness':
			title.text('Brandon Williams and Dimitri Aston - Brothers Living With Asthma');
			desc.text('Brandon and Dimitri have asthma. Their mother, Lakisha, knows the flu can worsen symptoms of asthma like wheezing, and can make breathing even more difficult.<sup>12-14</sup> That’s why she makes sure her family is vaccinated each and every year.');
			img.attr('src',root_path + '/images/thumbs/27.jpg');
			img.attr('alt','Brandon Williams and Dimitri Aston');
		break;
	}
}

//basically duplicate logic to what you see above (line 54)
//this is called in tracking.js to only submit GA event when form is valid
function mailFriendFormIsValid() {
	if($('#yourName').val().length < 2) {
		return false;
	}
	
	if(! $('#yourEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
		$('#yourEmailError').addClass('Error');
		$('#yourEmail').focus();
		return false;
	}
	
	if($('#friendName').val().length < 2) {
		$('#friendNameError').addClass('Error');
		$('#friendName').focus();
		return false;
	}
	
	if(! $('#friendEmail').val().match(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i)) {
		$('#friendEmailError').addClass('Error');
		$('#friendEmail').focus();
		return false;
	}
	return true;
}
