// Just holds the current URL of the popup page.
function D4PopupRedirectUrl() { return 'popup_page.html'; }

// This function is the same as mainRedirectUrl, but always
// return the "real" url of the default page instead of NULL.
// Currently: option 'usa' maps to the url 'index.html'
function D4MainAbsoluteUrl() {
   var url = D4MainRedirectUrl();

   // Check to see if the user has selected the default page
   // as their preferred homepage.  (NULL returned).  If so,
   // return the real URL of the default page instead of NULL.
   if (url == null) {
      url = 'index.html';
   }
   return url;
}

// Change case 'france', etc... to match the 4d site page URL's

function D4MainRedirectUrl() {
   var favorite = GetCookie('4dDefault');
   var url = null;
   if (favorite != null) {

   // Note!: Always have the DEFAULT page return NULL instead of a URL,
   // otherwise you'll get an infinite reload loop when the user has
   // selected the default page as their preferred homepage.

	switch (favorite) {
	case 'spain' : url = 'http://www.4dhispano.com/'; // change these!
	break;
	case 'france' : url = 'http://www.4d.fr/';
	break;
	case 'germany' : url = 'http://www.de.4d.com/';
	break;
	case 'japan' : url = 'http://www.4D-Japan.com/';
	break;
	case 'sweden' : url = 'http://www.4Dsweden.com/';
	break;
	case 'uk' : url = 'http://www.4duk.com/';
	break;
	case 'australasia' : url = 'http://www.4d.fr/ANZ/index.html';
	break;
	case 'international' : url = 'http://www.4d.com/world/';
	break;
    // DEFAULT: 'usa' = NULL
	case 'usa' : url = null;
		break;
	}
   }
   return url;
}

// Does two things: 
// 1) Launches the redirect popup if the user hasn't
//     chosen a default homepage yet
// 2) If they *have*, automatically redirects them to
//     their default.
function D4Default()
	{
  // Check to see if user has cookie at all.
  // If not, then automatically launch the 
  // default-page popup.
  var defaultCookie = GetCookie('4dDefault');
  if (defaultCookie == null)
	{
	D4LaunchEditionPopup();
	}
  else
	{
  // If they *do* have a cookie, see which one
  // it is.   If it's the default page cookie 
  // (currently 'US'), then stay on this page.
  // If it's a different cookie, then jump to
  // the appropriate page.
     var jumpUrl = D4MainRedirectUrl();
     if (jumpUrl != null)
		{
        window.location.href = jumpUrl;
	     }
  	}
}

// Launches the choose-a-default-site popup
function D4LaunchEditionPopup() {
   var WM_acceptsCookies = false;
   if ( document.cookie == '' ) {
      document.cookie = 'WM_acceptsCookies=yes'; // Try to set a cookie.
      if ( document.cookie.indexOf( 'WM_acceptsCookies=yes' ) != -1 ) {
         WM_acceptsCookies = true;
      } // If it succeeds, set variable
   } else { // there was already a cookie
      WM_acceptsCookies = true;
   }
   if ( !WM_acceptsCookies ) {
      alert( "In order to set your default edition you must accept cookies." );
   } else {
      var url = D4PopupRedirectUrl(); 
      var name = 'defaultpopup';
      var widgets = 'scrollbars=yes,width=630,height=300';
      popupWin = window.open(url, name, widgets);
      popupWin.opener = self;
      popupWin.focus();
   }
}

// When called from within the popup:
// 1) Closes the popup
// 2) If the main page is already open, redirects
//     it to the user's chosen default
// 3) If the main page has been closed, opens it
//     with the user's chosen default

function D4JumpToMain() {
   if (self.opener.closed) {
      windowPtr = window.open( "", "newWindow" );
   } else {
      windowPtr = self.opener;
   }
   var jumpUrl = D4MainAbsoluteUrl();
   windowPtr.location.href = jumpUrl;
   windowPtr.focus();
   top.close();
}



// Original:  Ronnie T. Moore
// Web Site:  The JavaScript Source
// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com/


function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
		endstr = document.cookie.length;  
	var cookieval = unescape(document.cookie.substring(offset, endstr));	
// alert( cookieval);
	return cookieval;
}


function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg)      
			return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;
	}
	return null;
}


function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
        var cookiename = name + "=" + escape (value) +
		((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
		((path == null) ? "" : ("; path=" + path)) +
		((domain == null) ? "" : ("; domain=" + domain)) +
		((secure == true) ? "; secure" : "");
	document.cookie = cookiename;
//	alert( cookiename);
}


function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}
