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=='addCartRow') {
    cky_art=xmlDoc.documentElement.getAttribute('insertedCkyArt');
    document.getElementById('cart_arrow'+cky_art).style.display='none';    
    document.getElementById('quantita'+cky_art).value='';    
    }

    if (op=='addEsCartRow') {
    cky_art=xmlDoc.documentElement.getAttribute('insertedCkyArt');
    document.getElementById('cart_arrow'+cky_art).style.display='none';
    document.getElementById('quantita'+cky_art).value='';
    }

    
    if (op=='addPropostaItem') {
    cod_risorsa=xmlDoc.documentElement.getAttribute('insertedCodRisorsa');
    sottoscorta=xmlDoc.documentElement.getAttribute('sottoscorta');
    riordino_max=xmlDoc.documentElement.getAttribute('riordino_max');
    document.getElementById('cart_arrow'+cod_risorsa).style.display='none';    
    document.getElementById('sottoscorta'+cod_risorsa).value=sottoscorta;
    document.getElementById('riordino_max'+cod_risorsa).value=riordino_max;
    document.getElementById('quantita_proprie'+cod_risorsa).value=quantita_proprie;
    } 
    
    if (op=='delPropostaItem') {
    cod_risorsa=xmlDoc.documentElement.getAttribute('deletedCodRisorsa');
    document.getElementById('cart_arrow'+cod_risorsa).style.display='none';    
    document.getElementById('sottoscorta'+cod_risorsa).value=0;    
    }       
    
    
    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' }          
    
          //ckyart
          cartRowCell = document.createElement('td');
          txt = document.createTextNode(x[i].getAttribute('cky_art'));          
          cartRowCell.appendChild(txt)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }            
          cartRow.appendChild(cartRowCell)          

          
          
          
          //descrizione
          cartRowCell = document.createElement('td');
          txt = document.createTextNode(x[i].getAttribute('carrello_articoli_descrizione'));          
          cartRowCell.appendChild(txt)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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);";
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellButton.setAttribute('className','piccolo');                   
            cartRowCellButton.onclick=new Function(saveFunctionText);
            } else {               
            cartRowCellButton.setAttribute('class','piccolo');
            cartRowCellButton.setAttribute('onclick',saveFunctionText); 
            }                 
          cartRowCellButton.setAttribute('type','button');
          cartRowCell.appendChild(cartRowCellButton)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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','disattiva');
            cartRowCellButton.onclick=new Function(deleteFunctionText);                   
            } else {               
            cartRowCellButton.setAttribute('class','disattiva');
            cartRowCellButton.setAttribute('onclick',deleteFunctionText); 
            }                    
          cartRowCellButton.setAttribute('type','button');       
          
          cartRowCell.appendChild(cartRowCellButton)
          cartRow.appendChild(cartRowCell);
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                              
          
          cartTbody.appendChild(cartRow);
          }
        } 
   
      totalCart=xmlDoc.documentElement.getAttribute('total');
         
      document.getElementById('totalCart').innerHTML=totalCart
               
    }

 if (op=='saveEsCartRow' || op=='delEsCartRow') {   
    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' }          
    
          //descrizione
          cartRowCell = document.createElement('td');
          txt = document.createTextNode(x[i].getAttribute('carrello_articoli_descrizione'));          
          cartRowCell.appendChild(txt)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          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)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          cartRow.appendChild(cartRowCell)
          
          //totale
          cartRowCell = document.createElement('td');
          cartRowCell.setAttribute('align','right');
          rowTotal=x[i].getAttribute('carrello_totale_riga')
          txt = document.createTextNode(rowTotal);          
          cartRowCell.appendChild(txt)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          cartRow.appendChild(cartRowCell)
          
          
          //salva
          cartRowCell = document.createElement('td');
          cartRowCellButton = document.createElement('button');
          
          saveFunctionText="saveEsCartRow('"+x[i].getAttribute('cky_art')+"', document.getElementById('"+x[i].getAttribute('cky_art')+"_quantity').value);";
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellButton.setAttribute('className','piccolo');                   
            cartRowCellButton.onclick=new Function(saveFunctionText);
            } else {               
            cartRowCellButton.setAttribute('class','piccolo');
            cartRowCellButton.setAttribute('onclick',saveFunctionText); 
            }                 
          cartRowCellButton.setAttribute('type','button');
          cartRowCell.appendChild(cartRowCellButton)
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                    
          cartRow.appendChild(cartRowCell);
          
          //delete
          cartRowCell = document.createElement('td');
          cartRowCellButton = document.createElement('button');
          
          deleteFunctionText="delEsCartRow('"+x[i].getAttribute('cky_art')+"')";
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCellButton.setAttribute('className','disattiva');
            cartRowCellButton.onclick=new Function(deleteFunctionText);                   
            } else {               
            cartRowCellButton.setAttribute('class','disattiva');
            cartRowCellButton.setAttribute('onclick',deleteFunctionText); 
            }                    
          cartRowCellButton.setAttribute('type','button');       
          
          cartRowCell.appendChild(cartRowCellButton)
          cartRow.appendChild(cartRowCell);
          if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
            cartRowCell.className=rowColor;   
            } else {                    
            cartRowCell.setAttribute('class',rowColor);
            }                              
          
          cartTbody.appendChild(cartRow);
          }
        } 
   
      totalCart=xmlDoc.documentElement.getAttribute('total');
         
      document.getElementById('totalCart').innerHTML=totalCart
               
    }


    } else {
    alert("Problem retrieving XML data")
    }
  }
}


