function fRfc(jsEmpre)
{
  document.location.href = "inicio_sesion.php?htEmpre="+jsEmpre;		
//  window.open('sesion.php?htEmpre='+jsEmpre, 'Cotizacion','width=450,height=240,scrollbars=yes');
}

function Envia(jsCodigo,jsEmpre)
{
	document.frmDatos.htDepar.value = jsCodigo;
	document.frmDatos.htEmpre.value = jsEmpre;
	document.frmDatos.submit();
}

function Selecciona(jsI, value) 
{
	jsNombreCantidad = 'htCantidad'+jsI; //Nombre del input de la cantidad
	if (value) //si esta seleccionado le pongo el foco
		 document.getElementById(jsNombreCantidad).focus();		
	else //El check no esta seleccionado
		 document.getElementById(jsNombreCantidad).value = '';		
}//fin dde la funcion

function SeleccionaPrecio(jsI, jsValue,jsPrecio) 
{
	jsTotal = 'htTotal'+jsI; //Suma total de todos los articulos
	jsNombrePrecio = 'PrecioTotal'+jsI; //Nombre del precio total por producto
	jsNombreCantidad = 'htCantidad'+jsI; //Nombre del input de la cantidad
	elem = document.getElementById('Total'); //Obtengo el id del total
	iva = document.getElementById('Iva');
	suma = document.getElementById('SumaTotal');
	precio = document.getElementById(jsNombrePrecio); //Obtengo el id del precio total producto
	//obtengo el id del precio total del articulo que se modifico
	elemPrecio = document.getElementById(jsNombrePrecio);
	//Obtengo el precio Total anterior
	jsPrecioTotal = parseFloat (document.frmDatos.htTotal.value);
	jsCantidad = document.getElementById(jsNombreCantidad).value;
	if (jsCantidad>0)
	{
		jsTotalProducto = parseFloat(jsPrecio) * parseFloat(jsCantidad);
	}
	else
	{
		jsTotalProducto = 0;
	}
	if (jsValue)
	{	
		//si esta seleccionado le pongo el foco a la cantidad
		 document.getElementById(jsNombreCantidad).focus();		
	}
	else //El check no esta seleccionado
	{
		document.getElementById(jsNombreCantidad).value= "";
		jsNuevoTotal = jsPrecioTotal - jsTotalProducto;
		document.frmDatos.htTotal.value = jsNuevoTotal;
		precio.innerHTML = 0;
		jsIva = jsNuevoTotal * .15;
		jsSumaTotal = jsNuevoTotal + jsIva;
		elem.innerHTML = addCommas(jsNuevoTotal.toFixed(2));
		suma.innerHTML = addCommas(jsSumaTotal.toFixed(2));
		iva.innerHTML = addCommas(jsIva.toFixed(2));
	}
}//fin dde la funcion

function CalculaPrecio(jsI,jsCantidad,event) 
{
		(event.keyCode) ? k=event.keyCode : k=event.which;
		jsAux = jsI + 1;
		jsSiguiente = 'htCantidad'+jsAux;
		jsActual = 'htCantidad'+jsI;
 		jsCheck = 'htArticulo'+jsI;
		jsNombrePrecio = 'PrecioTotal'+jsI; //Nombre del precio total por producto
		precio = document.getElementById(jsNombrePrecio); //Obtengo el id del precio total producto
		if (event.keyCode == 13 || event.keyCode == 9)
		{
			jsRegistros = document.frmDatos.htRegistros.value;
			if (jsAux<jsRegistros)
				document.getElementById(jsSiguiente).focus(); //le pongo el focus al siguiente
		}
		else
		{
			if (isNaN(jsCantidad))
			{
				document.getElementById(jsActual).value = "";	
			}
		}
		if (jsCantidad > 0)
		{
			document.getElementById(jsCheck).checked = true; //lo selecciono
		}
		else
		{
			document.getElementById(jsCheck).checked = false; //le quito la seleccion
			precio.innerHTML = 0;
		}
}

function Calcula(elemento,jsValue,event) 
{
/*CODIGOS
       	 8          -->        Borrar (<--) 
		 9          -->        Tabulador
		 13         -->        Enter
		 16         -->        Shift
		 37         -->        Flecha de regreso (<--)
		 39         -->        Flecha de avanzar(-->)
		 45         -->        Insertar
		 46         -->        Suprimir
*/
		(event.keyCode) ? k=event.keyCode : k=event.which;
		elemento = elemento + 2;
		if (event.keyCode == 13 || event.keyCode == 9)
		{
			i=elemento + 5;
			jsRegistros = document.frmDatos.htRegistros.value*3+4;
			if (i<jsRegistros)
				document.frmDatos.elements[i].focus(); //le pongo el focus al siguiente
		}
		else
		{
			if (isNaN(jsValue))
			{
				i = elemento + 2;				
				document.frmDatos.elements[i].value = "";	
			}
		}
		if (jsValue > 0)
			document.frmDatos.elements[elemento].checked = true; //lo selecciono
		else
			document.frmDatos.elements[elemento].checked = false; //le quito la seleccion
}

