var dom = (document.getElementById) ? true : false; 
var ns5 = ((navigator.userAgent.indexOf("Gecko")>-1) && dom) ? true: false; 
var ie5 = ((navigator.userAgent.indexOf("MSIE")>-1) && dom) ? true : false; 
var ns4 = (document.layers && !dom) ? true : false; 
var ie4 = (document.all && !dom) ? true : false; 

document.getElementsByClassName = function(cl) {
  var retnode = [];
  var myclass = new RegExp('\\b'+cl+'\\b');
  var elem = this.getElementsByTagName('*');
  for (var i = 0; i < elem.length; i++) {
    var classes = elem[i].className;
    if (myclass.test(classes)) retnode.push(elem[i]);
  }
  return retnode;
}

// mystring.trim() function
String.prototype.trim = function() {
  return this.replace(/^\s+|\s+$/g,"");
}

// set cookie whose name is "c_name" and store "value" in it for "expiredays" duration
function setCookie(c_name, value, expiredays) {
  var exdate = new Date();
  exdate.setDate(exdate.getDate() + expiredays);
  document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : ";expires=" + exdate.toUTCString());
}

function getCookie(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 "";
}

function state(id, dsply) {
  obj = document.getElementById(id);
  if (dsply=='hide') obj.style.visibility = 'hidden';
  if (dsply=='show') obj.style.visibility = 'visible';
  return;
}

function isIn(x, seq) {
// check to see if x is in seq
  l = seq.length; found = 0; i = 0;
  while ((found==0) && (i < l)) {
    if (seq[i] == x) found = 1;
    i++;
  }
  return found;
}

function printLine(str) {
// writes str to the position in the HTML file where 'printLine()' is called
// use this to write Active X content (object, embed, applet tags) as a workaround for the clicking required to activate an Active X control
  document.write(str);
}

// check if email is in valid format
function isValidEmail(email) {
  var AtPos = email.indexOf("@");
  var StopPos = email.lastIndexOf(".");
  if (email == "") return 0;
  if (AtPos == -1 || StopPos == -1) return 0;
  if (StopPos < AtPos) return 0;
  if (StopPos - AtPos == 1) return 0;
  return 1;
}

function pageId(page_url) {
// return the id (folder) of the page in the url (without arguments or hashes or "index.html" etc.)
  var p_url = page_url;

  // test for trailing slash on url, remove it if it is present
  if (page_url.charAt(page_url.length-1)=="/") {
    p_url = page_url.substr(0, page_url.length-1);}

  var temp = p_url.split("/");
  var temp2;
  if (temp.length > 1) temp2 = temp[temp.length - 2];
  else temp2 = temp;
  temp = temp[temp.length - 1].split("#");
  temp = temp[temp.length - 1].split("?");
  temp = temp[temp.length - 1];

  // do not return "index.html" etc, return previous page id instead
  var test_for = new Array("index.html", "index_html", "index.php", "index.asp", "index.htm", "index.shtml", "index.xhtml", "index.wml", "index.perl", "index.pl", "index.plx", "index.ppl", "index.cgi", "index.jsp", "index.js", "index.jp", "index.php5", "index.php4", "index.php3", "index.php", "index.phtml");
  if (isIn(temp, test_for)) return temp2;
  else return temp;
}

function getQuery(key_str) {
// return value of key_str variables query string of url
// Example: url = "index.html?alert=5&page=index"; if key_str = "alert" then it returns "5"
  if(window.location.search) {
    var query = window.location.search.substr(1);
    var pairs = query.split("&");
    for(var i = 0; i < pairs.length; i++) {
      var pair = pairs[i].split("=");
      if(unescape(pair[0]) == key_str) return unescape(pair[1]);
    }
  return null;
  }
}

function getObj(id) 
{ 
  if (dom) return document.getElementById(id);
  return (ns4) ? document.layers[id] : (ie4) ? document.all[id] : (ie5||ns5) ? document.getElementById(id) : null; 
}

// Array.indexOf( value, begin, strict ) - Return index of the first element that matches value
Array.prototype.indexOf = function( v, b, s ) {
 for( var i = +b || 0, l = this.length; i < l; i++ ) {
  if( this[i]===v || s && this[i]==v ) { return i; }
 }
 return -1;
};

