// JavaScript Document

    function clearTable(tbody) {
    while (tbody.rows.length > 0) {
        tbody.deleteRow(0);
    }
  }
  function calculatePriceTotal(tbody) {
    // Actualiza el total
    var precio = 0;
    var precioTotal = 0;
    for (var i = 1; i < tbody.rows.length; i++) {
      precio = tbody.rows[i].cells[5].childNodes[0].nodeValue;
      precio = parseFloat(precio);
      precioTotal += precio;
    }
    document.getElementById("totalPrice").value = precioTotal.toFixed(2);
  }
  
  function deleteItem(rowNbr) {
    var tbody = document.getElementById("minicartBody");
   
    tbody.deleteRow(rowNbr);
   
  }
  
  function updateTotal(tbody, id, quantity,unitPrice){
	  
	productInCart = document.getElementById("totalUnidades"); 
	priceInCart = document.getElementById("totalPesos");  
	
	if ( productInCart != null & priceInCart != null) {  

      productInCart.innerHTML = parseInt(productInCart.innerHTML) + quantity;
	  totalInCart = parseFloat(priceInCart.innerHTML) +  parseFloat(unitPrice);
	  priceInCart.innerHTML =   totalInCart.toFixed(2) 
    }  
	return;
  }
  
  function updateMinicart(id, quantity){
		productInCart = document.getElementById("quantity-" + id );  
		if (productInCart != null) {  
			productInCart.innerHTML = parseInt(productInCart.innerHTML) + 1;
			return true;
		}  
		return false;
  }
	
  function getFirstChild(elem) {
    var child = elem.firstChild;
    while (child != null && child.nodeType != 1)
      child = child.nextSibling;
    return child;
  }
  
	function appendToTable(tbody, id, model, name, quantity, unitPrice, image, productId, colorDesc, presDesc) {
    
		var tr, td;
		
		var currencyVal = document.getElementById("currencyValue").value;
	        unitPrice = (unitPrice * currencyVal).toFixed(2);
                updateTotal(tbody,id, 1,unitPrice);
	
		if (updateMinicart(id, 1)) {
			return;	
		}
 
    currency =  document.getElementById("currencyMiniCart").value;

		var largeName = name;
		if (name.length > 22) {
			name = name.substr(0,22) + "...";
		}

		var colorText = "";
		if (colorDesc != null && colorDesc != "") {
		  colorText = " (" + colorDesc+ ") ";	
		}
		var presText = "";
		if (presDesc != null && presDesc != "") {
		   presText = " (" + presDesc+ ") ";
		}

	  // First row
		var node = document.createElement("div");
		node.innerHTML = "<div style='float: left'><a href='productDetail.asp?Idp=" + productId + "'><img src='" + image + "' width='53' border='0' /></a></div><div class='productCode' style='padding-top: 2px'><span class='mc-pname'><a href='productDetail.asp?Idp=" + productId + "' title='"+ largeName +"'><span class='productCode style1' style='padding-top: 2px'>" + name + "</span></a></span>" + colorText + presText +
" #" + model + "</div> <div class='productCode style2'>Cant.: <span id='quantity-" + id + "'> " + (parseInt(quantity) + 1) + "</span> a: " + currency + "" + unitPrice + " c/u </div><div align='center'><img src='images/line.jpg' width='164' height='6' /></div>";
		
    var parent = document.getElementById("minicartBody");
    parent.insertBefore(node, getFirstChild(parent));

		//td.innerHTML = "<a href='#'>xxx</a>";
		// Borrar el ultimo si hay mas de 5 elementos.
		if (getChildCount(parent) >= 6) {
			deleteLastDiv();
		}
		
		changeEnd();
  }
	
	function changeEnd(){
	  end = document.getElementById("bottom");  
		if (end != null) {  
			end.innerHTML = "<a href='login.asp?checkout=1'><img src='images/IrACaja.gif' alt='Ir a Caja' border='0' /></a>";
		}  
	}
	
	
	function getLastChild(elem) {
   var child = elem.lastChild;
   while (child != null && child.nodeType != 1)
     child = child.previousSibling;
   return child;
 }
 
	function deleteLastDiv() {
   var parent = document.getElementById("minicartBody");
   var lastChild = getLastChild(parent);
   if (lastChild != null)
     parent.removeChild(lastChild);
 }
	function getChildCount(elem) {
   var count = 0;
   var child = elem.firstChild;
   while (child != null) {
     if (child.nodeType == 1)
       count++;
     child = child.nextSibling;
   }
   return count;
 }

