//aggiungi a carrello Ajax
var idCarrello


function acquistaAj(id){
  var url = '/EC/lib/addCarrello.asp';

  var params = 'idp=' + id;
	
	new Ajax.Request(url,
	{
		method: 'post',
		postBody:params,
		onComplete: function(req) { 
			//alert(req.responseText)
		  if(req.responseText != ''){
				idCarrello = req.responseText;
				goCarrello();
			};
		}
	});
}



function addCarrelloAj(id){	    			    
  var url = '/EC/lib/addCarrello.asp';

  
  //window.open('/EC/lib/addCarrello.asp?idp='+ id);
  
  var params = 'idp=' + id;
	
	new Ajax.Request(url,
	{
		method: 'post',
		postBody:params,
		onComplete: function(req) { 
			//alert(req.responseText)
		  if(req.responseText != ''){
				idCarrello = req.responseText;
				ricalcolaCarrelloSB();
				//alert('Prodotto aggiunto al carrello');
			};
		}
	});
}


function goCarrello(){
	document.location = '/EC/carrello.asp';
}


function ricalcolaCarrelloAj(){	  
	showAttendere();
  			    
  var url = '/EC/lib/aggiornaCarrello.asp';

  var params = $('mCarrello').serialize();
	
	//$('test').value=url+'?'+params;return;

	new Ajax.Request(url,
	{
		method: 'post',
		postBody:params,
		onComplete: function(req) { 
			//alert(req.responseText)
		  if(req.responseText != ''){
				idCarrello = req.responseText;
				if(IsNumber(idCarrello)){
					refreshCarrello(idCarrello);
					hideAttendere();
				}else{
					refreshCarrelloErr(idCarrello);
					hideAttendere();
				}
			};
		}
	});

}




function refreshCarrelloErr(idCarrello){	    			    
	$('contentCarrello').update('Spiacenti<br>Si è verificato un errore. Si prega di riprovare.<br><br>' + idCarrello);
	$('contentCarrello').innerHTML;
}


function refreshCarrello(idCarrello){	    			    
  var url = '/EC/lib/refreshCarrello.asp';

  var params = 'id=' + idCarrello;
	
	new Ajax.Request(url,
	{
		method: 'post',
		postBody:params,
		onComplete: function(req) { 
			//alert(req.responseText)
		  if(req.responseText != ''){
				$('contentCarrello').update(req.responseText);
				$('contentCarrello').innerHTML;
			};
		}
	});
}



function disabledProsegui(){
	$('bProsegui').disabled=true;
	$('bRicalcola').disabled=false;

	$('dSQ').update('0,00');
	$('dSQ').innerHTML;	
	$('dTotale').update('0,00');
	$('dTotale').innerHTML;
	$('dTotaleDovutoCarrello').update('0,00');
	$('dTotaleDovutoCarrello').innerHTML;
	$('dTotaleAltriSconti').update('0,00');
	$('dTotaleAltriSconti').innerHTML;	
	$('dTotaleSconti').update('0,00');
	$('dTotaleSconti').innerHTML;	
	$('SpeseSpedizione').update('0,00');
	$('SpeseSpedizione').innerHTML;		
	
	var myChilds = document.getElementsByClassName('dTotalePr');
	for (var i=0; i<myChilds.length; i++) {
		myChilds[i].update('0,00');
		myChilds[i].innerHTML;
	}	
}



function verDest(objF){
	var v = $F(objF);
	
	if(v=='-'){
				alert('Ci scusiamo ma al momento non siamo in grado di gestire automaticamente gli ordini da paesi extra-europei, la preghiamo di contattarci via mail, fax o telefono per poter soddisfare le Sue esigenze.');
				disabledProsegui();
	}else{	
		disabledProsegui();
	}
}


function fProsegui(){
	var objForm = $('mCarrello').TipoPagamento;
	var v='';

	for(var i=0; i<3; i++){
		if(objForm[i].checked){
			v = objForm[i].value;
			i=3;
		}
	}

	if(v!=''){
		document.location.href = "info.asp?p=" + v;
	}
}


function IsNumber(Expression){
    Expression = Expression.toLowerCase();
    RefString = "0123456789 ";
    
    if (Expression.length < 1) 
        return (false);
    
    for (var i = 0; i < Expression.length; i++) 
    {
        var ch = Expression.substr(i, 1);
        var a = RefString.indexOf(ch, 0);
        if (a == -1)
            return (false);
    }
    return(true);
}


// vis DIV Attendere.....
function showAttendere(){
	$('overlay').show();
	$('lightbox').show();
}

// nasconde DIV Attendere.....
function hideAttendere(){
	$('overlay').hide();
	$('lightbox').hide();
}