// remove array element if present, returns boolean
Array.prototype.reMove = function (element) {
	var result = false;
	var array = [];
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			result = true;
		} else {
			array.push(this[i]);
		}
	}
	this.clear();
	for (var i = 0; i < array.length; i++) {
		this.push(array[i]);
	}
	array = null;
	return result;
};

// Array.splice() - Remove or replace several elements and return any deleted elements
if( typeof Array.prototype.splice==='undefined' ) {
 Array.prototype.splice = function( a, c ) {
  var i = 0, e = arguments, d = this.copy(), f = a, l = this.length;
  if( !c ) { c = l - a; }
  for( i; i < e.length - 2; i++ ) { this[a + i] = e[i + 2]; }
  for( a; a < l - c; a++ ) { this[a + e.length - 2] = d[a - c]; }
  this.length -= c - e.length + 2;
  return d.slice( f, f + c );
 };
}

function showHTML(id, str) {
// display a string inside an HTML block element
  var obj = getObj(id);
  obj.innerHTML = str;
  return;
}

function setState(id, dsply) {
  var obj = getObj(id);
  if (dsply=='hide') obj.style.visibility = 'hidden';
  if (dsply=='show') obj.style.visibility = 'visible';
  return;
}

function setDisplay(id, dsply) {
  var obj = getObj(id);
  if (dsply=='none') obj.style.display = 'none';
  if (dsply=='block') obj.style.display = 'block';
  return;
}

function rollOver(id) {
  var doc = window.document;
  var img_on = id + '_on', img_off = id  + '_off';
  img_off = doc.getElementById(img_on);
  img_on = doc.getElementById(img_on);
  img_off.style.display = 'none';
  img_on.style.display = 'block';
}

function rollOut(id) {
  var doc = window.document;
  var img_on = id + '_on', img_off = id  + '_off';
  img_off = doc.getElementById(img_on);
  img_on = doc.getElementById(img_on);
  img_off.style.display = 'block';
  img_on.style.display = 'none';
}

// displays bruce's email inside a div whose id = "mailbruce"
function mailBruce() {
  var eml = new Array ("br", "uce", "@", "aaha", "learning", ".", "com");
  var em = "";
  for (var i=0;i<eml.length;i++) {
    em += eml[i];
  }
  var ml = "<a href='mailto:" + em + "'>" + em + "</a>";
  showHTML('mailbruce', ml);
}

// On page load check to see if MusicPlayer window is open or not, to decide what needs to be displayed in the page.
function checkMusic() {
  // Attempt to open an empty MusicPlayer window.  If it already exists it won't be empty.
  var musicwin = window.open('', 'musicplayer');
  if (musicwin.location == 'about:blank') musicwin.close();
  else {
    setDisplay("music_off", "none");
    setDisplay("music_on", "block");
  }
}

// Open a window with playlist and player, or move window to front if hidden
function viewMusicPlayer() { 
  // specify window properties
  var url = 'http://www.aahalearning.com/audio/MusicPlayer.html';
  var width  = 350;  					var height = 400;
  var left   = (screen.width  - width)/2;  		var top    = (screen.height - height)/2;
  var params = 'width='+width+', height='+height;  	params += ', top='+top+', left='+left;
  params += ', directories=no';				params += ', location=no';
  params += ', menubar=no';				params += ', resizable=yes';
  params += ', scrollbars=no';				params += ', status=no';
  params += ', toolbar=no';

  // Attempt to open an empty MusicPlayer window.  If it already exists it won't be empty, which is fine - otherwise load it with the MusicPlayer html.
  var musicwin = window.open('', 'musicplayer', params);
  if (musicwin.location == 'about:blank') {
    musicwin.location = url;
    setDisplay("music_off", "none");
    setDisplay("music_on", "block");
  }
  if (window.focus) musicwin.focus();
}

// Close the MusicPlayer window, which will stop the music if it is playing
function stopMusic() {
  var musicwin = window.open('', 'musicplayer');
  musicwin.close();
  setDisplay("music_on", "none");
  setDisplay("music_off", "block");
}
