// JavaScript Document
var cursorActual;

function enter (evt, next_id){
	switch (evt.keyCode){
        case 37: //left arrow
        case 39: //right arrow
        case 33: //page up  
        case 34: //page down  
        case 36: //home  
        case 35: //end
			break;
        case 13: //enter
			var id=document.getElementById(next_id);
			id.focus();
			break;
        case 9: //tab
        case 27: //esc
        case 16: //shift  
        case 17: //ctrl  
        case 18: //alt  
        case 20: //caps lock
        case 8: //backspace  
        case 46: //delete 
        case 38: //up arrow 
        case 40: //down arrow
            return;
		default:
			//var id=document.getElementById(next_id);
			//id.focus();
        break;
	}
	//alert (evt.keyCode);
	if (evt.keyCode == undefined){
		var id=document.getElementById(next_id);
		id.focus();
	}
}

function busca_enter(evt,items,btn){
   switch (evt.keyCode){
     case 13:
        if(btn){ $(items).click(); }else{ $(items).focus(); }
     break;
   }
}
function precio_ADM(e) { 
    tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==0) return true; //Tecla de retroceso 
    if (tecla==8) return true; //Tecla de retroceso  
	if (tecla==9) return true; //Tecla de retroceso (para poder borrar) 
	 patron =/([0-9.-])+/
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
}
function precio(e) { 
    tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==0) return true; //Tecla de retroceso 
    if (tecla==8) return true; //Tecla de retroceso  
	if (tecla==9) return true; //Tecla de retroceso (para poder borrar) 
	 patron =/([0-9.])+/
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
}

function numeros_ADM(e) { 
    tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==0) return true; //Tecla de retroceso 
    if (tecla==8) return true; //Tecla de retroceso  
	if (tecla==9) return true; //Tecla de retroceso (para poder borrar) 
	 patron =/([0-9-])+/
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
}

function numeros(e) { 
    tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==0) return true; //Tecla de retroceso 
    if (tecla==8) return true; //Tecla de retroceso  
	if (tecla==9) return true; //Tecla de retroceso (para poder borrar) 
	 patron =/([0-9])+/
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
}

function telef(e) { 
    tecla = (document.all) ? e.keyCode : e.which; 
	if (tecla==0) return true; //Tecla tabulador 
    if (tecla==8) return true; //Tecla para borrar caracteres.  
	if (tecla==9) return true; //Tecla de retroceso (para poder borrar) 
	 patron =/([0-9-/ ()])/
    te = String.fromCharCode(tecla); 
    return patron.test(te);  
}

function validaCorreo(valor){
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)){ return true; } else { return false; }
}

function txt_area(e) { 
    var evento= e || window.event;
	var tecla = evento.charCode || evento.keyCode;
	var pala;
   // tecla = (document.all) ? e.keyCode : e.which;
	if (tecla == 34 || tecla==39) {
	return false
	}else{//Tecla de enter 
	return true
	}
}

function money(e){
e.value=NumberFormat(e.value,2,'.',',')
}
function NumberFormat(num, numDec, decSep, thousandSep){ 
	var arg; 
	var Dec; 
	Dec = Math.pow(10, numDec);  
	if (typeof(num) == 'undefined') return;  
	if (typeof(decSep) == 'undefined') decSep = ','; 
	if (typeof(thousandSep) == 'undefined') thousandSep = '.'; 
	if (thousandSep == '.') 
	 arg=/./g; 
	else 
	 if (thousandSep == ',') arg=/,/g; 
	if (typeof(arg) != 'undefined') num = num.toString().replace(arg,''); 
	num = num.toString().replace(/,/g, '.');  
	if (isNaN(num)) num = "0"; 
	sign = (num == (num = Math.abs(num))); 
	num = Math.floor(num * Dec + 0.50000000001); 
	cents = num % Dec; 
	num = Math.floor(num/Dec).toString();  
	if (cents < (Dec / 10)) cents = "0" + cents;  
	for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++) 
	 num = num.substring(0, num.length - (4 * i + 3)) + thousandSep + num.substring(num.length - (4 * i + 3)); 
	if (Dec == 1) 
	 return (((sign)? '': '-') + num); 
	else 
	 return (((sign)? '': '-') + num + decSep + cents); 
}

function dformat(vari){
for (z=0;z<=vari.length;z++){
vari = vari.replace(',', '');
}
return ( parseFloat(vari) ) ;
}

function salir(pages){
   if(confirm('Se Perderan todos los datos que no fueron guardados.')){
     window.parent.location.href=pages;
   }
}

function out(){
   if(confirm('Se Perderan todos los datos que no fueron guardados.')){
     window.close();
   }
}

function eliminar_datos(destino,titulo,div){
   if(confirm(titulo)){
     open_page(destino,div,'');
   }
}

function vacio(q) {   
        for ( i = 0; i < q.length; i++ ) {   
                if ( q.charAt(i) != " " ) {   
                        return true   
                }   
        }   
        return false   
}

function limpiaNuevo(){
$('sku').value='';		$('cantidad').value='';		$('existencias').value='';		$('descripcion').value='';		
$('precio').value='';	$('descuento').value='';	$('desc1').value='';			$('desc2').value=''; 
$('subtotal').value='';	$('total').value='';	
}

