var actualiza=(window.XMLHttpRequest) ? new XMLHttpRequest() : ((window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : false);

window.onload=iniciar;

function iniciar() {
cambiar(0);
distancia=document.getElementById("cesta").offsetTop;
setInterval("mover()",30);
document.onmousedown=seleccionar;
imagenes=new Array();
for (i=1;i<5;++i) {
	imagenes[i]=new Image();
	imagenes[i].src="images/caja"+i+".gif";
	}
enviar();
}

function enviar() {
actualiza.open("POST","alertae/actualizar.php"); 
actualiza.onreadystatechange=revisar;
actualiza.send(null);
}


function revisar() {
if (actualiza.readyState==4 && actualiza.status==200) setTimeout("enviar()",(actualiza.responseText=="REPETIR") ? 0 : 20000);
}

function cambiar(modo) {
if (!document.getElementById("productos")) return;
productos=document.getElementById("productos").options[document.getElementById("productos").selectedIndex].value;
disabled=(productos==1 || productos==2 || productos==5) ? false : true;
document.getElementById("ordena").disabled=disabled;
document.getElementById("soporte").disabled=disabled;
document.getElementById("resoluciones").disabled=disabled;
document.getElementById("distancias").disabled=disabled;
if (modo) {
	document.getElementById("ordena").options[3]=(productos==5) ? new Option("Ordenar por Brillo","zoom") : new Option("Ordenar por Zoom","zoom");
	document.getElementById("texto").innerHTML=(productos==5) ? "Brillo" : "Zoom";
	seleccionado=0;
	marca=document.getElementById("marca").value;
	descatalogada=(document.getElementById("descatalogada").checked) ? 2 : 0;
	for (i=1;i<=marcas[productos][descatalogada].length;++i) {
		document.getElementById("marca").options[i]=new Option(marcas[productos][descatalogada+1][i-1],marcas[productos][descatalogada][i-1]);
		if (marcas[productos][descatalogada][i-1]==marca) seleccionado=i;
		}
	document.getElementById("marca").options.length=marcas[productos][descatalogada].length+1;
	document.getElementById("marca").selectedIndex=seleccionado;
	if (productos==1 || productos==2 || productos==5) {
		document.getElementById("soporte").selectedIndex=0;
		for (i=1;i<=soportes[productos][descatalogada].length;++i) document.getElementById("soporte").options[i]=new Option(soportes[productos][descatalogada+1][i-1],soportes[productos][descatalogada][i-1]);
		document.getElementById("soporte").options.length=soportes[productos][descatalogada].length+1;
		}

	}
}

function mover() {
movido=(window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop)-document.getElementById("cesta").offsetTop+distancia;
if (movido) {
	modo=((movido>0) ? 1 : -1)*Math.floor(Math.pow(Math.abs(movido),0.45));
	document.getElementById("cesta").style.top=document.getElementById("cesta").offsetTop+modo+"px";
	}
}

function seleccionar(evento) {
base=(evento) ? evento.target : event.srcElement;
if (base.tagName=="H3") {
	base=base.parentNode;
	if (base.id && base.id!="ficha") {
		if (base.className.indexOf("comparada")!=-1) {
			base.className=base.className.substring(0,base.className.indexOf("comparada")-1);
			for (i=0;i<cajas.length;++i) if (cajas[i]==base.id) break;
			cajas.splice(i,1);
			}
		else {
			if (cajas.length>0) {
				tipo=cajas[0].substr(0,1);
				if (base.id.substr(0,1)!=tipo) {
					for (i=0;i<cajas.length;++i) document.getElementById(cajas[i]).className=document.getElementById(cajas[i]).className.substring(0,document.getElementById(cajas[i]).className.indexOf("comparada")-1);
					cajas.length=0;
					}
				}
			if (cajas.length<6) {
				base.className+=" comparada";
				cajas[cajas.length]=base.id;
				}
			}
		document.getElementById("comparar").disabled=(cajas.length<2);
		}
	}
}

function comparativa() {
switch (cajas[0].substr(0,1)) {
	case "C": location="comparaCD.php3?compara="+cajas.join("."); break;
	case "V": location="comparaVCD.php3?compara="+cajas.join("."); break;
	case "P": location="comparaPY.php3?compara="+cajas.join("."); break;
	}	
}

function entrar(nombre,datos) {
if (nombre.indexOf("?")>0) {
	location=nombre.substring(0,nombre.indexOf("?")+1)+"ID="+document.inicio.ID.value+datos+"&"+nombre.substr(nombre.indexOf("?")+1);
	}
else {
	lugar=nombre+"?ID="+document.inicio.ID.value+datos+((nombre.indexOf("comprar.php3")!=-1) ? "&NT="+(new Date()).getTime() : "");
	if (nombre.indexOf("comprar.php3")!=-1 && document.URL.indexOf("comprar.php3")!=-1) location.replace(lugar); else location=lugar;
	}
return false;
}

function avisar() {
document.getElementById("botones").style.visibility="hidden";
document.getElementById("aviso").style.visibility="visible";
document.getElementById("correo").focus();
}

function sustituir(form) {
location.replace("?PD="+form.PD.value+"&correo="+form.correo.value);
return false;
}

function copiar(actual,campo) {
if (document.getElementById("rellena").checked) {
	document.personal[campo].value=actual.value;
	if (campo=="codigo2") {
		document.personal.provincia2.value=document.personal.provincia1.value;
		document.getElementById("texto2").innerHTML=document.getElementById("texto1").innerHTML;
		}
	}
}

function comprobar(formulario,datos) {
valor=false;
for (n=0;n<=datos;n++) {
	switch (formulario.elements[n].type) {
		case "text":
			valor=(formulario.elements[n].value=="");
			break;
		case "textarea":
			valor=(formulario.elements[n].value=="");
			break;
		case "select-one":
			valor=(formulario.elements[n].options[formulario.elements[n].selectedIndex].value=="");
			break;
		default:
			valor=false;
		}
	if (valor) {
		alert ("Introduzca la siguiente Información");
		formulario.elements[n].focus();
		return false;
		}
	}
return true;
}

function actualizar(form) {
var P=new Array(),V=new Array(),U=new Array();
if (form.P.value!=undefined) {
	P[0]=form.P.value;
	V[0]=form.V.value;
	U[0]=form.U.value;
	}
else {
	for (i=0;i<form.P.length;++i) {
		P[i]=form.P[i].value;
		V[i]=form.V[i].value;
		U[i]=form.U[i].value;
		}
	}
P=P.join(".");
V=V.join(".");
U=U.join(".");
location.replace("comprar.php3?P="+P+"&V="+V+"&U="+U);
return false;
}

function rellenar(casilla) {
if (casilla.checked) {
	document.personal.nombre2.value=document.personal.nombre1.value;
	document.personal.nif2.value=document.personal.nif1.value;
	document.personal.domicilio2.value=document.personal.domicilio1.value;
	document.personal.poblacion2.value=document.personal.poblacion1.value;
	document.personal.provincia2.value=document.personal.provincia1.value;
	document.personal.codigo2.value=document.personal.codigo1.value;
	document.getElementById("texto2").innerHTML=document.getElementById("texto1").innerHTML;
	document.personal.telefono2.value=document.personal.telefono1.value;
}
else {
	document.personal.nombre2.value="";
	document.personal.nif2.value="";
	document.personal.domicilio2.value="";
	document.personal.poblacion2.value="";
	document.personal.provincia2.value="";
	document.personal.codigo2.value="";
	document.getElementById("texto2").innerHTML="&nbsp;";
	document.personal.telefono2.value="";
	}
}

function verprovincia(codigo,campo) {
if (codigo.value.length==5 && Number(codigo.value.substring(0,2))>0 && Number(codigo.value.substring(0,2))<53) valor=provincias[Number(codigo.value.substring(0,2))]; else valor="";
document.getElementById("texto"+campo).innerHTML=(valor) ? "- "+valor : "&nbsp;";
document.getElementById("provincia"+campo).value=valor;
}

