// JavaScript Document
function popup(pagina,nombre,ancho,alto,parameters) {
  var w=screen.width;
  var h=screen.height;
  var x=Math.round((w-ancho+1)/2);
  var y=Math.round((h-alto+1)/2);
  var wp ='width='+ancho+',height='+alto+',hotkeys=NO,screenX='+x+',screenY='+y+',left='+x+',top='+y+parameters;
  var pp = window.open(pagina,nombre,wp);
  pp.focus();
}

function Vaciar()
{	document.f.reset();
}

function Guardar()
{	if (Validar())
	{	document.f.accion.value="guardar";
		document.f.submit();
	}
}

function Login()
{	if(Validar())
	{	//var j=Hiddens();
		j=1;
		if (j>0)
		{	document.f.accion.value="enviar";
			document.f.submit();
		}
		else
		{	alert("No se puede realizar un pedido si el carrito de compras está vacío.\nDebe agregar al menos un ítem de nuestras categorías");
		}
	}
}

function Newsletter()
{	var fo;
	fo=document.getElementById("f");
	if (fo)
	{	if (fo.email_address.value=="")	return;
		if (fo.firstname.value=="")	return;
		if (fo.lastname.value=="")	return;
	}
}

function Registrar()
{	document.f.action="crear_cuenta.php";
	document.f.submit();
}

function Buscar()
{	var t;
	t=document.getElementById("texto");
	if (t)
	{	//alert(t.value);
		if (t.value.length>0)
		{	document.f.action="buscar.php";
			document.f.accion.value="buscar";
			document.f.submit();
		}
		else
		{	Estado((location.href.indexOf("english")<0)?"Escriba un texto para búsqueda":"Type a text for search");
			t.focus();
		}
	}
}

function CajaBuscar(e)
{	var tecla;
	if (IE4)
	{	tecla=e.keyCode;
	}
	else
	{	tecla=e.which;
	}
	if (tecla==13)
	{	Buscar();
	}
}

function Estado(mensaje)
{	e=document.getElementById("estado");
	if (e)
	{	e.innerHTML="<font color='#800000'>"+mensaje+"</font>";
		//clearTimeout(BE);
	}
	else
	{	alert(mensaje);
	}
}

function checkForCharacters(inputString, checkString, startingIndex)
{	
  if (!startingIndex) startingIndex = 0;
  return inputString.indexOf(checkString);
}

function Validar()
{	var n	=document.getElementById("nombre");
	var e	=document.getElementById("email");
	var v	=document.getElementById("villa");
	var p0	=document.getElementById("passw");
	var p1	=document.getElementById("passw1");
	var a	=document.getElementById("adminenterado");
	var ces	=document.getElementById("comentarios_es");
	var cen	=document.getElementById("comentarios_en");
	var villa=document.getElementById("cdvillas");
	var te	=document.getElementById("tel");
	var ok	=true;
	numero=4;
	if (location.href.indexOf("english")<0)
	{	if (n) if (n.value.length==0)			{	Estado("Escriba por favor su nombre"); n.focus(); return false}
		if (e) if (e.value.length==0)			{	Estado("Escriba su e-mail por favor") ; e.focus(); return false}
		if (e) if (!isEmailAddress(e)) 		{	e.focus();	return false; }		
		if (te)		if (te.value=="")			{	Estado("Por favor escriba su numero de telefono"); te.focus(); return false; }		
		if (v) if (v.value.length==0)			{	Estado("Escriba la villa donde se le entregará su pedido") ; v.focus(); return false}
		if (p0) if (p0.value.length<numero)	{	Estado("Escriba una contraseña de por lo menos "+numero+" caracteres") ; p0.focus(); return false}
		if (p1) if (p1.value.length<numero)	{	Estado("Vuelva a escribir la contraseña de por lo menos "+numero+" caracteres") ; p1.focus(); return false}
		if (p0 && p1) if (p0.value!=p1.value)	{  	Estado("La contraseña y su confirmación no coinciden. Vuelva a escribir la contraseña"); p0.focus(); return false; }
		if (a)	if (a.value==-1 || a.value=="all" || a.value=="")	{	Estado("Por favor seleccione una opcion de la lista"); a.focus(); return false; }
		if (ces) if (ces.value=="")			{	Estado("Por favor escriba su informacion"); ces.focus(); return false; }
		if (villa)	if (villa.value==-1 || villa.value=="" || villa.value=="all")	{	Estado("Por favor seleccione una villa"); villa.focus(); return false; }
	}
	else
	{	if (n) if (n.value.length==0)			{	Estado("Please enter your name"); n.focus(); return false}
		if (e) if (e.value.length==0)			{	Estado("Please enter your e-mail") ; e.focus(); return false}
		if (e) if (!isEmailAddress(e)) 		{	e.focus();	return false; }
		if (te)		if (te.value=="")			{	Estado("Please type your phone number"); te.focus(); return false; }				
		if (v) if (v.value.length==0)			{	Estado("Please enter the villa where your order would be delivered") ; v.focus(); return false}
		if (p0) if (p0.value.length<numero)	{	Estado("Please type a password with at least "+numero+" characters") ; p0.focus(); return false}
		if (p1) if (p1.value.length<numero)	{	Estado("Please retype the password with at least "+numero+" characters") ; p1.focus(); return false}
		if (p0 && p1) if (p0.value!=p1.value)	{  Estado("Password and confirmation do not match. Please retype password"); p0.focus(); return false; }
		if (a)	if (a.value==-1 || a.value=="all" || a.value=="")	{	Estado("Please choose an option from list"); a.focus(); return false; }
		if (cen) if (cen.value=="")			{	Estado("Please type your info"); cen.focus(); return false; }
		if (villa)	if (villa.value==-1 || villa.value=="" || villa.value=="all")	{	Estado("Please select a villa"); villa.focus(); return false; }
	}
	return ok;
}

