var attr1;
var attr2;
var attr3;
// define variable so that eval an empty option does not crash the javascript
var arr_attrib_id___ = false;

function init_attr() {
	attr1 = document.getElementById("attrib1");
	attr2 = document.getElementById("attrib2");
	attr3 = document.getElementById("attrib3");
	//if (attr1.options) select_add_data(attr1, arr_attrib1, '');
	// CVilladangos. 2007-09-03. El combo se construye poniendo la disponibilidad.
	if (attr1.options) select_add_data_value(attr1, arr_attrib1, '');
	chAttrib1();
	chAttrib2();
}

// Params:
//		product_id is hopefully self-explanatory
// 		productsArray contains the list of product_ids shown in the scroller
//		picWidth is the width of each pic
//		picSeparation is the space between pics
//		position is the position that the current article should be
function getScrollPosition(product_id, productsArray, picWidth, picSeparation, position) {
	for ( i=0; i<productsArray.length ; i++ ) {
		if(productsArray[i] == product_id) {
			productPosition = i;
		}
	}
	//productPosition = 3;
	firstPosition = productPosition - position;
	if(firstPosition < 0) {
		firstPosition = 0;
	}
	scrollPosition = firstPosition * (picWidth + picSeparation);

	return scrollPosition;
}

function current_product_id() {
	if (attr1.options && attr1.options.length!=0) sel1 = attr1.options[attr1.selectedIndex].value;
	else sel1 = "unico";
	if (attr2.options && attr2.options.length!=0) sel2 = attr2.options[attr2.selectedIndex].value;
	else sel2 = "";
	if (attr3.options && attr3.options.length!=0) sel3 = attr3.options[attr3.selectedIndex].value;
	else sel3 = "";

	try	{
		eval ("idp = arr_attrib_id_"+sel1+"_"+sel2+"_"+sel3);
	}
	catch(e){}

	if(typeof(idp) == 'string') {
		document.getElementById("product_id").value = idp;
	}

	return idp;
}

function chAttrib1() {

	/*var newOptions = new Array();
	if(attr1.options) {
		newOptions[0] = new Option('Seleccionar '+attr1_label, '');
		for($i = 0; $i < attr1.options.length; $i++) {
			newOptions[$i + 1] = new Option(attr1.options[$i].value, attr1.options[$i].value)
		}
	}*/

	if (attr2.options){
		if (attr2.options.length!=0) sel_ant = attr2.options[attr2.selectedIndex].value;
		else sel_ant = "";
		select_remove_all(attr2);
		if (attr1.options)
			sel = attr1.options[attr1.selectedIndex].value;
		else
			sel = "unico";
		//eval ("select_add_data(attr2, arr_attrib2_"+sel+", sel_ant);");
		// CVilladangos. 2007-09-03. El combo se construye poniendo la disponibilidad.

		eval ("select_add_data_value(attr2, arr_attrib2_"+sel+", sel_ant);");
		chAttrib2();
	}
	id=current_product_id();
	select_qty(id);
}

function chAttrib2() {
	if (attr3.options){
		if (attr3.options.length!=0) sel_ant = attr3.options[attr3.selectedIndex].value;
		else sel_ant = "";
		select_remove_all(attr3);
		if (attr3.options)
			sel1 = attr1.options[attr1.selectedIndex].value;
		else
			sel1 = "unico";
		sel2 = attr2.options[attr2.selectedIndex].value;
		//eval ("select_add_data(attr3, arr_attrib3_"+sel1+"_"+sel2+", sel_ant);");
		// CVilladangos. 2007-09-03. El combo se construye poniendo la disponibilidad.
		eval ("select_add_data_value(attr3, arr_attrib3_"+sel1+"_"+sel2+", sel_ant);");

	}
	id=current_product_id();
	select_qty(id);
}

function chAttrib3() {
  id=current_product_id();
  select_qty(id);
}

isIE=document.all;
isNN=!document.all&&document.getElementById;
isN4=document.layers;

function hideMe(d){
  if (isIE||isNN) {
  	d.style.visibility="hidden";
  	d.style.display="none";

  } else if (isN4) {
  	document.d.visibility="hide";
  }
}

function showMe(d){
  if (isIE||isNN) {
  	d.style.visibility="visible";
  	d.style.display="block";
  } else if (isN4) {
  	document.d.visibility="show";
  }
}

