
function volver(){
	document.forms.catalogo.id.value = '';
	document.forms.catalogo.action = "ofertas.asp";
	document.forms.catalogo.submit();
}

function calcularPrecio(){
	total = 0;
	var i,j;
	for (i=0;i<grupos.length;i++){
		if ((grupos[i].length==1)&&(eval('document.forms.producto.grupo'+i)==undefined)) total = total + precios[grupos[i][0]];
		else {
			for (j=0;j<grupos[i].length;j++){
				if (eval('document.forms.producto.grupo'+i+'['+j+'].checked')){
					total = total + precios[grupos[i][j]];
					j = grupos[i].length;
				}
			}
		}
	}
	total = document.forms.producto.unidades.value * total;
	document.forms.producto.precioTotal.value=toEuros(total,',')+' € ';
}

function toEuros (centsAmount,separator) {
			eurosAmount = "000" + centsAmount;
			centsAmount = eurosAmount.substr(eurosAmount.length-2,eurosAmount.length);
			eurosAmount = eurosAmount.substr(0,eurosAmount.length -2 );
			return( formatInteger(parseInt(eurosAmount,10),'.')+separator+centsAmount);
}

function formatInteger (number,centSeparator) {
	var stringNumber;
	stringNumber = ""+number;
	if (stringNumber.length>3) {
		return(formatInteger(stringNumber.substr(0,stringNumber.length -3 ),centSeparator)+centSeparator+stringNumber.substr(stringNumber.length-3,stringNumber.length))
	}
	else return(stringNumber)
}

function preferidos( usuario , accion ) {
// accion 1 : mostrar cesta
// accion 2:  enviar a favoritos
	if (usuario=='')
	  mostrarAlerta('You must be a registered user to access this section.');
	else {
		if (accion == 1 ) document.forms["producto"].accion.value = 0;
		else {
		 	document.forms["producto"].accion.value = 10;
	    }

	  a = window.open("blanco.asp","Preferidos","width=553,height=300,window=yes,resizable=yes,scrollbars=yes");
	  document.forms.producto.target = 'Preferidos';
	  document.forms.producto.action = 'preferidos.asp';
	  document.forms.producto.submit();
	  a.focus();
	}
}

function comprar(){

	desglose = "";
	for (i=0;i<(grupos.length);i++){
		actual = "";

		indiceProductoWeb = -1; // caso de elegir ninguno
		// cálculo de código de producto web seleccionado
		if (eval('document.forms.producto.grupo'+i)){
			for (j=0;j<grupos[i].length;j++) {
				if (eval('document.forms.producto.grupo'+i+'['+j+'].checked')){
					indiceProductoWeb = j;
					j = grupos[i].length;
				}
			}
		}
		else indiceProductoWeb = 0;

		if (grupos[i][indiceProductoWeb]) {
			actual = "w"+grupos[i][indiceProductoWeb];

			// cálculo de producto aqua seleccionado
			if (pa[grupos[i][indiceProductoWeb]]) {
				productoComprado = obtenerCodigoProductoSeleccion(i,indiceProductoWeb);
				if ( typeof(productoComprado) == 'undefined' ) {
					 mostrarAlerta('Please check you have selected size and colours')
					 return false;
				}
				else {
					 actual=actual+"a"+productoComprado+"a";
					 if (eval('document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad1')){
						actual = actual + 't' +eval ('document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad1[document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad1.selectedIndex].value');
					 }
					 if (eval('document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad2')){
						actual = actual + 'c' +eval ('document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad2[document.forms.producto.G'+i+'P'+indiceProductoWeb+'Cualidad2.selectedIndex].value');
					 }
				}
			}
		}
		desglose = desglose+actual;
	}

	a = window.open("blanco.asp","Carrito","width=657,height=300,window=yes,resizable=yes,scrollbars=yes");
	document.forms.producto.desglose.value = desglose;
	document.forms.producto.target = 'Carrito';
	document.forms.producto.action='carrito.asp';
	document.forms.producto.submit();
	a.focus();
}


function comprarCombinado(){

	if ( typeof(tiposCualidades) == 'undefined' ) {
		document.forms["producto"].accion.value = 6;
	}
	else {
		document.forms["producto"].accion.value = 8;
		productoComprado = obtenerCodigoProductoSeleccion();
		if ( typeof(productoComprado) == 'undefined' ) {
			 mostrarAlerta('Please check you have selected size and colour')
			 return false;
		}
		document.forms["producto"].productoTallado.value = productoComprado;
	}

	a = window.open("blanco.asp","Carrito","width=657,height=300,window=yes,resizable=yes,scrollbars=yes");
	document.forms.producto.target = 'Carrito';
	document.forms.producto.action='carrito.asp';
	document.forms.producto.submit();
	a.focus();
}



