/**
 * JS
 * @author Darío Ruellan <druellan@ecimtech.com>
 * @copyright ecimTech 2008
 */

$(function()
{
	function showError(f, errorInfo)
	{
		// disabled all errors by default
		$("#contact_form .msg").removeClass("required").removeClass("bad").html("");
		
		for (var i=errorInfo.length-1; i >= 0; i--)
		{
			var fieldName;

			// radio button
			if (errorInfo[i][0].type == undefined)
				fieldName = errorInfo[i][0][0].name;
			else
				fieldName = errorInfo[i][0].name;

			// display the error
			$("#contact_form #"+fieldName).focus()
			.next(".msg").addClass("bad").html(errorInfo[i][1]).hide().fadeIn("slow");
		}

		if ( errorInfo.length == 0 )
		{
			// Testeamos captcha
			var c_user = calcMD5( $("#captcha").val().toUpperCase() );
			var c_check = $("#contact_form input[name=captcha_check]").val();
			
			if( c_user != c_check )
			{
				$("#contact_form #captcha").focus()
				.next(".msg").addClass("bad").html("Check the text").hide().fadeIn("slow");
				return false;
			}
			
			$("#contact_form_sending").show();
			return true;
		}
		else
			return false;

	}
	
	$("#contact_form").RSV(
	{
		customErrorHandler: showError,		
		rules:
		[
			"required,nam,Please enter your name",
			"required,las,Enter your last name",
			"required,mai,Please enter an email",
			"valid_email,mai,Email seems invalid!",
			"required,cou,Select your country",
			"required,mes,Write us a message",
			"required,captcha,Enter the verification text"
		]
	});
});