function select_qty( id )
{
	sq = document.getElementById('quantity');
	select_remove_all(sq);
	dades = new Array();

	d=document.getElementById('disponible');
	db=document.getElementById('disponible_boton');
	// nd=document.getElementById('no_disponible');

	// CVilladangos. Hay dos capas mas con disponibilidad.
	//* Monica (05/10/2007) Las capas de disponiblidad disponible_1 y no_disponible_1 han sido eliminadas para las nuevas campanas
	try {
		d1=document.getElementById('disponible_1');
		nd1=document.getElementById('no_disponible_1');
	}catch (e) {}

	//* Monica (04/10/2007) Si la cantidad es negativa tambi�n se oculta el bot�n de "anadir a mi cesta"
	if ( quantities[id] <= 0 )
	{
		hideMe(d);hideMe(db);
		//showMe(nd);
		//* Monica (05/10/2007) Las capas de disponiblidad disponible_1 y no_disponible_1 han sido eliminadas para las nuevas campanas
		try {
			hideMe(d1);showMe(nd1);
		}
		catch (e) {}
	}
	else
	{
		showMe(d);showMe(db);
		//hideMe(nd);
		//* Monica (05/10/2007) Las capas de disponiblidad disponible_1 y no_disponible_1 han sido eliminadas para las nuevas campanas
		try {
			showMe(d1);hideMe(nd1);
		}
		catch (e) {}
	}

	for ( i=1; (i<=quantities[id] && i<=5) ; i++ )
	{
		dades[i-1]=i;
	}
	select_add_data(sq, dades, "");
}

/**
* Selecciona item 'select-one'
*/
function selecciona_select(_v, _it) {
	var SI = _v.options.length;
	for(var i=0;i<SI;i++)
	{
		if (_v.options[i].value == _it) {
			_v.options[i].selected = true;
			return;
		}
	}
}

/**
* Borra tots els ítems d'un select
*/
function select_remove_all(sel) {
	var SI = sel.options.length;
	while (sel.options.length!=0) sel.remove(sel.options.length-1);
}

/**
* Posa dades a un select
*
* @param sel object item select
* @param dades array of data
* @param valor_actual string valor a seleccionar
*/
function select_add_data(sel, dades, valor_actual) {
	var SI = dades.length;
	var ds;
	for(var i=0;i<SI;i++) {
		ds = ''+dades[i];
		re = /[^a-zA-Z0-9\/\-]+/;
		nt = ds.replace( re , '_' );
		sel.options[sel.length] = new Option(dades[i], nt.toLowerCase() );
	}
}


/**
* Crea un combo de selecci�n html. Diferencia el valor que aparece en el option y el que se
* muestra. Utiliza el array global quantities que es el que almacena la disponibilidad.
*
* @param objet o_select. Objeto en el que se a�aden los elementos.
* @param mixed st_option_value. Array con los datos
* @param valor_actual string valor a seleccionar
*/
function select_add_data_value(sel, dades, valor_actual) {

	var SI = dades.length;
	var ds;
	var s_cantidad;
	var s_id_product;
	var i_cantidad;
	var s_option;

	// DGarcia: prepend empty option
	// CVilladangos. Por compatibilidad con anteriores microsites ya generados en los que no
	// existe la variable se pregunta por la existencia de la misma. Si no existe no se muestra el
	// Seleccionar en la primera posici�n.
	if(typeof(attr1_label) !='undefined')
		sel.options[0] = new Option('Selezionare taglia '+attr1_label, '');

	for(var i=0;i<SI;i++) {
		s_cantidad = "";
		ds = ''+dades[i];
		//re = /[^a-zA-Z0-9\/\-]+/;
		// Las variables solo pueden tener caracteres alfanumericos, el resto se substituyen por _
		re = /[^a-zA-Z0-9]+/g;
		nt = ds.replace( re , '_' );
		//Reemplazamos tambien signos de puntuacion por _
		re = /[,'�!�;:\.\?\/]/;
		nt = nt.replace( re , '_' );
		//hace falta otra reg exp q reemplace este valor, pero en Firefox da error
		re = "�"; nt = nt.replace( re , '_' );
		re = "�"; nt = nt.replace( re , '_' );
		s_option = ds.replace(/_/g, ' '); // lo que mostramos: quitamos el _ y se substituye por espacio.
		eval('s_id_product = arr_attrib_id_' + nt.toLowerCase()+'__');
		i_cantidad = quantities[s_id_product];

		//* Monica (04/10/2007) Aseguramos que si no hay stock (auque �ste sea negativo) salga el literal 'agotado':
		//* El stock disponible se calcula restando a la cantidad inicial ventas realizadas. Si por alg�n motivo
		//*	se modifica la cantidad inicial por un valor inferior al de las ventas, el stock calculado ser�a negativo.
		if(i_cantidad <= 0){
			s_cantidad = " -- finito";
		}
		else if(i_cantidad==1){
			s_cantidad = " -- ne rimane 1";
		}
		else if(i_cantidad==2){
			s_cantidad = " -- ne rimangono 2";
		}
		//sel.options[sel.length] = new Option(dades[i]+s_cantidad, nt.toLowerCase());
		// Se pone la descripcion del producto tal y como est� en la base de datos.
		//sel.options[sel.length] = new Option(dades[i]+s_cantidad, nt.toLowerCase());
		sel.options[sel.length] = new Option(s_option+s_cantidad, nt.toLowerCase());


	}

}
