function WindowOpenCentrado(url, name, param,w, h, perc) 
{ 	var winX = (screen.availWidth - w)*perc*.01;
	var winY = (screen.availHeight - h)*perc*.01;
	popupWin = window.open(url, name, 'width=' + w + ',height=' + h +',left=' + winX + ',top=' + winY + ','+param);
}

function VerImagen(Foto)
{
	if (navigator.appName=="Microsoft Internet Explorer") 
	{	WindowOpenCentrado('Grande.asp?I='+Foto,'Imagen','fullscreen=no,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no', 650, 70, 35);			}
	else //FireFox
	{	window.open(Foto,Foto,'width=620,height=420');}
}	

function AbrirVentana()
{	WindowOpenCentrado("loading.asp", 'animacion','resizable=no,statusbar=no,scrollbars=no', 500, 500, 50);	}

function Tecla(Campo,funcion)
{	if (self.event.keyCode == 13 )
	{	self.event.keyCode = 9;
		if (self.event.srcElement.name == Campo)
		{	if (funcion=='ValidarCliente')
			{	ValidarCliente();	}
			if (funcion=='EnviarFormularioBuscadorAlbaranes')
			{	EnviarFormularioBuscadorAlbaranes();	}
			if (funcion=='EnviarFormularioBuscadorPedidos')
			{	EnviarFormularioBuscadorPedidos();	}
			if (funcion=='EnviarFormularioBuscadorPedidos2')
			{	EnviarFormularioBuscadorPedidos2();	}
			
			
		}
	}	
}

function EnviarFormularioBuscadorPedidos()
{	document.FormularioBuscadorPedidos.submit();
}	

function EnviarFormularioBuscadorPedidos2()
{		document.FormularioBuscadorPedidos2.submit();
}	


function EnviarFormularioBuscadorAlbaranes()
{	document.FormularioBuscadorAlbaranes.submit();
}	


function EnviarComprar2(ContDE)
{	if ((document.getElementById('Dia').value=='') || (document.getElementById('Mes').value=='') || (document.getElementById('Any').value==''))
	{	alert("Debe informar la Fecha de Carga") ;
		document.getElementById('SuPedido').focus();
		return false;
	}
	
	else
	{	var hoy = new Date();
		var sFecha = new Date(document.getElementById('Any').value +','+ document.getElementById('Mes').value+'/'+document.getElementById('Dia').value);
		
		difDias= Math.floor((sFecha.getTime()-hoy.getTime())/(1000*60*60*24));
		
		if (difDias<0)
		{	alert("La Fecha de Entrega debe ser superior a hoy.") ;
			document.getElementById('Dia').focus();
			return false;
		}	
	}
		
		
	if (FormularioComprar2.DomicilioEnvio[ContDE].checked)	
	{	if (document.getElementById('RazonSocialOD').value=='')
		{	alert("Informe la Razon social");
			document.getElementById('RazonSocialOD').focus();
			return false;
		}
		if (document.getElementById('PaisOD').value=='')
		{	alert("Informe el país");
			document.getElementById('PaisOD').focus();
			return false;
		}
		if (document.getElementById('DireccionOD').value=='')
		{	alert("Informe la dirección");
			document.getElementById('DireccionOD').focus();
			return false;
		}
		if (document.getElementById('CPOD').value=='')
		{	alert("Informe el Código postal");
			document.getElementById('CPOD').focus();
			return false;
		}
		if (document.getElementById('PoblacionOD').value=='')
		{	alert("Informe la población");
			document.getElementById('PoblacionOD').focus();
			return false;
		}
		if (document.getElementById('TelefonoOD').value=='')
		{	alert("Informe el teléfono");
			document.getElementById('TelefonoOD').focus();
			return false;
		}
		if (document.getElementById('FaxOD').value=='')
		{	alert("Informe el fax");
			document.getElementById('FaxOD').focus();
			return false;
		}
	}
	
		
	if (!(document.getElementById('Condiciones').checked))
	{	alert("Debe aceptar las condiciones de venta.");
		document.getElementById('Condiciones').focus();
		return false;
	}
	document.FormularioComprar2.submit();
}

function ValidarCliente()
{	if (document.getElementById('Usuario').value=='')
	{	alert("Informe el usuario");
		document.getElementById('Usuario').focus();
		return false;
	}
	if (document.getElementById('Contrasena').value=='')
	{	alert("Informe la contraseña");
		document.getElementById('Contrasena').focus();
		return false;
	}	
	document.FormularioCliente.submit();	
}

function EnviarFormularioFamilias()
{	document.FormularioFamilias.action="resultados.asp";
	document.FormularioFamilias.submit();
}

