/**
 * functions that can be useful in more contexts
 * 
 * $RCSfile: utility_funcs.js,v $
 * $Revision: 1.2.2.4 $
 * $Date: 2009-11-06 10:46:05 $
 * $Author: bellofiore $
 */

// Removes leading and trailing spaces from the passed string. Also removes
// consecutive spaces and replaces it with one space. If something besides
// a string is passed in (null, custom object, etc.) then return the input.
function trim(inputString)
{
   if (typeof inputString != "string") { return inputString; }
   var retValue = inputString;
   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


// ------------------------------------------------------------------------------------------------------ //

// Set a cookie sName = sValue that expires in sSecs seconds
// sSecs can be a negative number ( to delete a cookie )
// cookie path is set to "/"
// secure cookie flag is not set
function setVenereCookie(sName, sValue, sSecs )
{
   var date    = new Date();
   var sCookie =  sName + "=" + ( sValue==null ? "" : sValue);
   if( sSecs != null ) {
      date.setSeconds( sSecs );
      sCookie += "; expires=" + date.toGMTString();    
   }
   sCookie += "; path=/"; //force path to be the root
   var hostname = document.location.hostname.toLowerCase();
   var idxVenereHost = hostname.indexOf("venere"); // tenere conto di viaggi.fab.io lastampa etc...
   var cookieDomain = "."+hostname.substr(idxVenereHost);
   sCookie += "; domain=" + cookieDomain;
   document.cookie = sCookie; 
}
// ------------------------------------------------------------------------------------------------------ //



// ------------------------------------------------------------------------------------------------------ //

/**
* str_replace
*
* This function returns a string or an array with all occurrences of
* [search] in [subject] replaced with the given [replace] value.
* If you don?t need fancy replacing rules (like regular expressions), you should always use this function.
*
* @param string search
* @param string replace
* @param string string
*/
function str_replace (search, replace, subject)
{
   var result = "";
   var oldi = 0;
   for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i))
   {
      result += subject.substring (oldi, i);
      result += replace;
      i += search.length;
      oldi = i;
   }
   return result + subject.substring (oldi, subject.length);
}
// ------------------------------------------------------------------------------------------------------ //




// ------------------------------------------------------------------------------------------------------ //
/*
* Get cookie value if exists. Function is case sensitive, hence 'example' or 'Example' cookie name are 
* different cookies
*
* @param string      the cookie name
*/
function readCookie(cookieName) {
   var cookieContent = new String();
   var documentCookies = document.cookie;
   if(documentCookies.length > 0) {
      var cookie = cookieName + '=';
      var cookieBegin = documentCookies.indexOf(cookie);
      var cookieEnd = 0;
      if(cookieBegin > -1) {
         cookieBegin += cookie.length;
         cookieEnd = documentCookies.indexOf(";",cookieBegin);
         if(cookieEnd < cookieBegin) { cookieEnd = documentCookies.length; }
            cookieContent = documentCookies.substring(cookieBegin,cookieEnd);
      }
   }
   
   return unescape(cookieContent);
} 
// ------------------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------------------ //
/*
* Set user preference cookie value if exists.
*
* now is only a wrapper to setVenereCookie but,
* in the future there will be a user_preferences cookie that contains
* all information
*
* @param pref_name (string) the name of the preference to be set
* @param value (string)     the value of the preference to be set
*
*/
function setUserPreference(pref_name, value) {
   return setVenereCookie(pref_name, value); // no seconds -> end of session
}
// ------------------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------------------ //
/*
* Get user preference cookie value if exists.
*
* now is only a wrapper to readCookie but,
* in the future there will be a user_preferences cookie that contains
* all information
*
*/
function readUserPreference(pref_name) {
   return readCookie(pref_name);
}
// ------------------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------------------ //
/*
* Get "pref_currency" cookie value if exists.
*
*/
function readUserPreferredCurrency() {
   return readUserPreference("pref_currency");
}
// ------------------------------------------------------------------------------------------------------ //

// ------------------------------------------------------------------------------------------------------ //
/*
* set "pref_currency" cookie value.
*
* @param value (string) the value of the cookie
*/
function setUserPreferredCurrency(value){
  setUserPreference("pref_currency", value);
}
// ------------------------------------------------------------------------------------------------------ //


// global var to trace if this file has already been loaded
utility_funcs_loaded = true;