$().ready(function() {

    // MANIPULACIÓN DEL SELECT MULTIPLE

    $.getJSON("/candidatos/alta/ajaxcategoriaspadre", function(p_categorias) {
        var categoriasWrapperHtml = '<span class="fieldset txtcenter">' +
            '<input id="btnCategoriasAdd" class="resetbutton color00 fontbig" tabindex="3" type="button" title="' + aVarios['VAR0106'] + '" value="' + aVarios['VAR0108'] + ' »"/>' +
            '<input id="btnCategoriasRemove" class="resetbutton color00 fontbig" tabindex="5" type="button" title="' + aVarios['VAR0107'] + '" value="« ' + aVarios['VAR0109'] + '"/>' +
                '</span>' +
                '<fieldset>' +
                    '<p>' + aVarios['VAR0110'] + '</p>' +
                    '<select tabindex="4" size="15" multiple="multiple" name="categorias_sel[]" id="categorias_sel" title="' + aVarios['VAR0111'] + '">' +
                    '</select>' +
                '</fieldset>';
        $("#categoriasWrapper").html(categoriasWrapperHtml);

        var aContenedor = new Array;
        for (i = 0; i < p_categorias.length; i++) {
            aContenedor[i] = new Object();
            aContenedor[i].text = p_categorias[i]["VALOR"];
            aContenedor[i].value = p_categorias[i]["ID_CATEGORIA"];
            aContenedor[i].padre = p_categorias[i]["VALOR_PADRE"];
            aContenedor[i].id_padre = p_categorias[i]["FK_ID_CATEGORIA"];
            aContenedor[i].from = true;
            aContenedor[i].to = false;
            aContenedor[i].hijas = p_categorias[i]["HIJAS"];
        }

        $("#p_categorias").dblclick(function(){
            moverSelect(aContenedor, $("#p_categorias option:selected").val(), "p_categorias", "categorias_sel", false, true, "color05");
        });
        $("#btnCategoriasAdd").click(function(){
            moverTodosSelect(aContenedor, "p_categorias", "p_categorias", "categorias_sel", false, true, "color05");
        });
        $("#categorias_sel").dblclick(function(){
            moverSelect(aContenedor, $("#categorias_sel option:selected").val(), "p_categorias", "categorias_sel", true, false, "color05");
        });
        $("#btnCategoriasRemove").click(function(){
            moverTodosSelect(aContenedor, "categorias_sel", "p_categorias", "categorias_sel", true, false, "color05");
        });
        $("#submitForm").click(function(){
            moverTodosSelect(aContenedor, "p_categorias", "p_categorias", "categorias_sel", false, true, "color05");
            $("#categorias_sel").each(function(){
                $("#categorias_sel option").attr("selected","selected");
            });
        });
        moverTodosSelect(aContenedor, "p_categorias", "p_categorias", "categorias_sel", false, true, "color05");
    });

    $.getJSON("/candidatos/alta/ajaxprovincias", function(p_provincias) {
        var provinciasWrapperHtml = '<span class="fieldset txtcenter">' +
            '<input id="btnProvinciasAdd" class="resetbutton color00 fontbig" tabindex="7" type="button" title="' + aVarios['VAR0112'] + '" value="' + aVarios['VAR0108'] + ' »"/>' +
            '<input id="btnProvinciasRemove" class="resetbutton color00 fontbig" tabindex="9" type="button" title="' + aVarios['VAR0113'] + '" value="« ' + aVarios['VAR0109'] + '"/>' +
                '</span>' +
                '<fieldset>' +
                    '<p>Provincias seleccionadas</p>' +
                    '<select tabindex="8" size="5" multiple="multiple" name="provincias_sel[]" id="provincias_sel" title="' + aVarios['VAR0111'] + '">' +
                    '</select>' +
                '</fieldset>';
        $("#provinciasWrapper").html(provinciasWrapperHtml);

        var aContenedor = new Array;
        for (i = 0; i < p_provincias.length; i++) {
            aContenedor[i] = new Object();
            aContenedor[i].text = p_provincias[i]["VALOR"];
            aContenedor[i].value = p_provincias[i]["ID_PROVINCIA"];
            aContenedor[i].from = true;
            aContenedor[i].to = false;
        }

        $("#p_provincias").dblclick(function(){
            moverSelect(aContenedor, $("#p_provincias option:selected").val(), "p_provincias", "provincias_sel", false, true);
        });
        $("#btnProvinciasAdd").click(function(){
            moverTodosSelect(aContenedor, "p_provincias", "p_provincias", "provincias_sel", false, true);
        });
        $("#provincias_sel").dblclick(function(){
            moverSelect(aContenedor, $("#provincias_sel option:selected").val(), "p_provincias", "provincias_sel", true, false);
        });
        $("#btnProvinciasRemove").click(function(){
            moverTodosSelect(aContenedor, "provincias_sel", "p_provincias", "provincias_sel", true, false);
        });
        $("#submitForm").click(function(){
            moverTodosSelect(aContenedor, "p_provincias", "p_provincias", "provincias_sel", false, true);
            $("#provincias_sel").each(function(){
                $("#provincias_sel option").attr("selected","selected");
            });
        });
        moverTodosSelect(aContenedor, "p_provincias", "p_provincias", "provincias_sel", false, true);
    });

    // VALIDACIÓN DEL FORMULARIO

    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();
    $("#altacandidato").validate({
        onkeyup: false,
        onfocusout: false,
        onclick: false,
		rules: {
			p_email: {
				required: true,
				email: true,
				maxlength: 255
			},
			"categorias_sel[]": {
			    required: true
			},
			"provincias_sel[]": {
			    required: true
			}
		},
		messages: {
			p_email: {
			    required: aErrores['ERR0001'],
			    email: aErrores['ERR0001'],
			    maxlength: jQuery.format(aErrores['ERR0003'], $("label[for='p_email']").attr("title"))
			},
			"categorias_sel[]": {
			    required: aErrores['ERR0006']
			},
			"provincias_sel[]": {
			    required: aErrores['ERR0007']
			}
		},
		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");
            }
        }
    });
});