function addPropostaItem(cod_risorsa, sottoscorta, riordino_max, quantita_proprie, stato) {
  sottoscorta=parseInt(sottoscorta);
  riordino_max=parseInt(riordino_max);
  quantita_proprie=parseInt(quantita_proprie);
  
  if (stato==2) {
    if (confirm('Confermare ATTIVAZIONE dell\'articolo '+cod_risorsa+' E RELATIVA NOTIFICA A SACEL SRL?')) {
      if (sottoscorta<1 || riordino_max<1 ) { 
        alert('Occorre specificare le quantità di riordino min e max!');
        return false;
        } else if (sottoscorta > riordino_max ) { 
        alert('Il valore del campo RIORDINO MIN deve essere inferiore al valore del campo RIORDINO MAX!');
        return false;
        } else if (quantita_proprie < 0 ) { 
        alert('Il valore del campo DISPONIBILITÀ BARILLA deve essere >= a 0!');
        return false;        
        }  else {
        document.getElementById('cart_arrow'+cod_risorsa).style.display='inline';
        url='/?op=addPropostaItem&cod_risorsa='+cod_risorsa+'&sottoscorta='+sottoscorta+'&riordino_max='+riordino_max+'&quantita_proprie='+quantita_proprie;
        ajaxCall(url);
        }
      }
    } else {
      if (sottoscorta<1 || riordino_max<1 ) { 
        alert('Occorre specificare le quantità di riordino min e max!');
        return false;
        } else if (sottoscorta > riordino_max ) { 
        alert('Il valore del campo RIORDINO MIN deve essere inferiore al valore del campo RIORDINO MAX!'+sottoscorta+' '+riordino_max);
        return false; 
        } else if (quantita_proprie < 0 ) { 
        alert('Il valore del campo DISPONIBILITÀ BARILLA deve essere >= a 0!');
        return false;               
        } else {
        document.getElementById('cart_arrow'+cod_risorsa).style.display='inline';
        url='/?op=addPropostaItem&cod_risorsa='+cod_risorsa+'&sottoscorta='+sottoscorta+'&riordino_max='+riordino_max+'&quantita_proprie='+quantita_proprie;
        ajaxCall(url);        
        }
      }
  
}

function delPropostaItem(cod_risorsa) {
  if (confirm('Confermare DISATTIVAZIONE articolo '+cod_risorsa+'  E RELATIVA NOTIFICA A SACEL SRL?')) {
    document.getElementById('cart_arrow'+cod_risorsa).style.display='inline';
    url='/?op=delPropostaItem&cod_risorsa='+cod_risorsa;
    ajaxCall(url);
    }
}


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 addEsCartRow(cky_art, quantity) {
  if (!quantity) {
  alert('Occorre specificare una quantita!');
  return false;
  }

  if (quantity<0) {
  alert('Occorre specificare una quantita maggiore di 0!');
  return false;
  }

  document.getElementById('cart_arrow'+cky_art).style.display='inline';
  url='/?op=addEsCartRow&cky_art='+cky_art+'&quantity='+quantity;
  ajaxCall(url);
}



function delCartRow(cky_art) {
  cart = document.getElementById('cartTable');
  cartTbody=cart.getElementsByTagName('tbody')[0]
  cartTbody.parentNode.removeChild(cartTbody);  
  cartTbody = document.createElement('tbody');
  cart.appendChild(cartTbody);  
  url='/?op=delCartRow&cky_art='+cky_art;
  ajaxCall(url);
}

function delEsCartRow(cky_art) {
	cart = document.getElementById('cartTable');
	cartTbody=cart.getElementsByTagName('tbody')[0]
	cartTbody.parentNode.removeChild(cartTbody);
	cartTbody = document.createElement('tbody');
	cart.appendChild(cartTbody);
	document.getElementById('totalCart').innerHTML='Aggiornamento in corso...';
	url='/?op=delEsCartRow&cky_art='+cky_art;
	ajaxCall(url);
	}


