String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	SM("F");
}
//
function ChN_BAR_OnChange(){
	document.getElementById('TxtHora').value=0;
}
//
function TxtCod_OnKeyUp(){
	if(document.getElementById("ChN_BAR").checked==false){
		document.getElementById('TxtHora').value=GetHora();
	}else{
		document.getElementById('TxtHora').value=0;
	}
}
//
function fCls(){
	sv("PRO_ID","");
	sv("PRO_COD","");
	sv("PRO_DES","");
	//
	sv("Cod","");
	sv("Des","");
	sv("Pie","");
	sv("Caj","");
	sv("TotPie","");
	sv("TotUni","");
	sv("TotUwi","");
	sv("Pre","");	
	sv("Imp","");
	jF("Cod");
	document.getElementById("DivUni").innerHTML="";
}
//**************************************************************************
function GetFactor(){
	var und=gv("Und");
	var faxtor=0;
	for(i=1;i<=UNI_TOT;i++){
		if(UNI_ABR[i]==und){
			faxtor=UNI_FAC[i];
		}
	}
	return faxtor;
}
//**************************************************************************
function cImporte(){
	var tot_uwi=gv("TotUwi");
	var pre=gv("Pre");
	var tot_imp=jVal(tot_uwi) * jVal(pre);
	sv("Imp",tot_imp.toFixed(2));
}
//**************************************************************************
function fImporte(){	
	var pie=jVal(gv("Pie"));
	var caj=jVal(gv("Caj"));
	var tot_pie=pie * caj;
	//
	sv("TotPie",tot_pie);
	//
	var fx=GetFactor();
	if(fx>0){
		var tot_und=tot_pie/GetFactor();
	}else{
		var tot_und=0;
	}
	sv("TotUni",tot_und.toFixed(3));
	sv("TotUwi",tot_und.toFixed(3));
	cImporte();
}
//**************************************************************************
function fK(cx){
	sv("PRO_KIL",cx);
	SM("INVC_KIL");
}
//**************************************************************************

//**************************************************************************
function fGetTabla(){
	var cod = gv("Cod");
	var des = gv("Des");
	//
	if(jL(des)>0 || jL(cod)>0){
		DivDatos = document.getElementById("TAB_AJAX");
		DivCaption = document.getElementById("TAB_CAPTION");
		ajax=CreateAJAX();
		var StrU="ajaz.php?CMD=INVOICE_LISTAR&P01="+cod+"&P02="+des;
		ajax.open("GET", StrU);	
		ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {
				var StrDatos=ajax.responseText;
				if(StrDatos.trim().length>0){
					StrDatos = replaceAll(StrDatos,"@", "<tr class=\"zz\" ");
					StrDatos = replaceAll(StrDatos,"#", "</tr>");
					StrDatos = replaceAll(StrDatos,";", "</td>");
					StrDatos = replaceAll(StrDatos,"%", "<td>");
					StrDatos = replaceAll(StrDatos,":", "ondblclick=");
					//											
					DivDatos.innerHTML = StrDatos;
					DivCaption.style.visibility="visible";
					DivDatos.style.visibility="visible";	
					document.getElementById("TAB_AJAX").scrollTop=0;
					//
					PINTAR_ZZ();					
				}else{
					DivCaption.style.visibility="hidden";
					DivDatos.style.visibility="hidden";	
				}
		   }
		}
		ajax.send(null);
	}

}
//**************************************************************************
function PINTAR_ZZ(){
	$('.zz').focus(function() {	
		this.style.backgroundColor="#FFCC00";
	}).blur(function() {
	   this.style.backgroundColor="white";
	});
	$('.zz').keydown(function(e) {
		if(e.which == 40) {
			var r=this.id;
			r=r.replace("z","");
			r=parseInt(r)+1;
			try {
				document.getElementById("z"+r).focus();
				e.preventDefault();
			} catch (e) {
				
			}
		}
		if(e.which == 38) {
			var r=this.id;
			r=r.replace("z","");
			r=parseInt(r)-1;
			if(r==0){
				r=1;
			}
			document.getElementById("z"+r).focus();
			e.preventDefault();
		}
		if(e.which == 13) {
			this.ondblclick();
			e.preventDefault();
		}
		if(e.which == 27) {
			CerrarDIV();			
			e.preventDefault();
			sv("TxtHora","0");
		}
	});	
}
//**************************************************************************

