

var iMaxNumberOrdered = 18;                         //Numero massimo di elementi contenuti nel carrello
var iMaxOrderedCookie = 4;
var strCookieName     = 'DynamitNumberOrdered';     //Nome del Cookie che contiene il numero dei prodotti ordinati
var strRootCookieName = 'DynamitOrder';             //Nome della radice del Cookie che contiene il prodotto ordinato
var cartBlink		  = false;

var timeoutID1, intervalID1;
//---------------------------------------------------------------------||
// FUNCTION:    AddToCart                                              ||
// PARAMETERS:  Form Object                                            ||
// RETURNS:     Cookie to user's browser, with prompt                  ||
// PURPOSE:     Adds a product to the user's shopping cart             ||
//---------------------------------------------------------------------||
function AddToCart(thisForm) 
{

    iNumberOrdered = 0;
    iNumberOrdered = GetCookie(strCookieName);
    iNumberOrdered++;

    if ( iNumberOrdered > iMaxNumberOrdered ) {
    
        alert(strCartFull);
        return;
    
    } else {
        
        if (!checkParametres(thisForm.QUANTITY.value, thisForm.ID_ARTICOLO.value)) {
        
            alert(strErrorPrameters);
        
        } else {

            strID_ARTICOLO = thisForm.ID_ARTICOLO.value;
            strQUANTITY = thisForm.QUANTITY.value;

            dbUpdatedOrder = strID_ARTICOLO + "|" + strQUANTITY;
            strNewOrder = strRootCookieName + iNumberOrdered;
            SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
            SetCookie(strCookieName , iNumberOrdered, null, "/");
            alert(strNotice);			
        }
    }
    
    HiddenCartLink(strID_ARTICOLO);

}


//---------------------------------------------------------------------||
// FUNCTION:    LinkAddToCart                                          ||
// PARAMETERS:  Id_Aricolo int, Quantity int                           ||
// RETURNS:     Cookie to user's browser, with prompt                  ||
// PURPOSE:     Adds a product to the user's shopping cart             ||
//---------------------------------------------------------------------||

function doBlink() {
	var blink = document.all.tags("BLINK" )
	for (var i=0; i<blink.length; i++)
	blink[i].style.visibility = blink[i].style.visibility == "" ? "hidden" : ""
}

function startBlink() 
{
	if ( !cartBlink )
	{
		setInterval("doBlink()",480)
		cartBlink = true;
	}
}

			
function LinkAddToCart(iId_Articolo, iQuantity) {
   
    iNumberOrdered = 0;
    iNumberOrdered = GetCookie(strCookieName );
	iNumberOrdered++;
	
   
	//iNumberOrdered  <---- numero di oggetti nel carrello
	
    if (!checkParametres(iQuantity, iId_Articolo)) {
    
        alert(strErrorPrameters);
    
    } else {
   
        if ( iNumberOrdered > iMaxNumberOrdered ) {
            
            alert(strCartFull);
            return;
            
        } else {
        
            strID_ARTICOLO = iId_Articolo;
            strQUANTITY = iQuantity;

            dbUpdatedOrder = strID_ARTICOLO + "|" + strQUANTITY;

            strNewOrder = strRootCookieName + iNumberOrdered;
			
			
			//crea N cookies che hanno nome DynamitOrder1 DynamitOrder2 DynamitOrder3 ecc e dentro ci mette "articolo|quantitą"
			
			/*
			for(i=1; i<=iNumberOrdered; i++)
			{
				contenuto = GetCookie(strRootCookieName +i );
				
				
				
				x=contenuto.split("|");
				if(x[0] == strID_ARTICOLO)
				{
					return;
				}
			}
			*/
			
			SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
			
			//crea un cookie che si chiama DynamitNumberOrdered e dentro ci mette quante cose ci sono nel carrello
            SetCookie(strCookieName , iNumberOrdered, null, "/");
			if(iNumberOrdered != 0)
				startBlink();
			
            alert(strNotice);

			
       }
    }

    
    HiddenCartLink(iId_Articolo);
    
    	// Aggiunta per Italylowcost: reindirizzamento automatico al carrello
		

	
	///////////window.location="index.php?id="+idCarrello; 
    

}


function AddMultiElementsToCart(iId_Articolo, iQuantity) {
   
    iNumberOrdered = 0;
    iNumberOrdered = GetCookie(strCookieName );
    iNumberOrdered++;

    if (!checkParametres(iQuantity, iId_Articolo)) {
        alert(strErrorPrameters);
    } else {
        if ( iNumberOrdered > iMaxNumberOrdered ) {
            alert(strCartFull);
        } else {
            strID_ARTICOLO = iId_Articolo;
            strQUANTITY = iQuantity;
            dbUpdatedOrder = strID_ARTICOLO + "|" + strQUANTITY;
            strNewOrder = strRootCookieName + iNumberOrdered;
            SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
            SetCookie(strCookieName , iNumberOrdered, null, "/");
       }
    }
    
}

