$(document).ready(function(){
    //Cuando hago click en el tilde de activar la tienda aparece el formulario de vendedor
    $("#storeactivation").click(function(){
        if ($("#storeactivation").attr("checked")) {
            $("#formvendedor").show("slow");
            activarCampo($("#NombreLocal").attr("id"));
            activarCampo($("#Texto").attr("id"));
            activarCampo($("#SubDominio").attr("id"));
            activarCampo($("#Tag").attr("id")); 
        }
        else {
            $("#formvendedor").hide("slow");
            desactivarCampo($("#NombreLocal").attr("id"));
            desactivarCampo($("#Texto").attr("id"));
            desactivarCampo($("#SubDominio").attr("id"));
            desactivarCampo($("#Tag").attr("id"));
        }
    });
    if ($("#storeactivation").attr("checked")) {
        $("#formvendedor").show("slow");
        activarCampo($("#NombreLocal").attr("id"));
        activarCampo($("#Texto").attr("id"));
        activarCampo($("#SubDominio").attr("id"));
        activarCampo($("#Tag").attr("id"));
    }
    $("#loading").css('display','none');
    $("#b_aceptar").click(function(){
        $("#loading").show();
    });
    $("#diferenteactivacion").click(function(){
        $("#formDiferentesDatos").toggle("slow");
    });
    $("#idpais").change(function() {
       cargaContenido($("#idpais").attr("id"));
    });
    $("#idpais_Vendedor").change(function() {
       cargaContenido($("#idpais_Vendedor").attr("id"));
    });
    //Checkbox de la pagina de facebook
    $("#IdFacebook").click(function(){
        if ($("#IdFacebook").attr("checked")) {
            $("#formfacebook").show("slow");
            activarCampo($("#FacebookPage").attr("id"));
        }
        else {
            $("#formfacebook").hide("slow");
            desactivarCampo($("#FacebookPage").attr("id"));
        }
    });
     //Checkbox del video de youtube
    $("#IdMovil").click(function(){
        if ($("#IdMovil").attr("checked")) {
            $("#formmovil").show("slow");
            activarCampo($("#Movil_Vendedor").attr("id"));
        }
        else {
            $("#formmovil").hide("slow");
            desactivarCampo($("#Movil_Vendedor").attr("id"));
        }
    });
    //Checkbox del Blog
    $("#IdBlog").click(function(){
        if ($("#IdBlog").attr("checked")) {
            $("#formblog").show("slow");
            activarCampo($("#BlogPage").attr("id"));
        }
        else {
            $("#formblog").hide("slow");
            desactivarCampo($("#BlogPage").attr("id"));
        }
    });
    //Checkbox del album de fotos
    $("#IdFoto").click(function(){
        if ($("#IdFoto").attr("checked")) {
            $("#formfoto").show("slow");
            activarCampo($("#Foto").attr("id"));
        }
        else {
            $("#formfoto").hide("slow");
            desactivarCampo($("#Foto").attr("id"));
        }
    });
    //FUNCION QUE ABRE TODO CUANDO RETORNA DE UN ERROR
    apertura();
    //Muestro el cuadro de mensajes
    $('#dialog').dialog({
            autoOpen: true,
            draggable: true,
            width: 300
        });

});
hideLoading= function(){
    $("#loading").hide();
};
function campoUnico(campo,pvalue,options){
    if (options.modo==='N'){
        var resultStyle = new Array();
        var resultText  = new Array();
        var field = campo;
        $.ajax({
            type: 'POST',
            url: options.url,
            data: {value: pvalue, field: field},
            success: function(response){
                if (response == '1'){
                    resultStyle[options.baseId] = options.availableClass;
                    resultText[options.baseId] = pvalue + ' ' + options.availableLabel;
                } else {
                    resultStyle[options.baseId] = options.unavailableClass;
                    resultText[options.baseId]= pvalue + ' ' + options.unavailableLabel;
                }
                if(options.location === 1) {
                    $("#"+campo).parent().children("#" + options.baseId).remove();
                    $("#"+campo).parent().append("<span id=\""+options.baseId+"\" class=\""+options.baseClass+"\"></span>");
                    $("#"+campo).parent().children("#" + options.baseId).addClass(resultStyle[options.baseId]).text(resultText[options.baseId]);
                } else {
                    $("#"+campo).prev("#" + options.baseId).remove();
                    $("#"+campo).before("<span id=\""+options.baseId+"\" class=\""+options.baseClass+"\"><span></span></span>");
                    $("#"+campo).prev("#" + options.baseId).addClass(resultStyle[options.baseId]).find("span").text(resultText[options.baseId]);
                }
                if (response == '1'){
                    return 1;
                } else if(response == '0'){
                    return 0;
                }
            }
        });
    }
}
function apertura() {
        if ($("#isStore").attr("value")==1) {
            $("#formvendedor").show();
            activarCampo($("#NombreLocal").attr("id"));
            activarCampo($("#Texto").attr("id"));
            activarCampo($("#SubDominio").attr("id"));
            activarCampo($("#Tag").attr("id"));
        }
        if ($("#diferenteactivacion").attr("checked")) {
           $("#formDiferentesDatos").show();
        }
        if ($("#IdMovil").attr("checked")) {
            $("#formmovil").show();
            //         activarCampo($("#Movil_Vendedor").attr("id"));
            $("#Movil_Vendedor").attr('disabled','');
        }
        if ($("#IdFacebook").attr("checked")) {
            $("#formfacebook").show();
            //activarCampo($("#FacebookPage").attr("id"));
            $("#FacebookPage").attr('disabled','');
        }
        if ($("#IdFoto").attr("checked")) {
            $("#formfoto").show();
            $("#Foto").attr('disabled','');
            //activarCampo($("#Foto").attr("id"));
        }
        if ($("#IdBlog").attr("checked")) {
             $("#formblog").show();
             $("#BlogPage").attr('disabled','');
             //activarCampo($("#BlogPage").attr("id"));
        }
    }
//Se le debe pasar el ID y activa el elemento
//Las funciones enable y disable son de la libreria LIVE VALIDATION
function activarCampo(objeto) {
	this[objeto].enable();
}
function desactivarCampo(objeto) {
    this[objeto].disable();
}
