﻿/*  
Nombre: Functions.js
Creacion: 24 Marzo 2009
Descripcion: Logica de control de ventanas:
Funciones:
    0. Inicia Pagina: pageLoad()
    1. Cargando: Loading()
    2. Abrir ventana: MenuClick()
    3. Cerrar ventana: OnClientCloseWin()
    4. Setear titulo: SetTitle()

Afecta: MasterPage.master , MasterPage2.master     

*/

//Cuando carga la pagina
function pageLoad(sender, eventArgs)
{
    window.setTimeout('Loading()', 1000);
   
    //FixRadControls();
    //setContador();
    //window.setInterval("setContador()", 3000);
}


//Muestra mensaje de espera (Cargando...) hasta que se cargue la Pagina. Implementado con Jquery.
function Loading() {
    $(document).ready(function() {
        $('#prog_form').ajaxForm({
            target: '#Content',
            beforeSubmit: showPreloader,
            success: hidePreloader
        });

        $("#preloader").hide();
        $("#cuerpo").hide();
    }); 
    
    function showPreloader() 
    { 
        $("#preloader").show(); 
        $("#cuerpo").show(); 
    } 

    function hidePreloader() 
    { 
        $("#preloader").hide(); 
        $("#cuerpo").hide(); 
    }
};


// Variables globales incremento de posicion ventanas
var xx = 0;
var pos = 30;
// Incrementa la posicion para que la ventana no cargue una encima de otra y haya traslación
function incrementar() {
    pos = pos + 30;
    return pos;
}
// Abre una ventana cuando se hace click en el menu.
function MenuClick(sender, eventArgs) {
    var menuItem = eventArgs.get_item();
    var oManager = GetRadWindowManager();
    var oWnd = oManager.getActiveWindow();
    // Obtiene url desde el item del menu.
    var Url = menuItem.get_value();
    var oWnd = GetRadWindowManager().getActiveWindow();
    var windows = oManager.get_windows();
    var ajaxManager = $find("<%= RadAjaxManager1.ClientID %>");
    var val = "Cascade";

    for (var i = 0; i < windows.length; i++) { //Segunda vez activa el control
        var wnd = "";
        wnd = windows[i];
        if (Url != null) {
            if (wnd.get_navigateUrl() == Url) {
                var bounds = wnd.getWindowBounds();
                var xpos = bounds.x; //returns the current left position of the RadWindow
                var ypos = bounds.y; //returns the current top position of the RadWindow
                //alert(Url);

                //alert(xpos);          
                //alert(ypos);  
                wnd.Show();
                wnd.Restore();
                wnd.set_offsetElementID("contenido_ventana");
                wnd.setSize(1100, 500); //tamaño


                wnd.moveTo(xpos, ypos);
                titulo = SetTitle(Url);

                wnd.cascade = true;
                wnd.SetTitle(titulo);
                //ConfirmClose("RadWindow1");      
                return;
            }
        }
        else {
            return;
        }
    }
    //Si no, abre una nueva ventana
    if (Url != null) {
        if (pos < 90) {
            if (pos < 30 || pos == 30) {
                titulo = SetTitle(Url);
                //alert(Url);
                wnd = radopen(Url, null);
                wnd.moveTo(0, 0);
                wnd.Show();
                wnd.setActive(true);
                // wnd.setSize(1100, 500);
                wnd.Maximize();
                //pos parte en 0            
                pos = incrementar();
                //pos = 30; 
                //alert('0');
            }
            // Entra desde la segunda vez e incrementa la posicion, 
            // Controla las instancias no mayor a x. 
            else {
                var titulo;
                titulo = SetTitle(Url);
                //wnd =  radopen(Url,"RadWindow2");
                wnd = radopen(Url, null);
                wnd.SetTitle(titulo);
                wnd.set_offsetElementID("contenido_ventana");
                wnd.Show();
                wnd.setActive(true);
                wnd.moveTo(pos, pos);
                wnd.setSize(1100, 500);


                pos = incrementar();
                //pos += 30 
                //alert(pos);
            }
        }
        else {
            radalert('Ha excedido el limite de ventanas permitidas, por favor cierre algunas.');
            return;
        }
    }
    else {
        return;
    }

} 

