$().ready(function() {

	// Asgino al enlace de obtener nueva imagen la actualizacion ajax

	$("#refrescar_captcha").click(function() {
		$.ajax({
            type: "POST",
            url: "/publicidad/newcaptcha",
            data: "",
            dataType: "json",
            success: function (htmlResponse) {
					$("#captcha_id").val(htmlResponse.id);
                	$("#captcha_img").html(htmlResponse.img);
            }
        });
		return false;
	});

    var errorWrapperHtml = '<div id="error"><span id="errorForm"></span></div>';
    $("#errorWrapper").html(errorWrapperHtml);
    $("#errorWrapper").hide();
    $("#formulario").validate({
        onkeyup: false,
        onfocusout: false,
		rules: {
    		nombre: {
				required: true,
				maxlength: 255
			},
	        cargo: {
                required: true,
                maxlength: 255
            },
    		company: {
				required: true,
				maxlength: 255
			},
    		email: {
				required: true,
				email: true,
				maxlength: 255
			},
			telefono: {
				required: true,
				digits: true,
				maxlength: 50
			},
			comentarios: {
				required: true,
				maxlength: 20000
			},
			captcha: {
				required: true
			}
		},
		messages: {
			nombre: {
			    required: "&#8226; " + aErrores['ERR0073'],
			    maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='nombre']").attr("title"))
			},
            cargo: {
                required: "&#8226; " + aErrores['ERR0077'],
                maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='cargo']").attr("title"))
            },
			company: {
				required: "&#8226; " + aErrores['ERR0110'],
				maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='company']").attr("title"))
			},
			email: {
				required: "&#8226; " + aErrores['ERR0004'],
				email: "&#8226; " + aErrores['ERR0004'],
				maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='email']").attr("title"))
			},
			telefono: {
				required: "&#8226; " + aErrores['ERR0060'],
				digits: "&#8226; " + jQuery.format(aErrores['ERR0050'], $("label[for='telefono']").attr("title")),
				maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='telefono']").attr("title"))
			},
			comentarios: {
				required: "&#8226; " + aErrores['ERR0074'],
				maxlength: "&#8226; " + jQuery.format(aErrores['ERR0003'], $("label[for='comentarios']").attr("title"))
			},
			captcha: {
			    required: "&#8226; " + aErrores['ERR0072']
			}
		},
		errorLabelContainer: "#errorForm",
		errorElement: "p",
        showErrors: function(errorMap, errorList) {
            $("#errorWrapper").hide();
            $("#errorFormWrapper").hide();
            $("#errorForm").html("");
            if (this.numberOfInvalids() > 0) {
                $("#errorWrapper").show();
        	    this.defaultShowErrors();
        	    window.location.hash="form";
            } else {
                $("#submitForm").attr("disabled", "disabled");
            }
        }
    });
});
