var pestanas = ["pestanaUsuario","pestanaPresentacion","pestanaForo","pestanaPresupuesto","pestanaContacto"];
comprobarIdentidad("");
var paginaAjax='login';

function openExternal(){
    if(!(!document.getElementsByTagName)){
        var anchors = document.getElementsByTagName('a');
        for(var i = 0; i < anchors.length; i++){
            var thisAnchor = anchors[i];
            if(thisAnchor.getAttribute('href') && thisAnchor.getAttribute('rel') == 'external'){
                thisAnchor.target = '_blank';
            }
        }
    }
}
function cargarPestanas(pestanaActiva){
    if(!pestanaActiva){
        pestanaActiva = "pestanaPresentacion";
    }
    for (var i=0;i<pestanas.length;i++){
        var pestana = document.getElementById(pestanas[i]);
        if(pestanaActiva==pestana.id){
            pestana.style.height="100%";
            cambiarContenido(pestana);
        }else{
            pestana.style.height="85%";
        }
    }
    openExternal();
}

function mostrarContenido(pestanaActual){
    for(var i = 0;i<pestanas.length;i++){
        var pestana = document.getElementById(pestanas[i]);
        if(pestanas[i]==pestanaActual.id){
            pestana.style.height="100%";
        }else{
            pestana.style.height="85%";
        }
    }
    cambiarContenido(pestanaActual);
}

function cambiarContenido(pestana){
    contenedorDetalles = document.getElementById('cuerpoContenido');
    contenedorDetalles.style.backgroundColor=pestana.style.backgroundColor;
    paginaAjax='cuerpoContenido';
    enviaPeticionAjax('manejadorPestanas.php','paginaPestana='+pestana.id);
}

function enviaPeticionAjax(url, variables){
	$.ajax({
		type: 'POST',
		url: url,
		data: variables,
		success: function(texto){
			document.getElementById(paginaAjax).innerHTML = texto;
			openExternal();
		},
		dataType: 'html'
	});
}

function comprobarIdentidad(formularioRecibido){
    paginaAjax='login';
    var variables = tratarFormulario(formularioRecibido);
    enviaPeticionAjax('login.php', variables)
}

function tratarFormulario(objetoFormulario){
    var variables = "";
    if(objetoFormulario!="")
    {
        var formulario = document.getElementById(objetoFormulario);
        var longitudFormulario = formulario.elements.length;
        var sepCampos = "";
        for (var i=0; i<longitudFormulario; i++)
        {
            variables += sepCampos+formulario.elements[i].id + '=' + encodeURI(formulario.elements[i].value);
            if(i<longitudFormulario-1)
            {
                sepCampos="&";
            }
            else
            {
                sepCampos="";
            }
        }
    }
    return variables;
}
