function getElementsByClass( searchClass, domNode, tagName) {
	if (domNode == null) domNode = document;
	if (tagName == null) tagName = '*';
	var el = new Array();
	var tags = domNode.getElementsByTagName(tagName);
	var tcl = " "+searchClass+" ";
	for(i=0,j=0; i<tags.length; i++) {
		var test = " " + tags[i].className + " ";
		if (test.indexOf(tcl) != -1)
			el[j++] = tags[i];
	}
	return el;
}

function id($id){
    return document.getElementById($id);
}

function pluralize(num, sing, plu){
    if(num!=1){
        return num+' '+plu;
    } else { return num+' '+sing; }
}

function refreshValues(opt){
    // targets
	var carrito = document.getElementById('carrito_holder');
    artTarget = document.getElementById('total_articulos');
    precTarget = document.getElementById('total_precio');
    
    // Containers
    artElements = getElementsByClass('articulo',document,'input');
    precElements = getElementsByClass('precio-tabla',document,'span');
    if (opt.stock){ stockElements = getElementsByClass('stock',document,'span'); }
    
    // Adquire
    var artSum = 0;
    var precSum = 0;
    for(var i=0;i<artElements.length;i++){
        if (opt.stock){
            if (/msie/i.test (navigator.userAgent)){
                tmp_stock = stockElements[i].innerHTML;
            } else {
                tmp_stock = stockElements[i].firstChild.textContent;
            }
            if (parseInt(artElements[i].value) > parseInt(tmp_stock)){
                alert('Debe escoger un número de articulos inferior o igual a los disponibles en stock');
                //artElements[i].value = stockElements[i].firstChild.textContent;
            } 
        } 
        artSum += parseInt(artElements[i].value);
    }
    for(var i=0;i<precElements.length;i++){
		if (/msie/i.test (navigator.userAgent)){
			onePriceStr = precElements[i].innerHTML;
		} else {
			onePriceStr = precElements[i].firstChild.textContent;
		}
        onePrice = parseFloat(onePriceStr.substring(0,onePriceStr.length-1));
        precSum += parseFloat(artElements[i].value)*onePrice;
    }
    
    // Create nodes
    var artNewNode = document.createTextNode('Total artículos: '+artSum);
    var artOldNode = artTarget.firstChild;
    artTarget.replaceChild(artNewNode, artOldNode);
    var precNewNode = document.createTextNode('Precio total: '+precSum.toFixed(2)+'€');
    var precOldNode = precTarget.firstChild;
    precTarget.replaceChild(precNewNode, precOldNode);
    
    // For last write article sum
    if (!carrito.artSum){
        var sumNode = document.createElement('input');
        sumNode.type = 'hidden';
        sumNode.name = 'artSum';
        sumNode.value = artSum;
        carrito.appendChild(sumNode);
    } else {
        carrito.artSum.value = artSum;
    }
    
}
function sendCart(){
    var stop = false;
    tallElements = getElementsByClass('talla',document,'span');
    artElements = getElementsByClass('articulo',document,'input');
    precElements = getElementsByClass('precio',document,'span');
    minElements = getElementsByClass('minimo',document,'span');
    stockElements = getElementsByClass('stock',document,'span');
    var carrito = document.getElementById('carrito_holder');
    
	if (/msie/i.test (navigator.userAgent)){
		carritoCount = parseInt(carrito.elements.length - 6);
	} else {
		productElements = getElementsByClass('producto',carrito,'input');
		carritoCount = parseInt(productElements.length);
	}
    
    if (minElements[0]){
        for(var el in stockElements){
            if (/msie/i.test (navigator.userAgent)){
                var tmp_min = minElements[el].innerHTML;
            } else {
                var tmp_min = minElements[el].firstChild.textContent;
            }
            var tmp_art = artElements[el];
            if(parseInt(tmp_art.value) == 0){
                continue;
            }
            if(parseInt(tmp_art.value) < parseInt(tmp_min)){
                if(!stop){ stop = true; }
                //tmp_art.value = tmp_min;
            }
        }
    }
    if(stop){
        alert('Debe hacer un pedido igual o superior al pedido mínimo en cada talla.');
        return;
    }
    
    pedidoMin = parseInt(carrito.pedidoMinimo.value);
    artSum = parseInt(carrito.artSum.value);
    if (tallElements.length > 0){
        if (artSum >= pedidoMin){
            // Creamos los nodos para el post
            for(var i=0;i<tallElements.length;i++){
                if (parseInt(artElements[i].value) > 0) {
                    var prodNode = document.createElement('input');
                    prodNode.type = 'hidden';
                    prodNode.name = 'prodLines[]';
                    if (/msie/i.test (navigator.userAgent)){
                        prodNode.value = tallElements[i].innerHTML +'#'+ artElements[i].value;
                    } else {
                        prodNode.value = tallElements[i].firstChild.textContent +'#'+ artElements[i].value;
                    }
                    prodNode.setAttribute('class','producto');
                    carrito.appendChild(prodNode);
                } else {
                    continue;
                }
            }
            // Enviamos los productos
            carrito.submit();
        } else {
            alert('Debe hacer un pedido mínimo de '+ pluralize(pedidoMin,'articulo.','articulos.')); 
        }
    }
}



var _switchCopy = true;
function switchCopy(){
    source = {
        direccion: id('facturacion-direccion'),
        cp: id('facturacion-cp'),
        localidad: id('facturacion-localidad'),
        provincia: id('facturacion-provincia'),
        pais: id('facturacion-pais'),
        telefono: id('facturacion-telefono')
    };
    
    dest = {
        direccion: id('envio-direccion'),
        cp: id('envio-cp'),
        localidad: id('envio-localidad'),
        provincia: id('envio-provincia'),
        pais: id('envio-pais'),
        telefono: id('envio-telefono')
    };
    
    if(!_switchCopy){
        for(var i in source){
            if (/msie/i.test (navigator.userAgent)){
                dest[i].value = source[i].innerHTML;
            } else {
                dest[i].value = source[i].firstChild.textContent;
            }
            dest[i].readOnly = true;
        }
        _switchCopy = true;
    } else {
        for(var i in dest){
            dest[i].value = '';
            dest[i].readOnly = false;
        }
        _switchCopy = false;
    }
    
}