String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
//**************************************************************************
function fFiltrar(){
	document.getElementById("TxtAc").value="F";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function VUELTO(){
	var StrTC=document.getElementById("TxtTC").value;
	var StrMon=document.getElementById("CboMon").value;
	var StrTot=document.getElementById("T_TOH").value;
	var StrPag=document.getElementById("TxtPagarCon").value;
	if(StrPag.trim().length==0){
		StrPag="0";
	}
	StrTC=parseFloat(StrTC);
	StrTot=parseFloat(StrTot);
	StrPag=parseFloat(StrPag);
	if(StrMon=="DOLARES"){
		StrPag=StrPag * StrTC;
		StrPag=StrPag.toFixed(2);
	}
	var Vuelto=parseFloat("0");
	if(StrTot>StrPag){
		document.getElementById("TxtVuelto").value="0.0";
		return;
	}else{
		Vuelto=StrPag - StrTot;
	}
	Vuelto=Vuelto.toFixed(2);
	document.getElementById("TxtVuelto").value=Vuelto;
}

//**************************************************************************
function TDP(){
	var StrTdp=document.getElementById("CboTdp").value;
	document.getElementById("CboMon").value="SOLES"
	if(StrTdp=="CONTADO"){
document.getElementById("TxtPagarCon").readOnly=false;
document.getElementById("TxtPagarCon").value=document.getElementById("T_TO").value;		
document.getElementById("TxtVuelto").value="0.0";
document.getElementById("CboMon").disabled=false;
	}else{
		document.getElementById("TxtPagarCon").readOnly=true;
		document.getElementById("TxtPagarCon").value="0.0";
		document.getElementById("CboMon").disabled=true;
	}
	
}
function AutoNumeracion(estado){
	document.getElementById("TxtSer").value="";
	document.getElementById("TxtNum").value="";
	var Plom=document.getElementById("TxtTC").style.backgroundColor;
	var Blan=document.getElementById("TxtCod").style.backgroundColor;
	if(estado>0){
		document.getElementById("TxtSer").readOnly=true;
		document.getElementById("TxtNum").readOnly=true;
		document.getElementById("TxtSer").style.backgroundColor=Plom;
		document.getElementById("TxtNum").style.backgroundColor=Plom;
		if(estado==1){
		document.getElementById("TxtCliente").focus();
		}
	}else{
		document.getElementById("TxtSer").readOnly=false;
		document.getElementById("TxtNum").readOnly=false;
		document.getElementById("TxtSer").style.backgroundColor=Blan;
		document.getElementById("TxtNum").style.backgroundColor=Blan;
		document.getElementById("TxtSer").focus();
	}
}
//**************************************************************************
function CalcularIMPORTE(){
	var Importe=document.getElementById("T_Importe").value;
	var stt=0;
	var igv=0;
	var tot=0;	
	var uno=0;
	if(Importe.trim().length==0) Importe="0";
	Importe=parseFloat(Importe);
	if(document.getElementById("ChN_INC").checked){
		stt=Importe;
		igv=stt * 0.18;
		igv=igv.toFixed(2);
		tot=parseFloat(stt) + parseFloat(igv);
	}else{
		uno=Importe / 118;
		uno=uno.toFixed(2);
		igv=uno * 18;
		igv=igv.toFixed(2);
		stt=parseFloat(Importe) - parseFloat(igv);
		tot=parseFloat(stt)+parseFloat(igv);
	}
	document.getElementById("T_ST").value=formato_numero(stt,2,".");
	document.getElementById("T_IG").value=formato_numero(igv,2,".");
	document.getElementById("T_TO").value=formato_numero(tot,2,".");	
}
function fCls(){
	document.getElementById("TxtIdCliente").value="0";
	document.getElementById("TxtCliente").value="";
	document.getElementById("TxtRuc").value="";
}
//**************************************************************************
function fImporte(){
	var Pre=document.getElementById("TxtPre").value;
	var Can=document.getElementById("TxtCant").value;
	var Des=document.getElementById("TxtDscto").value;
	var Importe=parseFloat("0");
	if(Des.trim().length==0)Des="0";
	if(Pre.trim().length==0)Pre="0";
	if(Can.trim().length==0)Can="0";
	Des=parseFloat(Des);
	Pre=parseFloat(Pre);
	Can=parseFloat(Can);
	Importe=(Pre - Des )* Can;
	Importe=Importe.toFixed(2);
	document.getElementById("TxtImp").value=Importe;
}
//**************************************************************************
function fKiller(lpIndex){
	document.getElementById("Pro_TxtId").value=lpIndex;
	document.getElementById("TxtAc").value="KILLER";
	document.getElementById("Frm").submit();
}
//**************************************************************************
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 fGetTabla(){
	DivDatos = document.getElementById("TAB_AJAX");
	DivCaption = document.getElementById("TAB_CAPTION");
	EsNew = document.getElementById("CboNew").value;
	if(EsNew=="SI"){
		DivCaption.style.visibility="hidden";
		DivDatos.style.visibility="hidden";	
		return;
	}	
var Cod=document.getElementById("TxtCod").value;
var Des=document.getElementById("TxtDes").value;
var Mar=document.getElementById("TxtMarca").value;
var Alm=document.getElementById("CboAlm").value;
//
Cod=Cod.replace("+", "@");
Des=Des.replace("+", "@");
//
if(Des.trim().length>0 || Cod.trim().length>0 || Mar.trim().length>0){		
	ajax=CreateAJAX();
var StrU="ajax_funciones.php?Cmd=VENTAS_PROD_FILTRAR&TxtPar="+Des+"&TxtPar2="+Alm+"&TxtPar3="+Mar+"&TxtPar4="+Cod;
	ajax.open("GET", StrU);	
	ajax.onreadystatechange=function() {
		   if (ajax.readyState==4) {
					var StrDatos=ajax.responseText;
					if(StrDatos.trim().length>0){			  
					   DivDatos.innerHTML = StrDatos;
					   DivCaption.style.visibility="visible";
					   DivDatos.style.visibility="visible";	
					}else{
						DivCaption.style.visibility="hidden";
						DivDatos.style.visibility="hidden";	
					}
		   }
	}
	ajax.send(null);
}
}
//**************************************************************************
function fGetProdDatos_Id(lpStrId){	
	if(lpStrId.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=VEN_GET_PROD_DATOS_X_ID&TxtPar="+lpStrId);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {			  
		   DivDatos.innerHTML = ajax.responseText
		   var Des=document.getElementById("Pro_TxtDes").value;
		   if(Des.trim().length==0){
			   document.getElementById("Pro_TxtId").value="";
			   document.getElementById("Pro_TxtDes").value="";
			   document.getElementById("Pro_TxtCod").value="";
			   document.getElementById("Pro_TxtMarca").value="";
			   document.getElementById("Pro_TxtPre").value="";
			   document.getElementById("Pro_TxtPreM").value="";
			   document.getElementById("TxtDes").value="";
			   document.getElementById("TxtMarca").value="";
			   document.getElementById("TxtPre").value="";				   
			   document.getElementById("TxtPreM").innerHTML="0.0";
			   document.getElementById("TxtCant").value="";
			   document.getElementById("TxtImp").value="";
			   document.getElementById("TxtCod").value="";				   
			   alert("Codigo no registrado");
			   document.getElementById("TxtCod").focus();
		   }else{
			   document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
			   document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
			   document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
			   document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
			   document.getElementById("TxtPreM").innerHTML=PP(document.getElementById("Pro_TxtPreM").value);
			   document.getElementById("TxtCant").value="1";
			   document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
			   document.getElementById("TxtPre").focus();				   
		   }
		   DivDatos = document.getElementById("TAB_AJAX");
		   DivCaption = document.getElementById("TAB_CAPTION");
		   DivCaption.style.visibility="hidden";
		   DivDatos.style.visibility="hidden";
	   }
	}
    ajax.send(null);
	}
}
function z(lpStrId){
	if(lpStrId.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=VEN_GET_PROD_DATOS_X_ID&TxtPar="+lpStrId);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {			  
		   DivDatos.innerHTML = ajax.responseText
		   var Des=document.getElementById("Pro_TxtDes").value;
		   if(Des.trim().length==0){
			   document.getElementById("Pro_TxtId").value="";
			   document.getElementById("Pro_TxtDes").value="";
			   document.getElementById("Pro_TxtCod").value="";
			   document.getElementById("Pro_TxtMarca").value="";
			   document.getElementById("Pro_TxtPre").value="";
			   document.getElementById("Pro_TxtPreM").value="";
			   document.getElementById("TxtDes").value="";
			   document.getElementById("TxtMarca").value="";
			   document.getElementById("TxtPre").value="";				   
			   document.getElementById("TxtPreM").innerHTML="0.00";
			   document.getElementById("TxtCant").value="";
			   document.getElementById("TxtImp").value="";
			   document.getElementById("TxtCod").value="";				   
			   alert("Codigo no registrado");
			   document.getElementById("TxtCod").focus();
		   }else{
			   document.getElementById("TxtCod").value=document.getElementById("Pro_TxtCod").value;
			   document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
			   document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
			   document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
			   document.getElementById("TxtPreM").innerHTML=PP(document.getElementById("Pro_TxtPreM").value);
			   document.getElementById("TxtCant").value="1";
			   document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
			   document.getElementById("TxtPre").focus();				   
		   }
		   DivDatos = document.getElementById("TAB_AJAX");
		   DivCaption = document.getElementById("TAB_CAPTION");
		   DivCaption.style.visibility="hidden";
		   DivDatos.style.visibility="hidden";						   		   
	   }
	}
    ajax.send(null);
	}
}
//**************************************************************************
function PP(Pre){
	var Mon=document.getElementById("CboMon").value;
	var Tca=document.getElementById("TxtTC").value;
	Mon=Mon.trim();Pre=Pre.trim();Tca=Tca.trim();
	if(Mon.length==0)Mon="SOLES";
	if(Pre.length==0)Pre="0";
	if(Tca.length==0)Tca="1";
	Pre=parseFloat(Pre);Tca=parseFloat(Tca);
	if(Mon=="DOLARES"){
		Pre=Pre / Tca;Pre=Pre.toFixed(2);		
	}	
	return ff(Pre,1,".","");
}
function fGetProdDatos(lpStrCod){
	EsNew = document.getElementById("CboNew").value;
	if(EsNew=="SI"){
		return;
	}	
	if(lpStrCod.trim().length>0){
	DivDatos = document.getElementById("DivDatosProd");
	ajax=CreateAJAX();
	ajax.open("GET", "ajax_funciones.php?Cmd=VEN_GET_PROD_DATOS&TxtPar="+lpStrCod);	
	ajax.onreadystatechange=function() {
	   if (ajax.readyState==4) {			  
		   DivDatos.innerHTML = ajax.responseText
		   var Des=document.getElementById("Pro_TxtDes").value;
		   if(Des.trim().length==0){
			document.getElementById("Pro_TxtId").value="";
			document.getElementById("Pro_TxtDes").value="";
			document.getElementById("Pro_TxtMarca").value="";
			document.getElementById("Pro_TxtPre").value="";
			document.getElementById("Pro_TxtPreM").value="";
			document.getElementById("TxtDes").value="";
			document.getElementById("TxtPre").value="";				   
			document.getElementById("TxtMarca").value="";				   
			document.getElementById("TxtCant").value="";
			document.getElementById("TxtImp").value="";
			document.getElementById("TxtCod").value="";
			document.getElementById("TxtPreM").innerHTML="0.00";		   
			alert("Codigo no registrado, si el producto es nuevo estableza *SI* en *Es Nuevo*");
			document.getElementById("TxtCod").focus();
		   }else{
			document.getElementById("TxtDes").value=document.getElementById("Pro_TxtDes").value;
			document.getElementById("TxtMarca").value=document.getElementById("Pro_TxtMarca").value;
			document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
			document.getElementById("TxtPre").value=PP(document.getElementById("Pro_TxtPre").value);
			document.getElementById("TxtPreM").innerHTML=PP(document.getElementById("Pro_TxtPreM").value);
			document.getElementById("TxtCant").value="1";
			document.getElementById("TxtImp").value=document.getElementById("TxtPre").value;
			document.getElementById("TxtPre").focus();
		   }
	   }
	}
    ajax.send(null);
	}
}
//**************************************************************************
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 fGetProveedor(lpStrRuc,Obj){
	var StrRucH=document.getElementById("TxtRuc_").value;
	var StrRuc=lpStrRuc;
	if(StrRuc!=StrRucH){
		ajax=CreateAJAX();
		ajax.open("GET", "ajax_funciones.php?Cmd=GET_PROVEEDOR&TxtPar="+StrRuc);	
		ajax.onreadystatechange=function() {
			   if (ajax.readyState==4) {
					   var StrPro=ajax.responseText;
					   if(StrPro.trim().length==0){
						   	alert("Numero de ruc no registrado");
						   	document.getElementById(Obj).value="";
						   	document.getElementById(Obj).focus();
					   }else{
							document.getElementById(Obj).value=StrPro;
					   }
			   }
		}
		ajax.send(null);
	}

}
//**************************************************************************
function fCancelarPHP(){
	document.getElementById("TxtAc").value="C";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fAgregar(){
	var EsNew=document.getElementById("CboNew").value;
	var Pro_Id=document.getElementById("Pro_TxtId").value;
	var Pro_Cod=document.getElementById("Pro_TxtCod").value;
	var Pro_Des=document.getElementById("Pro_TxtDes").value;
	
	var Cod=document.getElementById("TxtCod").value;
	var Des=document.getElementById("TxtDes").value;
	var Pre=document.getElementById("TxtPre").value;
	var Dsto=document.getElementById("TxtDscto").value;
	var PreM=document.getElementById("TxtPreM").innerHTML;
	var Can=document.getElementById("TxtCant").value;
	
	Pro_Id=Pro_Id.trim().toUpperCase();
	Pro_Cod=Pro_Cod.trim().toUpperCase();
	Pro_Des=Pro_Des.trim().toUpperCase();
	
	Cod=Cod.trim().toUpperCase();
	Des=Des.trim().toUpperCase();
	
	if(EsNew=="NO"){
		if(Cod!=Pro_Cod){
alert("El codigo del producto ha sido alterado, si el producto es nuevo estableza *SI* en *Es Nuevo*");
			document.getElementById("TxtCod").focus();
			return;
		}
		if(Des!=Pro_Des){
alert("El codigo del producto ha sido alterado, si el producto es nuevo estableza *SI* en *Es Nuevo*");
			document.getElementById("TxtDes").focus();
			return;
		}
	}
	//
	Dsto=Dsto.trim();
	Pre=Pre.trim();
	PreM=PreM.trim();
	if(Pre.length==0){
		alert("Ingrese el precio de venta");
		document.getElementById("TxtPre").focus();
		return;
	}
	if(Pre.length==0)Pre="0";
	if(PreM.length==0)PreM="0";
	if(Dsto.length==0)Dsto="0";
	Pre=parseFloat(Pre) - parseFloat(Dsto);
	PreM=parseFloat(PreM);
	if(PreM>Pre){
		var Rpta = confirm("El precio de venta menos el descuento ES MENOR al precio de venta minimo,¿Aun asi desea continuar?")		
		if (Rpta==false){
			document.getElementById("TxtPre").focus();
			return;
		}
	}
	//
	document.getElementById("TxtAc").value="ADD";
	document.getElementById("Frm").submit();
}
//**************************************************************************
function fGuardar(){
	var Alm=document.getElementById("CboAlm").value;
	var Fec=document.getElementById("TxtFec").value;
	var Com=document.getElementById("CboCom").value;
	var Num=document.getElementById("TxtNum").value;
	var Ser=document.getElementById("TxtSer").value;
	
	if(Alm.trim().length==0){
		alert("Seleccione un almacen");
		document.getElementById("CboAlm").focus();
		return;
	}
	if(Fec.trim().length==0){
		alert("Ingrese una fecha");
		document.getElementById("TxtFec").focus();
		return;
	}
	if(EsFecha(Fec)==false){
		alert("Fecha no valida, la fecha debe ser ingresada segun el formato");
		document.getElementById("TxtFec").focus();
		return;
	}	
	if(Com.trim().length==0){
		alert("Seleccione un comprobante");
		document.getElementById("CboCom").focus();
		return;
	}
	if(document.getElementById("ChN_AUTO").checked==false){
		if(Ser.trim().length==0){
			alert("Ingrese la serie del comprobante.");
			document.getElementById("TxtSer").focus();
			return;
		}
		if(Num.trim().length==0){
			alert("Ingrese el numero de comprobante");
			document.getElementById("TxtNum").focus();
			return;
		}
	}	
	if(document.getElementById("CmdG").value=="Guardar"){
		var Rpta = confirm("Esta seguro que desea Guardar?")		
		if (Rpta==false){
			return;
		}
		document.getElementById("TxtAc").value="G";
	}else{
		var Rpta = confirm("Esta seguro que desea modificar?")		
		if (Rpta==false){
			return;
		}
		document.getElementById("TxtAc").value="M";
	}
	document.getElementById("Frm").submit();
}
/**************************************************************************
	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); 
} 
//**************************************************************************
