$().ready(function() {

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

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

    var errorWrapperHtml = '<fieldset class="marginframe marginstandard back05">' +
    '<legend>' + aMensajes['MSG0001'] + '</legend>' +
    '<ol class="list styleoutside color08 fontmedium"><span id="errorForm"></span></ol>' +
    '</fieldset>';
    $("#errorWrapper").html(errorWrapperHtml);
    $("#errorWrapper").hide();
    $("#amigo_form").validate({
        onkeyup: false,
        onfocusout: false,
		rules: {
    		email: {
				required: true,
				email: true,
				maxlength: 255
			},
    		name: {
				required: true,
				maxlength: 255
			},
    		from: {
				required: true,
				email: true,
				maxlength: 255
			},
			captcha: {
				required: true
			}
		},
		messages: {
			email: {
			    required: aErrores['ERR0001'],
			    email: aErrores['ERR0001'],
			    maxlength: jQuery.format(aErrores['ERR0003'], $("label[for='email']").attr("title"))
			},
			name: {
			    required: aErrores['ERR0012'],
			    maxlength: jQuery.format(aErrores['ERR0003'], $("label[for='name']").attr("title"))
			},
			from: {
			    required: aErrores['ERR0001'],
			    from: aErrores['ERR0001'],
			    maxlength: jQuery.format(aErrores['ERR0003'], $("label[for='from']").attr("title"))
			},
			captcha: {
			    required: aErrores['ERR0101']
			}
		},
		errorLabelContainer: "#errorForm",
		errorElement: "li",
        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");
            }
        }
    });
});
