var cal = null;
var RUTA_MENSAJES_SERVIDOR = "/Seguridad/mensajes.do";
var ESTATUS_TERMINADO = 1;
var ANCHO_FRAME_IZQ = null;
if(typeof(Calendar)!="undefined"){
    cal = Calendar.setup({
        onSelect: function(calen) { calen.hide() }
    });
}
function cargaOpciones(aplicacion){
    var cadena = "funcionesApp.do?aplicacion="+aplicacion;
    parent.frames.Izquierda.location.href = cadena;
}

function clickFuncion(idImagen,idDiv){
    var imagen = document.getElementById(idImagen);
    if(imagen!=null&&typeof(imagen)!="undefined"){
        var srcImg = imagen.src;
        if(srcImg.indexOf("plus", 0)!=-1){
            imagen.src = "/static/Seguridad/imgs/minus.gif";
        }else{
            imagen.src = "/static/Seguridad/imgs/plus.gif";
        }
    }
    
    var div = document.getElementById(idDiv);
    if(div!=null&&typeof(div)!="undefined"){
        var displayDiv = div.style.display;
        if(displayDiv=="none"){
            div.style.display="inline";
        }
        else{
            div.style.display="none";
        }
    }
}

function abreURL(url, liga){
    var id = "ligaActual";
    var ligaActualImg = document.getElementById(id);
    if(typeof(ligaActualImg)=='undefined'||ligaActualImg==null){
        ligaActualImg = document.createElement('img');
        ligaActualImg.alt = "";
        ligaActualImg.src="/static/Seguridad/imgs/animated_bullet.gif";
        ligaActualImg.id = id;
    }else{
        ligaActualImg.parentNode.removeChild(ligaActualImg);
    }
    liga.parentNode.insertBefore(ligaActualImg, liga);
    
    if(url.indexOf("?")==-1){
        url = url+"?";
    }else{
        if(url.charAt(url.length-1) != "&"){
            url = url+"&";
        }
    }
    var nombreVar = document.getElementById("nombreVar").value;
    var ciclo = document.getElementById("ciclo").value;
    var cicloN = document.getElementById("cicloN").value;
    var usuario = document.getElementById("usuario").value;
    var usuarioN = document.getElementById("usuarioN").value;
    var usuarioAP = document.getElementById("usuarioAP").value;
    var usuarioAM = document.getElementById("usuarioAM").value;
    var separador = document.getElementById("separador").value;
    
    parent.frames.Principal.location.href=url+nombreVar+"="+ciclo+separador+cicloN+separador+usuario+separador+usuarioN+separador+usuarioAP+separador+usuarioAM;
}

function nombreParametroAutenticacion(){
    var frameIzq = parent.frames.Izquierda;
    return frameIzq.document.getElementById("nombreVar").value;
}

function candenaAutenticacion(){
    var frameIzq = parent.frames.Izquierda;
    var nombreVar = frameIzq.document.getElementById("nombreVar").value;
    var ciclo = frameIzq.document.getElementById("ciclo").value;
    var cicloN = frameIzq.document.getElementById("cicloN").value;
    var usuario = frameIzq.document.getElementById("usuario").value;
    var usuarioN = frameIzq.document.getElementById("usuarioN").value;
    var usuarioAP = frameIzq.document.getElementById("usuarioAP").value;
    var usuarioAM = frameIzq.document.getElementById("usuarioAM").value;
    var separador = frameIzq.document.getElementById("separador").value;
    return nombreVar+"="+ciclo+separador+cicloN+separador+usuario+separador+usuarioN+separador+usuarioAP+separador+usuarioAM;
}

function popUp(url, modal, datos, opciones){
    if(modal!=null&&typeof(modal)!="undefined"){
        return window.showModalDialog(url,datos,opciones);
    }else{
        return window.open(url, (new Date()).getTime(), "status=0,toolbar=0,location=0,menubar=0,resizable=0,scrollbars=1,directories=0");
    }
}

function adjustDims(){
    try{
        window.sizeToContent();
        var posTop =  (window.screen.availHeight/2)-(window.outerHeight/2);
        var posLeft =  (window.screen.availWidth/2)-(window.outerWidth/2);
        window.moveTo(posLeft, posTop);
    }catch(ex){
        //Internet Explorer
        var anchoMax = document.body.scrollWidth;
        for(var i=0;i<document.all.length;i++){
            if(document.all[i].scrollWidth>anchoMax){
                anchoMax = document.all[i].scrollWidth;
            }
        }
        
        var wHeight = document.body.scrollHeight>window.screen.availHeight?window.screen.availHeight-50:document.body.scrollHeight;
        var wWidth = (anchoMax+60)>window.screen.availWidth?window.screen.availWidth-50:(anchoMax+60);
        
        if(wHeight<150){
            wHeight = 150;
        }
        
        if(wWidth<250){
            wWidth = 250;
        }
        window.dialogHeight=wHeight+"px";
        window.dialogWidth=wWidth+"px";
        window.dialogTop  = (window.screen.availHeight/2)-(wHeight/2);
        window.dialogLeft = (window.screen.availWidth/2)-(wWidth/2);
    }
}