function mostrarAlerta( mensaje ){
	objetoCapaWeb.ocultar();
	objetoCapaWeb.escribecapa("<table width='256' border='0' cellspacing='0' cellpadding='0'><tr><td bgcolor='#FFFFFF' colspan='3' valign='bottom' height='40'>&nbsp;&nbsp;<img src='img/comunes/logoPopup.gif' width='110' height='28'></td></tr><tr><td bgcolor='#FFFFFF' width='10'></td><td bgcolor='#FFFFFF' class='negro'><br>"+mensaje+"<br></td><td bgcolor='#FFFFFF' width='10'></td></tr><tr><td height='38' align='right' valign='middle' bgcolor='#FFFFFF'  colspan='3'><a href='javascript:void(null)' onClick='objetoCapaWeb.ocultar();objetoCapaWeb.escribecapa(\"\")' onFocus='blur()'><img src='img/comunes/botonCerrarPopup.gif' width='52' height='13' border='0'></a>&nbsp;&nbsp;&nbsp;&nbsp;</td></tr></table>");
	objetoCapaWeb.mover_a(200,40)
	objetoCapaWeb.mostrar()
	window.scrollTo(0,0)	
}

function incrementarCantidad (escala,incremento){
// el incremento será incremento*escala
		cantidad = eval('parseInt(document.forms["producto"].unidades.value,10)+'+escala*incremento)
		if ((cantidad>0)&&(cantidad<100)) eval('document.forms["producto"].unidades.value='+cantidad)
		calcularPrecio()
}


function ini(){
	objetoCapaWeb=new objetoCapa('capaWeb');
	objetoCapaWeb.ocultar();
}

function mostrarImagen(imagen) {
	objetoCapaWeb.ocultar();
	objetoCapaWeb.escribecapa("<table border='0' cellpadding='0' cellspacing='0'><tr><td align='right' bgcolor='#ffffff'><a href='javascript:ocultarImagen()' onFocus='blur()'><img src='img/tienda/cerrarFoto.gif' border='0'></a></td></tr><tr><td><a href='javascript:ocultarImagen()' onFocus='blur()'><img src='img/productos/"+imagen+"' border='0'></a></td></tr></table>");
	objetoCapaWeb.mover_a(xmouse-15,ymouse-105)
	objetoCapaWeb.mostrar()
}

function ocultarImagen(){
	 objetoCapaWeb.ocultar();
	 objetoCapaWeb.escribecapa("");
	 objetoCapaWeb.mover_a(0,0)
}

function obtenerCodigoProductoSeleccion( grupo , ordenProducto ){
	var i;
	var j;

	// devuelve el codigo aqua de la combianción de cualidades seleccionadas
	for (i=0;i<pa[grupos[grupo][ordenProducto]].length;i++) {
		numeroCualidades = 0;
		for (j=0;j<tiposCualidades[grupos[grupo][ordenProducto]].length;j++){
			if ( typeof(cualidades[pa[grupos[grupo][ordenProducto]][i]][tiposCualidades[grupos[grupo][ordenProducto]][j]])!="undefined")
				if ( cualidades[pa[grupos[grupo][ordenProducto]][i]][tiposCualidades[grupos[grupo][ordenProducto]][j]].indexOf('|'+eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][j]+'.value')+'|') != -1)
						numeroCualidades++;
				else j = tiposCualidades[grupos[grupo][ordenProducto]].length;
			else j = tiposCualidades[grupos[grupo][ordenProducto]].length;
		}
		if (tiposCualidades[grupos[grupo][ordenProducto]].length == numeroCualidades)
			return(pa[grupos[grupo][ordenProducto]][i])
	}

}

function obtenerProductosCoincidentes ( grupo , ordenProducto , cualidadCambiada , valor ) {
	//devuelve los productos coincidentes
	productosCoincidentes = new Array();

	for (i=0;i<pa[grupos[grupo][ordenProducto]].length;i++)	{
		if (cualidades[pa[grupos[grupo][ordenProducto]][i]][cualidadCambiada]) {
			valores = cualidades[pa[grupos[grupo][ordenProducto]][i]][cualidadCambiada].split("|");
			for ( j = 0; j<valores.length; j++){
				if (valores[j] == valor) {
					productosCoincidentes[productosCoincidentes.length] = pa[grupos[grupo][ordenProducto]][i];
					j = valores.length;
				}
			}
		}
	}
	return(productosCoincidentes)
}


