﻿$(window).load(function() {
	$("input.numeric").each(function() {
		$(this).keydown(function(event) {
			//Exclude Delete, Backspace, Tab, Right Arrow, Left Arrow, Home and End
			if (event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 39 || event.keyCode == 37 || event.keyCode == 36 || event.keyCode == 35) {
				//Allow the even to be handled
			}
			else {
				//Ensure that it is a number, and if not, stop the keypress
				if (!((event.keyCode >= 48 && event.keyCode <= 57) || (event.keyCode >= 95 && event.keyCode <= 106)) || event.shiftKey) {
					event.preventDefault();
				}
			}
		});
	});

	$("textarea.maxLength").each(function() {
		var maxLength = parseInt($(this).attr("maxLength"));
		var thisId = $(this)[0].id + "LengthLabel";
		if ($("#" + thisId).length == 0) {
			$(this).after("<span id='" + thisId + "' style='color:#cccccc;font-size:0.8em;'>0/" + maxLength + "</span>");
		}
		$(this).keyup(function(event) {
			if (maxLength != NaN) {
				var currentLength = parseInt($(this).text().length);

				if (currentLength >= maxLength && event.keyCode != 46 && event.keyCode != 8) {
					var currentText = $(this).text();
					$(this).text(currentText.substring(0, maxLength));
					$("#" + thisId).text(maxLength + "/" + maxLength);
				}
				else {
					if ($("#" + thisId).length == 0) {
						$(this).after("<span id='" + thisId + "' style='color:#cccccc;font-size:0.8em;'>" + currentLength + "/" + maxLength + "</span>");
					}
					else {
						$("#" + thisId).text(currentLength + "/" + maxLength);
					}
				}
			}
		});
	});
});


function validateInputs() {
	var result = false;
	var err = "";
	$("#contact input.required,#contact textarea.required").each(function() {
		var fieldName = $(this).attr("FieldName");
		if (fieldName != null) {
			err += $(this)[0].value.length == 0 ? $(this).attr("FieldName") + " is required.\n" : "";
		}
	});
	if (err != null && err.length > 0) {
		alert(err);
	}
	else {
		result = true;
	}
	return result;
}