function validaCambioPass(forma){
    var ans = true;
    if(document.getElementById('cambioPass').style.display==''){
        var passnvo1 = document.getElementById('passnvo1');
        var passnvo2 = document.getElementById('passnvo2');
        if(passnvo1.value==""){
            alert("La Nueva Contraseña no puede estar vacía");
            ans = false;
        }else{
            if(passnvo1.value!=passnvo2.value){
                alert("La Nueva Contraseña y su Confirmación no coinciden");
                ans = false;
            }
        }
    }
    if(ans){
        ans = validateLoguinForm(forma);
    }
    return ans;
}

function cerrarSesion(){
    if(confirm("¿Esta seguro que desea cerrar su sesión?")){
        parent.frames.Izquierda.location.href = "/static/Seguridad/vacio.html";
        parent.frames.Principal.location.href = "/static/Seguridad/vacio.html";
        $Ajax("cerrarSesion.jsp",
        {
            metodo: $metodo.POST,
            tipoRespuesta: $tipo.TEXTO,
            onfinish: function(respuesta,idPeticion){
                alert("Su sesión se cerro con éxito");
                window.open("http://www.colegiomiguelaleman.edu.mx");
                window.top.opener=null;
                window.top.opener = self;
                window.top.close();
            },
            onerror: function(objE,idPeticion){
                alert(objE.code+": "+objE.message+" en petición con id:"+idPeticion);
            }
        });
    }
}

function maxMinWrkArea(){
    var btn = $("maxBtn");
    var frameP = top.document.getElementById("FP");
    if(ANCHO_FRAME_IZQ==null){
        ANCHO_FRAME_IZQ = frameP.cols;
    }
    if(btn.src.indexOf("max")!=-1){
        btn.src = "/static/Seguridad/imgs/minimize.png";
        btn.alt = "Area de Trabajo Normal" ;
        btn.title = "Area de Trabajo Normal" ;
        frameP.cols = "0,*";
    }else{
        btn.src = "/static/Seguridad/imgs/maximize.png";
        btn.alt = "Maximizar Area de Trabajo";
        btn.title = "Maximizar Area de Trabajo" ;
        frameP.cols = ANCHO_FRAME_IZQ;
    }
}

function cargaPagina(noPag){
    if(typeof(paginas)!="undefined"&&paginas.length>0){
        var cuerpoTabla = "<table border='0' cellspacing='2'>"+
            "<tr id='cabeceraPags'>";
        for(var i=0;i<columnas.length;i++){
            cuerpoTabla+="<th align='center'>"+columnas[i]+"</th>";
        }
        
        cuerpoTabla+="</tr>";
        var c=0;
        var j=0;
        for(j=(paginas.length-1);c!=noPag;j--,c++);
        
        cuerpoTabla+=paginas[j];
        cuerpoTabla+="</table>";
        
        var encabezado = "";
        
        if(paginas.length>1){
            encabezado+="<tr class='ctrlPaginas'>";
            
              encabezado+="<td style='border-width:1px 0 1px 1px;border-style:solid;border-color:black;'>";
              if(noPag!=0){
                  encabezado+="<a href='#' onclick='cargaPagina("+(noPag-1)+")'><font size='3'><</font></a>";
              }else{
                  encabezado+="&nbsp;";
              }
              encabezado+="</td>";

              encabezado+="<td align='center' style='border-width:1px 0;border-style:solid;border-color:black;'>";
              for(var p=1;p<=paginas.length;p++){
                  if((p-1)==noPag){
                      encabezado+="&nbsp;<font size='4'>"+p+"</font>";
                  }else{
                      encabezado+="&nbsp;<a href='#' onclick='cargaPagina("+(p-1)+")'><font size='3'>"+p+"</font></a>";
                  }
              }
              encabezado+="</td>";

              encabezado+="<td align='right' style='border-width:1px 1px 1px 0;border-style:solid;border-color:black;'>";
              if(noPag!=(paginas.length-1)){
                  encabezado+="<a href='#' onclick='cargaPagina("+(noPag+1)+")'><font size='3'>></font></a>";
              }else{
                  encabezado+="&nbsp;";
              }
              encabezado+="</td>";
            
            encabezado+="</tr>";
        }
        $("paginacion").innerHTML="<table border='0' cellspacing='0'>"+encabezado+"<tr><td colspan='3'>"+cuerpoTabla+"</td></tr>"+encabezado+"</table>";
    }else{
        $("paginacion").innerHTML="<font size='5'>No se encontraron resultados</font>"
    }
}

function seleccionaDeseleccionaTodos(){
    var masterChkBox = $("masterChkBox");
    if(typeof(masterChkBox.ultimoEstado)=="undefined"){
        masterChkBox.ultimoEstado = false;
    }
    
    var checks = $$("input.chkBoxGroup");
    for(var i=0;i<checks.length;i++){
        checks[i].checked = !masterChkBox.ultimoEstado;
    }
    masterChkBox.ultimoEstado = !masterChkBox.ultimoEstado;
}