// JavaScript Document

function login()
{	
	if(document.logon.usuario.value == ''){
		alert('Debe ingresar su nombre de usuario obligatoriamente');
		document.logon.usuario.focus();
		return;
	}
	
	if(document.logon.password.value == ''){
		alert('Debe ingresar su contraseña obligatoriamente');
		document.logon.password.focus();
		return;
	}	
	
	document.logon.submit();	
}

function recCntrsn(mail)
{
	var recCntrsn = document.createElement('script');
	recCntrsn.setAttribute("language", "javascript");
	recCntrsn.setAttribute("type", "text/javascript");
	recCntrsn.setAttribute("src","frgtpsswrd.php?mail="+escape(mail));
	document.body.appendChild(recCntrsn);
}

function buscarArticulos()
{
	var codigo=document.getElementById('buscar').value;	
	listarArticulos('', 'b', codigo);
}
function  listarArticulos(src, tipo, codigo)
{		
	/*	Parametros:
		Src: nombre de pagina completa a colocar.
		Tipo:			
			a: articulo, Listo Articulos de una Familia
			f: listo familias de rubro.
			b: buqueda, Listo Articulo que coincidan con la busqueda			
		Codigo: Codigo del Tipo
	*/	

	if (src != '') {
			document.getElementById('listaArticulos').style.overflow = 'visible';//hidden,visible
			document.getElementById('listaArticulos').innerHTML = '<iframe src="'+src+'" name="contenido" width="532" marginwidth="0" height="550" marginheight="0" align="middle" scrolling="yes" frameborder="0"></iframe>';			
	}else{

		document.getElementById('listaArticulos').style.overflow = 'auto';
		document.getElementById('listaArticulos').innerHTML = '<br><br><br><br><br><br><br><br><center><b>...CARGANDO...</b></center>';

		var lstrrtcls = document.createElement('script');
		lstrrtcls.setAttribute("language", "javascript");
		lstrrtcls.setAttribute("type", "text/javascript");
		
		if (tipo=='f'){	//Listo Familias de un rubro		
			lstrrtcls.setAttribute("src","listarFamilias.php?tipo="+tipo+
												 													"&codigo="+codigo);
		}else{	//Listo Articulos de una familia o busqueda
			lstrrtcls.setAttribute("src","listarArticulos.php?tipo="+tipo+
												 													"&codigo="+codigo);
		}			
		document.body.appendChild(lstrrtcls);
	}	
}

function SubirScroll()
{	
	document.getElementById('listaArticulos').scrollTop=document.getElementById('listaArticulos').scrollTop+200;
}

function BajarScroll()
{	
	document.getElementById('listaArticulos').scrollTop=document.getElementById('listaArticulos').scrollTop-200;
}

function sumar(nombre)
{	
	document.getElementById(nombre).value = parseInt(document.getElementById(nombre).value)+1;
}

function restar(nombre)
{
	document.getElementById(nombre).value = parseInt(document.getElementById(nombre).value)-1;
	if(parseInt(document.getElementById(nombre).value) <= 0)
	{
		document.getElementById(nombre).value = '0';
	}
}

function comprarArticulo(articulo, cantidad, cantidadC)
{
	var cmprrrtcl = document.createElement('script');
	cmprrrtcl.setAttribute("language", "javascript");
	cmprrrtcl.setAttribute("type", "text/javascript");
	cmprrrtcl.setAttribute("src","frmTicket.php?articulo="+articulo+"&cantidad="+cantidad+"&cantidadC="+cantidadC);
	document.body.appendChild(cmprrrtcl);
}

function vaciar()
{
	var vcr = document.createElement('script');
	vcr.setAttribute("language", "javascript");
	vcr.setAttribute("type", "text/javascript");
	vcr.setAttribute("src","frmTicket.php?articulo=none&cantidad=0");
	document.body.appendChild(vcr);
}

