/*********************************************************
FILE:  angjava.js
DESC:  Java Script functions for the website
DATE:  15-DEC-2001
**********************************************************
Note: document.forms["angopts"] only works on MSIE, but
      document.forms[0] works on both NS and IE.
All references to document.forms["angopts"] have been changed to document.forms[0]
*********************************************************/

function GetCookie (varname)
//
//  DESC: Retrieve value from cookie with the specified variable name.
//  USE:  val = GetCookie(varname);
//        Returns value associated with the given varname within the cookie
//
{
  var varname = varname + "=";
  var varnamelen = varname.length;
  var cookielen = document.cookie.length;
  var i = 0;
  var endstr, j;
  var retstr = null;
  while (i < cookielen && retstr == null)
  {
    j = i + varnamelen;
    if (document.cookie.substring(i, j) == varname)
    {
      endstr = document.cookie.indexOf (";", j);
      if (endstr < 0) endstr = cookielen;
      retstr = unescape(document.cookie.substring(j, endstr));
    }
    else
    {
      i = document.cookie.indexOf (" ", j) + 1;
      if (i <= 0) i = cookielen;
    }
  }
  return retstr;
}

function SetCookie (name, value)
{
	var path = location.pathname;
	if (path == null) path = "";
	path = path.substring(0,path.lastIndexOf('/')) +'/';
	var expires = new Date();
	expires.setTime(expires.getTime() + (365 * 24 * 3600 * 1000));
	var CookieVal = name + "=" + escape (value) +
	  "; expires=" + expires.toGMTString() +
	  "; path=" + path;
	document.cookie = CookieVal;
}

function SetWindowName(winval)
{
	// Set window or navigate frame name to winval
	if (parent.frames[0] == null)
	{
		window.name = winval; // Frames not in use
	}
	else
	{
		// Frames in use
		if (winval.length == 0)
		{
			parent.frames[0].name = "navigate";
		}
		else
		{
			parent.frames[0].name = winval;
		}
	}
}

function GetWindowName()
{
	var referdoc;
	if (parent.frames[0] == null)
	{
		referdoc = window.name; // Frames not in use
	}
	else
	{
		referdoc = parent.frames[0].name;
		if (referdoc == "navigate") referdoc = "";
	}
	return referdoc;
}

function ApplyMusic()
{
	var mywin = window.open('', "angmail_muswindow", "height=270,width=500,scrollbars=no");
	if (mywin.closed || (!mywin.document.URL) || (mywin.document.URL.indexOf("about") == 0))
	{
		mywin.location = "muspopup.htm"; // Set URL if window just now opened
		mywin.blur(); // Lose focus on the popup window
		window.focus(); // Grab focus for main window - makes the new window a popunder!
		SetWindowName("angmail_music_enabled"); // Change name of window to say music enabled (Yuck!)
	}
	return mywin;
}