function Enter(event) 
{
		(event.keyCode) ? k=event.keyCode : k=event.which;
		if (event.keyCode == 13)
		{
			document.frmDatos.submit();
		}
}

function Foco()
{
	document.frmDatos.htBuscar.focus();	
		history.go(1);
}

function validarEmail(valor) 
{
  if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor))
   	return (true)
  else 
  {
  	 alert("La dirección de email " + valor    + " es incorrecta.") 
	 return (false);
  }
}

function Pedido()
{
	jsEmpre = document.frmDatos.htEmpre.value;
	document.frmDatos.action = "ver_carrito.php";
	document.frmDatos.submit();
}
function Buscar()
{
	document.frmDatos.action = "cotizacion.php";
	document.frmDatos.submit();	
}
function Corregir()
{
	document.frmDatos.action = "cotizacion.php";
	document.frmDatos.submit();	
}
function Imprime()
{
	document.getElementById('btnImprime').style.visibility = 'hidden';
	window.print();
	document.getElementById('btnImprime').style.visibility = 'visible';
}

function Impresion(jsFolio,jsTipo)
{
	jsEmpre = document.frmDatos.htEmpre.value;
	window.open('imprime_carrito.php?htEmpre='+jsEmpre+'&htFolio='+jsFolio+'&htTipo='+jsTipo,'Impresion','width=790,height=535,resizable=no,scrollbars=yes');
}
function Impresion2(jsFolio,jsTipo)
{
	jsEmpre = document.frmDatos.htEmpre.value;
	window.open('imprime_carrito2.php?htEmpre='+jsEmpre+'&htFolio='+jsFolio+'&htTipo='+jsTipo,'Impresion','width=732,height=535,resizable=no,scrollbars=yes');
}
function ImpresionCotizacion(jsFolio,jsTipo)
{
	jsEmpre = document.frmDatos.htEmpre.value;
	window.open('imprime_cotizacion.php?htEmpre='+jsEmpre+'&htFolio='+jsFolio+'&htTipo='+jsTipo,'Impresion','width=702,height=535,resizable=no,scrollbars=yes');
}

function ImpresionCotizacion2(jsFolio,jsTipo)
{
	jsEmpre = document.frmDatos.htEmpre.value;
	window.open('imprime_cotizacion2.php?htEmpre='+jsEmpre+'&htFolio='+jsFolio+'&htTipo='+jsTipo,'Impresion','width=732,height=535,resizable=no,scrollbars=yes');
}

function ImprimeCotizacion()
{

	jsFolio = document.frmDatos.htFolio.value;
	jsEmpre = document.frmDatos.htEmpre.value;
	window.open('imprime_cotizacion.php?htFolio=' + jsFolio+'&htEmpre=' + jsEmpre,'Impresion','width=790,height=535,resizable=no,scrollbars=yes');
}

function fImprimePagina(jsPagina)
{
	window.open(jsPagina,'Impresion','width=790,height=535,resizable=no,scrollbars=yes');		
}

function Borra ()
{
	document.frmDatos.action = "borra_pedido.php";
	document.frmDatos.submit();
}

function Registro()
{
	alert ("Su RFC no esta registrado. Por favor registre su RFC para realizar el pedido.");
	document.location.href = "index.php";
}

function AbreVentana(jsArticulo,jsEmpre)
{
	window.open('muestra_imagen_01.php?articulo='+jsArticulo+'&htEmpre='+jsEmpre, 'Articulos','width=800,height=500,scrollbars=no');
}

function Busqueda()
{
	document.frmDatos.action = "muestra_articulos.php";
	document.frmDatos.submit();
}

function Buscar_articulos()
{
	document.frmDatos.action = "muestra_articulos.php";
	document.frmDatos.submit();
}

function Busca_cliente()
{
	document.frmDatos.action = "guarda_correo.php";
	document.frmDatos.submit();
}
function Guarda_cliente()
{
	document.frmDatos.action = "guarda_correo.php";
	document.frmDatos.submit();
}

function Guarda (jsTemporal,jsCantidad)
{
	document.frmDatos.htTemporal.value = jsTemporal * jsCantidad;	
}

