// Javascript functions in alphabetical order, used by site.

var style_cookie_name = "style" ;
// Flag to determine which colour box was filled last.
var picker = 1;

function BrowserInfo()
{	
  this.name = navigator.appName;	
  this.codename = navigator.appCodeName;	
  this.version = navigator.appVersion.substring(0,4);	
  this.platform = navigator.platform;
  this.javaEnabled = navigator.javaEnabled();	
  this.screenWidth = screen.width;	
  this.screenHeight = screen.height;	
}

function erase_cookie(name) {
	set_cookie(name,"",-1);
}

function erase_cookies()
{
	erase_cookie("bgColour");
	erase_cookie("textSize");
	erase_cookie("textColour");
}

// Get the value from the "color" picker's text box.
function getColour(selectedBox)
{
	// Return the colour from whichever text box to use.
	if (selectedBox == 1)
	{
		return document.frmCP.color.value;
	}
	else if (selectedBox == 2)
	{
		return document.frmCP.color2.value;
	}
	else
	{
		// both have values. Determine which was selected last.
		if (picker == 1)
		{
			return document.frmCP.color.value;
		}
		else
		{
			return document.frmCP.color2.value;
		}
	}
}

// Get a cookie by its name.
function get_cookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}

// Dreamweaver automatically-produced function to evaluate javascript expressions.
function MM_callJS(jsStr) { //v2.0
  return eval(jsStr)
}

// Set the background property of the "BODY" element of the document.
function setBackground(colour)
{
	// Get body element
	document.getElementById('SSIDM_BODY').style.backgroundColor = colour;
}

// Set a cookie name/value pair. Expires in 30 days. If no domain name is specified, default of "/" is used.
function set_cookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	// alert("cookie set : " + document.cookie);
}

function setFontColour(colour)
{
	// Set the font colour of the "body" attribute.
	text = document.getElementById("SSIDM_BODY");
	// alert ("colour = " + colour);
	text.style.color = colour;
}

// Set font size from string attribute (e.g., "small, "medium", "large").
function setFontSize(size)
{
	// Set the font size of the "body" attribute.
	text = document.getElementById("SSIDM_BODY");
	text.style.fontSize = size;
	// alert ("font size = " + size);
	text.style.lineHeight = 1.5;
}
 
function set_style_from_cookie()
{
		/*
	  // Retrieve the title of the stylesheet from a cookie.
	  var css_title = get_cookie( style_cookie_name );	  
	  // If found, activate the stylesheet with that title.
	  if (css_title.length) 
	  {
		switch_style( css_title );
	  }
	  */
	  
	  //  Set the textsize, text colour, background colour and user name from previous visit.
	  var textSize = get_cookie("textSize");
	  var textColour = get_cookie("textColour");
	  var bgColour = get_cookie("bgColour");
	  // alert("bgColour = " + bgColour);
	  setFontSize(textSize);
	  setFontColour(textColour);
	  setBackground(bgColour);
	  //displayName();
}

// Switch a stylesheet based on its title.
function switch_style ( css_title )
{
// You may use this script on your site free of charge provided
// you do not remote this notice or the URL below. Script from
// http://www.thesitewizard.com/javascripts/change-style-sheets.shtml
  var i, link_tag ;
  
  // Find all elements with a "link" tag and iterate through them.
  for (i = 0, link_tag = document.getElementsByTagName("link") ; i < link_tag.length ; i++ ) 
  {
	// Select only those link tags which belong to stylesheets and have a title.
    if ((link_tag[i].rel.indexOf( "stylesheet" ) != -1) && link_tag[i].title) 
	{
	  // Disable them all,		
	  link_tag[i].disabled = true ;
	  // then enable the one of interest.
	  if (link_tag[i].title == css_title) 
	  {
		link_tag[i].disabled = false ;
	  }
	}
	// Store the title of the stylesheet in a cookie.
    set_cookie( style_cookie_name, css_title, 365);
  }
}

// Checks for an entry in the colour picker's text box. Applies to any page on which the colour picker appears. It appears on all pages, since it is part of the Dreamweaver template.
// Returns -1 for none has values, 1 for first colour box only has a value, 2 for second only has value and 3 for both have values.
function validateColour()
{
	// If  the first colour box is blank
	if (document.frmCP.color.value=="")
	{
		// check the second colour box.
		if (document.frmCP.color2.value=="")
		{
			// If it is blank, alert.
			alert("Pick a colour first.");
			// Focus both boxes.
			var elem = document.getElementById('color2');
			elem.focus();
			var elem2 = document.getElementById('color');
			elem2.focus();
			return -1;
		}
		else
		{
			// Otherwise, it is not blank.
			return 2;
		}
	}
	else
	{
		// First colour box is not blank. Is second colour box blank?
		if (document.frmCP.color2.value=="")
		{
			// If it is, use first.
			return 1;
		}
		else
		{
			// Both have values. Return code 3 to indicate so.
			return 3;
		}
	}
}

// Overall validation for colour picker.
function validateCP()
{
	// If colour box 1 is blank
	//		if colour box 2 is blank
	//			alert error.
	//		else
	//			use colour box 2
	// else
	// 		if colour box  2 is blank
	//			use colour box 1
	// else
	//		// both have values. Determine which one was filled last and use that.
	
	// Check that a radio button is selected.
	// Get the value of the colour.
	// Get which radio button is selected:
	//  get the colour
	// 	if "text", call the setFontColour function and set cookie.
	// 	if "background", call the setBackground function and set cookie. 
	var selectedBox = validateColour();
	if (selectedBox != -1)
	{
		c = getColour(selectedBox);
		var color = new RGBColor(c);
		if (color.ok) 
		{
			rgbcolor = 'rgb(' + color.r + ', ' + color.g + ', ' + color.b + ')';
			//alert("Colour got = " + rgbcolor);
			option = validateCPRadios();
			if (option == 1) // Background radio button selected
			{
				setBackground(rgbcolor);
				set_cookie("bgColour", rgbcolor, 365);
			}
			else
			{
				setFontColour(rgbcolor);
				set_cookie("textColour", rgbcolor, 365);
			}
			return true;
		}
		else
		{
			return false;
		}
	}
}

// Validates the colour picker radio buttons on any page.
function validateCPRadios()
{
	var option = -1;
	for (i = 0; i < document.frmCP.rbCP.length; i++)
	{
		if (document.frmCP.rbCP[i].checked)
			option = i;
	}
	
	if (option == -1)
	{
		alert ("Select \"text\" or \"background\".");
		return -1;
	}
	else
	{
		// alert("option = " + option);
		return option;
	}
}
	


