$("document").ready(
	function()
	{
		$(".errorMsg").hide();
		$.getScript("/jsf/jquery.form.js", function()
			{
				fvMakeEmpty();
			
				fvFormRequired();

				fvInitialen();
				fvCapitalize();
				fvLowercase();
				fvInt();
				fvFloat();
				fvTelefoon();
				fvPostcode();
				fvUrl();
				fvRequired();
				fvEmail();

				//Set maxlength of all the textarea (call plugin)  
				// Deze plugin staat onderaan dit bestand
				$().maxlength();
			}
		);
	}
);

function fvMakeEmpty()
{
	$(".makeempty").each(
			function()
			{
				if ($(this).val() == '')
				{
					$(this).val($(this).attr("title"));
				}
			}
	);
	
	$(".makeempty").focus(
			function()
			{
				if ($(this).val() == $(this).attr("title"))
				{
					$(this).val('');
				}
			}
	).blur(
			function()
			{
				if ($(this).val() == '')
				{
					$(this).val($(this).attr("title"));
				}
			}
	);
}

function fvAddError(comp)
{
	if (!comp.hasClass('error'))
	{
		comp.addClass('error');
		comp.parent().addClass('error');
	}
}

function fvRemoveError(comp)
{
	if (comp.hasClass('error'))
	{
		comp.removeClass('error');	
		comp.parent().removeClass('error');
	}
}


function fvFormRequired()
{
	$("form.validate").submit(function()
		{
			var bCorrect = true;
			$(".required").each(function()
				{

					if ($(this).fieldValue() == '')
					{
						bCorrect = false;
						fvAddError($(this));
					}
					else 
					{
						fvRemoveError($(this));
					}
				}
			);
			
			if (bCorrect)
			{
				$(".errorMsg").hide();
			}
			else
			{
				$(".errorMsg").show();
			}
			
			$("input.error:eq(0)").each(
				function()	
				{
					$(this).focus();
				}	
			);
			
			return bCorrect;
		}
	);
}

function fvRequired()
{
	$(".required").blur(function()
		{
			if ($(this).fieldValue() == '')
			{
				bCorrect = false;
				fvAddError($(this));
			}
			else
			{
				fvRemoveError($(this));
			}
		}
	);
}

function fvEmail()
{
	$(".email").blur(function()
		{
			var email = $(this).fieldValue();
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

			if (reg.test(email) == false)
			{
				fvAddError($(this));
			}
			else
			{
				fvRemoveError($(this));
			}
		}
	);
}

function fvInitialen()
{
	$(".initialen").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';
	
			value = value.toUpperCase();

			for (var i = 0; i < value.length; i++)
			{
				if (value.charCodeAt(i) >= 65 &&  value.charCodeAt(i) <= 90)
				{
					value2 = value2+value[i]+".";
				}
			}

			$(this).val(value2);
		}
	);
}

function fvLowercase()
{
	$(".lowercase").keyup(
		function()
		{
			var value = $(this).val();
			$(this).val(value.toLowerCase());
		}
	);
}

function fvCapitalize()
{
	$(".capitalize").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';
			var start = 1;

			for (var i = 0; i < value.length; i++)
			{
				var letter = value[i];
				letter = letter.toLowerCase();

				if ((value.charCodeAt(i) == 32 || value.charCodeAt(i) == 45) && i > 0)
				{
					value2 = value2+value[i];	
					start = 1;
				}
				else if (letter.charCodeAt(0) >= 97 && letter.charCodeAt(0) <= 122)
				{
					if (start == 1)
					{
						value2 = value2+letter.toUpperCase();
					}
					else
					{
						value2 = value2+letter;
					}

					start = 0;
				}
			}

			$(this).val(value2);
		}
	);
}

function fvInt()
{
	$(".int").unbind("keyup");
	$(".int").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';

			for (var i = 0; i < value.length; i++)
			{
				var letter = value[i];
				
				if (letter.charCodeAt(0) >= 48 && letter.charCodeAt(0) <= 57)
				{
					value2 = value2+letter;
				}
			}

			$(this).val(value2);
		}
	);
}

function fvFloat()
{
	$(".float").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';

			for (var i = 0; i < value.length; i++)
			{
				var letter = value[i];
				
				if (letter.charCodeAt(0) >= 48 && letter.charCodeAt(0) <= 57 || letter.charCodeAt(0) == 46)
				{
					value2 = value2+letter;
				}
				else if (letter.charCodeAt(0) == 44)
				{
					value2 = value2+".";
				}
			}

			$(this).val(value2);
		}
	);
}

function fvTelefoon()
{
	$(".telefoon").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';

			for (var i = 0; i < value.length; i++)
			{
				var letter = value[i];
				
				if ((letter.charCodeAt(0) >= 48 && letter.charCodeAt(0) <= 57) || (letter.charCodeAt(0) == 45))
				{
					value2 = value2+letter;
				}
			}

			$(this).val(value2);
		}
	);
}

function fvPostcode()
{
/*
	$(".postcode").keyup(
		function()
		{
			var value = $(this).val();
			var value2 = '';

			value = value.toUpperCase();

			for (var i = 0; i < value.length; i++)
			{
				if ((i <= 3) && (value.charCodeAt(i) >= 48 && value.charCodeAt(i) <= 57))
				{
					if (i == 3)
					{
						value2 = value2 + value[i]+" ";
					}
					else
					{
						value2 = value2 + value[i];
					}
				}
				else if ((i > 3 && i < 7) && (value.charCodeAt(i) >= 65 && value.charCodeAt(i) <= 90)) 
				{
					value2 = value2 + value[i];
				}
			}

			$(this).val(value2);
		}
	);
	*/
}

function fvUrl()
{
	$(".url").change(
		function()
		{
			var value = $(this).val();
			var start = value.substring(7, 0);

			if (start == 'http://')
			{
				value = value.substring(7);
				$(this).val(value);
			}
		}
	);
}

/* PLUGINS */

jQuery.fn.maxlength = function(){

	$("textarea[maxlength]").keypress(function(event){
		var key = event.which;

		//all keys including return.
		if(key >= 33 || key == 13) {
			var maxLength = $(this).attr("maxlength");
			var length = this.value.length;
			if(length >= maxLength) {
				return false;

//				event.preventDefault();
			}
		}
	});
}
