
// Si el navegador es Firefox cambia la propiedad innerText por textContent

var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox/') > -1;  
if (is_firefox ){

    HTMLElement.prototype.__defineGetter__("innerText",function () { return(this.textContent); });   
    HTMLElement.prototype.__defineSetter__("innerText",function (txt) { this.textContent = txt; });

}


mostrar_capa_precio = function(id) {


    var elemento = document.getElementById(id);
    
    elemento.style.display = 'block';
;

}

function ocultar_capa_precio (id){
    
    var elemento = document.getElementById(id);

    
    elemento.style.display = 'none';
    
}


function LlenarCombo(json, combo){
    
    
    combo.options[0] = new Option('color', "");
    
    for (var i = 0;i<json.length;i++){
        combo.options[i + 1] = new Option(json[i].descripcion, json[i].clau);
    }
    
}

function VaciarCombo(combo)
{
    //vacia un combo dinamicamente
    
    while(combo.length > 0){
        combo.remove(combo.length - 1);
    } 
}


function cambiar_color_precio(talla, combo, clau, lang)	
{
    //cambia los colores disponibles segun la talla seleccionada mediante ajax
   
    combo = document.getElementById(combo); 
   
    VaciarCombo(combo);
   
	if(talla.options[talla.selectedIndex].value != ""){
		$.ajax({
			type: 'get',
			dataType: 'json',
			url: 'ajax.php',
			data: {calcular: 'no', clau: clau, talla: talla.options[talla.selectedIndex].value, lang: lang, color:'', cantidad:''},
			success: function(json){
				LlenarCombo(json, combo);          
			}

		});
	}
    else{
        combo.options[0] = new Option('color', "");
    }
  

}

function BorrarFila (fila, clau, especial, language){
    
    var precio, total, calcular, tot1, tot2, tot3, tot4, tot5;
    var nada = "";
    var cant = document.getElementById('cantidad_camisetas');
    
    
    switch (language){
        case 'esp':
        var frase = "Cantidad Total:";
        break;
        case 'cat':
        var frase = "Quantitat Total:";
        break;
        case 'ang':
        var frase = "Total Quantity:";
        break;       
    }
    
    
    
    
    cantidad = document.getElementById("quantitat"+fila);
    precio = document.getElementById("precio"+fila);
    total = document.getElementById("total"+fila);
    talla = document.getElementById("talla"+fila);
    color = document.getElementById("color"+fila);
    totalG = document.getElementById("total");
     

    if (especial == 'comprobar'){
        
        //si en la funcion comprobar sale que se han olvidado algun dato borra de esa fila el precio y el total, 
        //ademas borra el total general.
        
        precio.innerText = "";
        total.innerText = "";
        totalG.innerText = "";   
        
    }
    else{
        
        //borrar fila normal y despues llamar a calcular
        
        talla.options.selectedIndex = "";
        color.options.selectedIndex = "";
        cantidad.value = "";
        precio.innerText = "";
        total.innerText = "";
        calcular = 'si';
    
    }


    tot1 = document.getElementById("total1");
    tot2 = document.getElementById("total2");
    tot3 = document.getElementById("total3");
    tot4 = document.getElementById("total4");
    tot5 = document.getElementById("total5");

    
    cantidadG = CalcularCantidadG();
    
    if(tot1.innerText == "" && tot2.innerText == "" && tot3.innerText == "" && tot4.innerText == "" && tot5.innerText == ""){
       
       //Si todos los subtotales estan vacios vaciamos el total y anulamos en calcular
        totalG.innerText = "";
        cant.innerText = frase;
        calcular = 'no';       
        
    }
    

    
    if (calcular == 'si'){
        Calcular (clau, 'no', cantidadG, language);
    }
    
  
    
}		


function Comprobar(clau, language){
    
    //comprueba que no se haya rellenado algun campo de una fila y se hayan dejado uno sin rellenar
    
    var mensaje = document.getElementById('alerta');
    var error = 'no';
    var cant = document.getElementById('cantidad_camisetas');
    var max = document.getElementById('max');
    
    
    switch (language){
        case 'esp':
        var frase = "Cantidad Total:";
        break;
        case 'cat':
        var frase = "Quantitat Total:";
        break;
        case 'ang':
        var frase = "Total Quantity:";
        break;       
    }    
    
    
    cantidadG = CalcularCantidadG();

    if (cantidadG < 2000){
        
        max.style.display = 'none';
    } 

    
    for (i=1;i<=5;i++){
        cantidad = document.getElementById("quantitat"+i);
        talla = document.getElementById("talla"+i);
        color = document.getElementById("color"+i);
        if(cantidad.value != "" || talla.options[talla.selectedIndex].value != "" || color.options[color.selectedIndex].value != ""){
            if (cantidad.value == "" || isNaN(cantidad.value) || talla.options[talla.selectedIndex].value == "" || color.options[color.selectedIndex].value == ""){
                if (cantidadG < 2000){
                    mensaje.style.display = 'block'; //muestra el div de alerta si hay algun error
                }
                BorrarFila(i,clau,'comprobar');
                cant.innerText = frase; 
                error = 'si'; 
                           

            }
        }
        
    }

    if (error == 'no'){
        Calcular(clau, error, cantidadG, language);
    }

}



