/**
 * @author Pau
 * Script per a manipular cookies
 **/

function setCookie(name, value) {
    //If name is the empty string, it places a ; at the beginning
    //of document.cookie, causing clearCookies() to malfunction.
    if(name != '')
    document.cookie = name + '=' + value;
}

function setCookieIdioma(name, value, url) {
    //creem la cookie idioma
    setCookie(name,value);
    //refresquem la pàgina
    document.location.href=url;
}

function getCookie(name) {
    //Without this, it will return the first value 
    //in document.cookie when name is the empty string.
    if(name == '')
    return('');
    
    name_index = document.cookie.indexOf(name + '=');
    
    if(name_index == -1)
    return('');
    
    cookie_value =  document.cookie.substr(name_index + name.length + 1, 
                                        document.cookie.length);
    
    //All cookie name-value pairs end with a semi-colon, except the last one.
    end_of_cookie = cookie_value.indexOf(';');
    if(end_of_cookie != -1)
    cookie_value = cookie_value.substr(0, end_of_cookie);
    
    //Restores all the blank spaces.
    space = cookie_value.indexOf('+');
    while(space != -1)
      { 
      cookie_value = cookie_value.substr(0, space) + ' ' + 
      cookie_value.substr(space + 1, cookie_value.length);
    				 
      space = cookie_value.indexOf('+');
      }
    
    return(cookie_value);
}

function clearCookie(name){                  
    expires = new Date();
    expires.setYear(expires.getYear() - 1);
    
    document.cookie = name + '=null' + '; expires=' + expires; 		 
}
         
function clearCookies() {
    Cookies = document.cookie;
    Cookie = Cookies;
    expires = new Date();
    expires.setYear(expires.getYear() - 1);
    
    while(Cookie.length > 0) {
    //All cookie name-value pairs end with a semi-colon, except the last one.
    Cookie = Cookies.substr(0, Cookies.indexOf(';'));
    Cookies = Cookies.substr(Cookies.indexOf(';') + 1, Cookies.length);
    
    if(Cookie != '')
        document.cookie = Cookie + '; expires=' + expires;
    else
        document.cookie = Cookies + '; expires=' + expires;			  			  	  
    }		 		 
}

//rep parametres per get
function getParameter ( queryString, parameterName ) {
   // Add "=" to the parameter name (i.e. parameterName=value)
   var parameterName = parameterName + "=";
   if ( queryString.length > 0 ) {
      // Find the beginning of the string
      begin = queryString.indexOf ( parameterName );
      // If the parameter name is not found, skip it, otherwise return the value
      if ( begin != -1 ) {
         // Add the length (integer) to the beginning
         begin += parameterName.length;
         // Multiple parameters are separated by the "&" sign
         end = queryString.indexOf ( "&" , begin );
      if ( end == -1 ) {
         end = queryString.length
      }
      // Return the string
      return unescape ( queryString.substring ( begin, end ) );
   }
   // Return "null" if no parameter has been found
   return "null";
   }
}