function CerrarDIV(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	DivCaption.style.visibility="hidden";
	DivDatos.style.visibility="hidden";	
	document.getElementById("TxtDes").focus();
}

//--------------------------------------------------------------------------
function z(lpStrId){
	if(lpStrId>0){
		ajax=CreateAJAX();
		ajax.open("GET","ajaz.php?CMD=INVOICE_GET_PRO&P01="+lpStrId);	
		ajax.onreadystatechange=function() {
			   if (ajax.readyState==4) {			  
					   var d = ajax.responseText
					   var r=d.split("|");
					   if(r[1]=="OK"){
						   var cod=r[3];
						   var des=r[4];
						   var pre=r[5];
						   var uni=r[6];
						   sv("PRO_COD",cod);
						   sv("Cod",cod);
						   sv("PRO_DES",des);
						   sv("Des",des);
						   sv("PRO_PRE",pre);
						   sv("Pre",pre);
						   jF("Pie");
						   document.getElementById("DivUni").innerHTML=uni;
						   sv("Und",uni);
					   }
					   //
					   DivDatos = document.getElementById("TAB_AJAX");
					   DivCaption = document.getElementById("TAB_CAPTION");
					   DivCaption.style.visibility="hidden";
					   DivDatos.style.visibility="hidden";		
							   
			   }
		}
		ajax.send(null);
		svv("PRO_ID,PRO_COD,PRO_DES,PRO_PRE");
		svv("Cod,Des,Pre,Pie,Caj,TotPie,Imp");
	}
}

