function isCapsLockOn(e){
	kc = e.keyCode ? e.keyCode : e.which;
	sk = e.shiftKey ? e.shiftKey : (kc == 16);
	return ((kc >= 65 && kc <= 90 && !sk) || (kc >= 97 && kc <= 122 && sk));
}

$(document).ready(function(){
	
	//add rounded corners to submit buttons
	$('li.submit div.input').append('<b class="tl">&nbsp;</b><b class="tr">&nbsp;</b><b class="bl">&nbsp;</b><b class="br">&nbsp;</b>');
	
	//initialize popup links
	$("a.popup").addClass("pop").attr("title", "[Opens in new window]").bind("click", function(){
		var popup = window.open($(this).attr("href"), "_blank", "height=500,width=600,scrollbars=yes");
		popup.focus();
		return false;
	});

	//initialize tooltips
	$("div.tooltip").hide();
	$("a.tooltip").bind("mouseover", function(){
		$("div.tooltip").show();
	}).bind("mouseout", function(){
		$("div.tooltip").hide();
	});

	//associate Caps Lock Warning where needed
	// inline-block display needed for IE7 "haslayout" issues with relative positioning
	$("#tigrRegForm .capsLock").parents("li").css({position: "relative", display: "inline-block"}).find("input").bind("keypress", function(event){
		if (isCapsLockOn(event)) {
			var h = $(this).parents("li").find("div.input").height();
			var w = $(this).parents("li").find("div.input").width() - 60;
			$(this).parents("li").find(".capsLock").html("<b>Warning!</b><br />Caps Lock is on").css("bottom", h+"px").css("left", w+"px").show();
		} else {
			$(this).parents("li").find(".capsLock").text("").hide();
		}
	}).bind("blur", function(){
		$(this).parents("li").find(".capsLock").text("").hide();
	});

	//validate field and show/hide field tip on events
	$("#tigrRegForm :text, #tigrRegForm :password").bind("focus", function(){
		if ($(this).val().length == 0) {
			$(this).parents("li:not(:has(span.errorMsg))").find("span.tipMsg").show();
		}
	}).bind("blur", function(){
		$(this).parents("li").find("span.tipMsg").hide();
		$(this).parents("li").validateTigrForm();
	});

	//associate "foreign" checkbox with ZIP code field
	$("#tigrRegForm .foreign input").bind("change click", function(){
		if ($(this).is(":checked")) {
			$("li.zip").removeError();
			$("li.zip input").attr("disabled", "disabled").val("").css("background-color","#eee").blur();
		} else {
			$("li.zip input").removeAttr("disabled").css("background-color","#fff");
		}
	}).change();

	//validate form on submit
	$("#tigrRegForm form").bind("submit", function(){
		return $(this).validateTigrForm();
	});

	//hide field tips, show error messages, and put focus on first input field (if first input is not a submit button)
	$("span.tipMsg").hide();
	if($("#tigrRegForm input:first").attr('type') != 'submit'){
		$("#tigrRegForm input:first").focus();
	}
});

function isBlank(input) {
	return ($(input).val() == "");
}
function isValidEmail(input) {
	var filter  = /^([a-zA-Z0-9_.-/+])+@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,6})$/;
	return (filter.test($(input).val()));
}
function isWordPassword(input) {
	return ($(input).val() == "password");
}
function matchesEmail(input) {
	return ($(input).val() == $("#email").val());
}
function isValidPassword(input) {
	return ($(input).val().length >= 8);
}
function isValidUsername(input) {
	var filter  = /^([a-zA-Z0-9]{6,15})$/;
	return (filter.test($(input).val()));
}

function isValidDate(input_MM, input_DD, input_YYYY) {
	return true;
}
function isUnderage(input_MM, input_DD, input_YYYY) {
	var neededAge = 13;
	var birthdate  = Date.UTC($(input_YYYY).val(), $(input_MM).val()-1, $(input_DD).val());
	var today = new Date();
	var neededBirthdate = Date.UTC(today.getFullYear()-neededAge, today.getMonth(), today.getDate());
	return (birthdate > neededBirthdate);
}
function isChecked(input) {
	return ($(input).is(":checked"));
}
function isValidZip(input) {
	var filter  = /^([0-9]{5})$/;
	return (filter.test($(input).val()));
}
function isChecked(input) {
	return ($(input).is(":checked"));
}
function matchesPreviousInput(input) {
	return ($(input).val() == $(input).parents("li").prev("li").find("input").val());
}