function CalculaTotal(jsPrecio, jsI, jsCantidad)
{
	jsNombreTotal = 'htTotal'+jsI; //Nombre del total
	jsNombrePrecio = 'PrecioTotal'+jsI; //Nombre del precio total por producto
	//obtengo el id del total
	elem = document.getElementById('Total');
	iva = document.getElementById('Iva');
	suma = document.getElementById('SumaTotal');
	//obtengo el id del precio total del articulo que se modifico
	jsPrecio = parseFloat(jsPrecio); 
	elemPrecio = document.getElementById(jsNombrePrecio);
	//Obtengo el precio Total
	jsPrecioTotal = parseFloat (document.frmDatos.htTotal.value);
	//Obtengo el precio anterior
	jsTemporal = document.frmDatos.htTemporal.value;
	//Obtengo el nuevo precio del producto
	jsNuevoPrecio = parseFloat(document.getElementById(jsNombreTotal).value)*jsCantidad;
	//Calculo el nuevo total. Al Total que tenia antes del cambio, le resto lo que tenia antes y le sumo el nuevo valor
	jsTotal  = jsPrecioTotal - jsTemporal + jsNuevoPrecio;
	//Guardo el nuevo Total
	document.frmDatos.htTotal.value = jsTotal;
	//Cambio los precios
	jsIva = jsTotal * .15;
	jsSumaTotal = jsTotal + jsIva;
	elem.innerHTML = addCommas(jsTotal.toFixed(2));
	suma.innerHTML = addCommas(jsSumaTotal.toFixed(2));
	iva.innerHTML = addCommas(jsIva.toFixed(2));
	elemPrecio.innerHTML = addCommas(jsNuevoPrecio.toFixed(2));
}

function CalculaPrecioTotal(jsPrecio, jsI, jsCantidad)
{
	jsNombrePrecio = 'PrecioTotal'+jsI; //Nombre del precio total por producto
	//obtengo el id del total
	elem = document.getElementById('Total');
	iva = document.getElementById('Iva');
	suma = document.getElementById('SumaTotal');
	//obtengo el id del precio total del articulo que se modifico
	elemPrecio = document.getElementById(jsNombrePrecio);
	//Obtengo el precio Total
	jsPrecioTotal = parseFloat (document.frmDatos.htTotal.value);
	//Obtengo el precio anterior
	jsTemporal = document.frmDatos.htTemporal.value;
	//Obtengo el nuevo precio del producto
	jsNuevoPrecio = parseFloat(jsPrecio)*jsCantidad;
	//Calculo el nuevo total. Al Total que tenia antes del cambio, le resto lo que tenia antes y le sumo el nuevo valor
	jsTotal  = jsPrecioTotal - jsTemporal + jsNuevoPrecio;
	//Guardo el nuevo Total
	document.frmDatos.htTotal.value = jsTotal;
	//Cambio los precios
	jsIva = jsTotal * .15;
	jsSumaTotal = jsTotal + jsIva;
	elem.innerHTML = addCommas(jsTotal.toFixed(2));
	suma.innerHTML = addCommas(jsSumaTotal.toFixed(2));
	iva.innerHTML = addCommas(jsIva.toFixed(2));
	elemPrecio.innerHTML = addCommas(jsNuevoPrecio.toFixed(2));
}


function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function revisa()
{
	aux = 1;
	if 	(document.frmDatos.htEmail.value == "")
	{
			aux = 0;
	   		alert("Por favor teclee el Correo electrónico" );
			document.frmDatos.htEmail.focus();
	}
	else 
	{
		correcto = validarEmail (document.frmDatos.htEmail.value);
		if (correcto)
		{
			if 	(document.frmDatos.htClave.value == "")
			{
				aux = 0;
				alert("Por favor teclee la clave del cliente" );
				document.frmDatos.htClave.focus();
				return;
			}
			if 	(document.frmDatos.htNombr.value == "")
			{
				aux = 0;
				alert("Por favor teclee el nombre del cliente" );
				document.frmDatos.htNombr.focus();
				return;
			}
			if 	(document.frmDatos.htUsuar.value == "")
			{
				aux = 0;
				alert("Por favor teclee la cuenta del cliente" );
				document.frmDatos.htUsuar.focus();
				return;
			}
			if 	(document.frmDatos.htPassw.value != document.frmDatos.htPassw2.value)
			{
				aux = 0;
				alert("El NIP no coincide" );
				document.frmDatos.htPassw.focus();
				return;
			}
			if 	(document.frmDatos.htPassw.value == '')
			{
				aux = 0;
				alert("Por favor teclee el NIP del cliente");
				document.frmDatos.htPassw.focus();
				return;
			}
			if 	(document.frmDatos.htPassw2.value == '')
			{
				aux = 0;
				alert("Por favor teclee la confirmación de la contraseña del cliente");
				document.frmDatos.htPassw2.focus();
				return;
			}
		}
		else
			aux=0;
		if (aux == 1)
			document.frmDatos.submit();
	}
}