function sumarTicket(nombre)
{	
/*
	alert(nombre);
	alert(document.getElementById(nombre).value);
*/

	document.getElementById(nombre).value = parseInt(document.getElementById(nombre).value)+1;
	var smrTckt = document.createElement('script');
	smrTckt.setAttribute("language", "javascript");
	smrTckt.setAttribute("type", "text/javascript");
	smrTckt.setAttribute("src","frmTicket.php?articulo="+nombre+"&cantidad="+document.getElementById(nombre).value);
	document.body.appendChild(smrTckt);
}

function restarTicket(nombre)
{
	document.getElementById(nombre).value = parseInt(document.getElementById(nombre).value)-1;
	if(parseInt(document.getElementById(nombre).value) <= 0)
	{
		document.getElementById(nombre).value = '0';
	}
	var rstrTckt = document.createElement('script');
	rstrTckt.setAttribute("language", "javascript");
	rstrTckt.setAttribute("type", "text/javascript");
	rstrTckt.setAttribute("src","frmTicket.php?articulo="+nombre+"&cantidad="+document.getElementById(nombre).value);
	document.body.appendChild(rstrTckt);
}

function detalle(articulo)
{
	var rtcl = document.createElement('script');
	rtcl.setAttribute('language', 'javascript');
	rtcl.setAttribute('type', 'text/javascript');
	rtcl.setAttribute('src','detalleArticulo.php?articulo='+articulo);
	document.body.appendChild(rtcl);
}


function noimage(o){
	o.src='../imagenes/producto_sin_imagen.jpg'; 
}	

function resize(img){
	if(img.width>70){
		img.height = parseInt(img.height * 70 / img.width);
		img.width = 70;
	}
}

function solicitarPedido(TipoMoneda)
{
	var slctrPdd = document.createElement('script');
	slctrPdd.setAttribute("language", "javascript");
	slctrPdd.setAttribute("type", "text/javascript");
	slctrPdd.setAttribute("src","solicitarPedido.php?TipoMoneda="+TipoMoneda);
	document.body.appendChild(slctrPdd);
}

function solicitarPedidoTipoMoneda()
{
	opciones="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,width=300,height=180 top=300,left=320";
	window.open('solicitarPedidoTipoMoneda.php', 'src',opciones);
	
	/*var slctrPdd = document.createElement('script');
	slctrPdd.setAttribute("language", "javascript");
	slctrPdd.setAttribute("type", "text/javascript");
	slctrPdd.setAttribute("src","solicitarPedido.php");
	document.body.appendChild(slctrPdd);*/
}


function envioPedido(pedido,TipoMoneda)
{
	var nvPdd = document.createElement('script');
	nvPdd.setAttribute("language", "javascript");
	nvPdd.setAttribute("type", "text/javascript");
	nvPdd.setAttribute("src","envioPedido.php?pedido="+pedido+"&TipoMoneda="+TipoMoneda);
	document.body.appendChild(nvPdd);
}

function cargaPedidos(cliente)
{
	if(cliente != "")
	{
		var crgPdds = document.createElement('script');
		crgPdds.setAttribute('language', 'javascript');
		crgPdds.setAttribute('type', 'text/javascript');
		crgPdds.setAttribute('src','cargarPedidos.php?cliente='+cliente);
		document.body.appendChild(crgPdds);
	}
	else
	{
		document.getElementById('listaArticulos').innerHTML = "";
		alert('No existen pedidos disponibles');
	}
}

function zoomPedido(pedido)
{
	var zmPdd = document.createElement('script');
	zmPdd.setAttribute("language", "javascript");
	zmPdd.setAttribute("type", "text/javascript");
	zmPdd.setAttribute("src","zoomPedido.php?pedido="+pedido);
	document.body.appendChild(zmPdd);
}

