ajaxFile = "tasteofthedanforth_survey.php";
var checking = null;

$(document).ready(function(){

	$('#memberBirthday').datepicker({
		clickInput:true,
		showYearNavigation:true,
		changeMonth: true,
		changeYear: true,
		yearRange: "1920:2008",
		dateFormat: "MM d, yy",
		defaultDate: "-50y"
	});

	$("input[name='q5_s1']").click(function(){
		if ($(this).val() < 4) {
			$("#whyText").hide();
		} else {
			$("#whyText").show().find("textarea").focus().val("");
		}
	});

	$("#q3_s9, #q3_s10, #q3_s11").click(function(){
		var $this = $(this);
		if ($this.attr("checked")) {
			$this.prev().show().find("input").focus().val("");
		} else {
			$this.prev().hide();
		}
	});
	
	$("#q4_s1").click(function(){ $("#q4_s1_slider").parent().show(); });
	$("#q4_s2").click(function(){ $("#q4_s1_slider").parent().hide(); });
	
	$("#submitButton").click(sumbitForm);
	
});

function sumbitForm() {
	var success = true;
	var options = {};
	var ctrl = null;
	
	if (success) {
		ctrl = $("#memberEmail").get(0);
   	success = validateControl("email", ctrl, "Please enter a valid email address");
		options.email = ctrl.value;
   }
	if (success) {
		ctrl = $("#memberFname").get(0);
		success = validateControl("text", ctrl, "Please enter your first name");
		options.firstname = ctrl.value;
	}
	if (success) {
		ctrl = $("#memberLname").get(0);
		success = validateControl("text", ctrl, "Please enter your last name");
		options.lastname = ctrl.value;
	}
	if (success) {
		ctrl = $("#memberCity").get(0);
		success = validateControl("text", ctrl, "Please enter your city name");
		options.city = ctrl.value;
	}
	if (success) {
		ctrl = $("#memberState").get(0);
		success = validateControl("text", ctrl, "State/province is required");
		options.state = ctrl.value;
	}
	if (success) {
		ctrl = $("#memberPostal").get(0);
		success = validateControl("text", ctrl, "Zip/postal code is required");
		options.postal = ctrl.value;
	}
	if (success) {
		ctrl = $("input[name='memberGender']");
		success = validateControl("radio", ctrl, "Select your gender");
		options.gender = ctrl.filter(":checked").val();
	}
	if (success) {
		ctrl = $("#memberBirthday").get(0);
		success = validateControl("text", ctrl, "Select your birth date");
		options.birthdate = ctrl.value;
	}

	if (success) {
		ctrl = $("input[name='memberEducation']");
		success = validateControl("radio", ctrl, "Select your education");
		options.education = ctrl.filter(":checked").val();
	}
	if (success) {
		ctrl = $("input[name='memberEmployment']");
		success = validateControl("radio", ctrl, "Select your employment choice");
		options.employment = ctrl.filter(":checked").val();
	}
	if (success) {
		ctrl = $("input[name='memberMaritals']");
		success = validateControl("radio", ctrl, "Select your marital status");
		options.status = ctrl.filter(":checked").val();
	}

	// income is not required, but save it if it's there
	ctrl = $("input[name='memberIncome']:checked");
	if (ctrl.length > 0) {
		options.income = ctrl.val();
	}

	if (success) {
		var $peeps = $(".peeps");
		var total = 0;
		for (var i=0; i<$peeps.length; i++) {
			total += $peeps.eq(i).val();
		}
		if (total == 0) {
			success = false;
			scrollToElement($("#sliderQuestion"));
			alert("Select the number of people in your party");
			$peeps.eq(0).focus();
		}
		options.partymaleold = $("#q1_s1").val();
		options.partyfemaleold = $("#q1_s2").val();
		options.partymaleyoung = $("#q1_s3").val();
		options.partyfemaleyoung = $("#q1_s4").val();
	}

	if (success) {
		ctrl = $("input[name='q2_s1']");
		success = validateControl("radio", ctrl, "Indicate your satisfaction");
		options.satisfaction = ctrl.filter(":checked").val();
	}

	if (success) {
		var $checks = $(".q3");
		var total = 0;
		for (var i=0; i<$checks.length; i++) {
			if ($checks.eq(i).attr("checked")) total++;
		}
		if (total == 0) {
			success = false;
			scrollToElement($checks.eq(0));
			$checks.eq(0).focus();
			alert("How did you hear about the festival?");
		}
		options.ad_subway = $("#q3_s1").attr("checked") ? 1 : 0;
		options.ad_radio = $("#q3_s2").attr("checked") ? 1 : 0;
		options.ad_tv = $("#q3_s3").attr("checked") ? 1 : 0;
		options.ad_ts = $("#q3_s4").attr("checked") ? 1 : 0;
		options.ad_metro = $("#q3_s5").attr("checked") ? 1 : 0;
		options.ad_dineto = $("#q3_s6").attr("checked") ? 1 : 0;
		options.ad_totdweb = $("#q3_s7").attr("checked") ? 1 : 0;
		options.ad_friend = $("#q3_s8").attr("checked") ? 1 : 0;
		options.ad_article = $("#q3_s9").attr("checked") ? 1 : 0;
		ctrl = $("#q3_s9_text");
		if (ctrl.val() != "") {
			options.ad_article_text = ctrl.val();
		}
		options.ad_interview = $("#q3_s10").attr("checked") ? 1 : 0;
		ctrl = $("#q3_s10_text");
		if (ctrl.val() != "") {
			options.ad_interview_text = ctrl.val();
		}
		options.ad_other = $("#q3_s11").attr("checked") ? 1 : 0;
		ctrl = $("#q3_s11_text");
		if (ctrl.val() != "") {
			options.ad_other_text = ctrl.val();
		}
	}
	
	if (success) {
		options.frequent_goer = 0;
		// "Have you ever been" will be a number of times -> 0-14
		ctrl = $("input[name='q4_s1']");
		success = validateControl("radio", ctrl, "Have you been to the Taste of the Danforth before?");
		if (ctrl.filter(":checked").val() == 1) {
			options.frequent_goer = $("#q4_s1_slider").val();
		}
	}
	
	if (success) {
		ctrl = $("input[name='q5_s1']");
		success = validateControl("radio", ctrl, "Comming back next year?");
		options.coming_back = ctrl.filter(":checked").val();
		if (options.coming_back > 3) {
			options.not_coming_back = $("#q5_s1_text").val();
		}
	}

	// Save the comment if not empty
	ctrl = $("#q6_s1_text");
	if (ctrl.val() != '') {
		options.comments = ctrl.val();
	}
	
	if (success) {
		// Send the info to the server
		options.action = "saveTotDmember";
		$.post(ajaxFile, options, function (data) {
			if (data == false) {
				alert("Problem posting survey. Please contact administrator");
				return;
			}

			if (typeof data.memberExists != "undefined") {
				window.location = "/tasteofthedanforth_survey.php?memberExists="+options.email;
			} else {
				window.location = "/tasteofthedanforth_survey.php?thankyou";
			}
		});
	}
	return false;
}


function scrollToElement(el) {
	var top = $(el).offset().top;
	window.scrollTo(0, top);
}

function validateControl(type, el, message) {
	switch (type) {
		case "email":
			if (validateEmail(el.value)) return true;
			alert(message);
			el.focus();
			break;
		case "text":
			var str = el.value.replace(/^\s*|\s*$/,'');
			if (str != "") return true;
			alert(message);
			el.focus();
			break;
		case "radio":
			var radio = el.filter(":checked");
			if (radio.length > 0) return true;
			alert(message);
			el.eq(0).focus();
			break;
	}
	return false;
}

function validateEmail(email) {
	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
	return email.match(re);
}