jQuery.fn.extend({
	//addError: add the error classes and message to a field
	addError: function(txt){
		$(this).find("label, input").addClass("error");
		$(this).find("span.errorMsg").text(txt);
		if ($(this).find("span.errorMsg").text() != txt) {
			$(this).find("div.tip").append("<span class=\"errorMsg\">" + txt + "</span>")
		}
	},
	//removeError: remove the error styling from a field
	removeError: function(){
		$(this).find("label, input").removeClass("error");
		$(this).find("span.errorMsg").remove();
	},
	//validation can be applied to the form or a specific input field
	validateTigrForm: function(){
    	var hasErrors = false;
    	$(this).find("li").andSelf().each(function() {
    		if ($(this).hasClass("email")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(EMAIL_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidEmail($(this).find("input"))) {
    				$(this).addError(EMAIL_INVALID_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("emailConfirm")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(CONFIRM_EMAIL_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!matchesPreviousInput($(this).find("input"))) {
    				$(this).addError(EMAIL_MATCH_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("password")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(PASSWORD_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (isWordPassword($(this).find("input"))) {
    				$(this).addError(PASSWORD_IS_PASSWORD_ERROR);
    				hasErrors = true;
    			}
    			else if (matchesEmail($(this).find("input"))) {
    				$(this).addError(PASSWORD_IS_EMAIL_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidPassword($(this).find("input"))) {
    				$(this).addError(PASSWORD_INVALID_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("passwordConfirm")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(CONFIRM_PASSWORD_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!matchesPreviousInput($(this).find("input"))) {
    				$(this).addError(PASSWORD_MATCH_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("newPassword")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(PASSWORD_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (isWordPassword($(this).find("input"))) {
    				$(this).addError(PASSWORD_IS_PASSWORD_ERROR);
    				hasErrors = true;
    			}
    			else if (matchesEmail($(this).find("input"))) {
    				$(this).addError(PASSWORD_IS_EMAIL_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidPassword($(this).find("input"))) {
    				$(this).addError(PASSWORD_INVALID_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("newPasswordConfirm")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(CONFIRM_PASSWORD_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!matchesPreviousInput($(this).find("input"))) {
    				$(this).addError(PASSWORD_MATCH_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("firstname")) {
    			if (isBlank($(this).find("input"))) {
    				$(this).addError(FIRSTNAME_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("lastname")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(LASTNAME_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("username")) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(USERNAME_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidUsername($(this).find("input"))) {
    				$(this).addError(USERNAME_INVALID_ERROR);
    				hasErrors = true;
    			}
				
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("zip") && !($("#tigrRegForm .foreign input").is(":checked"))) {
				if (isBlank($(this).find("input"))) {
    				$(this).addError(ZIP_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidZip($(this).find("input"))) {
    				$(this).addError(ZIP_INVALID_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("birthDate")) {
				var month = $(this).find("option:selected").eq(0);
				var day = $(this).find("option:selected").eq(1);
				var year = $(this).find("option:selected").eq(2);
				if (isBlank(month) || isBlank(day) || isBlank(year)) {
    				$(this).addError(BIRTHDATE_BLANK_ERROR);
    				hasErrors = true;
    			}
    			else if (isUnderage(month, day, year)) {
    				$(this).addError(BIRTHDATE_UNDERAGE_ERROR);
    				hasErrors = true;
    			}
    			else if (!isValidDate(month, day, year)) {
    				$(this).addError(BIRTHDATE_INVALID_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    		else if ($(this).hasClass("tos")) {
    			if (!isChecked($(this).find("input"))) {
    				$(this).addError(I_AGREE_ERROR);
    				hasErrors = true;
    			}
    			else {
    				$(this).removeError();
    			}
    		}
    	});

    	if ($(this).is("form")) {
    		$(this).find("input").blur().filter(":submit").focus();
    	}

    	return !hasErrors;
	}
});