function llenaDatos(valor){
if(valor!='<'){ $(cursorActual).value=$F(cursorActual)+valor;  }
muestraDatos();
}

function muestraDatos(){
var precio;
 if( (cursorActual!='pv')&&(cursorActual!='cantu') ){
	 if( (cursorActual=='cantidad')||(cursorActual=='descuento') ){
		if(vacio($F('cantidad'))==true){ 
		  if( ( parseInt($F('cantidad')) <= parseInt($F('existencias')) )&&( parseInt($F('cantidad')) > 0)||( vacio($F('cantidad'))==false) ){ 
		    precio=dformat($F('precio'))*parseInt($F('cantidad'));
	        $('total').value=NumberFormat(precio,2,'.',',');
	        $('subtotal').value=NumberFormat(dformat($F('precio')),2,'.',',');
		  }else{
			alert('SOLO SE CUENTA CON UNA DISPONIBILIDAD DE '+$F('existencias')+' PIEZAS.'); 
			$('cantidad').value=1;
		    precio=dformat($F('precio'))*parseInt($F('cantidad'));
	        $('total').value=NumberFormat(precio,2,'.',',');
	        $('subtotal').value=NumberFormat(dformat($F('precio')),2,'.',',');			
			}
		}
	 }
	 if($F('desc1')=='X'){ descuentos(1); } // pesos
	 if($F('desc2')=='X'){ descuentos(2); } // %
	 if( (cursorActual=='tpesos')||(cursorActual=='tdolar')||(cursorActual=='ttarjeta') ){
		if(cursorActual=='tdolar'){
		  var dolar = dformat($F('tdolar3'));  
		  var resp = dformat($F('tdolar'))*dolar;
		  $('tdolar2').value = NumberFormat(resp,2,'.',','); 
		}
		var dll=0; var pss=0; var tar=0;
	   if(vacio($F('tdolar2'))==false){ dll=0; }else{ dll=dformat($F('tdolar2')); }	
	   if(vacio($F('ttarjeta'))==false){ tar=0; }else{ tar=dformat($F('ttarjeta')); }	
	   if(vacio($F('tpesos'))==false){ pss=0; }else{ pss=dformat($F('tpesos')); }
	   $('pagoCliente').value = dll + tar + pss;
	   var lana =  $F('pagoCliente') - dformat($F('vTotal')); 
	   $('cambio').value=NumberFormat(lana,2,'.',',');
	 }
	 
	 if(cursorActual=='pagoCliente'){
		 if($F('tip2')=='X'){ $('cambio').value=0.00; } // pago tarjeta
		 if($F('tip1')=='X'){ // efectivo
		   var lana =  $F('pagoCliente') - dformat($F('vTotal')); 
		   if(lana<0){lana=lana*(1);}
		   $('cambio').value=NumberFormat(lana,2,'.',',');
		 } 
		 if($F('tip3')=='X'){ // dolares 
		   var lana =  dformat($F('pagoCliente')-dformat($F('vDolar'))); 
		   if(lana<0){lana=lana*(1);}
		   lana=lana*$F('tc');
		   $('cambio').value=NumberFormat(lana,2,'.',',');;
		 } 	 
	 }	
 }else{
	precio=dformat($F('vPrecio')); // precio unitario.
	if( ( parseInt($F('cantu')) <= parseInt($F('vCant')) )&&( parseInt($F('cantu')) >= 0 )||( vacio($F('cantu'))==false) ){
	  	$('precio').value=NumberFormat(precio*parseInt($F('cantu')),2,'.',',');
	}else{
		alert('SOLO SE CUENTA CON UNA DISPONIBILIDAD DE '+$F('vCant')+' PIEZAS.'); 
		$('cantu').value=''; $('precioF').value=''; $('precio').value=NumberFormat(precio,2,'.',',');;
	}
 }

}// fin de la funcion

function descuentos(op){
var precio=dformat($F('precio'))*parseInt($F('cantidad')); 
var totalA=0;
  if($F('descuento')!=''){
	if(op==1){// descuento en pesos 
	    totalA=precio-parseInt($F('descuento'));
		$('total').value=NumberFormat(totalA,2,'.',',');
	}else{// descuento en porsentaje
	    var porsentaje=parseInt($F('descuento'))/100;
	    var descuento=(precio*porsentaje);
		totalA=precio-descuento;
		$('total').value=NumberFormat(totalA,2,'.',',');		
	}
    $('subtotal').value=NumberFormat(totalA/parseInt($F('cantidad')),2,'.',',');	
  }else{
	$('desc1').value=''; $('desc2').value=''; 
	llenaDatos('<');
  }
}

function borraDato(){
  var cadena = new String($F(cursorActual));
  $(cursorActual).value = cadena.substr(0,cadena.length-1);
  llenaDatos('<');
}

function llenaDatos2(valor){
if(valor!='<'){ $(cursorActual).value=$F(cursorActual)+valor;  }

}

function borraDato2(){
  var cadena = new String($F(cursorActual));
  $(cursorActual).value = cadena.substr(0,cadena.length-1);
  llenaDatos2('<');
}