function Valida()
{
	var aux = 0;
	if 	(document.frmDatos.htEmail.value == "")
	{
			aux = 0;
	   		alert("Por favor teclee el Correo electrónico" );
			document.frmDatos.htEmail.focus();
	}
	else 
	{
		correcto = validarEmail (document.frmDatos.htEmail.value);
		if (correcto)
			aux = 1;
		else
			aux=0;
		if (aux == 1)
			document.frmDatos.submit();
	}
}

function Login(jsPagina)
{
	document.frmDatos.action = "inicio_sesion2.php";
	document.frmDatos.submit();
}
function Login2(jsPagina)
{
	document.location.href = jsPagina;
}

function Login5(jsPagina)
{
	document.frmDatos.action = "inicio_sesion5.php";
	document.frmDatos.submit();
}

function fAceptar(jsAccion) 
{
	if (fValidaDatos()) 
	{
		document.frmDatos.htAccion.value = jsAccion;
		document.frmDatos.submit();
	}
}

function fBorrar(jsAccion) 
{
	if (jsAccion==3)
		respuesta=confirm("¿Esta seguro que desea borrar el Registro?");
	else
		respuesta=confirm("¿Esta seguro que desea borrar los Registros Seleccionados?");
	if (respuesta==true)
	{
		document.frmDatos.htAccion.value = jsAccion;
		document.frmDatos.submit();
	}
}

function fValidaDatos() 
{
	var campo = "";
	var errores = "";
	var i=0;
	dml = document.forms[0];
	len = dml.elements.length;
	for(i=0 ; i<len ; i++) 
	{
		dato = document.forms[0].elements[i].title;
		campo = dml.elements[i].id;
		if (dato == "*") 
				if (dml.elements[i].value == "")
					errores += campo + "- Es requerido\n";					
		if (dato == "#") 
					if (!parseFloat(dml.elements[i].value))
				  		errores += campo + "- Debe ser un número\n";
					else
						if(parseFloat(dml.elements[i].value) < 0)
				  			errores += campo + "- No puede ser un número negativo\n";
	}
	if (errores != "") 
	{
		alert('Los siguientes campos tienen error:\n\n'+errores);
		return false;
	}
	return true;
}

function fConsulta(jsTipo,jsAccion,event) 
{
	(event.keyCode) ? k=event.keyCode : k=event.which;
	if (jsTipo == 1) 
	{
		if (event.keyCode == 13) 
		{
			document.frmDatos.htAccion.value = jsAccion;
			document.frmDatos.submit();
		}
	}
	else if (jsTipo == 2) 
	{
		document.frmDatos.htAccion.value = jsAccion;
		document.frmDatos.submit();
	}	
}

function fAbrePagina (jsPag)
{
	document.location.href=jsPag;
}

function fPopup(jsPagina,jsParams)
{
	window.open(jsPagina+'&htPag="'+jsParams+'"','GenteNet','width=790,height=535,resizable=no,scrollbars=yes');
}

function fRefresca()
{
	window.opener.document.frmDatos.htAccion.value=8;
	window.opener.document.frmDatos.submit();
//	window.close();	
}

function fSelecciona(jsCheck,jsValor)
{
	if (jsCheck)
		document.frmDatos.htTemp.value+= jsValor + '|';
	else
	{
		var jsTemp = '';
		var jsTexto = ''
		jsTexto+=document.frmDatos.htTemp.value;
		var jsBusca = jsValor + '|';
		jsTemp = strReplace(jsBusca,"",jsTexto) 
		document.frmDatos.htTemp.value = jsTemp;
	}
}

function fSeleccionaTodos(jsCheck)
{
   for (i=0;i<document.frmDatos.elements.length;i++)
      if(document.frmDatos.elements[i].type == "checkbox")
         if (jsCheck)
		 {
			 document.frmDatos.elements[i].checked=1;
			 var jsTexto = '';
			 jsTexto+=document.frmDatos.elements[i].value + '|';
			 document.frmDatos.htTemp.value+= jsTexto;
		 }
		 else
		  {
			 document.frmDatos.elements[i].checked=0;
			 document.frmDatos.htTemp.value = "";
		  }
}

function strReplace(busca, sust, cadena) 
{     
	return cadena.split(busca).join(sust); 
}

function fReemplazar(texto,textoBuscado,nuevaCadena) 
{  
  while (texto.indexOf(textoBuscado)>-1) 
  {
   pos= texto.indexOf(textoBuscado);
   texto = "" + (texto.substring(0, pos) + nuevaCadena 
     + texto.substring((pos + textoBuscado.length), texto.length));
  }
  return texto;
}
