Cookie = {
  
  /*
  Function: create
    creates a new cookie
    
  Arguments:
    name - the name of the cookie
    value - the value of the cookie
    sec - optional, expiration time in seconds from now
  */
  create: function(name, value, sec) {
	
    if (sec) {
      var today = new Date(), expiration;
      today.setTime(today.getTime() + (sec * 24 * 60 * 60));
      expiration = '; expires=' + today.toGMTString();
    } 
    else {
      expiration = '';
    }
	
	var domain = location.hostname.split( '.' );
	
	document.cookie = null;		
    document.cookie = name + "=" + value + expiration + "; path=/" +"; domain=" + domain[1]+ "." + domain[2];
    //window.status='cookie ' + value;
    
  },
  
  /*
  Property: get
    gets a cookie with a specific name and returns its value
    
  Arguments:
    name - the name of a cookie
  
  Returns:
    the value of a cookie with a specific name
    
  */
  get: function(check_name) {
	 
	 
	  	var a_all_cookies = document.cookie.split( ';' );
		var a_temp_cookie = '';
		var cookie_name = '';
		var cookie_value = '';
		var b_cookie_found = false; // set boolean t/f default f
		
		for ( i = 0; i < a_all_cookies.length; i++ )
		{
			
			
			// now we'll split apart each name=value pair
			a_temp_cookie = a_all_cookies[i].split( '=' );

			
			// and trim left/right whitespace while we're at it
			cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

			// if the extracted name matches passed check_name
			if ( cookie_name == check_name )
			{
				b_cookie_found = true;
				// we need to handle case where cookie has no value but exists (no = sign, that is):
				if ( a_temp_cookie.length > 1 )
				{
					cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
				}
				// note that in cases where cookie is initialized but no value, null is returned
				return cookie_value;
				break;
			}
			a_temp_cookie = null;
			cookie_name = '';
		}
		if ( !b_cookie_found )
		{
			return null;
			
		}

  },
  
  /*
  Function: check
    check if cookie exists and if its value needs to be changed
    
  Arguments:
    name - the name of the cookie
   
  */
  check: function(name) {
	 
	  var pathArray = location.href.split( '.' );
	    //alert(location.hostname);
		
	  
		
	  var locale = "";
	  if(pathArray[0].indexOf("us")>=0)
		  locale="en_US";
	  else if(pathArray[0].indexOf("en")>=0)
		  locale="en";
	  else if(pathArray[0].indexOf("it")>=0)
		  locale="it_IT";
	  else if(pathArray[0].indexOf("fr")>=0)
		  locale="fr_FR";
	  else if(pathArray[0].indexOf("jp")>=0)
		  locale="ja_JP";
	  else if(pathArray[0].indexOf("kr")>=0)
		  locale="ko_KR";
	  else if(pathArray[0].indexOf("zh-tw")>=0)
		  locale="zh_TW";
	 else if(pathArray[0].indexOf("zh-cn")>=0)
		  locale="zh_CN";
	
	

	 var cookie_value = Cookie.get(name);
	 //alert(cookie_value);
	 // check if cookie already exists
	 if(cookie_value) 
	 {
		 // locale has been changed. change value of ATGLocale cookie
		 if(cookie_value != locale)
		 {
			 //alert('locale has been changed from ' + cookie_value + ' to ' + locale);
			 //Cookie.remove(name);
			 Cookie.create(name, locale, 10000);
		 }
		 /*else
			alert('cookie exists and its value is '+ locale);*/
	 }
	 else
	 {		
		 //alert('cookie does not exist. Create new cookie with value ' + locale);
		 Cookie.create(name, locale, 10000);
	 }
	 
	
  },
  
  /*
  Function: remove
    redirect user to the correct web site using the cookie saved in the user's browser  
    
  Arguments:
    name - the name of a cookie
  */
  redirect: function(name) {
	  
	  
	  	var cookie_value = Cookie.get(name);
	  	if(Cookie.get(name)) {
			
			var locale = "";
			 
			if(cookie_value == "en_US")
				locale="us";
			else if(cookie_value == "en")
				locale="en";
			else if(cookie_value == "it_IT")
				locale="it";
			else if(cookie_value == "fr_FR")
				locale="fr";
			else if(cookie_value == "ja_JP")
				locale="jp";
			else if(cookie_value == "ko_KR")
				locale="kr";
			else if(cookie_value == "zh_TW")
				locale="zh-tw";
			else if(cookie_value == "zh_CN")
				locale="zh-cn";		
				
			if(location.hostname.indexOf("bulgari.group")>=0){
				window.location.href = "http://" + locale + ".bulgari.group";    				
			}
			else
			{
				window.location.href = "http://" + locale + ".bulgari.com";    
			}
			
	  		            
	  		return true;
	  	}
	  	return false;
	  
},
  
 
  /*
  Function: remove
    removes a cookie with a specific name by setting its expiration date to a time in the past (-1)
    
  Arguments:
    name - the name of a cookie
  */
  remove: function(name) {

    Cookie.create(name, '', -1);
   
  }
  
};