function cambiarEstado(pedido, ant, valor)
{
	ok = false;
	if(valor != '')
		{
		if(valor == "N" && ant != "Finalizado" && ant != "Anulado" )
		{
			ok = true;
		}
		if(valor == 'P' && ant == "En preparación")
		{
			ok = true;
		}
		if(valor == 'E' && (ant == "Pendiente" || ant == "Armado"))
		{
			ok = true;
		}
		if(valor == 'A' && (ant == "En preparación" || ant == "En reparto"))
		{
			ok = true;
		}
		if(valor == 'R' && ant == "Armado")
		{
			ok = true;
		}
		if(valor == 'F' && ant == "En reparto")
		{
			ok = true;
		}
		if(ok)
		{
			var cmbrstd = document.createElement('script');
			cmbrstd.setAttribute("language", "javascript");
			cmbrstd.setAttribute("type", "text/javascript");
			cmbrstd.setAttribute("src","cambiarEstado.php?pedido="+pedido+"&estado="+valor);
			document.body.appendChild(cmbrstd);
		}
		else
		{
			alert("No puede pasar el estado actual de este Pedido al estado seleccionado");
		}
	}
}

function abmClientes(cliente)
{
	//	document.getElementById('tituloRubro').innerHTML = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>'+"MIS DATOS PERSONALES</b>";
	var bmClnts = document.createElement('script');
	bmClnts.setAttribute("language", "javascript");
	bmClnts.setAttribute("type", "text/javascript");
	bmClnts.setAttribute("src","abmClientes.php?cliente="+cliente);
	document.body.appendChild(bmClnts);
}

function modificarCliente(nombre)
{
	var dia=document.frmClientes.FecDia.value;
	var mes=document.frmClientes.FecMes.options[document.frmClientes.FecMes.selectedIndex].value;
	var ano=document.frmClientes.FecAno.value;
/*
	if (validarFecha(dia,mes,ano)){
		alert(' Fecha de Nacimiento Invalida. ');	
	}else{		
		document.frmClientes.submit();
	}
*/

	document.frmClientes.submit();
}

function VerificaAcceso(id)
{
	if(document.MP.ModoPermiso.checked)
	{
		window.open('popPermisos.php?objeto='+id, 'permisos', "width=230, height=320, rezisable=NO, location=NO, menubar=NO, toolbar=NO, titlebar=NO, status=NO");
		return;
	}
	else
	{
		Vrfcms = document.createElement('script');
		Vrfcms.setAttribute("language", "javascript");
		Vrfcms.setAttribute("type", "text/javascript");
		Vrfcms.setAttribute("src","../admin/verifUsuario.php?objeto="+id);
		//alert(Vrfcms.src);
		document.body.appendChild(Vrfcms);
	}
}

function onClickAltaUsuario()
{
	ok = true;
	if(document.abmUsuarios.usuario.value == '')
	{
		ok = false;
	}
	if(document.abmUsuarios.password1.value == '')
	{
		ok = false;
	}
	if(document.abmUsuarios.password2.value == '')
	{
		ok = false;
	}
	if(document.abmUsuarios.password1.value != document.abmUsuarios.password2.value)
	{
		ok = false;
	}
	if(ok)
	{
		var ltsr = document.createElement('script');
		ltsr.setAttribute("language", "javascript");
		ltsr.setAttribute("type", "text/javascript");
		var nombre = escape(document.abmUsuarios.usuario.value);
		var clave = escape(document.abmUsuarios.password1.value);
		for(x=0; x<document.abmUsuarios.estPermisos.length; x++)
		{
			if(document.abmUsuarios.estPermisos[x].checked)
			{
				var estPermiso = document.abmUsuarios.estPermisos[x].value;
			}
		}
		if(modo == 'alta')
		{
			ltsr.setAttribute("src","../admin/altaUsuario.php?nombre="+nombre+"&clave="+clave+"&ep="+estPermiso);
		}
		if(modo == 'edicion')
		{
			ltsr.setAttribute("src","../admin/updateUsuario.php?nombre="+nombre+"&clave="+clave+"&ep="+estPermiso+"&id="+idUsr);
		}
		document.body.appendChild(ltsr);
	}
	else
	{
		alert('Error\nAlguno o todos los datos están en blanco o\nlas contraseñas no coinciden');
		return;
	}
}