//STAND BY
//top.frames[contenido_ventana.id].document.layers[top.frames[7].name].document.close(); 
//function crearFrame(url) {
////    var testFrame = document.createElement("IFRAME");
////    testFrame.id = "testFrame";
////    testFrame.src = "http://www.google.es";
////    var control = document.getElementById("testFrame")
////    if (control == null) {
////        document.body.appendChild(testFrame);
//    //    }
//    //alert(pos);    
//    var contenido_ventana = document.createElement("IFRAME");
//    contenido_ventana.id = "contenido_ventana"+pos;
//    //contenido_ventana.src = url ;
//    //var control = document.getElementById("contenido_ventana")
//    contenido_ventana.src = url;
//    contenido_ventana.frameborder = "0";
//    
//    var control = document.getElementById(contenido_ventana.id)
//    if (control == null) {
//        document.body.appendChild(contenido_ventana);
//    }
//    //top.frames['contenido_ventana'].location = url;
//    
//    //top.frames[contenido_ventana.id].location = url;
//    //alert(top.frames[contenido_ventana.id].location.href);
//    
//    //for (i = 0; i < top.frames.length; i++)
//    //    alert('Nombre del frame ' + i + ': ' + top.frames[i].name );        
//        //alert(top.frames[i].location);        
//        //alert('Nombre del frame ' + i + ': ' + top.frames[i].location + "<br>");
//    //alert(top.frames.length);
//    
////    $('#iframe').each(function() {
////        this.contentWindow.location.reload(true);
////    })
////pos = pos + 30;
//}

// Al cerrar la ventana resta una posicion para que la siguiente vez que abra la ventana abra
// en la posicion de la ultima que se cerro.
function OnClientCloseWin()
{ 
  pos = pos - 30;  
  //alert(pos);
}

// Formatea el titulo para cada ventana    
function SetTitle(Url){
var str= Url;
var titulo;
    titulo = str.replace("/E-FASTCO_2_0_2/","");
    titulo = titulo.replace("/E-FASTCO_2_0_2/", "");
    titulo = titulo.replace("/F-FASTCO/", "");
    titulo = titulo.replace("/f-fastco/", "");
    titulo = titulo.replace("/efastco/","");
    titulo = titulo.replace(".aspx","");
    titulo = titulo.replace("ADM","Administracion");
    titulo = titulo.replace("GTN_","");
    titulo = titulo.replace("RPT_","");
    titulo = titulo.replace("RPT","Reportes");
    titulo = titulo.replace("RCD","Recaudacion ");                   
    titulo = titulo.replace("TSR","Tesoreria ");
    titulo = titulo.replace("/"," - ");
    titulo = titulo.replace("_"," ");
    titulo = titulo.replace("_"," ");
    titulo = titulo.replace("_", " ");
    titulo = titulo.replace("_", " "); 
    titulo = titulo.replace("Conciliacion","Conciliacion"); 
    titulo = titulo.replace("Avansada","Avanzada");
    titulo = titulo.replace("CLI", " ");
    titulo = titulo.replace("- ", " ");
    titulo = titulo.replace("P-fastco/", " ");
    titulo = titulo.replace("Bci-fastco/", " ");
    titulo = titulo.replace("F-fastco/", " ");
    titulo = titulo.replace("/", " - ");
    //alert(titulo); 
    return titulo;
}

//Solucion Problema: Arregla problema Decorator.
function FixRadControls(){   
   var inputs = document.getElementsByTagName("INPUT");
   for (var i=0; i < inputs.length; i++)
   {
        inputs[i].style.zoom = "1";                
   }  
}
//Solucion Problema: Algunos botones en RadGrid (como "next page", "prev page") no son visibles 
/*Telerik.Web.UI.RadFormDecorator.prototype.decorateButtonsByTagName = function x(tagName, rootElement)*/
function x(tagName, rootElement)
{
   if (!rootElement) rootElement = document.body;
   
    var realInputButtons = rootElement.getElementsByTagName(tagName);
    var numberInputs = realInputButtons.length;
	
    for (i = 0; i < numberInputs; i ++)
    {
	    var inputButton = realInputButtons[i];						
	    var inputType = inputButton.getAttribute('type');
											
	    if(tagName == "button" || (inputType == 'submit' || inputType == 'reset' || inputType == 'button'))
	    {
	        /* Nuevo: Salta botones que tienen un className establecido - asumimos que el desarrollador
	        o alguien ya le ha establecido un estilo*/
	        if (inputButton.className) continue;
		
		    var buttonValue = (tagName == "button" ? inputButton.innerHTML : inputButton.value);
	        var skinnedButton = this.getSkinnedButton(inputButton, buttonValue);				
		    
		    if (skinnedButton)
            {
                inputButton.className = 'radfdRealInputButton';
                inputButton.insertAdjacentElement('beforeBegin', skinnedButton);
            }
	    }
    }
};