function RevisarCantidad(obj,Enviar,BotellasCaja)
{	if (obj.value!="")
	{	var number = parseFloat(obj.value);
		if (isNaN(number)==true)
		{	alert("Sólo valores numéricos ..."); 
			obj.focus();
			obj.select();
			return false;
		}
		else
		{	if(number==0)
			{	alert("Informar un valor mayor de 0."); 
				obj.focus();
				obj.select();
				return false;
			}
			else
			{	if((number % BotellasCaja)!=0)
				{	alert("Sólo múltiples de "+BotellasCaja+"."); 
					obj.focus();
					obj.select();
					return false;
				}
			}
		}
	}
	else
	{	alert("Informe el nº de botellas ..."); 
		obj.focus();
		obj.select();
		return false;
	}
	
	if (Enviar=="Si")
	{	document.getElementById('froculta').src='AddCesta.asp?IdLibro='+document.getElementById('CA').value+'&Unidades='+document.getElementById('NumBotellas').value;	}
	
	if (!(isNaN(parseFloat(Enviar))))
	{	document.getElementById('froculta').src='ModificarCesta.asp?IdLibro='+document.getElementById('CA'+Enviar).value+'&UdsNuevas='+document.getElementById('NumBotellas'+Enviar).value+'&Uds='+document.getElementById('Unidad'+Enviar).value }
	
	if (Enviar=="Calcular")
	{	if (document.getElementById('UnidadesRegalo').value=="0") //no hay regalo
		{	document.getElementById('NBotellas').value=document.getElementById('NumBotellas').value;
			document.getElementById('NCajas').value=document.getElementById('NumBotellas').value/BotellasCaja;
			document.getElementById('PrecioTotal').value=parseFloat(document.getElementById('NumBotellas').value)*document.getElementById('Precio').value;	
			document.getElementById('PrecioTotal').value = redondear(document.getElementById('PrecioTotal').value, 2);
		}
		else
		{	document.getElementById('NBotellasRegalo').value=(document.getElementById('NumBotellas').value*document.getElementById('UnidadesRegalo').value) / (parseInt(document.getElementById('UnidadesMinimasRegalo').value)+parseInt(document.getElementById('UnidadesRegalo').value)); //Regla de 3 si de 12(11+1)(UnidadesMinimasRegalo+UnidadesRegalo) unidades regalan 1(UnidadesRegalo) de 120 (lo q infoma el usuario) pues x.
			document.getElementById('NBotellas').value=document.getElementById('NumBotellas').value-document.getElementById('NBotellasRegalo').value;
			document.getElementById('NCajas').value=document.getElementById('NumBotellas').value/BotellasCaja;
			document.getElementById('PrecioTotal').value=parseFloat(document.getElementById('NBotellas').value)*document.getElementById('Precio').value;	
			document.getElementById('PrecioTotal').value = redondear(document.getElementById('PrecioTotal').value, 2);
		}	
				
		NumeroDePalets = parseFloat(document.getElementById('NumBotellas').value/BotellasCaja)/document.getElementById('CajasPalet').value
		//alert(NumeroDePalets);

		Decimal = String(NumeroDePalets).indexOf(".");
			
		if (Decimal == -1) //Si no es decimal 
		{	document.getElementById('NPalets').value = NumeroDePalets ;	}
		else	//Si es decimal me da 1.2 o 1.6 palets pongo q son 2
		{	document.getElementById('NPalets').value = parseInt(NumeroDePalets+1);		
			document.getElementById('PaletNoCompleto').innerHTML = "Palet NO completo";
		}
		
		document.getElementById('PesoPalet').value = parseInt((document.getElementById('PesoCaja').value.replace(',','.') * document.getElementById('NCajas').value)+25);
	}
}

function redondear(cantidad, decimales) 
{	
	var cantidad = parseFloat(cantidad);
	var decimales = parseFloat(decimales);
	decimales = (!decimales ? 2 : decimales);
	return Math.round(cantidad * Math.pow(10, decimales)) / Math.pow(10, decimales);
}    



var all;
var period;

function time(){
	var currentDate=new Date()
	
	hour=currentDate.getHours()
	min=currentDate.getMinutes()
	sec=currentDate.getSeconds()
	date=currentDate.getDate()
	
	if(sec<10){sec="0"+sec}
	if(min<10){min="0"+min}
	
	/*if(hour>12){hour-=12;period="pm"} else {period=" a.m."}
	if(currentDate.getHours()==12){period=" p.m."}
	if(currentDate.getHours()==24){period=" a.m."}*/
	
	var all=hour+":"+min+":"+sec//+period	
	//window.status=all	
	document.all("Hora").innerText = all
	window.setTimeout("time()",300)
}

function RevisarCantidad2(objeto)
{	var valorCampo; 
	var evento_key = window.event.keyCode; 
	
	switch (evento_key) 
	{ 	case 48: 
		case 49: 
		case 50: 
		case 51: 
		case 52: 
		case 53: 
		case 54: 
		case 55: 
		case 56: 
		case 57: 
		case 45: 
		case 47: 
		break; 
		default: 
		window.event.keyCode = 0; 
		return false; 
	} 
	return true; 

}


function SoloNumeros(objeto)
{	var evento_key = window.event.keyCode; 
	
	switch (evento_key) 
	{ 	case 48: 
		case 49: 
		case 50: 
		case 51: 
		case 52: 
		case 53: 
		case 54: 
		case 55: 
		case 56: 
		case 57: 
		case 45: 
		case 47: 
		break; 
		default: 
		window.event.keyCode = 0; 
		return false; 
	} 
	return true; 

}
