﻿jQuery(function() {
    var p = jQuery("#formContact").validate({
        submitHandler: function(form) {
            for (var i = 0; i < $(".modPerfil div input[type$='checkbox']").length; i++) {
                if ($(".modPerfil div input[type$='checkbox']").get(i).checked) {
                    if ($(".modPerfil div input[type$='checkbox']").get(i).name == "chkOtros1") { checkOtros = 1; }
                    $(".modPerfil div input[type$='checkbox']").get(i).parentNode.className = "checkedItem";
                }
            }

            submitItemForm(form);
        },
        errorLabelContainer: null
    });
    // show a simple loading indicator
    var loader = jQuery('<div id="loader">Enviando...</div>')
			    .css({ position: "fixed", top: "1em", textAlign: "center" })
			    .appendTo("body")
			    .hide();
    jQuery().ajaxStart(function() {
        var backItem = $("#BackAlertLay");
        backItem.show();
        loader.show();
    }).ajaxStop(function() {
        loader.hide();
        p.resetForm();
    }).ajaxError(function(a, b, e) {
        throw e;
    });
    jQuery("#back").click(function() {
        open("/Contacto", "_self");
        return false;
    });
});

function submitItemForm(form) {
    var checkOtros = 0;
    var txtPerfil = "";
    var txtComillas = "";
    
    for (var p = 0; p < $(".checkedItem").length; p++) {
        if (p == 0) { txtComillas = ""; }
        else { txtComillas = ", " }
        if (checkOtros && ($(".checkedItem span").get(p).innerHTML == "Otros")) {
            txtPerfil += txtComillas + "Otros ('" + $("input[name$='txtOtros1']").val() +"')";
        }
        else {
            txtPerfil += txtComillas + $(".checkedItem span").get(p).innerHTML;
        }
    }

    $("input[name$='txtPerfil']").val(txtPerfil);

    form.submit();
    return false;
}

$(document).ready(function() {
    if ($(".modPerfil").length != 0) {
        $(".modPerfil h5").click(function() {
            $("#" + this.parentNode.id + " div").toggle(400);
        });
    }

    if (sendMsg == 'true') { openWin.init(['alertOK', 'Envío de Curriculum', 'Gracias, tu currículum se ha enviado correctamente.', ""]); }
});	