//**************************************************************************
function fGetRuc(lpStrPro,Obj){
	var StrProveedor=lpStrPro;
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=GET_RUC&TxtPar="+StrProveedor);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {			   	   
				   document.getElementById(Obj).value=ajax.responseText;
				   Obj=Obj+"_";
				   document.getElementById(Obj).value=ajax.responseText;
		   }
	}
    ajax.send(null);
}
//**************************************************************************
function fCancelarPHP(){
	SM("L");
}
//**************************************************************************
function DetOK(cod,des,pie,caj){
	var j=jVal(gv("DET_TOT"));
	for(i=1;i<=j;i++){
		if(DET_COD[i]==cod && DET_DES[i]==des && jVal(DET_PIE[i])==jVal(pie)){
			   return i;
		}
	}
	return 0;
}
//**************************************************************************
function fAgregar(){
	var hwn=gv("HW");
	var pro_cod=gv("PRO_COD");
	var pro_des=gv("PRO_DES");	
	//
	var cod=gv("Cod");
	var des=gv("Des");
	var pie=gv("Pie");
	var caj=gv("Caj");
	var und=gv("Und");
	var tot_pie=gv("TotPie");
	var tot_uni=gv("TotUni");
	var tot_uwi=gv("TotUwi");
	var pre=gv("Pre");
	var imp=gv("Imp");
	//
	if(jL(pro_cod)==0){
		fCls();
		return AF("No se puede determinar el "+
		"codigo del producto seleccionado","Cod");
	}
	if(jL(pro_des)==0){
		fCls();
		return AF("No se puede determinar la "+
		"descripcion del producto seleccionado","Cod");
	}
	//
	if(jL(cod)==0){
		return AF("No se puede determinar el codigo","Cod");
	}
	if(jL(des)==0){
		return AF("El campo descripcion esta vacio","Des");
	}
	if(jL(pre)==0){
		return AF("Ingrese el precio del producto","Pre");
	}
	if(jL(pie)==0){
		return AF("Ingrese la cantidad de piezas","Pie");
	}
	if(jL(caj)==0){
		return AF("Ingrese la cantidad de cajas","Pie");
	}
	if(jVal(imp)<=0){
		return AF("El importe debe ser mayor a cero","Imp");
	}
	var det_ok=DetOK(cod,des,pie,caj);
	if(det_ok==0){
		document.getElementById("CmdAdd").disabled=true;
		SM("INVC_ADD");
	}else{
		alert("Codigo, descripcion y piezas ya registrado en el item "+det_ok.toString());
		jF("Pre");
	}
}
//**************************************************************************
function fGuardar(){
	var cod=gv("TxtId");
	var prv=gv("Prv");	
	var pro_ser=gv("ProSer");
	var pro_num=gv("ProNum");
	var inv_ser=gv("InvSer");
	var inv_num=gv("InvNum");
	var fec=gv("Fec");
	var emp=gv("Emp");	
	var ti=gv("TXT_TI");
	//
	if(jVac(prv,"Seleccione un proveedor","Prv"))return;
	if(jVac(pro_ser,"Ingrese la serie de la proforma","ProSer"))return;
	if(jVac(pro_num,"Ingrese el numero de la proforma","ProNum"))return;
	if(jVac(inv_ser,"Ingrese la serie del invoice","InvSer"))return;
	if(jVac(inv_num,"Ingrese el numero del invoice","InvNum"))return;
	if(jVac(fec,"Ingrese una fecha","Fec"))return;	
	if(!EsFecha(fec)){
		return AF("Fecha no valida","Fec");
	}
	if(jVac(emp,"Seleccione una empresa","Emp"))return;
	//
	if(jVal(ti)==0){
		return AF("La lista esta vacia, agrege uno o mas productos","Cod");
	}
	//
	if(jVal(cod)==0){
		if(pre("¿Esta seguro que desea guardar?")){
			document.getElementById("CmdG").disabled=true;
			SM("G");
		}
	}else{
		if(pre("¿Esta seguro que desea modificar?")){
			document.getElementById("CmdG").disabled=true;
			SM("M");
		}
	}
}
/**************************************************************************
	onkeypress="return pulsar(event);"
**************************************************************************/
function pulsar(e) { 	
	var tecla = (document.all) ? e.keyCode :e.which; 
	var Obj=event.srcElement.name;	
	if(Obj=="TxtNom"){
		if(tecla==13){			
			document.getElementById('CmdG').click();			
		}
	}	
	return (tecla!=13); 
} 
//**************************************************************************
function cDes(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	if(k==40){
		if(DivDatos.style.visibility!="hidden"){
			document.getElementById("z1").focus();
			event.preventDefault();	
		}
		return true;
	}
	if(k==13){
		//jF("TxtPre");		
		DivCaption.style.visibility="hidden";
		DivDatos.style.visibility="hidden";	
		event.preventDefault();
		return true;
	}
}
//
function cDesUP(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;	
	if(k==40 || k==27 || k==13){
		
	}else{
		sv('TxtHora',GetHora());
	}
}
//
function InsPro(){
	Ow("proveedores_p.php",470*ZO(),220*ZO());
}
//
function fProveedores(){
	R=fLista("pro_nombre","proveedores","id_proveedor>=0");
	FCBO('CboPro',R);
	jF("CboPro");
}
//
function EMP_OC(){
	var emp=gv("Emp");
	svv("Dir,Ruc");
	if(jL(emp)>0){
		var d=AZ("EMPC_GD",emp);
		var r=d.split("|");
		if(r[1]=="OK"){
			sv("Dir",r[6]);
			sv("Ruc",r[5]);
		}else{
			alert("No se pudo obtener respuesta");
		}
	}
}
//
function PER_OC(){
	var per=gv("Per");
	sv("Per1","");
	sv("Per2","");
	if(jL(per)>0){
		var d=AZ("PER_G",per);
		var r=d.split("|");
		if(r[1]=="OK"){
			sv("Per1",r[2]);
			sv("Per2",r[3]);
		}else{
			alert("No se pudo obtener respuesta");
		}
	}
}
//
function PRV_OC(){
	var cod=gv("TxtId");
	var prv_ori=gv("PRV_ORI");
	var prv_num=gv("PRV_NUM");
	//++
	var prv=gv("Prv");
	sv("PrvAni","");
	sv("ProSer","");
	sv("ProNum","");
	sv("InvSer","");
	sv("InvNum","");
	sv("ExpAge","");
	sv("ExpAgeAcc","");
	sv("TheBan","");
	sv("Add","");
	sv("Swi","");	
	//
	if(jL(prv)>0){
		var d=AZ("PRV_GSA",prv,cod,prv_ori,prv_num);
		var r=d.split("|");
		if(r[1]=="OK"){
			sv("ProSer",r[2]);
			sv("InvSer",r[2]);
			sv("ExpAge",r[3]);
			sv("ExpAgeAcc",r[4]);
			sv("TheBan",r[5]);
			sv("Add",r[6]);
			sv("Swi",r[7]);
			sv("PrvAni",r[8]);
			sv("ProNum",r[9]);
			sv("InvNum",r[9]);
		}else{
			alert("No se pudo obtener respuesta");
		}
	}
}
//
function fLEliminar(){
	if(pre("¿ Esta seguro que desea eliminar?")){
		SM("E");
	}
}
//
function CHE_NUM(){
	if(document.getElementById("CheNum").checked==true){
		document.getElementById("ProNum").readOnly=false;
		document.getElementById("ProNum").style.backgroundColor="#FFFFFF";
		document.getElementById("InvNum").readOnly=false;
		document.getElementById("InvNum").style.backgroundColor="#FFFFFF";
		jF("ProNum");
	}else{
		document.getElementById("ProNum").readOnly=true;
		document.getElementById("ProNum").style.backgroundColor="#CCC";
		document.getElementById("InvNum").readOnly=true;
		document.getElementById("InvNum").style.backgroundColor="#CCC";
	}
}
//
function CHE_CLI(){
	if(document.getElementById("CheFle").checked==true){
		document.getElementById("PreFle").readOnly=false;
		document.getElementById("PreFle").style.backgroundColor="#FFFFFF";
		document.getElementById("NumFle").readOnly=false;
		document.getElementById("NumFle").style.backgroundColor="#FFFFFF";
		//
		//sv("PreFle","");
		sv("NumFle","1");
		//sv("AmoFle","");
		//
		sv("PreCfr",gv("PreFob"));
		sv("NumCfr","1");
		sv("AmoCfr",gv("AmoFob"));		
		//++
		sv("SayTot",NumeroALetrasEx(gv("AmoCfr")));
		jF("PreFle");
	}else{
		document.getElementById("PreFle").readOnly=true;
		document.getElementById("PreFle").style.backgroundColor="#CCC";
		document.getElementById("NumFle").readOnly=true;
		document.getElementById("NumFle").style.backgroundColor="#CCC";
		//
		sv("PreFle","");
		sv("NumFle","");
		sv("AmoFle","");
		//
		sv("PreCfr","");
		sv("NumCfr","");
		sv("AmoCfr","");
		//++
		sv("SayTot",NumeroALetrasEx(gv("AmoFob")));
	}
	sv("TotAmo",gv("AmoFob"));
}
//
function kalcular(){
	var Pre_Fle=jVal(gv("PreFle"));
	var Num_Fle=jVal(gv("NumFle"));
	var Amo_Fle=jVal(gv("AmoFle"));
	//
	var Pre_Fob=jVal(gv("PreFob"));
	var Num_Fob=jVal(gv("NumFob"));
	var Amo_Fob=jVal(gv("AmoFob"));
	//
	var Pre_Cfr=jVal(gv("PreCfr"));
	var Num_Cfr=jVal(gv("NumCfr"));
	var Amo_Cfr=jVal(gv("AmoCfr"));
	//
	if(document.getElementById("CheFle").checked==true){
		Amo_Fle=redondear(Pre_Fle * Num_Fle,3);		
		sv("AmoFle",Amo_Fle);
		
		Pre_Cfr=redondear(Pre_Fle + Pre_Fob,3);
		sv("PreCfr",Pre_Cfr);
		
		Amo_Cfr=Amo_Fle + Amo_Fob;
		sv("AmoCfr",Amo_Cfr);
		//++
		sv("SayTot",NumeroALetrasEx(gv("AmoCfr")));
		sv("TotAmo",gv("AmoCfr"));
	}else{
		sv("PreFle","");
		sv("NumFle","");
		sv("AmoFle","");
		//
		sv("PreCfr","");
		sv("NumCfr","");
		sv("AmoCfr","");
		//++
		sv("SayTot",NumeroALetrasEx(gv("AmoFob")));
		sv("TotAmo",gv("AmoFob"));
	}
}

