function aggiungiCarrello(modelloVersione, categoriaPelle, quantita) {
  if (!quantita) { 
  alert('Occorre specificare una quantita');
  return false;
  }
  url='/?op=addCartRow&modelloVersione='+modelloVersione+'&categoriaPelle='+categoriaPelle+'&quantita='+quantita;
  ajaxCall(url);
}

function ajaxCall(url) {
  xmlhttp=null  
  // code for Mozilla, etc.
  if (window.XMLHttpRequest) {
  xmlhttp=new XMLHttpRequest()
  // code for IE
  } else if (window.ActiveXObject) {
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
  
  if (xmlhttp!=null) {  
  xmlhttp.onreadystatechange=state_Change
  xmlhttp.open("GET",url,true)
  xmlhttp.send(null)
  } else {
  alert("Your browser does not support XMLHTTP.")
  } 
}

function state_Change()
{
// if xmlhttp shows "loaded"
if (xmlhttp.readyState==4) {
  // if "OK"
  if (xmlhttp.status==200) {
    xmlDoc=xmlhttp.responseXML;
    x=xmlDoc.documentElement.childNodes;    
    op=xmlDoc.documentElement.getAttribute('op');
    
    if (op=='jr') {      
      location.href='/modelli/reports/output.pdf';   
      }    
    
    curSymbol="€"; 
    if (op=='addCartRow') {
      cky_art=xmlDoc.documentElement.getAttribute('insertedCkyArt');
      document.getElementById('cart_arrow'+cky_art).style.display='none';     
    }
    
    if (op=='saveCartRow' || op=='delCartRow') {   
    cart = document.getElementById('cartTable');
    cartTbody=cart.getElementsByTagName('tbody')[0]     
          
      rowColor='scura';
      for (var i=0;i<x.length;i++) {
        if ((x[i].nodeType!=3)&&(x[i].nodeType!=8)) {
          cartRow = document.createElement('tr');
          if (rowColor=='chiara') { rowColor='scura' } else { rowColor='chiara' }
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRow.className=rowColor;   
            } else {                    
            cartRow.setAttribute('class',rowColor);
            }  
          
    
          //immagine
          cartRowCell = document.createElement('td');
          cartRowCellDiv = document.createElement('div');
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellDiv.setAttribute('className','bordo-foto-cart');
            } else {               
            cartRowCellDiv.setAttribute('class','bordo-foto-cart');
            }         
          cartRowCellImg = document.createElement('img');
          cartRowCellImg.src='img.php?pid='+x[i].getAttribute('cky_art').substring(0, 5);
          cartRowCellDiv.appendChild(cartRowCellImg)     
          cartRowCell.appendChild(cartRowCellDiv)
          cartRow.appendChild(cartRowCell)            
    
          //ckyart
          cartRowCell = document.createElement('td');
          txt = document.createTextNode(x[i].getAttribute('cky_art'));          
          cartRowCell.appendChild(txt)
          cartRow.appendChild(cartRowCell)          
          
          //descrizione
          cartRowCell = document.createElement('td');
          txt = document.createTextNode(x[i].getAttribute('carrello_articoli_descrizione'));          
          cartRowCell.appendChild(txt)
          cartRow.appendChild(cartRowCell)          
          

          //prezzo_unitario
          cartRowCell = document.createElement('td');
          cartRowCell.setAttribute('align','right');
          cartRowCell.width=42;
          
          txt = document.createTextNode(x[i].getAttribute('carrello_prezzo_unitario'));          
          cartRowCell.appendChild(txt)
          cartRow.appendChild(cartRowCell)
          
          //quantita
          cartRowCell = document.createElement('td');
          cartRowCell.align='right';   
          
          cartRowInput = document.createElement('input');
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowInput.style.width='18px';          
            } else {                    
            cartRowInput.setAttribute('style', 'width:18px;');
            }
          cartRowInput.setAttribute('id',x[i].getAttribute('cky_art')+'_quantity');
          
          cartRowInput.value=x[i].getAttribute('carrello_quantita')       
          cartRowCell.appendChild(cartRowInput)
          cartRow.appendChild(cartRowCell)
          
          //totale
          cartRowCell = document.createElement('td');
          cartRowCell.setAttribute('align','right');
          rowTotal=x[i].getAttribute('carrello_quantita')*x[i].getAttribute('carrello_prezzo_unitario')
          txt = document.createTextNode(rowTotal);          
          cartRowCell.appendChild(txt)
          cartRow.appendChild(cartRowCell)
          
          //note
          cartRowCell = document.createElement('td');
          cartRowInput = document.createElement('input');
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowInput.style.width='100px';          
            } else {                    
            cartRowInput.setAttribute('style','width:100px;'); 
            }            
          cartRowInput.setAttribute('id',x[i].getAttribute('cky_art')+'_notes');          
          cartRowInput.value=x[i].getAttribute('carrello_note');
         
          cartRowCell.appendChild(cartRowInput)
          cartRow.appendChild(cartRowCell)
          
          //salva
          cartRowCell = document.createElement('td');
          cartRowCellButton = document.createElement('button');
          
          saveFunctionText="saveCartRow('"+x[i].getAttribute('cky_art')+"', document.getElementById('"+x[i].getAttribute('cky_art')+"_quantity').value, document.getElementById('"+x[i].getAttribute('cky_art')+"_notes').value);";
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellButton.setAttribute('className','save');                   
            cartRowCellButton.onclick=new Function(saveFunctionText);
            } else {               
            cartRowCellButton.setAttribute('class','save');
            cartRowCellButton.setAttribute('onclick',saveFunctionText); 
            }                 
          cartRowCellButton.setAttribute('type','button');
          cartRowCell.appendChild(cartRowCellButton)
          cartRow.appendChild(cartRowCell);
          
          //delete
          cartRowCell = document.createElement('td');
          cartRowCellButton = document.createElement('button');
          
          deleteFunctionText="delCartRow('"+x[i].getAttribute('cky_art')+"')";
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellButton.setAttribute('className','delete');
            cartRowCellButton.onclick=new Function(deleteFunctionText);                   
            } else {               
            cartRowCellButton.setAttribute('class','delete');
            cartRowCellButton.setAttribute('onclick',deleteFunctionText); 
            }                    
          cartRowCellButton.setAttribute('type','button');       
          
          cartRowCell.appendChild(cartRowCellButton)
          cartRow.appendChild(cartRowCell);          
          
          cartTbody.appendChild(cartRow);
          }
        }      
      totalCart=xmlDoc.documentElement.getAttribute('total');                   
      divTotalCart=document.getElementById('totalCartDiv');      
      
      while (divTotalCart.childNodes[0]) {
        divTotalCart.removeChild(divTotalCart.childNodes[0]);
        }          
      divTotalCart.appendChild(document.createTextNode(totalCart+curSymbol));
      //document.getElementById('totalCart').innerHTML="";
      //document.getElementById('totalCart').innerHTML=totalCart;
               
    }
    } else {
    alert("Problem retrieving XML data")
    }
  }
}