function ApplyOptions()
//
// DESC: Get values from the cookie and set up the options as required
//       NB Currently will only work with Internet Explorer 4.0 or later.
// USE:  ApplyOptions();
//
{
	// Get background style information
	var randno, mywin, referdoc, siteindex;
	var bground = GetCookie("bground");
	var bgtype = GetCookie("bgtype");
	var music = GetCookie("music");
	var curmusic = GetCookie("curmusic");
	if (music == null) music = "000(x)";
	if (curmusic == null) curmusic = "000(x)";
	(bground == null) ? bground = "images/fiesta bkgrd.jpg" : bground = bground.substr(3, bground.length - 3);
	(bgtype == null) ? bgtype = "FIXED" : bgtype = bgtype.substr(1, bgtype.length - 1);
	if (bground == 'x')
	{
		// Random background pattern selector
		randno = Math.round(Math.random() * 23);
		if (randno < 12)
		{
			if (randno < 6)
			{
				if (randno == 0) bground = "images/fiesta bkgrd.jpg";
				if (randno == 1) bground = "images/water.gif";
				if (randno == 2) bground = "images/redarrow.gif";
				if (randno == 3) bground = "images/stucco1.gif";
				if (randno == 4) bground = "images/nature.jpg";
				if (randno == 5) bground = "images/onecloud.gif";
			}
			else
			{
				if (randno == 6) bground = "images/clearday.jpg";
				if (randno == 7) bground = "images/balloons.jpg";
				if (randno == 8) bground = "images/flowers.jpg";
				if (randno == 9) bground = "images/pattern1.gif";
				if (randno == 10) bground = "images/pattern2.gif";
				if (randno == 11) bground = "images/citpunch.gif";
			}
		}
		else
		{
			if (randno < 18)
			{
				if (randno == 12) bground = "images/back-003.gif";
				if (randno == 13) bground = "images/back-027.gif";
				if (randno == 14) bground = "images/back-025.gif";
				if (randno == 15) bground = "images/bubbles.gif";
				if (randno == 16) bground = "images/fiesta2.gif";
				if (randno == 17) bground = "images/fiesta3.gif";
			}
			else
			{
				if (randno == 18) bground = "images/fiesta4.gif";
				if (randno == 19) bground = "images/fiesta5.gif";
				if (randno == 20) bground = "images/nihao.gif";
				if (randno == 21) bground = "images/henhao.gif";
				if (randno == 22) bground = "images/greenred.gif";
				if (randno == 23) bground = "images/sstone.gif";
			}
		}
	}
	if (document.all)
	{
		// Modify the BODY stylesheet in IE
		var docbody = document.all["angdocbody"].style;
		docbody.background = "url(" + bground + ") " + bgtype; // Background image
	}
	else if (document.tags)
	{
		// Modify the BODY stylesheet in Netscape
		document.tags.BODY.backgroundImage = "url(" + bground + ")"; // Background image
	}
	//
	// Now deal with the music (if any)
	//
	if (music == curmusic)
	{
		// Music setting has not changed
		referdoc = GetWindowName(); // Get window name
		if (referdoc == null) referdoc = ""; // Just in case!
		if (music != "000(x)" && referdoc == "")
		{
			// There is music required && we have just entered this site
			mywin = ApplyMusic();
		}
	}
	else
	{
		// Music setting has been changed
		// Replace, create, or get rid of music as appropriate
		SetCookie("curmusic", music); // Pipeline the music setting
		mywin = window.open('', "angmail_muswindow", "height=270,width=500,scrollbars=no");
		mywin.close(); // Get rid of the music popup
		SetWindowName(""); // Remove window name to say no music enabled (Yuck!)
		if (music != "000(x)")
		{
			mywin = ApplyMusic(); // Required to set/change the music
		}
	}
}

function GetSettings()
{
	var bgtypeN, bgroundN, musicN;
	var bgtypeV = GetCookie("bgtype");
	var bgroundV = GetCookie("bground");
	var musicV = GetCookie("music");
	if (bgtypeV == null) bgtypeV = "0FIXED"; // Default if null
	if (bgroundV == null) bgroundV = "000images/fiesta bkgrd.jpg"; // Default if null
	if (musicV == null) musicV = "000(x)"; // Default if null (no music)
	bgtypeN = bgtypeV.substr(0, 1) * 1; // Bg type
	bgroundN = bgroundV.substr(0, 3) * 1; // Bg image
	musicN = musicV.substr(0, 3) * 1; // Music
	document.forms[0].bgtype[bgtypeN].checked = true; // Bg type
	document.forms[0].bground[bgroundN].checked = true; // Bg image
	document.forms[0].music[musicN].checked = true; // Music
}

function UpdateSettings(n)
{
	var i, numbax, txt, bgtype0;
	bgtype0 = document.forms[0].bgtype[0].checked;
	(bgtype0 == null) ? i = 0 : ((bgtype0 == true) ? i = 0 : i = 1);
	txt = document.forms[0].bgtype[i].value; // Both NS and IE
	// txt = document.forms["angopts"].bgtype[i].value; // IE only
	SetCookie("bgtype", txt); // Set bg type
	i = 0; // Deal with the background image
	txt = null;
	numbax = document.forms[0].bground.length;
	while (i < numbax && txt == null)
	{
		if (document.forms[0].bground[i].checked == true)
		{
			txt = document.forms[0].bground[i].value;
		}
		else
		{
			i ++; // Look at the next element
		}
	}
	if (txt == null) txt = "000images/fiesta bkgrd.jpg"; // Default if we know no better
	SetCookie("bground", txt); // Set bg image
	i = 0; // Deal with the music
	txt = null;
	numbax = document.forms[0].music.length;
	while (i < numbax && txt == null)
	{
		if (document.forms[0].music[i].checked == true)
		{
			txt = document.forms[0].music[i].value;
		}
		else
		{
			i ++; // Look at the next element
		}
	}
	if (txt == null) txt = "000(x)"; // Default if we know no better (no music)
	SetCookie("music", txt); // Set music option
	ApplyOptions(); // Apply the new options
}

function GetDefaults()
{
	document.forms[0].bgtype[0].checked = true; // Default bg type FIXED
	document.forms[0].bground[0].checked = true; // Default bg image
	document.forms[0].music[0].checked = true; // Default music (none)
	UpdateSettings(); // Update settings to the default values
}

/*****
**
**  Code that is executed as the page is loaded.
**
*****/
ApplyOptions(); // Apply the options