// JavaScript Document


function cambiacolor() { //v4.0
//alert();
var filas=document.getElementById("cuentafilas").value;
for(var i=0;i<filas;i++){
var formulario="form"+i;
var posicion ="posicion"+i;
var tallas="id_talla"+i;
var colores="color_hid"+i;
var stocks="stock_hid"+i;
var idstocks="idstock_hid"+i;
var foto="imagen_hid"+i;
var scolores=String(document.forms[formulario][colores].value).split("color=");
var sstocks=String(document.forms[formulario][stocks].value).split("stock=");
var sidstocks=String(document.forms[formulario][idstocks].value).split("idstock=");
var sfoto=String(document.forms[formulario][foto].value).split("imagen=");

var colorescombo=String(scolores[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("/");
var stockscombo=String(sstocks[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("/");
var idstockscombo=String(sidstocks[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("/");
var fotocombo=String(sfoto[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("$");

var rellenacolores="";
//borrar todos los colores actuales
//var comboacambiar=("form"+document.forms[formulario][posicion].value+".id_color"+document.forms[formulario][posicion].value);

var limite =document.forms["form"+document.forms[formulario][posicion].value]["id_color"+document.forms[formulario][posicion].value].length;

for(var k=0;k<limite;k++){
document.forms["form"+document.forms[formulario][posicion].value]["id_color"+document.forms[formulario][posicion].value].remove(0);
}


for(var k=0;k<colorescombo.length-1;k++){
var opt = document.createElement("option"); 
opt.value=colorescombo[k];
opt.label=colorescombo[k];
document.forms["form"+document.forms[formulario][posicion].value]["id_color"+document.forms[formulario][posicion].value].appendChild(opt); 
opt.text=colorescombo[k];
}

var nombrestock ="stock"+i;

//alert(idstock);
if(stockscombo[0]>0){
//alert("si");
document.getElementById(nombrestock).src="images/ico_stock_ok.gif";
document.getElementById(nombrestock).alt="Disponemos de stock";
}else{
//alert("no");
document.getElementById(nombrestock).src="images/ico_stock_off.gif";
document.getElementById(nombrestock).alt="No hay stock en estos momentos";
}

var idfoto ="foto"+i;
document.getElementById(idfoto).src="images/productos/"+fotocombo[0];

var ididstock ="idstock"+i;
document.getElementById(ididstock).value=idstockscombo[0];





} //fin for

//alert(fotocombo);

} // fin funcion


function cambiastock(posicion, nombre) { //v4.0
//alert(posicion);
//alert(nombre);
var i=String(nombre).slice(8,10);

var formulario="form"+i;
var tallas="id_talla"+i;
var stocks="stock_hid"+i;
var idstocks="idstock_hid"+i;
var foto="imagen_hid"+i;

var sstocks=String(document.forms[formulario][stocks].value).split("stock=");
var sidstocks=String(document.forms[formulario][idstocks].value).split("idstock=");
var sfoto=String(document.forms[formulario][foto].value).split("imagen=");

var stockscombo=String(sstocks[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("/");
var idstockscombo=String(sidstocks[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("/");
var fotocombo=String(sfoto[Number(document.forms[formulario][tallas].selectedIndex)+1]).split("$");

var idstock ="stock"+i;
var ididstock ="idstock"+i;

//alert(idstock);
if(stockscombo[posicion]>0){
//alert("si");
document.getElementById(idstock).src="images/ico_stock_ok.gif";
document.getElementById(idstock).alt="Disponemos de stock";
}else{
//alert("no");
document.getElementById(idstock).src="images/ico_stock_off.gif";
document.getElementById(idstock).alt="No hay stock en estos momentos";
}

document.getElementById(ididstock).value=idstockscombo[posicion];
//alert(document.getElementById(ididstock).value);

var idfoto ="foto"+i;
document.getElementById(idfoto).src="images/productos/"+fotocombo[posicion];
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function cargaprecio(cadenaprecios, talla, cadenatallas, j){
	cadena = cadenatallas.split("III");
	var subalmacadena="";
	for(i=1;i<cadena.length;i++){
		cadena2 = cadena[i].split("###");
		if (cadena2[0] == talla){
			tipo=cadena2[1];
		}		
	}

	cadenap = cadenaprecios.split("###");
	precio3= cadenap[0];
	precio1= cadenap[1];
	precio4= cadenap[2];
	precio2= cadenap[3];
				
	if (tipo=="adulto"){
		if (document.getElementById('mc'+j).checked==true){
			// MANGA CORTA ADULTO
			document.getElementById('precionuevo'+j).innerHTML =precio1 + " &#8364;";
		} else {
			// MANGA LARGA ADULTO
			document.getElementById('precionuevo'+j).innerHTML =precio2 + " &#8364;";
		}
	} else {
		if (document.getElementById('mc'+j).checked==true){
			// MANGA CORTA NIÑO
			document.getElementById('precionuevo'+j).innerHTML =precio3 + " &#8364;";
		} else {
			// MANGA LARGA NIÑO
			document.getElementById('precionuevo'+j).innerHTML =precio4 + " &#8364;";
		}
	}
}

function cargaprecio2(precio, precionino, talla, cadenatallas, j){

	cadena = cadenatallas.split("III");
	var subalmacadena="";
	var tipo="";
	for(i=1;i<(cadena.length);i++){
		cadena2 = cadena[i].split("###");
		if (cadena2[0] == talla){
			tipo=cadena2[1];
		}		
	}
							
	if (tipo=="adulto"){
			document.getElementById('precionuevo'+j).innerHTML =precio + " &#8364;";
	} else {
			document.getElementById('precionuevo'+j).innerHTML =precionino + " &#8364;";
	}
}

function comprar(x,l){

if (Number(document.getElementById('uds'+l).value) < Number(document.getElementById('pedidominimo'+l).value)) {
		alert ("El pedido minimo es " + document.getElementById('pedidominimo'+l).value);
} else {
	
	
	if(x=='1'){//alert(document.getElementById('uds'+l).value);	
	var direccion="add.php?id=" + document.getElementById('idstock'+l).value + "&uds=" + document.getElementById('uds'+l).value + "&mangacortalarga=" + x + "&mc=" + document.getElementById('mc'+l).checked + "";
	}else{
	var direccion="add.php?id=" + document.getElementById('idstock'+l).value + "&uds=" + document.getElementById('uds'+l).value + "&mangacortalarga=" + x + "";
	}
	if(document.getElementById('idstock'+l).value!=""){
		MM_openBrWindow(direccion,'Carrito','width=640,height=500')
	}
}
}

function abrecarri(x, p, c){
// ABRE LA FICHA
	var istck=document.forms["form"+x]["idstock"+x].value
	var unidadessend=document.forms["form"+x]["uds"+x].value
	window.location="ficha.php?id="+p+"&cat="+c+"&ids="+istck+"&uds="+unidadessend +"";
}
