$(document).ready(function() {
	paginate();
	
	$("[rel^=maxlength=]").each(function(itemCount,DOMObject) {
		$(DOMObject).keydown(function(eventData) {
			console.log("start");
			MaxLength = parseInt($(DOMObject).attr("rel").split("maxlength=")[1]);
			
			window.status = (MaxLength-DOMObject.value.length) + " characters remaining.";
			$(this).parent().find("[rel=maxLengthLabel]").text(window.status).css("color","#00CC00");
			if (DOMObject.value.length >= MaxLength) {
				if (eventData.keyCode != 8) {
					DOMObject.value = DOMObject.value.substr(0,MaxLength);
					$(this).parent().find("[rel=maxLengthLabel]").text("Zero characters remaining.").css("color","#CC0000");
					return false;
				}
			}
			console.log("end");
		});
		
		$(DOMObject).keyup(function(eventData) {
			$(DOMObject).keydown(eventData);
		});
	});
	
	// capitalize first character of street names
	$("input.capitalize-first").blur(function() {
		var val = $(this).val();
		if(val.length >= 2) {
			val = val.substring(0,1).toUpperCase() + val.substring(1);
			$(this).val(val);
		}
	});
});

function nextPage() {
	if (validateCurrentPage()) {
		if (window.currentPage < window.pageCount) {
			window.currentPage ++;
			setProgressBar(window.currentPage);
			flipPage();
		
			if (window.currentPage == window.pageCount) {
				$(".nextbutton").hide();
			}
			
			if (window.currentPage > 1) {
				$(".prevbutton").show();
			}
		} else {
			return false;
		}
	}
}

function submitForm() {
	$("input[type=submit], .nominationprogress, .prevbutton, .nextbutton").hide();
	$("#stage5 h4").remove();
	$("#stage5 fieldset *").hide();
	$("#stage5 fieldset").append("<h4 style='text-align: center; margin-top: 50px;'>Please Wait</h4><p style='text-align: center;'><img src='/graphics/loader.gif' width='128' height='15'/><p>Your nomination is now being submitted. Please be aware that if you have chosen to upload supporting documentation, this procedure may take a number of minutes (depending on your internet connection.)</p>");
	return validateCurrentPage();
}

function previousPage() {
	if (window.currentPage > 1) {
		window.currentPage --;
		setProgressBar(window.currentPage);
		flipPage();
		
		if (window.currentPage == 1) {
			$(".prevbutton").hide();
		}
		
		if (window.currentPage < window.pageCount) {
			$(".nextbutton").show();
		}
	} else {
		return false;
	}
}

function validateCurrentPage() {
	// find all required fields on this page
	var requirementsMet = true;
	$("#stage" + window.currentPage).find("[rel=required]").each(function(itemCount,DOMObject) {
		if (DOMObject.tagName == "SELECT") {
			if (DOMObject.options[DOMObject.selectedIndex].text == "Please Select") {
				requirementsMet = false;
				$(DOMObject).css({
									backgroundColor:	"#d1526c",
									fontWeight:			"bold",
									color:				"white"
								});
			} else {
				$(DOMObject).css({
									backgroundColor:	"white",
									fontWeight:			"normal",
									color:				"black"
								});
			}
		} else {
			if (DOMObject.value.length == 0) {
				requirementsMet = false;
				$(DOMObject).css({
									backgroundColor:	"#d1526c",
									fontWeight:			"bold",
									color:				"white"
								});
			} else {
				$(DOMObject).css({
									backgroundColor:	"white",
									fontWeight:			"normal",
									color:				"black"
								});
			}
		}
	});
	
	$("#stage" + window.currentPage).find("[rel^=required,g]").each(function(itemCount,DOMObject) {
		var groupName = $(DOMObject).attr("rel").split("g=")[1];
		
		if (DOMObject.value.length == 0) {
			var totalLength = $("#stage" + window.currentPage).find("[rel$=" + groupName + "]").length;
			var checkLength = $("#stage" + window.currentPage).find("[rel$=" + groupName + "]").filter(":checked").length;
			
			if (!checkLength) {
				var valueLength = 0;
				
				if ($("#stage" + window.currentPage).find("[rel$=" + groupName + "]").filter(":checkbox").length == 0) {
					$("#stage" + window.currentPage).find("[rel$=" + groupName + "]").each(function(itemCount,DOMObject) {
						if (DOMObject.value.length > 0) {
							valueLength ++;
						}
					});
				}
				
				if (!valueLength) {
					requirementsMet = false;
					$(DOMObject.parentElement).css({
										backgroundColor:	"#d1526c",
										fontWeight:			"bold",
										color:				"white"
									});
				} else {
					$(DOMObject.parentElement).css({
									backgroundColor:	"white",
									fontWeight:			"normal",
									color:				"black"
								});
				}
			} else {
				$(DOMObject.parentElement).css({
									backgroundColor:	"white",
									fontWeight:			"normal",
									color:				"black"
								});
			}
		} else {
			$(DOMObject.parentElement).css({
								backgroundColor:	"white",
								fontWeight:			"normal",
								color:				"black"
							});
		}
	});
	
	if (!requirementsMet) {
		alert("You must complete all the required fields before moving to the next stage. The required fields are highlighted red.");
	}
	
	return requirementsMet;
}

function flipPage() {
	$("div.formstage").hide();
	$("#stage" + window.currentPage).show();
}

function paginate() {
	window.pageCount = $("div.formstage").length;
	window.currentPage = 1;
	$(".prevbutton").hide();
	$("div.formstage").filter(":not(#stage1)").hide();
	
	setProgressBar(1);
	
	/* bind */
	$(".prevbutton").click(function() {previousPage()});
	$(".nextbutton").click(function() {nextPage()});
}

function setProgressBar(page) {
	var newWidth = 211;
	if ((page*45) < 211) {
		newWidth = (page*45);
	}
	
	$('#progressindicator').animate({width:newWidth+"px"},1000);
}