function editUsuario()
{
	var radio;
	var tabla = document.getElementById('tblUsuarios');
	for(x=0; x < document.abmUsuarios.elements.length; x++)
	{
		radio = document.abmUsuarios.elements[x];
		if (radio.type == 'radio' && radio.checked)
		{
			for(y=1; y < tabla.rows.length; y++)
			{
				if(tabla.rows[y].id == radio.id)
				{
					document.abmUsuarios.usuario.value = tabla.rows[y].cells[2].innerHTML;
					document.abmUsuarios.password1.value = tabla.rows[y].cells[4].innerHTML;
					document.abmUsuarios.password2.value = tabla.rows[y].cells[4].innerHTML;
					idUsr = radio.id;
				}
			}
		}
	}
}

function delUsuario()
{
	var radio;
	var tabla = document.getElementById('tblUsuarios');
	for(x=0; x < document.abmUsuarios.elements.length; x++)
	{
		radio = document.abmUsuarios.elements[x];
		if (radio.type == 'radio' && radio.checked)
		{
			for(y=1; y < tabla.rows.length; y++)
			{
				if(tabla.rows[y].id == radio.id)
				{
					document.abmUsuarios.usuario.value = tabla.rows[y].cells[2].innerHTML;
					document.abmUsuarios.password1.value = tabla.rows[y].cells[4].innerHTML;
					document.abmUsuarios.password2.value = tabla.rows[y].cells[4].innerHTML;
					idUsr = radio.id;
					if(confirm('Desea dar de baja a '+tabla.rows[y].cells[2].innerHTML))
					{
						var dlsr = document.createElement('script');
						dlsr.setAttribute("language", "javascript");
						dlsr.setAttribute("type", "text/javascript");
						dlsr.setAttribute("src","../admin/deleteUsuario.php?idUsr="+idUsr);
						document.body.appendChild(dlsr);
					}
				}
			}
		}
	}
}

function redirect(pagina)
{
	window.location = pagina;
}

function toggle(id)
{
	try {
		if(document.getElementById(id).style.display == 'block'){
			document.getElementById(id).style.display = 'none';
		}else{
			document.getElementById(id).style.display = 'block';
		}
	}
	catch(e){
	}
}

function cargar(pagina)
{
	var crgr = document.createElement('script');
	crgr.setAttribute("language", "javascript");
	crgr.setAttribute("type", "text/javascript");
	crgr.setAttribute("src","../admin/"+pagina);
	document.body.appendChild(crgr);
}

//***********************************************************************************************
// validarFecha(dia,mes, año)
//
// Valida que el día y el mes introducidos sean correctos. Además valida que el año introducido
// sea o no bisiesto
//
//***********************************************************************************************
function validarFecha(dia,mes,anio)
{
	var elMes = parseInt(mes);

	if(parseInt(dia) > 31 || parseInt(dia) < 1){
		return 1;
	}
	if(parseInt(mes) > 12 || parseInt(mes) < 1){
		return 1;
	}
	if(parseInt(anio) > 2999 || parseInt(anio) < 1900){
		return 1;
	}
	
	// MES FEBRERO
	if(elMes == 2){
		if(esBisiesto(anio)){
			if(parseInt(dia) > 29){
				return 1;
			}
			else
				return 0;
		}
		else{
			if(parseInt(dia) > 28){
				return 1;
			}
			else
				return 0;
		}
	}
	//RESTO DE MESES
	
	if(elMes== 4 || elMes==6 || elMes==9 || elMes==11){
		if(parseInt(dia) > 30){
			return 1;
		}
	}
	
	return 0;
}
//*****************************************************************************************
// esBisiesto(anio)
//
// Determina si el año pasado com parámetro es o no bisiesto
//*****************************************************************************************
function esBisiesto(anio)
{
	var BISIESTO;
	if(parseInt(anio)%4==0){
		if(parseInt(anio)%100==0){
			if(parseInt(anio)%400==0){
				BISIESTO=true;
			}
			else{
				BISIESTO=false;
			}
		}
		else{
			BISIESTO=true;
		}
	}
	else
		BISIESTO=false;

	return BISIESTO;
}
