var email = '';

function checkEmail(email,field_index) {
	var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var emailVal = $("." + email+":eq("+field_index+")").val();
	//return pattern.test(emailVal);
	if (pattern.test(emailVal)) {
		$.cookie('email', emailVal, {path: '/', expires:7});
		// alert('contents of email cookie = '+$.cookie('email'));
		return true;
	} else {
		$.cookie('email', null);
		// alert('contents of email cookie = '+$.cookie('email'));
		return false;
	}
}

function pageTwo(){
	$('#viewport').animate({left:'-1120px'});
	var step_two = $('#step2').innerHeight();
	$('#pages').animate({height:step_two});
}

$(function(){
	/* AJAX LOADING MESSAGES */
	$('.question,.email').ajaxStart(function(){
	   $('#ajax').fadeIn();
       $('input').attr('disabled','disabled');
	});
	$('.question,.email').ajaxStop(function(){
		$('#ajax').fadeOut();
        $('input').removeAttr('disabled');
	});
	/* AJAX LOADING MESSAGES */
	$('.subForm').ajaxStart(function(){
	   $('#ajax_email').fadeIn();
       $('input').attr('disabled','disabled');
	});
	$('.subForm').ajaxStop(function(){
		$('#ajax_email').fadeOut();
        $('input').removeAttr('disabled');
	});

	var step_one = $('#step1').innerHeight();
	$('#pages').css('height',step_one);
	var question = $('.question:eq(0)').innerHeight();
	$('#interview').css('height',question);

	$('.learn_more').click(function(){
		pageTwo();
		return false;
	});

	$(".subForm input:submit").click(function() {
		var form = $(this).parent().parent();
		// First, disable the form from submitting
		$(form).submit(function() { return false; });
		// Grab form action
		var formAction = $(form).attr("action");
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		var id = "hjjdkh";
		var emailId = id + "-" + id;
		var field_index = $(form).index();
		// Validate email address with regex
		if (!checkEmail(emailId,field_index)){
			alert("Please enter a valid email address");
			return;
		}
		// Serialize form values to be submitted with POST
		var str = $(form).serialize();
		// Add form action to end of serialized data
		// CDATA is used to avoid validation errors
		//<![CDATA[
		var serialized = str + "&action=" + formAction;
		// ]]>
		// Submit the form via ajax
		$.ajax({
			url: "proxy.php",
			type: "POST",
			data: serialized,
			success: function(data){
				// Server-side validation
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				} else {
					// get email
					email = $("." + emailId+":eq("+field_index+")").val();
					// get hash
					var hash = $('#interview').data('hash');
					// save email to db
					$.ajax({
						type: 'post',
						url: 'logic.php',
						dataType: 'text',
						data: 'action=save_email&hash='+hash+'&email='+email,
						success: function(html) {
							// if successful, paginate
							pageTwo();
						}
					});
				}
			}
		});
	});

	// NEW FORM =================================================

	$(".subForm2 input:submit").click(function() {
		// PAGINATION
		var q_index = $('#interview').data('current'); // GETS CURRENT PAGE

		var form = $(this).parent().parent();
		// First, disable the form from submitting
		$(form).submit(function() { return false; });
		// Grab form action
		var formAction = $(form).attr("action");
		// Hacking together id for email field
		// Replace the xxxxx below:
		// If your form action were http://mysiteaddress.createsend.com/t/r/s/abcde/, then you'd enter "abcde" below
		var id = "hjjdkh";
		var emailId = id + "-" + id;
		var field_index = 1;
		// Validate email address with regex
		if (!checkEmail(emailId,field_index)){
			alert("Please enter a valid email address");
			return;
		}
		// Serialize form values to be submitted with POST
		var str = $(form).serialize();
		// Add form action to end of serialized data
		// CDATA is used to avoid validation errors
		//<![CDATA[
		var serialized = str + "&action=" + formAction;
		// ]]>
		// Submit the form via ajax
		$.ajax({
			url: "proxy.php",
			type: "POST",
			data: serialized,
			success: function(data){
				// Server-side validation
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				} else {
					// get email
					email = $("." + emailId+":eq("+field_index+")").val();
					// get hash
					var hash = $('#interview').data('hash');
					// save email to db
					$.ajax({
						type: 'post',
						url: 'logic.php',
						dataType: 'text',
						data: 'action=save_email&hash='+hash+'&email='+email,
						success: function(html) {
							// if successful, paginate
							var question = $('.question:eq('+q_index+')').innerHeight();
							//alert("q_index = "+q_index+"\nheight = "+question);
							$('#interview').animate({height:question+'px'});

							var left = q_index * interview_width; // SETS LEFT VAL
							$('#current').animate({left:'-'+left+'px'});
							$('#interview').data('current',q_index);
						}
					});
				}
			}
		});
	});

	// QUESTIONS =================================================

	// SET INITIAL PAGE VARS
	$('#interview').data('current','1');
	var interview_width = $('#interview').width()+60;

	// SAVE RESULTS IN DB
	$('.question input[type=radio]').click(function(){

		// PAGINATION
		var q_index = $('#interview').data('current'); // GETS CURRENT PAGE
		//alert("it was: " + q_index);

		// QUESTION
		var hash = $('#interview').data('hash'); // GET HASH
		var answer = $(this).val();

		if (q_index >= 3) {
			if (!email) {
				var left = q_index * interview_width;
			} else {
				var left = q_index * interview_width + 300;
			}
		} else {
			var left = q_index * interview_width;
			//alert(left);
			//if (q_index == 4) {

				//var left = q_index * interview_width + 300;
				//q_index++;
		   		//var left = parseInt(left)+300;
		    //}
			//if (q_index == 6) {
				//var left = q_index * interview_width +600;
			//}
		}

		//if (!left) {
		//	var left = q_index * interview_width;
		//	if (q_index == 4) {
				//q_index++;
		//		var left = parseInt(left)+300;
		//	}
		//}
		// SAVE ANSWER
		$.ajax({
			type: 'post',
			url: 'logic.php',
			dataType: 'text',
			data: 'action=next_question&hash='+hash+'&q_id='+q_index+'&answer='+answer,
			success: function(html) {

				var q_index = $('#interview').data('current');
				//alert('q_index = '+q_index);
				q_index++;
				$('#interview').data('current',q_index);
				if (q_index == 4 && !email) {
					var question = $('.email:eq(0)').innerHeight();
				} else {
					var temp = parseInt(q_index) - 1;
					var question = $('.question:eq('+temp+')').innerHeight();
				}
				// if(q_index == 7 || q_index == 8){
				// 	question = 250;
				// }
				//alert('q_index = '+q_index+"\nquestion = "+question);
				$('#interview').animate({height:question+'px'});
				$('#current').animate({left:'-'+left+'px'});
				//alert("it is now: " + q_index + "|| moving to: " + left + " pixels");
			}
		});
	    q_index++;
		return false;
	});

});