//
// Variabili generali
//
var max_iteration = 1000; // il massimo numero possibile come indice dell'array
// ---------------------------
// eventi carrello

// funzione per il controllo dell'input inserito
//
function noNumbers(e){
    var keynum;
    var keychar;
    var numcheck;
    
    if (window.event) {
        keynum = e.keyCode;
    }
    else 
        if (e.which) {
            keynum = e.which;
        }
    keychar = String.fromCharCode(keynum);
    ret_v = false;
    if (keynum == 8) {
        ret_v = true;
    }
    if (keynum >= 48 && keynum <= 57) {
        ret_v = true;
    }
    return ret_v;
}

// update del carrello
function cart_update_qta(ident, position){
    qta = document.getElementById("qta_" + ident);
    prezzo = document.getElementById("prezzo_" + ident);
       
    // aggiorno il cookie
    updateCookie('qta', qta.value, position);
    updateCookie('importo', qta.value * parseInt(prezzo.innerHTML), position);
	
	setTimeout(window.location.reload(), 1);
}

function delete_cart(id, posizione){
	if(confirm("Eliminare questo elemento")){
		deleteCookie('id', posizione);
	    deleteCookie('qta', posizione);
	    deleteCookie('prezzo', posizione);
		
		setTimeout(window.location.reload(), 1);
		return true;
	}
	return false;
}
// ---------------------------

//
// Inserisce una voce nel carrello 
// 
function addCart(id, qta, prezzo_unitario){
    prezzo = prezzo_unitario; // se trovo il prezzo scontato uso quello
    // controllo se esiste il cookie, in base all'id
    // se esiste allora ritorno il valore dell'id altrimenti ritorno 0
    ident = cookie_exist('id', id);
	posizione = getCookieLength("id");

    if (ident < 0) {
        addCookie('id', id, posizione);
        addCookie('qta', qta, posizione);
        addCookie('prezzo', prezzo, posizione);
    }
    else {
        qta_n = parseInt(getCookie('qta', ident)) + parseInt(qta);
        prezzo = parseInt(getCookie('prezzo', ident));
        updateCookie('qta', qta_n, ident);
    }
    
}

function addInputCart(id, qta_input, prezzo){
    qta = document.getElementById(qta_input).value; // prendo il valore dell'input
    addCart(id, qta, prezzo); // aggiungo al carrello
    document.getElementById(qta_input).value = 1; // ritorno al valore normale
    //alert('Il prodotto \n e\' stato aggiunto al carrello.');
    var host = location.hostname;
    var path = location.pathname;
    lingua = path.split("/");
    var v_http = "http://" + host + "/" +lingua[1] + "/carrello-step1/carrello-step1.html";
	window.location.href = v_http;
    //setTimeout(window.location.reload(), 1);
}

//
// Inserisce un cookie
// @name - il nome del cookie (la posizione � automatica)
// @value - il valore
//
function cookie_exist(name, valore){
    var ident = -1;
    var data = "";
    
    for (i = 0; i <= 1000; i++) {
    	data = getCookie(name, i);
        if (data == valore){
        	ident = i;
            break;
        }	
        if (data == null) 
            break;
    }
    return ident;
}


//
// Inserisce un cookie
// @name - il nome del cookie (la posizione � automatica)
// @value - il valore
//
function addCookie(name, value, posizione){
    var expiredays = 1;
    var expires = new Date();
    var index = posizione; //getCookieLength(name); // identificativo posizione
    expires.setDate(expires.getDate() + expiredays);
    
    // creo la stringa cookie
    var cookieString = 'cart[' + name + '][' + index + ']' + "=" + escape(value) + ((expires) ? ";expires=" + expires.toGMTString() : "") + "; path=/";
    document.cookie = cookieString;
    
}

//
// modifica il valore di un cookie
// @name - il nome del cookie (la posizione � automatica)
// @value - il valore
//
function updateCookie(name, value, position){
    var expiredays = 1;
    var expires = new Date();
    expires.setDate(expires.getDate() + expiredays);
    
    // creo la stringa cookie
    var cookieString = 'cart[' + name + '][' + position + ']' + "=" + escape(value) + ((expires) ? ";expires=" + expires.toGMTString() : "") + "; path=/";
    document.cookie = cookieString;
}


//
// modifica il valore di un cookie
// @name - il nome del cookie (la posizione è automatica)
// @value - il valore
//
function deleteCookie(name, position){
	
    var expires = new Date();
    
    // creo la stringa cookie
    var cookieString = 'cart[' + name + '][' + position + ']' + "=;expires=Thu, 01-Jan-1970 00:00:01 GMT; path=/";
	document.cookie = cookieString;
}


//
// Chiama un cookie
// @name - il nome del cookie
// @position - la posizione del cookie
//
// return: il valore del cookie
//
function getCookie(name, position){
    var string_cookie = 'cart[' + name + '][' + position + ']';
    var start = document.cookie.indexOf(string_cookie + "=");
    var len = start + string_cookie.length + 1;
    if ((!start) && (string_cookie != document.cookie.substring(0, string_cookie.length))) 
        return (null);
    if (start == -1) 
        return (null);
    var end = document.cookie.indexOf(";", len);
    if (end == -1) 
        end = document.cookie.length;
    return (unescape(document.cookie.substring(len, end)));
}

//
// Ritorna la lunghezza dell'array 
// @name - nome dell'array
//
// return: lunghezza dell'array
//
function getCookieLength(name){
    var lunghezza = 0;
    var data = "";
    
    for (i = 0; i <= 1000; i++) {
        data = getCookie(name, i);
		
        if (data == null) 
            break;
        else 
            lunghezza++;
    }
    return lunghezza;
}

//
// Ritorna il totale della somma dell'array 
// @name - nome dell'array
//
// return: lunghezza dell'array
//
function getTotalCookie(name){
    var totale = 0;
    var data = "";
    
    for (i = 0; i <= max_iteration; i++) {
        data = getCookie(name, i);
        if (data == null) 
            break;
        else 
            totale += parseFloat(data);
    }
    
    return totale;
}
