$(function() {
/***************************
	*	Blur form field default values
	*/
		
	$('.blurit').focus(function() {          
		if (this.value == this.defaultValue){  
			this.value = ''; 
		} 
		if(this.value != this.defaultValue){  
			this.select();  
		}  
	});  
	$('.blurit').blur(function() {  							 
		if (this.value == ''){ 		
			this.value = this.defaultValue;  
		}  
	});


/***************************************************
	* Make radio groups jquerified		
	*/
		
	$(".radio").buttonset();
	
/***************************************************
	* jquerify all buttons, links and submit buttons that have a .button class		
	*/
	$("button, input:submit, a", ".button").button();
	
/***************************************************
	* Build slider for home page banners.
	* All options included to override all defaults.
	*/
	$("#slider").easySlider({
		prevId: 		'prevBtn',
		prevText: 		'',
		nextId: 		'nextBtn',	
		nextText: 		'',
		controlsShow:	true,
		controlsBefore:	'',
		controlsAfter:	'',	
		controlsFade:	true,
		firstId: 		'firstBtn',
		firstText: 		'First',
		firstShow:		false,
		lastId: 		'lastBtn',	
		lastText: 		'Last',
		lastShow:		false,				
		vertical:		false,
		speed: 			1000,
		auto:			true,
		pause:			4000,
		continuous:		true, 
		numeric: 		false,
		numericId: 		'controls'						
	});		

/***************************************************
	* Used to change the CSS class for the search field on focus/blur
	*/
	$('#search').addClass("search_blur");  
    $('#search').focus(function() {  
        $(this).removeClass("search_blur").addClass("search_focus");  
        if (this.value == this.defaultValue){  
            this.value = ''; 
			
        }  
        if(this.value != this.defaultValue){  
            this.select();  
        }  
    });  
    $('#search').blur(function() {  
        $(this).removeClass("search_focus").addClass("search_blur");  
        if (this.value == ''){ 		
			this.value = this.defaultValue;  
		} 
    });
	
/***************************************************
	* Probiotic profile
	*/	
	
	//Hide everything but contact form and corresponding buttons
	$("#part_1, #part_2, #part_3, #suggested_product").hide();
	$("#showPart2, #showPart3, #showProduct, #restart").hide();
	
	
	//Hide contact form and show part 2
	$("#showPart1").click(function(){
								   
		//Validate the form
		var formValues = $("#subscribeform").serialize();
		var name = $("#attribute20").val();
		var address = $("#attribute5").val();
		var city = $("#attribute7").val();
		var state = $("#attribute17").val();
		var zip = $("#attribute8").val();
		var email = $("#email").val();
		var emailconfirm = $("#emailconfirm").val();
		var sample = $("#attribute27").val();
		
		if(name == "" || address == "" || city == "City" || state == "101" || zip == "Zip" || email == "" || emailconfirm == "" || sample=="0"){
			alert("Please fill out all fields and select a FREE sample");
			return false;
		}
		
		if(email != emailconfirm){
			alert("Your email does not match");
			return false;
		}
		$.ajax({
		   type: "POST",
		   url: "/profile/recordcontact",
		   data: "name="+name+"&address="+address+"&city="+city+"&state="+state+"&zip="+zip+"&email="+email+"&emailconfirm="+emailconfirm+"&sample="+sample,
		   success: function(msg){
				//alert( "Data Saved: " + msg );
				//return false;
			}
		})
		//return false;
		$("#showPart1, #contactForm,#questionnaire_intro").fadeOut('slow', function() {									 
			//Fade in next section
			$("html").animate({ scrollTop: 0 }, "fast");
			$('#part_1,#showPart2').fadeIn('slow', function() {});			
		 });						   
	});
	
	//Hide part 1 and show part 2
	$("#showPart2").click(function(){
		//Check section questions
		var sex = $("input[@name='1']:checked").val();
		var age = $('[name=2]').val();
		var formValues = $("#profile").serialize();
		
		//alert($("#profile").serialize());

		if(/^-?\d+$/.test(sex)==false){
			alert("Please select a gender before proceeding");
			return false;
		}
		
		if(/^-?\d+$/.test(age)==false){
			alert("Please enter your age in integer form");
			return false;
		}
		
		
		//Part 1 rules
		//1. If the user is 0-2; go directly to 2 products (baby-dophilus and jarro-dophilus powder)
		//2. If the user is 3-4; go directly to 2 products (baby-dophilus and yum-yum dophilus)
		//3. If the user is 5-17; go directly to 2 products (jarro-dophilus powder and yum-yum dophilus) 
		//4. If the user is 18+ and male, hide the femdophilus question
		//5. If the user is 18+ and female, show all questions

		var skip2product = false;
		var skipFemQuestion = false;
		var maxChildAge = 17;
		var defaultProduct1 = "";
		var defaultProduct2 = "";
		
		//Skip to products since child is under age 
		if(age <= maxChildAge){	
			//Determine which 2 default products to use
			$.ajax({
				   type: "POST",
				   url: "/profile/process",
				   data: formValues,
				   success: function(msg){
						//alert( "Data Saved: " + msg );
						$('#suggested_product').html(msg);
					}
			})
			
			
			$("#showPart2, #part_1").fadeOut('slow', function() {
				$("html").animate({ scrollTop: 0 }, "fast");
				$('#suggested_product, #restart').fadeIn('slow', function() {
					
				});
			});
		}else{		
			//Fade out button
			$("#showPart2, #part_1").fadeOut('slow', function() {									 
				//Fade in next section
				$("html").animate({ scrollTop: 0 }, "fast");
				$('#part_2,#showPart3').fadeIn('slow', function() {
					if(sex == 1 || age < 4){
						$("#cat_11").hide();
						
					}
				});			
			 });
		}
	});
	
	//Hide part 2 and show part 3
	$("#showPart3").click(function(){								   				
		var formValues = $("#profile").serialize();
		//alert(formValues);
		var valuesArray = formValues.split('&');
		var arrayLenght = valuesArray.length;
		//alert(arrayLenght);
		var sex = $("input[@name='1']:checked").val();
		if(sex == 1){
			//For Males
			if(arrayLenght <= 17){
				//alert("Please answer all questions before continuing");
				//return false;
			}
		}else{
			//For Females
			if(arrayLenght <= 18){
				//alert("Please answer all questions before continuing");
				//return false;
			}
		}
		
		//Fade out button
		$("#showPart3, #part_2").fadeOut('slow', function() {
			//Fade in next section
			$("html").animate({ scrollTop: 0 }, "fast");
			$('#part_3,#showProduct').fadeIn('slow', function() {});
     	 });				
	});
	
	//Hide part 3 and show results
	$("#showProduct").click(function(){								   				
		var formValues = $("#profile").serialize();
		var valuesArray = formValues.split('&');
		var arrayLenght = valuesArray.length;
		//alert(arrayLenght);
		var sex = $("input[@name='1']:checked").val();
		if(sex == 1){
			//For Males
			if(arrayLenght <= 22){
				//alert("Please answer all questions before continuing");
				//return false;
			}
		}else{
			//For Females
			if(arrayLenght <= 23){
				//alert("Please answer all questions before continuing");
				//return false;
			}
		}
		
		//Fade out button
		$("#part_3,#showProduct").fadeOut('slow', function() {
				
				$.ajax({
					   type: "POST",
					   url: "/profile/process",
					   data: formValues,
					   success: function(msg){
							//alert( "Data Saved: " + msg );
							$('#suggested_product').html(msg);
							$('#restart').fadeIn('slow', function() {});
						}
				})
				
				//Fade in next section				
				$('#suggested_product').fadeIn('slow', function() {
					//$('#suggested_product').html("<img src='/images/loaders/bar.gif' />");
									
     			});
     	 });				
	});

});//END document.ready


