﻿// FUNCIONES AJAX
var http_request = false;

function obtenerRequest(url, parametros, funcion, tabla, campo)
{
    if(campo==="" || campo==="vehiculo")
        bloquearPantalla(true); //realizamos el bloqueo modal de pantalla
    //else
    //  cargando(campo);
    
    http_request = false;

    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/xml');
        }
    } 
    else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } 
        catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } 
            catch (e) {}
        }
    }

    if (!http_request) {
         alert('No se puede crear una instancia XMLHttpRequest');
         return false;
    }

    parametrosGet='';
    if(tabla!=""){
        parametrosGet+='tabla='+tabla;
    }

    if(campo!=""){
        if(parametrosGet!='') parametrosGet+='&';
            parametrosGet+='campo='+campo;
    }

    if(parametrosGet!='') parametrosGet='?'+parametrosGet;


    http_request.onreadystatechange = function() {
        if(http_request.readyState == 4 && http_request.status == 200){
            desbloquearPantalla();
            funcion(campo);
        }
    }
        
    if(parametros!=''){
        http_request.open('POST', url+parametrosGet, true);

        //Creamos las cabeceras de envio post
        http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        http_request.setRequestHeader("Content-length", parametros.length);
        http_request.setRequestHeader("Connection", "close");

        http_request.send(parametros);
    }else{
        http_request.open('GET', url, true);
        http_request.send('null');
    }
  
}
function cargando(campo)
{
    var elSel = MM_findObj("slc_"+campo);
    if(elSel!=null && typeof(elSel.options)!="undefined")
        elSel.options.length=0;
    
    if(elSel!=null)
    {
        opcion = document.createElement('option');
        opcion.text = "Cargando datos...";
        opcion.value = -1;
        try{
            elSel.add(opcion, null); //Mozilla, etc
        }catch(e){
            elSel.add(opcion); //IE
        }   
        elSel.selectedIndex=0; // ==  elSel[0].selected=true; 
    }
    
}

function bloquearPantalla(reloj){

    cargando();

    window.scroll(0,0);
    
    //alertSize();
    //getScrollXY();
    
    oElement = MM_findObj('bodyTarificador');
    oElement.scrolling='no';
//    oElement.overflow-y = "hidden";
//    oElement.overflow-x = "hidden"; 
	
	dim = getDim();
	//dim = getDimensions(oElement);

    cb = MM_findObj("capaBloqueo");
	tc = MM_findObj('capaReloj');
	fo = MM_findObj('iframe_oculto');
	
	if(reloj){
		tc.style.width=dim.w+"px";
    	tc.style.height=dim.h+"px";
		tc.style.display="block";
		ref=tc;
	}else{
		cb.style.width=dim.w+"px";
		cb.style.height=dim.h+"px";
		cb.style.display="block";
		ref=cb;
	}
	if(document.all){
				
		fo.style.filter="alpha(opacity=0)"; // Opacidad para IE
		fo.style.opacity="0.00"; // Ocapacidad para Netscape

        fo.style.display = "block";
        fo.style.width = "250px";
		fo.style.height = "450px";
//		fo.style.top = ref.style.top;
//		fo.style.left = ref.style.left;
	}

}

function desbloquearPantalla(){

    //oElement.scrolling='auto';
    cb = MM_findObj("capaBloqueo");
    fo = MM_findObj("iframe_oculto");
    tc = MM_findObj('capaReloj');
    
    cb.style.display = "none";
    fo.style.display = "none";
    tc.style.display = "none";
}



function getDim() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth +' Height = ' + myHeight );
  
    myWidth = document.body.clientWidth + 10;
    
    var currentYPosition = (document.all) ? document.body.scrollTop : window.pageYOffset;
    myHeight = document.body.clientHeight + currentYPosition + 10 ;
 
    myWidth = "2000"; //window.screen.availWidth-25;
    myHeight = "2000";  //window.screen.availHeight-135;
 
   
  return {x: 0, y: 0, w: myWidth, h: myHeight}; 
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}



function openAlert(texto,verBoton){
    
    if(typeof(verBoton)!="undefined")
        MM_findObj('img_Cerrar').style.visibility='hidden';
    else
        MM_findObj('img_Cerrar').style.visibility='visible';
    
    oElement = MM_findObj('bodyTarificador');
	dim = getDimensions(oElement);
	
	ancho=420;
	alto=250;
	tp=150;
	lf=(dim.w/2)-(ancho/2);
	
	MM_findObj('txtAlert').innerHTML = texto;
	
	tablaCg = MM_findObj('divAlert'); 
	
	tablaCg.style.position="absolute";
	tablaCg.style.top=tp+"px";
	tablaCg.style.left=lf+"px";
	tablaCg.style.zIndex=55;
	
	bloquearPantalla(false);
	
	tablaCg.style.display="block";
	
}

function closeAlert(){
    MM_findObj('divAlert').style.display="none";
	desbloquearPantalla();
}