//---------------------------------------------------------------------||
// FUNCTION:                                                           ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:                                                            ||
//---------------------------------------------------------------------||
function UpdateOrder(thisForm) 
{
    iNumberOrdered = GetCookie(strCookieName);

    for ( i = 1; i <= iNumberOrdered; i++ ) {
        strCookieOrder = strRootCookieName + i;
        database = "";
        database = GetCookie(strCookieOrder);
        
        aOrder = database.split('|');
        
        if(aOrder[0] == thisForm.ID_ARTICOLO.value) {
                        
            dbUpdatedOrder = aOrder[0] + "|" + thisForm.QUANTITA.value;
            SetCookie(strCookieOrder , dbUpdatedOrder, null, "/"); 
            break;
            
        }
    
    }

}


//---------------------------------------------------------------------||
// FUNCTION:                                                           ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:                                                            ||
//---------------------------------------------------------------------||

function UpdateCart()
{
	
    iNumberOrdered = GetCookie(strCookieName);
	//alert(iNumberOrdered);
    for ( i = 1; i <= iNumberOrdered; i++ ) {
    
        strCookieOrder = strRootCookieName + i;
        database = "";
        database = GetCookie(strCookieOrder);
        aOrder = database.split('|');
        dbUpdatedOrder = aOrder[0] + "|" + document.getElementById(aOrder[0]).value;
        SetCookie(strCookieOrder , dbUpdatedOrder, null, "/"); 
        //document.location.reload();
        
    }

}


//---------------------------------------------------------------------||
// FUNCTION:    getCookieVal                                           ||
// PARAMETERS:  offset                                                 ||
// RETURNS:     URL unescaped Cookie Value                             ||
// PURPOSE:     Get a specific value from a cookie                     ||
//---------------------------------------------------------------------||
function getCookieVal (offset) {
   var endstr = document.cookie.indexOf (";", offset);

   if ( endstr == -1 )
      endstr = document.cookie.length;
   return(unescape(document.cookie.substring(offset, endstr)));
}


//---------------------------------------------------------------------||
// FUNCTION:    FixCookieDate                                          ||
// PARAMETERS:  date                                                   ||
// RETURNS:     date                                                   ||
// PURPOSE:     Fixes cookie date, stores back in date                 ||
//---------------------------------------------------------------------||
function FixCookieDate (date) {
   var base = new Date(0);
   var skew = base.getTime();

   date.setTime (date.getTime() - skew);
}


//---------------------------------------------------------------------||
// FUNCTION:    GetCookie                                              ||
// PARAMETERS:  Name                                                   ||
// RETURNS:     Value in Cookie                                        ||
// PURPOSE:     Retrieves cookie from users browser                    ||
//---------------------------------------------------------------------||
function GetCookie (name) {
   var arg = name + "=";
   var alen = arg.length;
   var clen = document.cookie.length;
   var i = 0;

   while ( i < clen ) {
      var j = i + alen;
      if ( document.cookie.substring(i, j) == arg ) return(getCookieVal (j));
      i = document.cookie.indexOf(" ", i) + 1;
      if ( i == 0 ) break;
   }

   return(null);
}


//---------------------------------------------------------------------||
// FUNCTION:    SetCookie                                              ||
// PARAMETERS:  name, value, expiration date, path, domain, security   ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Stores a cookie in the users browser                   ||
//---------------------------------------------------------------------||
function SetCookie (name,value,expires,path,domain,secure) {
   document.cookie = name + "=" + escape (value) +
                     ((expires) ? "; expires=" + expires.toGMTString() : "") +
                     ((path) ? "; path=" + path : "") +
                     ((domain) ? "; domain=" + domain : "") +
                     ((secure) ? "; secure" : "");
}


//---------------------------------------------------------------------||
// FUNCTION:    DeleteCookie                                           ||
// PARAMETERS:  Cookie name, path, domain                              ||
// RETURNS:     null                                                   ||
// PURPOSE:     Removes a cookie from users browser.                   ||
//---------------------------------------------------------------------||
function DeleteCookie (name,path,domain) {
   if ( GetCookie(name) ) {
      document.cookie = name + "=" +
                        ((path) ? "; path=" + path : "") +
                        ((domain) ? "; domain=" + domain : "") +
                        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
   }
}


function DeleteAllCookies() {

    iNumberOrdered = GetCookie(strCookieName);

    for ( i = 1; i <= iNumberOrdered; i++ ) {

        strCookieOrder = strRootCookieName + i;
        DeleteCookie(strCookieOrder, "/");
    } 

    DeleteCookie(strCookieName, "/");

}

//---------------------------------------------------------------------||
// FUNCTION:    RemoveFromCart                                         ||
// PARAMETERS:  Order Number to Remove                                 ||
// RETURNS:     Null                                                   ||
// PURPOSE:     Removes an item from a users shopping cart             ||
//---------------------------------------------------------------------||
function RemoveFromCart(RemOrder, RemRivista) {

   if ( confirm(strRemoveMsg) ) {
      
       iNumberOrdered = GetCookie(strCookieName);

       for ( i = 1; i <= iNumberOrdered; i++ ) {
       
           strCookieOrder = strRootCookieName + i;
           database = "";
           database = GetCookie(strCookieOrder);
           aOrder = database.split('|');
           
           if(aOrder[0] == RemOrder) {
                DeleteCookie(strCookieOrder, "/");
                break;                       
           }
           
       }      

       for ( index=i; index <= iNumberOrdered; index++ ) {
            NewOrder1 = strRootCookieName + (index+1);
            NewOrder2 = strRootCookieName + (index);
            database = GetCookie(NewOrder1);
            SetCookie (NewOrder2, database, null, "/");
       }
      
       SetCookie (strCookieName , iNumberOrdered-1, null, "/");
       
       //location.href=location.href;
       ajax_update(RemRivista);
   }
   
}