function addCartRow(cky_art, quantity) {
  if (!quantity) { 
  alert('Occorre specificare una quantita');
  return false;
  }
  document.getElementById('cart_arrow'+cky_art).style.display='inline';
  url='/?op=addCartRow&cky_art='+cky_art+'&quantity='+quantity;
  ajaxCall(url);
}



function delCartRow(modelloVersione, categoriaPelle) {
  cart = document.getElementById('cartTable');
  cartTbody=cart.getElementsByTagName('tbody')[0]
  cartTbody.parentNode.removeChild(cartTbody);  
  cartTbody = document.createElement('tbody');
  cart.appendChild(cartTbody);  
  url='/?op=delCartRow&modelloVersione='+modelloVersione+'&categoriaPelle='+categoriaPelle;
  ajaxCall(url);
}

function saveCartRow(modelloVersione, quantity, notes) {
  if (quantity>0) {
    cart = document.getElementById('cartTable');
    cartTbody=cart.getElementsByTagName('tbody')[0]
    cartTbody.parentNode.removeChild(cartTbody);  
    cartTbody = document.createElement('tbody');
    cart.appendChild(cartTbody);
    url='/?op=saveCartRow&modelloVersione='+modelloVersione+'&quantity='+quantity+'&notes='+notes; 
    ajaxCall(url); 
    } else {
    alert('Per modificare la quantità occorre specificare un valore maggiore di 0.')
    }
}

function espandiVersione(ele) {
  var scheda = document.getElementById('scheda');
  
  if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
  trScheda=scheda.all.tags('TR');
  cssString='block';
  } else {
  trScheda=scheda.getElementsByTagName('tr');
  cssString='table-row';
  }  
   
  for(i=0; i < trScheda.length; i++) {
  if (trScheda[i].getAttribute("id") != null) { 
    if (trScheda[i].getAttribute("id") != ele.id && trScheda[i].getAttribute("id").substring(0,12)=='rigaversione') { 
      document.getElementById(trScheda[i].getAttribute("id")).style.display='none';      
      }
    }
  }  
  ele.style.display=cssString;
}

function jrCall(cod_listino, selezione_id) {
  document.getElementById('jrResponse').innerHTML='Attendere...';  
  ajaxCall('/?op=jr&cod_listino='+cod_listino+'&selezioni_id='+selezione_id);
  }
