emailRegex = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/
zipRegex = /^\d{5}$/
zipCARegex = /^[ABCEGHJKLMNPRSTVXY]\d[A-Z] \d[A-Z]\d$/
phoneRegex = /^\(?(\d{3})\)?[- ]?(\d{3})[- ]?(\d{4})$/

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	} 
	return this;
}

function isDate(month, day, year)
{
	var daysInMonth = DaysArray(12)
	
	if (month < 1 || month > 12) {
		return false;
	}
	
	if (day < 1 || day > 31 || (month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month]) {
		return false;
	}	
	return true;
}

function appendError($input, message) {
	$inputError = $input.closest(".input").find(".input-error");
	if(!$inputError.length) {
		$inputError = $("<span class=\"input-error\" />");
		$input.closest(".input").append($inputError);
	}
	$inputError.html(message);
}

function validateExtrasForm($form) {

	var success = true;

	if ($form.find("input[name=FirstName]").val() == "") {
		appendError($form.find("input[name=FirstName]"), "This field is required.");
		success = false;
	}

	if ($form.find("input[name=LastName]").val() == "") {
		appendError($form.find("input[name=LastName]"), "This field is required.");
		success = false;
	}

	if (($form.find("input[name=EmailAddress]").val() == "")) {
		appendError($form.find("input[name=EmailAddress]"), "This field is required.");
		success = false;
	}
	else if(!emailRegex.test($form.find("input[name=EmailAddress]").val())) {
		appendError($form.find("input[name=EmailAddress]"), "Please enter a valid email address.");
		success = false;
	}

	if ($form.find("input[name=EmailAddress]").val() != $form.find("input[name=EmailAddress2]").val()) {
		appendError($form.find("input[name=EmailAddress2]"), "Both email address entries must match.");
		success = false;
	}
	
	var dobm = $form.find("select[name=DOBM]").val();
	var dobd = $form.find("select[name=DOBD]").val();
	var doby = $form.find("select[name=DOBY]").val();
	
	if (
		dobm == ""
		|| dobd == ""
		|| doby == ""
	) {
		appendError($form.find("select[name=DOBM]"), "This field is required.");
		success = false;
	}
	else if (!isDate((dobm * 1), (dobd * 1), (doby * 1))) {
		appendError($form.find("select[name=DOBM]"), "Please enter a valid birthday.");
		success = false;
	}

	if ($form.find("input[name=Zip]").val() == "")
	{
		appendError($form.find("input[name=Zip]"), "This field is required.");
		success = false;
	}
	else if(window.location.pathname.indexOf('/mobile') == -1 && zipCARegex.test($form.find("input[name=Zip]").val().toUpperCase())) {
		appendError($form.find("input[name=Zip]"), "Please enter a valid zip code.<br>(From Canada? Click  <a href=\"https://arbysextras.fbmta.com/forms/arbyscanada/join.asp\"><u>here</u></a>.)");
		success = false;
	}
	else if(window.location.pathname.indexOf('/mobile') > -1 && zipCARegex.test($form.find("input[name=Zip]").val().toUpperCase())) {
		appendError($form.find("input[name=Zip]"), "Please enter a valid zip code.");
		if (confirm("You have entered a Canadian zip code, would you like to go to the Canadian page?")){
			window.location = 'https://arbysextras.fbmta.com/forms/arbyscanada/join.asp';
		}
		success = false;
	}
	else if(!zipRegex.test($form.find("input[name=Zip]").val())) {
		appendError($form.find("input[name=Zip]"), "Please enter a valid zip code.");
		success = false;
	}
	
	if (
		($form.find("input[name=RecieveMobileOffers]").attr("checked") == true)
		&& (
			$form.find("input[name=MobilePhone]").val() == ""
			|| !(phoneRegex.test($form.find("input[name=MobilePhone]").val()))
		)
	) {	
		appendError($form.find("input[name=RecieveMobileOffers]"), "Please enter a valid phone number if you would like to recieve mobile offers.");
		success = false;
	}
	
	return success;
}

$(function() {

	// Remove/add input label
	$(".label-input").each(function() {

		var value = $(this).val();
	
		// Remove input label
		$(this).focus(function(evt) {
			if($(this).val() == value) {
				$(this).val("");
			}
		});
		
		//Re-add input label
		$(this).blur(function(evt) {
			if($(this).val() == "") {
				$(this).val(value);
			}
		});
	});

	$("#extras-signup-form .other-input input[type=text]").focus(function(evt) {
		$(this).prevAll("input[type=checkbox]").attr("checked", "checked");
	});
	
	$("#extras-signup-form").submit(function(evt) {
		
		$(".input-error").html("");
		var success = validateExtrasForm($(this));
		
		if(!success) {
			
			if(!$(this).find(".error-message").length) {
				$(this).prepend("<p class=\"error-message\">Please complete all required fields.</p>");
			}
			$(window).scrollTop(0);
			
			evt.preventDefault();
			return false;
		}
		
		// Set value of hidden birthdate field
		var dobm = $(this).find("select[name=DOBM]").val();
		var dobd = $(this).find("select[name=DOBD]").val();
		var doby = $(this).find("select[name=DOBY]").val();
		$(this).find("input[name=Birthdate]").val(dobm + "/" + dobd + "/" + doby);
		
		// Add list ID if mobile number provided
		if($(this).find("input[name=RecieveMobileOffers]").attr("checked") == true)
		{
			$(this).find("input[name=ListID]").val($(this).find("input[name=ListID]").val() + ",27917287486");
		}

		return true;
	});
	
$("#mobile-extras-signup-form").submit(function(evt) {
		
		$(".input-error").html("");
		var success = validateMobileExtrasForm($(this));
		
		if(!success) {
			
			if(!$(this).find(".error-message").length) {
				$(this).prepend("<p class=\"error-message\">Please complete all required fields.</p>");
			}
			$(window).scrollTop(0);
			
			evt.preventDefault();
			return false;
		}
		
		// Set value of hidden birthdate field
		var dobm = $(this).find("input[name=DOBM]").val();
		var dobd = $(this).find("input[name=DOBD]").val();
		var doby = $(this).find("input[name=DOBY]").val();
		$(this).find("input[name=Birthdate]").val(dobm + "/" + dobd + "/" + doby);
		
		// Add list ID if mobile number provided
		if($(this).find("input[name=RecieveMobileOffers]").attr("checked") == true)
		{
			$(this).find("input[name=ListID]").val($(this).find("input[name=ListID]").val() + ",27917287486");
		}

		return true;
	});	
});

