function search(loginDataPassed, showCartFlagPassed, languageIndexPassed, enciclopaediaPassed) {
   // loginDataPassed: variabile di sessione (login e sessione)
  // alert(loginDataPassed);
  // alert(showCartFlagPassed);
  // alert(languageIndexPassed);
   keywordsToSearch = document.form.keywords.value;
   typeOfSearch = "LBR";
   nuOb = document.form.nuOb.options[document.form.nuOb.selectedIndex].value;
   scale = document.form.scale.options[document.form.scale.selectedIndex].value;
   order = document.form.order.selectedIndex;
   resultsPerPage = document.form.resultsPerPage.options[document.form.resultsPerPage.selectedIndex].value;
   currentPage = "1"; // quando faccio una nuova ricerca imposto sempre la prima pagina di visualizzazione
   /*
   if (document.form.enciclopaediaCheck.checked == true) {
      enciclopaedia = "1";
   } else {
      enciclopaedia = "0";
   }
   */
   searchParameters = keywordsToSearch + "<;>" + typeOfSearch + "-" + nuOb + "-" + scale + "-" + order + "-" + resultsPerPage + "-" + currentPage + "-" + enciclopaediaPassed;
   //alert(searchParameters);
   
   /*
   document.form.searchParameters.value = searchParameters;
   document.form.loginData.value = loginDataPassed;
   // Quando faccio una nuova ricerca annullo tutto quanto relativo alle colonne laterali
   document.form.rightColumnAction.value = '';
   document.form.leftColumnAction.value = '';
   document.form.showCartFlag.value = showCartFlagPassed;
   document.form.languageIndex.value = languageIndexPassed;
   document.form.submit();
   */
   
   
   //newLocation = "start.php?" + encode64(searchParameters + "<:>" + decode64(loginDataPassed) + "<:><:><:>" + showCartFlagPassed + "<:>" + languageIndexPassed);
   newLocation = "start.php?" + encode64(searchParameters + "<:>" + decode64(loginDataPassed) + "<:><:><:>" + showCartFlagPassed + "<:>" + languageIndexPassed);
   
   document.location = newLocation;
   
   
}


function startSearch(loginDataPassed, showCartFlagPassed, languageIndexPassed, enciclopaediaPassed) {
   // Fa parire la ricerca dalla pressione del tasto INVIO sulla text
   if (window.event.keyCode == 13) {
      search(loginDataPassed, showCartFlagPassed, languageIndexPassed, enciclopaediaPassed);
   }
}


function login(searchParametersPassed, sessionIdPassed, rightColumnActionPassed, leftColumnActionPassed, showCartFlagPassed, languageIndexPassed) {
   // Funzione che effettua il login
   
   document.form.searchParameters.value = decode64(searchParametersPassed);
   //***************************************************************************
   // Costruisco il parametro per forzare il login
   email = document.form.LOGIN.value;
   password = document.form.password.value;
   document.form.loginData.value = email + "<,>" + password + "<;>" + sessionIdPassed;
   //***************************************************************************
   document.form.rightColumnAction.value = rightColumnActionPassed;
   document.form.leftColumnAction.value = leftColumnActionPassed;
   document.form.showCartFlag.value = showCartFlagPassed;
   document.form.languageIndex.value = languageIndexPassed;
   
   document.form.submit();
}

function logout(searchParametersPassed, sessionIdPassed, rightColumnActionPassed, leftColumnActionPassed, showCartFlagPassed, languageIndexPassed) {
   // Funzione che effettua il logout annullando il valore dell'id dell'utente
   
   //newLocation = "start.php?" + searchParametersPassed + "<:><;>" + sessionIdPassed + "<:>" + rightColumnActionPassed + "<:>" + leftColumnActionPassed + "<:>" + showCartFlagPassed + "<:>" + languageIndexPassed;
   newLocation = "start.php?" + searchParametersPassed + "<:><;>" + sessionIdPassed + "<:>" + rightColumnActionPassed + "<:>" + leftColumnActionPassed + "<:>" + showCartFlagPassed + "<:>" + languageIndexPassed;
   document.location = newLocation;
   
}


function changeLocation(locationPassed) {
   
   document.location = locationPassed;
   
}