//
// Funzioni accessorie
//

//Controlla il contenuto del carrello per nascondere 
//il link per i prodotti gią inseriti
function CheckCartOnLoad()
{	
	if ( GetCookie(strCookieName) > 0 )
		startBlink();
}


//Controlla il contenuto del carrello per nascondere 
//il link per i prodotti gią inseriti
function CheckCartContent()
{	
	
    for (i=1; i <= GetCookie(strCookieName); i++ ) {
        
        dbOrder = GetCookie(strRootCookieName + i);
        
        arrayOrder = dbOrder.split('|');
        if(document.getElementById('nobuy' + arrayOrder[0]) != null) 
		{
            document.getElementById('nobuy' + arrayOrder[0]).style.display = '';
            document.getElementById('buy' + arrayOrder[0]).style.display = 'none';
			
            document.getElementById('b_nobuy' + arrayOrder[0]).style.display = '';
            document.getElementById('b_buy' + arrayOrder[0]).style.display = 'none';
        
        }    
        
    }
}


function HiddenCartLink(iId_articolo)
{
	
	nb_alto =  document.getElementById('nobuy' + iId_articolo);
	b_alto = document.getElementById('buy' + iId_articolo);
	nb_basso=  document.getElementById('b_nobuy' + iId_articolo);
	b_basso = document.getElementById('b_buy' + iId_articolo);	
	
	if(nb_alto != null)
		nb_alto.style.display = '';
	
	if(b_alto != null)	
		b_alto.style.display = 'none';
		
	if(nb_basso != null)
		nb_basso.style.display = '';
	
	if(b_basso != null)	
		b_basso.style.display = 'none';

}


function checkParametres(quantity, id_articolo)
{

    if (isblank(quantity)) return false;
    if (!checkNumber(quantity)) return false;
    if (isblank(id_articolo)) return false;
    if (!checkNumber(id_articolo)) return false;
    
    return true;

}


//------------------------------------------------//
// FUNCTION:    checkNumber                       //
// Verifica che il parametro sia un numero        //
//------------------------------------------------//
function checkNumber(s) {
    var b;
    var ch=" .-_:/(),;#*'";
    for (i=0;i<s.length;i++) {
        if (s.charAt(i)<'0' || s.charAt(i)>'9') {
            b=true;
            for (j=0;j<ch.length;j++)
            if (s.charAt(i)==ch.charAt(j)) {
                b=false;
                break;
            }
            if (b) return false;
        }
    }
    return true;
}


//--------------------------------------------------------------//
// FUNCTION:    isblank                                         //
// Verifica che il parametro non sia                            // 
// una seria di caratteri vuoti                                 //
//--------------------------------------------------------------//
function isblank(s)
{
    for(var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
    }
    return true;
}

//
// FUNZIONE ORIGINALE
//
////Funzioni per i controlli dei formulari
//function up(which, step) {
//    val = parseFloat(document.getElementById(which).value);
//    newval = val + step;
//    if (newval >= 999) newval = 1;
//    document.getElementById(which).value = newval;
//}

//Funzioni per i controlli dei formulari
function up(which, step, availability) {
    val = parseFloat(document.getElementById(which).value);
    newval = val + step;
    if (newval >= 999) newval = 1;
    if (newval > availability) {
	alert(strQuantita); 
	newval = val;
    }
    document.getElementById(which).value = newval;
    UpdateCart();
    ajax_update();
}


function down(which,step) {
    val = parseFloat(document.getElementById(which).value);
    newval = val - step;
    if (newval <= 0) newval = 1;
    document.getElementById(which).value = newval;
    UpdateCart();
    ajax_update();
}

function ajax_update(idRivista) 
{

	//CheckCartContent();
	if (idRivista==4)
	{		
		callPHP('index.php?id=296', ajax_update_callback, true, 'get');
	} else if (idRivista==2) {
		
		callPHP('index.php?id=815', ajax_update_callback, true, 'get');	
	} else if (idRivista==3) {
		
		callPHP('index.php?id=642', ajax_update_callback, true, 'get');	
	} else if (idRivista==5) {
		
		callPHP('index.php?id=741', ajax_update_callback, true, 'get');	
	}
}

function ajax_update_callback(par) 
{
	//alert(par);
	//pippo=par.split(";");
	//alert(pippo[0]);
	
	document.getElementById("table_cart").innerHTML = par;
	
	
}

function dataOggi()
{
<!-- 
today = new Date()
document.write( today.getDate(), '/', today.getMonth() , '/' , today.getYear() );
// -->
}