function saveCartRow(cky_art, quantity) {
  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&cky_art='+cky_art+'&quantity='+quantity; 
    ajaxCall(url); 
    } else {
    alert('Per modificare la quantità occorre specificare un valore maggiore di 0.')
    }
}

function saveEsCartRow(cky_art, quantity) {
	if (quantity>0) {
		cart = document.getElementById('cartTable');
		cartTbody=cart.getElementsByTagName('tbody')[0]
		cartTbody.parentNode.removeChild(cartTbody);
		cartTbody = document.createElement('tbody');
		cart.appendChild(cartTbody);
		document.getElementById('totalCart').innerHTML='Aggiornamento in corso...';	
		url='/?op=saveEsCartRow&cky_art='+cky_art+'&quantity='+quantity;
		ajaxCall(url);
		} else {
		alert('Per modificare la quantita occorre specificare un valore maggiore di 0.')
		}
	}




function chiudiDettagli() {
  var catalogo = document.getElementById('catalogo');
  
  if (window.navigator.userAgent.indexOf('MSIE')!=-1) {
  tbodyCatalogo=catalogo.all.tags('TBODY');
  cssString='block';
  } else {
  tbodyCatalogo=catalogo.getElementsByTagName('tbody');
  cssString='table-row';
  }  
   
  for(i=0; i < tbodyCatalogo.length; i++) {
  alert(i)
    if (tbodyCatalogo[i].getAttribute("id").substring(0,5)=='tbody') { 
      document.getElementById(tbodyCatalogo[i].getAttribute("id")).style.display='none';      
      }
  }  
  
}

function closeDettagli() {
  trPagina=document.getElementsByTagName('tr');
  for(i = 0; i < trPagina.length; i++) {
    if(trPagina[i].getAttribute('id') == 'trDettagli') {      
      trPagina[i].parentNode.removeChild(trPagina[i]);
      }
    }   
  }

function viewDettagli(cky_art) {
    closeDettagli();
    
    
    var trNode = document.createElement('tr');
    var tdImmagineNode = document.createElement('td');
    var tdDescrizioneNode = document.createElement('td');    
    var imgNode = document.createElement('img');
    var aNode = document.createElement('a');
    var buttonNode = document.createElement('button');
    

    trNode.setAttribute('id','trDettagli');
    trNode.setAttribute('valign','top');
    trNode.setAttribute('style','font-size:11px;line-height:12px');
    
    
    aNode.setAttribute('href','img/'+cky_art+'.detail.jpg');
    aNode.setAttribute('rel','lightbox');
    aNode.setAttribute('title',cky_art);
    
    imgNode.setAttribute('src','/img/'+cky_art+'.jpg');
    imgNode.setAttribute('style','margin:10px 10px 10px 0');
    imgNode.setAttribute('alt','articolo '+cky_art);
    
    
    closeDettagliText="closeDettagli();";
    
    
        
           
    aNode.appendChild(imgNode);
    tdImmagineNode.appendChild(aNode);
    
    descrizione=document.getElementById('descrizione'+cky_art).childNodes[0].cloneNode(true);
    tdDescrizioneNode.appendChild(descrizione);

    
    if (window.navigator.userAgent.indexOf('MSIE')!=-1) {             
      tdDescrizioneNode.colSpan='7';
      buttonNode.onclick=new Function(closeDettagliText);
      } else {      
      tdDescrizioneNode.setAttribute('colspan','7');     
      buttonNode.setAttribute('onClick', closeDettagliText);                
      }
      
    txt = document.createTextNode('chiudi');
    buttonNode.appendChild(txt);      
    tdDescrizioneNode.appendChild(buttonNode);             
     
    trNode.appendChild(tdImmagineNode);
    trNode.appendChild(tdDescrizioneNode);    
    tbart=document.getElementById('tbody'+cky_art);
    tbart.appendChild(trNode);
    
    //ho ripreso questa porzione di codice dal costruttore del lightbox
    //con l'aggiornamento della DOM vanno loopati i TAG con referenza LIGHTBOX per l'aggiunta degli eventi
    //********************INIZIO    
		if (!document.getElementsByTagName){ return; }
		var anchors = document.getElementsByTagName('a');

		// loop through all anchor tags
		for (var i=0; i<anchors.length; i++){
			var anchor = anchors[i];
			
			var relAttribute = String(anchor.getAttribute('rel'));
			
			// use the string.match() method to catch 'lightbox' references in the rel attribute
			if (anchor.getAttribute('href') && (relAttribute.toLowerCase().match('lightbox'))){
				anchor.onclick = function () {myLightbox.start(this); return false;}
			}
		}    
		//********************FINE
     
    }    