function addToCart(searchParameters, userId, session, rightColumnAction, leftColumnAction, cartAction, languageIndex) {
   // Aggiunge un nuovo articolo al carrello della spesa
   if (session == '') {
      // Se la sessione è nulla llora ne impongo la creazione
      session = "create";
   }
   
   
   //newLocation = "start.php?" + encode64(decode64(searchParameters) + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction + "<:>" + languageIndex);
   newLocation = "start.php?" + encode64(decode64(searchParameters) + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction + "<:>" + languageIndex);
   
   document.location = newLocation;
   
   
}

function removeFromCart(searchParameters, userId, session, rightColumnAction, leftColumnAction, cartAction, languageIndex) {
   // Rimuove un articolo dal carrello della spesa
   
   //newLocation = "start.php?" + encode64(searchParameters + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction + "<:>" + languageIndex);
   newLocation = "start.php?" + encode64(searchParameters + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction + "<:>" + languageIndex);
   
   document.location = newLocation;
   
   
}

function checkEnciclopaedia() {
   // Seleziona la checkbox relativa all'enciclopedia
   
   if (document.form.enciclopaediaCheck.checked == true) {
      document.form.enciclopaediaCheck.checked = false;
   } else {
      document.form.enciclopaediaCheck.checked = true;
   }
   
}

function changeTrBackground(trId, color){
   // modifica il colore di sfondo del TR in questione
   document.getElementById(trId).style.backgroundColor = color;
}





/*
function increaseQuantity(searchParameters, userId, session, rightColumnAction, leftColumnAction, cartAction) {
   // Rimuove un articolo dal carrello della spesa
   
   newLocation = "TEST2/start.php?" + searchParameters + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction;
   
   document.location = newLocation;
   
   
}


function decreaseQuantity(searchParameters, userId, session, rightColumnAction, leftColumnAction, cartAction) {
   // Rimuove un articolo dal carrello della spesa
   
   newLocation = "TEST2/start.php?" + searchParameters + "<:>" + userId + "<;>" + session + "<:>" + rightColumnAction + "<:>" + leftColumnAction + "<:>" + cartAction;
   
   document.location = newLocation;
   
   
}
*/



function halloWorld() {
   
   alert('Hello world');
   
   base64 = encode64("ciao ciao ciao");
   
   alert('codificato: ' + base64);
   
   
   decodificato = decode64(base64);
   
   alert('decodificato: ' + decodificato);
   
   // Codifico tutti i paramteri in base64 e poi lo metto nell'url da richiamare
   
   
   
   //document.location = "http://www.google.it?qweqwe=" + document.form.keywords.value;
   
   
}


   var keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";

   function checkIt(origText, userText, encode) {
      if (encode) {
         if (encode64(origText) == userText) {
            alert("You encoded the phrase: \"" + origText + "\" correctly!!");
         } else {
            alert("You did not encode the phrase: \"" + origText + "\" correctly.\nPlease try again.");
         }
      } else {
         if (decode64(origText) == userText) {
            alert("You decoded the base64: \"" + origText + "\" correctly!!");
         } else {
            alert("You did not decode the base64: \"" + origText + "\" correctly.\nPlease try again.");
         }
      }
   }

   function encode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      do {
         chr1 = input.charCodeAt(i++);
         chr2 = input.charCodeAt(i++);
         chr3 = input.charCodeAt(i++);

         enc1 = chr1 >> 2;
         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
         enc4 = chr3 & 63;

         if (isNaN(chr2)) {
            enc3 = enc4 = 64;
         } else if (isNaN(chr3)) {
            enc4 = 64;
         }

         output = output + 
            keyStr.charAt(enc1) + 
            keyStr.charAt(enc2) + 
            keyStr.charAt(enc3) + 
            keyStr.charAt(enc4);
         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";
      } while (i < input.length);

      return output;
   }

   function decode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
      var base64test = /[^A-Za-z0-9\+\/\=]/g;
      if (base64test.exec(input)) {
         alert("There were invalid base64 characters in the input text.\n" +
               "Valid base64 characters are A-Z, a-z, 0-9, '+', '/', and '='\n" +
               "Expect errors in decoding.");
      }
      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

      do {
         enc1 = keyStr.indexOf(input.charAt(i++));
         enc2 = keyStr.indexOf(input.charAt(i++));
         enc3 = keyStr.indexOf(input.charAt(i++));
         enc4 = keyStr.indexOf(input.charAt(i++));

         chr1 = (enc1 << 2) | (enc2 >> 4);
         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
         chr3 = ((enc3 & 3) << 6) | enc4;

         output = output + String.fromCharCode(chr1);

         if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
         }
         if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
         }

         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";

      } while (i < input.length);

      return output;
   }