function NumeroALetrasEx(num){
	var r=NumeroALetras(num);
	var d=replaceAll(r,"UN MIL", "MIL");
	return d;
}

function FILL_1(obj){
	var n=gv(obj.id);
	if(jL(n)>0){
		if(jL(n)==1)n="00000"+n;
		if(jL(n)==2)n="0000"+n;
		if(jL(n)==3)n="000"+n;
		if(jL(n)==4)n="00"+n;
		if(jL(n)==5)n="0"+n;
		sv(obj.id,n);
	}
}


function cCod(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	if(k==40){
		if(DivDatos.style.visibility!="hidden"){
			document.getElementById("z1").focus();
			event.preventDefault();
			
		}
		return true;
	}
	if(k==13){
		var cod=gv("Cod");		
		if(jL(cod)==0){
			jF("Des");
			return;
		}
		if(k==13){
			GetCod(cod);
		}
	}
}

function GetCod(Cod){	
	svv("PRO_ID,PRO_COD,PRO_DES");
	svv("Cod,Des,Pre,Pie,Caj,TotPie,Imp");
	
	var d=AZ("GET_PRO_COD",Cod);
	var r=d.split("|");
	if(r[1]=="MS"){
		alert(r[2]);
	}else{
		if(r[1]=="OK"){
			var kod=r[2];
			var cod=r[3];
			var des=r[4];
			var pre=r[5];
			//
			sv("PRO_ID",kod);
			sv("PRO_COD",cod);
			sv("PRO_DES",des);
			sv("Cod",cod);
			sv("Des",des);
			sv("Pre",pre);
			jF("Pie");
			//
		}else{
			alert("Error de conexion");
		}
	}
}