function rellenarCombos ( grupo , ordenProducto , cualidadCambiada ){

	if (cualidadCambiada != null) {
		valorSeleccionado = eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+cualidadCambiada+'.value')
		productosBusqueda = obtenerProductosCoincidentes ( grupo , ordenProducto , cualidadCambiada , valorSeleccionado )
	}
	else{
	 	valorSeleccionado = null;
		productosBusqueda = pa[grupos[grupo][ordenProducto]];
	}


	for (i=0;i<=tiposCualidades[grupos[grupo][ordenProducto]].length;i++){

		// obtenemos el conjunto de valores únicos viendo las cualidades de cada producto aqua
		// lo meteremos en un string separado por pipes (|)
		if (tiposCualidades[grupos[grupo][ordenProducto]][i] == cualidadCambiada) arrayBusqueda ='pa[grupos[grupo][ordenProducto]]';
		else arrayBusqueda ='productosBusqueda';

		valoresUnicosCualidad = '|';
		for (j=0;eval('j<'+arrayBusqueda+'.length');j++){
			if ( cualidades[eval(arrayBusqueda+'[j]')][tiposCualidades[grupos[grupo][ordenProducto]][i]] )	{
				eval('valoresCualidad = cualidades['+arrayBusqueda+'['+j+']][tiposCualidades[grupos[grupo][ordenProducto]]['+i+']].split("|");')
				for (k=0;k<valoresCualidad.length;k++){
					if ( (valoresCualidad[k]!="") && ( valoresUnicosCualidad.indexOf('|'+valoresCualidad[k]+'|') ==-1 )) {
						valoresUnicosCualidad+= valoresCualidad[k]+'|';
					}
				}
			}
		}

		if (eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][i])) {
			valorAnterior = eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][i]+'.value')
			// vaciamos el combo
			eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][i]+'.length=1')
			// rellenamos los valores
			comboValores = valoresUnicosCualidad.split("|");
			// ordenamos alfabeticamente
			//Quicksort(comboValores,1,comboValores.length-2);
			for (j=0;j<comboValores.length;j++)
			   if (""+comboValores[j]!="") {
				   eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][i]+'[j] = new Option("'+comboValores[j]+'","'+comboValores[j]+'") ')
				   if (valorAnterior==comboValores[j]) eval('document.forms.producto.G'+grupo+'P'+ordenProducto+'Cualidad'+tiposCualidades[grupos[grupo][ordenProducto]][i]+'.selectedIndex=j')
			   }
		}
	}
}


function Quicksort( arrayValores , inicio, fin)
// ordenacion alfabética de un combo con quicksort
{

   var pivote, nuevoInicio, nuevoFin, temp;

   // ordenando 2
  if (fin - inicio == 1){
	if (arrayValores[inicio] > arrayValores[fin])
	{
	  temp = arrayValores[inicio];
	  arrayValores[inicio] = arrayValores[fin];
	  arrayValores[fin] = temp;
	}
	return;
  }

  // ordenando mas de dos
  pivote = arrayValores[parseInt((inicio + fin) / 2,10)];
  arrayValores[parseInt((inicio + fin) / 2,10)] = arrayValores[inicio];
  arrayValores[inicio] = pivote;
  nuevoInicio = inicio + 1;
  nuevoFin = fin;

  do {

	// encuentro el nuevo Inicio
	while (nuevoInicio <= nuevoFin && arrayValores[nuevoInicio] <= pivote)
		nuevoInicio++;

	// encontrando el nuevo Fin
	while (arrayValores[nuevoFin] > pivote)
		nuevoFin--;

	// cambiamos los valores si hay que hacerlo
	if (nuevoInicio < nuevoFin) {
 		temp = arrayValores[nuevoInicio];
		arrayValores[nuevoInicio] = arrayValores[nuevoFin];
		arrayValores[nuevoFin] = temp;
	}
  } while (nuevoInicio < nuevoFin);

  arrayValores[inicio] = arrayValores[nuevoFin];
  arrayValores[nuevoFin] = pivote;


  // llamamos a la funcion recursivamente por divide y venderes

  // primera mitad
  if (inicio < nuevoFin - 1)
	Quicksort(arrayValores, inicio, nuevoFin - 1);

  // segunda mitad
  if (nuevoFin + 1 < fin)
	Quicksort(arrayValores, nuevoFin + 1, fin);
}


