var anunciosEnComparacion = 0;
var comparaciones = new Array();

function incluyeComparacion(caja, idAnuncio, modelo, precio, fecha, foto) {

	if (caja.checked) {

		if (anunciosEnComparacion == 3) {
			caja.checked = false;
			alert("El número máximo de comparaciones es de 3 vehículos");
			return;
		}

		sw = false;
		for (j = 1; j < 3; j++) {
			if (comparaciones[j] == idAnuncio)
				sw = true;
		}
		if (sw) {
			return;
		}

		anunciosEnComparacion += 1;
		comparaciones[anunciosEnComparacion] = idAnuncio;

		document.getElementById("modeloCoche" + anunciosEnComparacion).innerHTML = modelo;
		document.getElementById("precioCoche" + anunciosEnComparacion).innerHTML = precio;
		document.getElementById("fechaCoche" + anunciosEnComparacion).innerHTML = fecha;
		document.images["fotoCompara" + anunciosEnComparacion].src = foto;

		document.getElementById("vehiculoCompara" + anunciosEnComparacion).style.display = "block";
		
		if (anunciosEnComparacion > 1)
			document.getElementById("botonCompara").style.display = "block";
		} else {
			i = 1;
			while ((comparaciones[i] != idAnuncio) && (i < 4)) i++;
			if (i == anunciosEnComparacion) {
				document.getElementById("vehiculoCompara" + i).style.display = "none";
			} else {
				for (j = i; j < 3; j++) {
					document.getElementById("modeloCoche" + j).innerHTML = document.getElementById("modeloCoche" + (j + 1)).innerHTML;
					document.getElementById("precioCoche" + j).innerHTML = document.getElementById("precioCoche" + (j + 1)).innerHTML;
					document.getElementById("fechaCoche" + j).innerHTML = document.getElementById("fechaCoche" + (j + 1)).innerHTML;
					document.images["fotoCompara" + j].src = document.images["fotoCompara" + (j + 1)].src;
					comparaciones[j] = comparaciones[j + 1];
				}
				document.getElementById("vehiculoCompara" + anunciosEnComparacion).style.display = "none";
				comparaciones[anunciosEnComparacion] = "";
			}
			anunciosEnComparacion -= 1;
			if (anunciosEnComparacion == 1)
				document.getElementById("botonCompara").style.display = "none";
	}
}

function quita(posAnuncio) {

	if (document.getElementById("chk" + comparaciones[posAnuncio]) != null) {
		document.getElementById("chk" + comparaciones[posAnuncio]).checked = false;
	}

	if (posAnuncio == anunciosEnComparacion) {
		document.getElementById("vehiculoCompara" + posAnuncio).style.display = "none";
	} else {
		for (j = posAnuncio; j < 3; j++) {
			document.getElementById("modeloCoche" + j).innerHTML = document.getElementById("modeloCoche" + (j + 1)).innerHTML;
			document.getElementById("precioCoche" + j).innerHTML = document.getElementById("precioCoche" + (j + 1)).innerHTML;
			document.getElementById("fechaCoche" + j).innerHTML = document.getElementById("fechaCoche" + (j + 1)).innerHTML;
			document.images["fotoCompara" + j].src = document.images["fotoCompara" + (j + 1)].src;
			comparaciones[j] = comparaciones[j + 1];
		}
		document.getElementById("vehiculoCompara" + anunciosEnComparacion).style.display = "none";
	}
	comparaciones[anunciosEnComparacion] = "";
	anunciosEnComparacion -= 1;
	if (anunciosEnComparacion == 1)
		document.getElementById("botonCompara").style.display = "none";
}

function compara() {

	document.fcomparativa.coche1.value = comparaciones[1];
	document.fcomparativa.coche2.value = comparaciones[2];
	if (comparaciones[3]) document.fcomparativa.coche3.value = comparaciones[3];
	document.fcomparativa.submit();
	
}