function cCodUP(e){	
	var k;
	k=(document.all) ? e.keyCode :e.which;	
	if(k==40 || k==27 || k==13){
		
	}else{
		sv('TxtHora',GetHora());
	}
}

function myStopFunction(){
	clearInterval(myVar);
}

$(document).keydown(function(e){
	var AT=gv("TxtAT");
	
	if(e.which==71 && e.altKey){		
		if(AT=="1"){	
		document.getElementById("CmdG").click();
		return;
		}
	}
	
	if(e.which==78 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdC").click();
		return;
		}
	}
	
	if(e.which==75 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdE").click();
		return;
		}
	}

	if(e.which==66 && e.altKey){
		if(AT=="1"){	
		document.getElementById("CmdB").click();
		return;
		}
	}

	if(e.which==77 && e.altKey){
		if(AT=="1"){
		document.getElementById("CmdZ").click();
		return;
		}
	}
  
});

function myTimer(){
	t=GetHora();
	tt=document.getElementById("TxtHora").value;
	if(tt>0 && document.getElementById("ChN_BAR").checked==false){	
		if(t-tt>=1){
			document.getElementById("TxtHora").value=0;
			fGetTabla();		
		}
	}
}

function fE(cx){
	var hw=gv("HW");
	Ow("ges_invoice_editar.php?TMP_KOD="+cx+"&HWN="+hw,1300,470);
}

function fR(cx,pid,ite){
	var inv_kod=gv("TxtId");
	if(jVal(inv_kod)==0){
		alert("No se puede determinar el Id del registro activo, guarde o active un registro");
		return;
	}
	if(jVal(pid)==0){
		alert("No se puede determinar el producto del item seleccionado");
		return;
	}
	var hwn=gv("HW");
	Ow("ges_inv_prod_edit.php?INV_KOD="+inv_kod+"&INV_ITE="+ite+"&INV_PID="+pid+"&HWN="+hwn+"&TMP_KOD="+cx,1300,670);
}