function CalcularCantidadG(){
    
   //calcula la cantidad total de productos para hacer el calculo de la tarifa
    
    var cantidadG = 0;
    
    for (i=1;i<=5;i++){
        cantidad = document.getElementById("quantitat"+i);
        talla = document.getElementById("talla"+i);
        color = document.getElementById("color"+i);
        if(cantidad.value != "" || talla.options[talla.selectedIndex].value != "" || color.options[color.selectedIndex].value != ""){
            
            if(isNaN(cantidad.value)==false && talla.options[talla.selectedIndex].value != "" && color.options[color.selectedIndex].value != ""){
                cantidadG = (parseInt(cantidadG) + parseInt(cantidad.value));
            }
        }
        
    }
    
    
    return cantidadG;
    
}



function Calcular(clau, error, cantidadG, language){

    var mensaje = document.getElementById('alerta');
    var fallo = document.getElementById('fallo');
    var cant = document.getElementById('cantidad_camisetas');
    var max = document.getElementById('max');
    
    
    switch (language){
        case 'esp':
        var frase = "Cantidad Total:";
        break;
        case 'cat':
        var frase = "Quantitat Total:";
        break;
        case 'ang':
        var frase = "Total Quantity:";
        break;       
    }
    
    
    fallo.style.display = 'none';
    
    if (error == 'no'){
        
        //si todo va bien anula la alerta de error

        mensaje.style.display = 'none';
    
    }

    general = preu = preu2 = preu3 = preu4 = preu5 = 0;
       
    Calcular1(clau, cantidadG, language);
    Calcular2(clau, cantidadG, language);
    Calcular3(clau, cantidadG, language);
    Calcular4(clau, cantidadG, language);
    Calcular5(clau,cantidadG, language);

    general = 0;
    
    if(cantidadG > 0 && cantidadG < 2000){
        
        cant.innerText = frase + ' ' + cantidadG;
       
        
    }
    else if(cantidadG > 0 && cantidadG >= 2000){
        
        max.style.display = 'block';
        
    }
    
           

}



function Calcular1(clau, cantidadG, language){
   
 
     var fallo = document.getElementById('fallo');
    var total1, precio1, total;
    
   	cantidad1 = document.getElementById("quantitat1");
    talla1 = document.getElementById("talla1");
    color1 = document.getElementById("color1");
	precio1 = document.getElementById('precio1');
	total1 = document.getElementById("total1");
    total = document.getElementById("total");
    
	if(cantidad1.value > 0){
        

    cantidadG = CalcularCantidadG();

    
    		$.ajax({
    			type: 'get',
    			dataType: 'json',
    			url: 'ajax.php',
    			data: {calcular: 'si', clau: clau, talla: talla1.options[talla1.selectedIndex].value, color: color1.options[color1.selectedIndex].value, cantidad: cantidadG, lang: ''},
    			success: function(json){
	               if (json[0].precio == 'fallo'){ //comprueba si hay un fallo en el sql del precio
                    if (cantidadG < 2000){
	                 fallo.style.display = 'block';
                    }
                     BorrarFila('1', clau, 'comprobar', language);  
	               }
                   else{
			         precio1.innerText = json[0].precio;
    			     preu1 = (json[0].precio * cantidad1.value);
                     preu1 = parseFloat(preu1);
                     preu1 = preu1.toFixed(2);
                     total1.innerText = preu1;
                     general = (parseFloat(general) + parseFloat(preu1));
                     total.innerText = general.toFixed(2);
                    }
    			}
    
    		});

    }
    
}

function Calcular2(clau, cantidadG, language){
    
    var fallo = document.getElementById('fallo');
   	var  total2, precio2, total;
    cantidad2 = document.getElementById("quantitat2");
    talla2 = document.getElementById("talla2");
    color2 = document.getElementById("color2");
	precio2 = document.getElementById("precio2");
	total2 = document.getElementById("total2");
    total = document.getElementById("total");
	
 	if(cantidad2.value > 0){
		
    cantidadG = CalcularCantidadG();
    
    		$.ajax({
    			type: 'get',
    			dataType: 'json',
    			url: 'ajax.php',
    			data: {calcular: 'si', clau: clau, talla: talla2.options[talla2.selectedIndex].value, color: color2.options[color2.selectedIndex].value, cantidad: cantidadG, lang: ''},
    			success: function(json){
	               if (json[0].precio == 'fallo'){
                    if (cantidadG < 2000){
	                 fallo.style.display = 'block';
                    }
                     BorrarFila('2', clau, 'comprobar', language);  
	               }
                   else{
			         precio2.innerText = json[0].precio;
                     preu2 = (json[0].precio * cantidad2.value);
                     preu2 = parseFloat(preu2);
                     preu2 = preu2.toFixed(2);
                     total2.innerText = preu2;
                     general = (parseFloat(general) + parseFloat(preu2));
                     total.innerText = general.toFixed(2);
                    }
    			}
    
    		});


	}
}

