//-------------------------------------------------------//
// Author:Rodolfo Machon
// Company:WebSoft, SA de CV 
// Nombre del archivo: mandamientos_mdfx.js
// Created: 2016-08-03
// Updates: 2016-08-03
// Copyright - All Rights Reserved
//-------------------------------------------------------//
//-------------------------------------------------------//
var FechaHoy	= "";
var ultimo		= 1;
var isIE 		= document.all?true:false;
var isNS 		= document.layers?true:false;
var OpenWindows = new Object();
var temp_total;
var aux;
var xmlHttp;
var xmlHttpStream;
var xmlHttp1;
var xmlHttpStream1;
var xmlHttp2;
var xmlHttpStream2;
var xmlHttp10;
var xmlHttpStream10;
var xmlHttp11;
var xmlHttpStream11;
//-------------------------------------------------------//
window.onload = function(){

};
//-------------------------------------------------------//
// LEVEL 1
function loadFeatures () {
	appid	= parent.list.document.data.appid.value;
	lan		= parent.list.document.data.lan.value;
	clase	= parent.list.document.data.clase.value;

	if(parent.list.document.data.exist.value=="false"){
		getFechaHoy();		
		getUserCajaDepartamento();
		getUserFunctions();
		getUserViews();
	}
}
function showSuccesfull () {
	getExpedienteMandamientosActivos();
	activarOpciones();
}
function saveMandamiento() {
	LastValidation();
	Salvar();
}
//-------------------------------------------------------//
// LEVEL 2
function getFechaHoy(){
    var URL     = "../pl/get_fecha_hoy.pl?appid="+appid;
    xmlHttp10	= GetXmlHttpObject(stateChangedGetFechaHoy);
    xmlHttp10.open("GET",URL,true) ;
    xmlHttp10.send(null);	
}
function stateChangedGetFechaHoy(){
    if (xmlHttp10.readyState==4 || xmlHttp10.readyState=="complete"){ 
        xmlHttpStream10=xmlHttp10.responseText;
        if (xmlHttpStream10!=="0" && xmlHttpStream10!==""){
           document.form.dt_emision.value=xmlHttpStream10;
           document.form.dt_vencimiento.value=xmlHttpStream10;
           document.form.dt_cuota.value=xmlHttpStream10;
        }
    } 
}
function getUserCajaDepartamento(){
    var URL     = "../pl/user.caja.departamento.get.pl?appid=LaUnion&lan=es";
    xmlHttp11	= GetXmlHttpObject(getUserCajaDepartamentoSC);
    xmlHttp11.open("GET",URL,true) ;
    xmlHttp11.send(null);
}
function getUserCajaDepartamentoSC(){
    if (xmlHttp11.readyState==4 || xmlHttp11.readyState=="complete"){ 
        xmlHttpStream11=xmlHttp11.responseText;
		if (xmlHttpStream11!=="false" && xmlHttpStream11!==""){
			var datos=xmlHttpStream11.split("|");
			if(parent.list.document.data.id.value===""){
				document.form.i8_user_id.value				= datos[0];
				document.form.i8_emisor_id.value			= datos[0];
				document.form.v32_lname.value				= datos[1];			
				document.form.v32_fname.value				= datos[2];
				document.form.i2_tipo_departamento_id.value	= datos[3];
				document.form.v64_tipo_departamento.value	= datos[4];
				document.form.i2_cajero_id.value			= datos[5];
			}
		}
    }
}
function getUserFunctions(){
	var cid		= "mdfx";
	var clase	= "mandamiento";
	var oid		= "5000";
    var URL     = "../pl/get_user_option_functions.pl?appid="+appid+"&lan="+lan+"&clase="+clase+"&cid="+cid+"&oid="+oid;
    xmlHttp12	= GetXmlHttpObject(getUserFunctionsSC);
    xmlHttp12.open("GET",URL,true) ;
    xmlHttp12.send(null);
}
function getUserFunctionsSC(){
    if (xmlHttp12.readyState==4 || xmlHttp12.readyState=="complete"){ 
        xmlHttpStream12=xmlHttp12.responseText;
        if (xmlHttpStream12!=="false" && xmlHttpStream12!==""){
			var ol 		= document.getElementById("functions_ol");
			var funs_a	= xmlHttpStream12.split("~~");
			for(var i=0;i<funs_a.length-1;i++ ) {
				var li		= document.createElement("li");
				var mems_a	= funs_a[i].split("[]");
				li.innerHTML= "<a href='#' id='"+mems_a[0]+"' onClick='"+mems_a[1]+"'>"+mems_a[2]+"</a>";
				ol.appendChild(li);
			}			
        }
    }
}
function getUserViews(){
	var cid		= "mdfx";
	var clase	= "mandamiento";
	var oid		= "5000";
    var URL     = "../pl/get_user_option_views.pl?appid="+appid+"&lan="+lan+"&clase="+clase+"&cid="+cid+"&oid="+oid;
    xmlHttp13	= GetXmlHttpObject(getUserViewsSC);
    xmlHttp13.open("GET",URL,true) ;
    xmlHttp13.send(null);
}
function getUserViewsSC(){
    if (xmlHttp13.readyState==4 || xmlHttp13.readyState=="complete"){ 
        xmlHttpStream13=xmlHttp13.responseText;
		//alert(xmlHttpStream13);
        if (xmlHttpStream13!=="false" && xmlHttpStream13!==""){
			var ol 		= document.getElementById("views_ol");
			var vis_a	= xmlHttpStream13.split("~~");
			for(var i=0;i<vis_a.length-1;i++ ) {
				var li		= document.createElement("li");
				var mems_a	= vis_a[i].split("[]");
				var abridor = mems_a[13].replace("()","");
				li.innerHTML= "<a href='#' id='"+mems_a[0]+"' onClick="+abridor+"('"+mems_a[0]+"','"+mems_a[2]+"','"+mems_a[3]+"','"+mems_a[4]+"','"+mems_a[6]+"','"+mems_a[7]+"','"+mems_a[8]+"','"+mems_a[9]+"','"+mems_a[10]+"','"+mems_a[11]+"','"+mems_a[12]+"')>"+mems_a[1]+"</a>";
				ol.appendChild(li);
			}			
        }
    }
}
function getExpedienteMandamientosActivos(){
    var winame 	= "ListaMandamientos";
    var i8_mandamiento_id=document.form.i8_mandamiento_id.value*1;
    var i8_expediente_id =document.form.i8_expediente_id.value*1;
    if(i8_mandamiento_id>0){
        if(i8_expediente_id!=""){	
			if (!OpenWindows[winame] || OpenWindows[winame].closed){
				var win ="window.open('../pl/expediente.mandamientos.activos.get.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&i8_expediente_id="+i8_expediente_id+"','ListadoMandamientos','left=20,top=20,width=800,height=400,scrollbars=1,toolbar=0,menubar=0,resizable=1,location=0,status=0')";
				OpenWindows[winame]=eval(win);
			}else{OpenWindows[winame].focus()}
		}
	}
}
function LastValidation(){
    sustituir_expresion('document.form.v128_contribuyente','prohibido',' ');
    sustituir_expresion('document.form.txt_concepto','prohibido',' ');
}
function Salvar(){
	var mensaje	= "Los siguientes campos no pueden estar vacios:\n";
	var aux		= document.form.v128_contribuyente.value.toUpperCase();
	document.form.v128_contribuyente.value	= aux;
	aux=document.form.txt_direccion.value.toUpperCase();
	document.form.txt_direccion.value = aux;
	aux=document.form.txt_concepto.value.toUpperCase();
	document.form.txt_concepto.value = aux;
	aux=document.form.txt_expediente_direccion.value.toUpperCase();
	document.form.txt_expediente_direccion.value=aux;
	aux=document.form.txt_expediente_propietarios.value.toUpperCase();
	document.form.txt_expediente_propietarios.value=aux;
	if(document.form.v128_contribuyente.value=="" || document.form.i8_contribuyente_id.value==""|| document.form.v64_tipo_pago.value=="" || document.form.v64_tipo_departamento.value==""){
		if(document.form.i8_contribuyente_id.value==""){mensaje=mensaje+"Id Contribuyente\n";}
		if(document.form.v128_contribuyente.value==""){mensaje=mensaje+"Nombre Contribuyente\n";}
		if(document.form.v64_tipo_pago.value==""){mensaje=mensaje+"Forma de Pago\n";}
		if(document.form.v64_tipo_departamento.value==""){mensaje=mensaje+"Departamento\n";}
		alert(mensaje );
    }else if(document.form.subtotal__foot.value<=0){
		alert("El total no es valido");
    }else{
        validar=comprobarcantidad();
        if(validar=="0"){
            alert("Debe especificar una cantidad para cada tarifa");
        }else{
            limpiar(1);
			document.form.d82_total.value=document.form.subtotal__foot.value;
            Save();
            limpiar(2);
        }
    }
}
//-------------------------------------------------------//
// AUXILIAR
function activarOpciones() {
	if (document.all.opciones_div.style.display=='inline') {
		document.all.opciones_div.style.display	= 'none';
    }else{
		document.all.opciones_div.style.display	= 'inline';		
	}
}
function noReturn(e) {
	if (e.which == 13) {        
        e.preventDefault();
    }
}
function sustituir_expresion(sCampo,sTipo,sCaracterSustituto){
    var er_fecha 	= /^(\d{4})(-)(0[1-9]|1[012])\2([012][0-9]|3[0|1])$/   //aaaa-mm-dd
    var er_hora 	= /^(0[0-9]|1\d|2[0-4]):([0-5]\d):([0-5]\d)$/ //hh:mm:ss
    var er_decimal	= /(\d{,2})\.?(\d{,2})/ //00.00
    var er_numero	= /^\d+/ //00
    var er_car_p	= /[&<>@"|?]+/ // caracteres prohibidos: <
    var er_general	= "";
    var sVAR 		= eval(sCampo+".value;");
    switch(sTipo){
		case "fecha":
			er_general = er_fecha;
			break;
		case "hora":
			er_general = er_hora;
			break;
		case "decimal":
			er_general = er_decimal;
			break;
		case "prohibido":
			er_general = er_car_p;
			break;
	} 
    var contador=0;
    while(evaluar_expresion(sVAR,sTipo) && contador<1000){
        var nuevo_valor = sVAR.replace(er_general,sCaracterSustituto,"gi");
        eval(sCampo+".value='"+nuevo_valor+"';");
        sVAR = eval(sCampo+".value;");
        contador=contador+1;
    }
}
function evaluar_expresion(sVAR,sTipo){
    var er_fecha 	= /^(\d{4})(-)(0[1-9]|1[012])\2([012][0-9]|3[0|1])$/   //aaaa-mm-dd
    var er_hora 	= /^(0[0-9]|1\d|2[0-4]):([0-5]\d):([0-5]\d)$/ //hh:mm:ss
    var er_decimal	= /(\d{,2})\.?(\d{,2})/ //00.00
    var er_numero	= /^\d+/ //00
    var er_car_p	= /[&<>@"|]+/ // caracteres prohibidos: &<>@"|
    switch(sTipo){
		case "fecha":
			if (!er_fecha.test(sVAR)) {return false;}
			break;
		case "hora":
			if (!er_hora.test(sVAR)) {return false;}
			break;
		case "decimal":
			if (!er_decimal.test(sVAR)) {
				if(!er_numero.test(sVAR)){return false;}
			}
			break;
		case "prohibido":
			if (!er_car_p.test(sVAR)) {return false;}
			break;
	} 
    return true;
}
function Padg(objRef,form,field){
	var field_name 	= objRef.name;
	var pers	= eval("parent.form.document.form."+field_name+".value");
	if (!openwindow1 || openwindow1.closed){
		win ="window.open('../bin/padg.pl?lan="+lan+"&appid="+appid+"&clase="+clase+"&form="+form+"&field="+field_name+"&pers="+pers+"','"+clase+"','left=200,top=200,width=560,height=350,scrollbars=yes,toolbar=no,menu=no,resizable=yes')";
		openwindow1=eval(win);
	}else{openwindow1.focus()}
}
function ActivarNombre(){
    document.form.i8_contribuyente_id.value =99999999;
	document.getElementById("v128_contribuyente").readOnly=false;
}
function GetFiestas(chk){//Re-Hacer esta funcion trayendo los datos desde tarifas
    if(chk==false){
		//var fiesta		  = (temp_total*0.05);
		eval("document.form.d82_cantidad__"+ultimo+".value =''");
		eval("document.form.c16_tarifa_id__"+ultimo+".value = ''");
		eval("document.form.c16_cuenta_ingreso_id__"+ultimo+".value =''");				
		eval("document.form.v128_tarifa__"+ultimo+".value =''");
		eval("document.form.d82_tarifa__"+ultimo+".value =''");
		eval("document.form.subtotal__"+ultimo+".value =''");
			
		document.form.subtotal__foot.value = temp_total;
    }else{
		temp_total		  = document.form.subtotal__foot.value;
		var fiestas		  = precisionRound((temp_total*0.05), 2);
		var subtotal2		  = (temp_total*1)+fiestas;
		for(i=1;i<=12;i++){
			aux=eval("document.form.c16_tarifa_id__"+i+".value");
			//alert(aux);
			if(aux!=""){ultimo=i+1;}
		}		
		eval("document.form.d82_cantidad__"+ultimo+".value = 1");
		eval("document.form.c16_tarifa_id__"+ultimo+".value = 'ACINMFIENNAMC001'");
		eval("document.form.c16_cuenta_ingreso_id__"+ultimo+".value ='12114'");		
		eval("document.form.v128_tarifa__"+ultimo+".value ='FIESTAS'");
		eval("document.form.d82_tarifa__"+ultimo+".value ="+fiestas);
		eval("document.form.subtotal__"+ultimo+".value ="+fiestas);
		document.form.subtotal__foot.value = subtotal2;	
    }
}
function limpiar(opc){
    if (opc=="1"){
		temp=0;
		for(i=1;i<=11;i++){
			aux2=eval("document.form.c16_tarifa_id__"+i+".value");
			if(aux2!=""){
				temp=temp+1;
			}
		} 
    }
    if (opc=="2"){
		for(i=temp+1;i<=11;i++){
		   eval("document.form.d82_cantidad__"+i+".value =''");
		   eval("document.form.c16_tarifa_id__"+i+".value = ''");
		   eval("document.form.v128_tarifa__"+i+".value =''");
		   eval("document.form.d82_tarifa__"+i+".value =''");
		   eval("document.form.subtotal__"+i+".value =''"); 
		}  
    }
}
//-------------------------------------------------
function GetExpediente(){
    var generador = document.form.v64_tipo_generador.value.toLowerCase();
    if(generador!=""){
	var command = "GetBack('LTMD','contribuyente_"+generador+"s','i8_"+generador+"_id|i8_expediente_id,txt_ubicacion|txt_expediente_direccion,txt_propietarios|txt_expediente_propietarios','parent.opener.form',100,100,'i8_contribuyente_id','i8_expediente_id')";
	//alert(command);
	eval(command);
    }else{alert("Seleccione el Generador")}
}
function colocarfecha(value){
	alert(value)
	var Members 	= value.split("-");
	var NewDate	= new Date(Members[0],Members[1]-1,Members[2]);
	var NewString	= (String(weekdays[NewDate.getDay()]))+" "+(String(NewDate.getDate()))+" DE "+(String(months[NewDate.getMonth()]))+", "+(String(NewDate.getFullYear()));
	return NewString.toUpperCase();
}
function comprobarcantidad(){
	bandera=1;
	for(i=1;i<=11;i++){
		aux2=eval("document.form.c16_tarifa_id__"+i+".value");
		aux3=eval("document.form.d82_cantidad__"+i+".value");
		if(aux2!="" && aux3==""){
			bandera=0
		}
	}
	return bandera;
}
function change(id,h){
    var new_hght= 0;
    var ids	= id.split(',');
    var owidth  = '750';
    var oheight = '660';
    var hght	= parent.document.body.clientHeight;
    var wdth	= parent.document.body.clientWidth;    
    for(var i=0;i<ids.length;i++){
	identity = document.getElementById(ids[i]);
	if(identity.style.display=="none"){
	    identity.style.display = "";
	    new_hght= hght+h;
	    eval("parent.window.resizeTo(750,"+new_hght+")");
	}else{
	    identity.style.display="none";
	    new_hght= hght-h+72;
	    eval("parent.window.resizeTo(750,"+new_hght+")");
	}
    }
}
function Clean(s){
    var elements = s.split(',');
    for(var i=0;i<elements.length;i++){    
        eval("document.form."+elements[i]+".value=''");
    }
}
//--------------------------//
//Evaluar y Borrar
		//capturarDepartamento();
		//capturarCajeroAsignado();	
function capturarDepartamento(){
    var URL     = "../pl/get_user_department.pl?appid=LaUnion&lan=es";
    xmlHttp2	= GetXmlHttpObject(stateChangedcapturarDepartamento);
    xmlHttp2.open("GET",URL,true) ;
    xmlHttp2.send(null);
}
function stateChangedcapturarDepartamento(){
    if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete"){ 
        xmlHttpStream2=xmlHttp2.responseText;
	if (xmlHttpStream2!="false" && xmlHttpStream2!=""){
        var datos=xmlHttpStream2.split("|");
           document.form.i2_tipo_departamento_id.value=datos[0];
           document.form.v64_tipo_departamento.value=datos[1];
           //document.form.v64_elabora.value=datos[2];
           document.form.i8_user_id.value=datos[3];
           document.form.i8_emisor_id.value=datos[3];		   
        }
    }
}
function capturarCajeroAsignado(){
    var appid 	= "RTM_v30";
    var lan 	= "es";
    var URL     = "../pl/capturar_cajero_asignado.pl?appid="+appid;
    xmlHttp1	= GetXmlHttpObject(stateChangedcapturarCajeroAsignado);
    xmlHttp1.open("GET",URL,true) ;
    xmlHttp1.send(null);	
}
function stateChangedcapturarCajeroAsignado(){
	if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete"){ 
		xmlHttpStream1=xmlHttp1.responseText;
		if (xmlHttpStream1!="0" && xmlHttpStream1!=""){
			//document.form.i2_cajero_id[xmlHttpStream1-1].selected = "1"
		}
	} 
}
function precisionRound(number, precision) {
	var factor = Math.pow(10, precision);
	return Math.round(number * factor) / factor;
}