

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 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 LinkAddToCart(iId_Articolo, iQuantity) {
   
    iNumberOrdered = 0;
    iNumberOrdered = GetCookie(strCookieName );
    iNumberOrdered++;
    
    
    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;
            SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
            SetCookie(strCookieName , iNumberOrdered, null, "/");
            alert(strNotice);
       }
    }

    
    HiddenCartLink(iId_Articolo);

    return;

}


//---------------------------------------------------------------------||
// FUNCTION: Prodotti composti da più articoli                         ||
// PARAMETERS:                                                         ||
// RETURNS:                                                            ||
// PURPOSE:                                                            ||
//---------------------------------------------------------------------||

function AddMultiElementsToCart(thisForm) {
   
    iNumberOrdered = 0;
    iNumberOrdered = GetCookie(strCookieName );
    iNumberOrdered++;

    if ( iNumberOrdered > iMaxNumberOrdered ) {
    
        alert(strCartFull);
        return;
    
    } else {

        strID_ARTICOLO_MODELLO      = thisForm.ID_ARTICOLO_MODELLO.value;
        strID_ARTICOLO_MISURA       = thisForm.ID_ARTICOLO_MISURA.value;
        strID_ARTICOLO_PIEDE        = thisForm.ID_ARTICOLO_PIEDE.value;
        strID_ARTICOLO_RIVESTIMENTO = thisForm.ID_ARTICOLO_RIVESTIMENTO.value;

        dbUpdatedOrder = strID_ARTICOLO_MODELLO + "|" + strID_ARTICOLO_MISURA + "|" + strID_ARTICOLO_RIVESTIMENTO + "|" + strID_ARTICOLO_PIEDE;

        strNewOrder = strRootCookieName + iNumberOrdered;


        SetCookie(strNewOrder, dbUpdatedOrder, null, "/");
        SetCookie(strCookieName , iNumberOrdered, null, "/");
        //alert(strNotice);
        
        CheckProductInCart();
    }
}

//---------------------------------------------------------------------||
// 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);

    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, "/"); 
    }

}


//---------------------------------------------------------------------||
// 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) {

   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;
   }
   
}


//
// Funzioni accessorie
//

//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';
        
        }    
        
    }
}


function HiddenCartLink(iId_articolo)
{
    document.getElementById('nobuy' + iId_articolo).style.display = '';
    document.getElementById('buy' + iId_articolo).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;
}


//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;
}


function down(which,step) {
    val = parseFloat(document.getElementById(which).value);
    newval = val - step;
    if (newval <= 0) newval = 1;
    document.getElementById(which).value = newval;
}


//FUnzioni per GimsItalia

//Verifica se il prodotto completo è stato selezionato
function CheckProductInCart()
{

    var objIdArticoloRivestimento = document.getElementById('ID_ARTICOLO_RIVESTIMENTO');
    var objIdArticoloPiede = document.getElementById('ID_ARTICOLO_PIEDE');
    var objIdArticoloMisura = document.getElementById('ID_ARTICOLO_MISURA');
    var objIdArticoloModello = document.getElementById('ID_ARTICOLO_MODELLO');

    if (objIdArticoloRivestimento.value && objIdArticoloPiede.value && objIdArticoloMisura.value && objIdArticoloModello.value) {
        
        iNumberOrdered = GetCookie(strCookieName);

        for ( i = 1; i <= iNumberOrdered; i++ ) {
        
            strCookieOrder = strRootCookieName + i;
            database = GetCookie(strCookieOrder);
            aOrder = database.split('|');
            
            if(aOrder[0] == objIdArticoloModello.value && aOrder[1] == objIdArticoloMisura.value && aOrder[2] == objIdArticoloRivestimento.value && aOrder[3] == objIdArticoloPiede.value) {
                document.getElementById('buy').style.display = 'none';
                document.getElementById('nobuy').style.display = '';
                return;
            }

        }

        document.getElementById('buy').style.display = '';
        document.getElementById('nobuy').style.display = 'none';

    } else {
        document.getElementById('buy').style.display = 'none';
        document.getElementById('nobuy').style.display = 'none';

    }
}

