var xmlHttp;
var xmlHttpStream;
var xmlHttp1;
var xmlHttpStream1;
var xmlHttp2;
var xmlHttpStream2;
var usuario;
var openwindow;
function GetXmlHttpObject(handler){
	var objXmlHttp=null;	
	if(window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
		objXmlHttp = new XMLHttpRequest();
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler 		
	}
	if(window.ActiveXObject){// code for IE6, IE5
		objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		objXmlHttp.onreadystatechange=handler 		
	}
	if(objXmlHttp==null){alert("Your Browser Does Not Suppport AJAX")}
	return objXmlHttp;
}
function GetUser(){
    var URL     = "../pl/get_user.pl?appid=LaUnion&lan=es";
	//alert(URL);
    xmlHttp0	= GetXmlHttpObject(stateChangedGetUser);
    xmlHttp0.open("GET",URL,true) ;
    xmlHttp0.send(null);
}
function stateChangedGetUser(){
    if (xmlHttp0.readyState==4 || xmlHttp0.readyState=="complete"){ 
        xmlHttpStream0 = xmlHttp0.responseText;
		//alert(xmlHttpStream0);
		var elementos  = xmlHttpStream0.split('|');
		//alert(xmlHttpStream0);
		usuario = elementos[2]
		//alert(usuario);
	}
}
function GetCajeros(){
    var URL     = "../pl/get_cajeros.pl";
    xmlHttp	= GetXmlHttpObject(stateChangedGetCajeros);
    xmlHttp.open("GET",URL,true) ;
    xmlHttp.send(null);
}
function stateChangedGetCajeros(){
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
        xmlHttpStream = xmlHttp.responseText;
		//alert(xmlHttpStream);
		var Cajeros   = xmlHttpStream.split('~');
		var selec 	  = document.getElementById("i8_cajero_id"); 
		for(var i=0;i<Cajeros.length-1;i++){
			var elementos  		= Cajeros[i].split('|');
			var newOption 		= document.createElement("option");
			newOption.value		= elementos[0];			
			if (typeof newOption.textContent === 'undefined'){
				newOption.innerText = elementos[1];
			}else{
				newOption.textContent = elementos[1];
			}
			selec.appendChild(newOption);
		}	
    }
}
function GetEmisores(){
    var URL     = "../pl/get_emisores.pl";
    xmlHttp1	= GetXmlHttpObject(stateChangedGetEmisores);
    xmlHttp1.open("GET",URL,true) ;
    xmlHttp1.send(null);
}
function stateChangedGetEmisores(){
    if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){ 
        xmlHttpStream1 = xmlHttp1.responseText;
		//alert(xmlHttpStream1);
		var Emisores   = xmlHttpStream1.split('~');
		var selec 	  = document.getElementById("i8_emisor_id");
		for(var i=0;i<Emisores.length-1;i++){
			var elementos  		= Emisores[i].split('|');
			var newOption 		= document.createElement("option");
			newOption.value		= elementos[0];			
			if (typeof newOption.textContent === 'undefined'){
				newOption.innerText = elementos[1];
			}else{
				newOption.textContent = elementos[1];
			}			
			selec.appendChild(newOption);
		}	
    }
}
function GetDepartamento(){
    var URL     = "../pl/get_user_department.pl";
    xmlHttp2	= GetXmlHttpObject(stateChangedGetDepartamento);
    xmlHttp2.open("GET",URL,true) ;
    xmlHttp2.send(null);
}
function stateChangedGetDepartamento(){
    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){ 
        xmlHttpStream2 = xmlHttp2.responseText;
		//alert(xmlHttpStream2);
		var elementos  = xmlHttpStream2.split('|');		
		if(elementos[0]!='5'){
			if (usuario!='admin') {
				document.form.i2_tipo_departamento_id.value=elementos[0];
			}else{document.form.i2_tipo_departamento_id.value=0}
		}else{
			document.form.i2_tipo_departamento_id.value=0
		}
    }
}
function GetToday(){
    var today 		= new Date();
    var monthday        = today.getDate();
    var month 		= today.getMonth()+1;
    var year 		= today.getFullYear();
    if(monthday<10){montday="0"+monthday}
    if(month<10){month="0"+month}
    var fetc = year+"-"+month+"-"+monthday;
    return fetc;
}
function ShowCalendar(form,datetype,separator){
        var openwindow;
	if (!openwindow || openwindow.closed){
		win ="window.open('../libraries/html/clnd.html?pbf="+form+"&datetype="+datetype+"&separator="+separator+"','Calendar','left=200,top=200,width=250,height=250,scrollbars=no,toolbar=no,menu=no')";
		openwindow=eval(win);
	}else{openwindow.focus()}
}
function Generar(){
	var desde = document.form.dt_desde.value;
	var hasta = document.form.dt_hasta.value;
	var hasta = document.form.dt_hasta.value;
	var hasta = document.form.dt_hasta.value;
	var caja  = document.form.i2_cajero_id.value;
	var cajero= document.form.i8_cajero_id.value;
	var emisor= document.form.i8_emisor_id.value;
	var depto = document.form.i2_tipo_departamento_id.value;	
	if(desde==""){alert("Favor Ingresar la Fecha Desde");return}	
	if(hasta==""){alert("Favor Ingresar la Fecha Hasta");return}
	if (!openwindow || openwindow.closed){
		win = "window.open('../pl/ingresos_recibos_p.pl?dt_desde="+desde+"&dt_hasta="+hasta+"&i2_cajero_id="+caja+"&i8_cajero_id="+cajero+"&i8_emisor_id="+emisor+"&i2_tipo_departamento_id="+depto+"','GenerarIngresosRecibos','left=100,top=20,width=675,height=550,scrollbars=yes,toolbar=no,menubar=yes,resizable=yes')";
		openwindow=eval(win);
	}else{openwindow.focus()}
	//close();
}
function LoadToday(){
	FechaHoy = GetToday();
	document.form.dt_desde.value=FechaHoy;
	document.form.dt_hasta.value=FechaHoy;
}