function ReservarVilla()
{	if (Validar())
	{	var cdv;
		cdv=document.getElementById("cdvillas");
		if (cdv)
		{	document.f.action="acapulco_catering_contacto.php?cdvillas="+cdv.value;
			document.f.submit();
		}
	}
}

function isEmailAddress(theElement, nombre_del_elemento )
{
var s = theElement.value;
var filter=/^[A-Za-z][A-Za-z0-9_]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
if (s.length == 0 ) return true;
if (filter.test(s))
return true;
else
	Estado((location.href.indexOf("english")<0)?"La direccion de correo no parece ser correcta. Ingrese una direccion valida":"E-mail address appears not valid. Type a correct e-mail address");
theElement.focus();
return false;
}

function Rotar()
{	var f;
	f=document.getElementById('foto');
	if (f)
	{	f.src=fotofoto[i];
		f.title=fotonotas[i];
		(i>=n)?i=1:i++;
	}
	//Ro=setTimeout('Rotar()',15000); 
}

var Ro;

function test()
		{	//analizza se il browser e' compatibile con Javascript
			browserName=navigator.appName.charAt(0);
			browserVer=parseInt(navigator.appVersion);
			if (browserName=="M" && browserVer >= 3) abilitato=1;
			if (browserName=="N" && browserVer >= 3) abilitato=2;
			if (browserName=="N" && browserVer >= 4) abilitato=3;
			if (browserName=="M" && browserVer >= 4) abilitato=4;
		}

		function keyPress(e) 
		{	if (abilitato==3) 
			{	//var ns4=1;
				var nKey=e.which; var ieKey=0;
				var realkey = String.fromCharCode(e.which);
				strin="Codice tecla in Netscape: "+nKey+". Premuto tecla "+realkey
				if (nKey==13)	
				{	Buscar();
				}
			}
			if (abilitato==4) 
			{	//var ns4=0;
				var ieKey=event.keyCode; var nKey=0;
				var realkey = String.fromCharCode(event.keyCode);
				strin="Codice tecla in Explorer: " + ieKey+". Premuto tecla "+realkey
				if (ieKey==13) 
				{	Buscar();
				}
			}
		}
		
		var t, abilitato=0;
		t=document.getElementById("texto");
		if (t)	
		{	//alert("Enlazando evento");
			t.onkeypress = keyPress;
		}
		test();
		
function Pagina(cual)
{	var p,q;
	i=1;
	// Mostrar la página correspondiente
	while (p=document.getElementById("Pagina"+i))
	{	q=document.getElementById("SPagina"+i);
		if (cual!='all')
		{	p.style.display="none";
		}
		else
		{	p.style.display="";
		}
		if (q)
		{	q.style.display=p.style.display;
		}
		i++;
	}
	if (cual!="all")
	{	p=document.getElementById("Pagina"+cual);
		q=document.getElementById("SPagina"+cual);
		if (p)	p.style.display="";
		if (q)
		{	q.style.display=p.style.display;
		}
	}
}

function Idioma(cual)
{	var n, p, l, d;
	n=checkForCharacters(location.href,"cateringdiamante");
	p=location.pathname;
	l=location.host;
	d=location.href.split("?");
	if (n>-1)
	{	p=p.split("/");
		switch (cual)
		{	case "es":
				location.href="http://"+l+""+p[0]+"/"+p[2]+(d.length>1?"?"+d[1]:"");
				break;
			case "en":
				location.href="http://"+l+""+p[0]+"/english/"+p[1]+(d.length>1?"?"+d[1]:"");
				break;
		}
	}
	else
	{	p=p.split("/");
		switch (cual)
		{	case "es":
				location.href="http://"+l+"/"+p[1]+"/"+p[3]+(d.length>1?"?"+d[1]:"");
				break;
			case "en":
				location.href="http://"+l+"/"+p[1]+"/english/"+p[2]+(d.length>1?"?"+d[1]:"");
				break;
		}
	}
}

function checkForCharacters(inputString, checkString, startingIndex)
{
  if (!startingIndex) startingIndex = 0;
  return inputString.indexOf(checkString);
}

function NuevoPass()
{	var n,e,a,fo;
	if (Validar())
	{	n=document.getElementById("nombre");
		e=document.getElementById("email");	
		a=document.getElementById("accion");
		fo=document.getElementById("f");
		if (n.value && e.value)
		{	a.value="enviar";
			fo.submit();
		}
	}
}

var IE4 = document.all;
var NS4 = document.layers;
var NS6 = document.getElementById && !document.all; 