function Calcular3(clau, cantidadG, language){
    
    var fallo = document.getElementById('fallo');
    var total3, precio3, total;
   	cantidad3 = document.getElementById("quantitat3");
    talla3 = document.getElementById("talla3");
    color3 = document.getElementById("color3");
	precio3 = document.getElementById("precio3");
	total3 = document.getElementById("total3");
    total = document.getElementById("total");
	
   	if(cantidad3.value > 0){
			
     cantidadG = CalcularCantidadG();

    		$.ajax({
    			type: 'get',
    			dataType: 'json',
    			url: 'ajax.php',
    			data: {calcular: 'si', clau: clau, talla: talla3.options[talla3.selectedIndex].value, color: color3.options[color3.selectedIndex].value, cantidad: cantidadG, lang: ''},
    			success: function(json){
	               if (json[0].precio == 'fallo'){
                    if (cantidadG < 2000){
	                 fallo.style.display = 'block';
                    }
                     BorrarFila('3', clau, 'comprobar', language);  
	               }
                   else{
    			    precio3.innerText = json[0].precio;
    			    preu3 = (json[0].precio * cantidad3.value);
                    preu3 = parseFloat(preu3);
                    preu3 = preu3.toFixed(2);
                    total3.innerText = preu3;
                    general = (parseFloat(general) + parseFloat(preu3));
                    total.innerText = general.toFixed(2);
                    }
                    
    			}
    
    		});

	}
    
}


function Calcular4(clau, cantidadG, language){
    
    var fallo = document.getElementById('fallo');
    var total4, precio4, total;
   	cantidad4 = document.getElementById("quantitat4");
    talla4 = document.getElementById("talla4");
    color4 = document.getElementById("color4");
	precio4 = document.getElementById("precio4");
	total4 = document.getElementById("total4");
    total = document.getElementById("total");
	
	if(cantidad4.value > 0){
			
    cantidadG = CalcularCantidadG(); 

    		$.ajax({
    			type: 'get',
    			dataType: 'json',
    			url: 'ajax.php',
    			data: {calcular: 'si', clau: clau, talla: talla4.options[talla4.selectedIndex].value, color: color4.options[color4.selectedIndex].value, cantidad: cantidadG, lang: ''},
    			success: function(json){
	               if (json[0].precio == 'fallo'){
                    if (cantidadG < 2000){
	                 fallo.style.display = 'block';
                    }
                     BorrarFila('4', clau, 'comprobar',language);  
	               }
                   else{
        			    precio4.innerText = json[0].precio;
        			    preu4 = (json[0].precio * cantidad4.value);
                        preu4 = parseFloat(preu4);
                        preu4 = preu4.toFixed(2);
                    total4.innerText = preu4;
                    general = (parseFloat(general) + parseFloat(preu4));
                    total.innerText = general.toFixed(2);
                    }

    			}
    
    		});

	}
    
}


function Calcular5(clau, cantidadG, language){
    
    var fallo = document.getElementById('fallo');
    var total5, precio5, total;
   	cantidad5 = document.getElementById("quantitat5");
    talla5 = document.getElementById("talla5");
    color5 = document.getElementById("color5");
	precio5 = document.getElementById("precio5");
	total5 = document.getElementById("total5");
    total = document.getElementById("total");
	
    if(cantidad5.value > 0){
    
     cantidadG = CalcularCantidadG();
    
    		$.ajax({
    			type: 'get',
    			dataType: 'json',
    			url: 'ajax.php',
    			data: {calcular: 'si', clau: clau, talla: talla5.options[talla5.selectedIndex].value, color: color5.options[color5.selectedIndex].value, cantidad: cantidadG, lang: ''},
    			success: function(json){
	               if (json[0].precio == 'fallo'){
                    if (cantidadG < 2000){
	                 fallo.style.display = 'block';
                    }
                     BorrarFila('5', clau, 'comprobar',language);  
	               }
                   else{
			         precio5.innerText = json[0].precio;
			         preu5 = (json[0].precio * cantidad5.value);
                     preu5 = parseFloat(preu5);
                     preu5 = preu5.toFixed(2);
                     total5.innerText = preu5;
                     general = (parseFloat(general) + parseFloat(preu5));
                     total.innerText = general.toFixed(2);
                    }
    			}
    
    		});

	}
}

