/*  Some Global Functions
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function addUnloadEvent(func) {
  var oldonunload = window.onunload;
  if (typeof window.onunload != 'function') {
    window.onunload = func;
  } else {
    window.onunload = function() {
      oldonunload();
      func();
    }
  }
}

function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element,value) {
  if (!element.className) {
    element.className = value;
  } else {
    newClassName = element.className;
    newClassName+= " ";
    newClassName+= value;
    element.className = newClassName;
  }
}

/*  Search form on top of every page
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/


function resetFields(whichform) {
  if (!document.getElementById("search")) return false;
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}
function prepareForms() {
    if (!document.forms.mainsearch) return false;
    var thisform = document.forms.mainsearch;
    resetFields(thisform);
}
function validateSearch() {
    var searchstring  = document.forms.mainsearch.KeyWords;
    if (searchstring.value == "search jane's free content") {
      alert('Please enter a search term');
	  searchstring.focus();
	  addClass(searchstring,"searchalert");
	  return false;
	}
}
	
/* Highlight odd table rows if the table has an ID of stripeTables
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/

function stripeTables() {
  if (!document.getElementById) return false;
  if (!document.getElementById("stripeTable")) return false;
  var tables = document.getElementById("stripeTable");
    var odd = false;
    var rows = tables.getElementsByTagName("tr");
    for (var j=0; j<rows.length; j++) {
      if (odd == true) {
        addClass(rows[j],"odd");
        odd = false;
      } else {
        odd = true;
    }
  }
}
function highlightRows() {
  if (!document.getElementById) return false;
  if (!document.getElementById("stripeTable")) return false;
  if (!document.getElementsByTagName) return false;
  var thistable = document.getElementById("stripeTable");
  var rows = thistable.getElementsByTagName("tr");
  for (var i=0; i<rows.length; i++) {
    rows[i].oldClassName = rows[i].className
    rows[i].onmouseover = function() {
      addClass(this,"highlight");
    }
    rows[i].onmouseout = function() {
      this.className = this.oldClassName
    }
  }
}

function doPopups() {
  if (!document.getElementsByTagName) return false;
  var links=document.getElementsByTagName("a");
  for (var i=0; i < links.length; i++) {
    if (links[i].className.match("popup")) {
      links[i].onclick=function() {
        window.open(this.href, "", "top=40,left=40,width=500,height=450,scrollbars=1"); return false;
      }
    }
  }
}   

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/; domain=janes.com";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0){
			var cookievalue = c.substring(nameEQ.length,c.length);
			var cookienovalue = "novalue";
			if (cookievalue !== "")	return cookievalue;
			else return cookienovalue;
		}	
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

/* Login In/Out Text
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   */

function LoginText() {
  alreadyRun = true;
  if (!document.getElementById) return false;
  if (!document.getElementById("s-loggedin")) return false;
  var readlogincookie = readCookie('ERIGHTS')
  if (readlogincookie != '' && readlogincookie != 'novalue' && readlogincookie != null) {
	  writeLoggedIn();
  } else if (readlogincookie == 'novalue') {
	  writeLoggedOut();
  } else if (readlogincookie == null) {
  var imgsrc = 'http://www2.janes.com/docs/access/loggedin.jpg';
  var img = new Image();
  img.src = imgsrc;
  img.onerror = function() {
	  writeLoggedOut();
  }
  img.onload = function() {
	  writeLoggedIn();
  }
}
}

function writeLoggedIn() {
   var loggeindiv = document.getElementById("s-loggedin");
   var thiscontainer = document.getElementById("subNav");
   var login = document.getElementById("s-login");
   var logout = document.getElementById("s-logout");
   var txtyes = document.createTextNode("You are logged in");
   loggeindiv.setAttribute("id","s-loggedingreen");
   loggeindiv.appendChild(txtyes);
   login.style.display = 'block';
   logout.style.display = 'block';
   login.style.borderRightStyle = 'solid';
   login.style.borderRightColor = '#AAA';
   login.style.borderRightWidth = '1px';
}

function writeLoggedOut() {
   var loggeindiv = document.getElementById("s-loggedin");
   var thiscontainer = document.getElementById("subNav");
   var login = document.getElementById("s-login");
   var logout = document.getElementById("s-logout");
   var snav = document.getElementById("subNav");
   var txtno = document.createTextNode("You're not logged in");
   loggeindiv.setAttribute("id","s-loggedinred");
   loggeindiv.appendChild(txtno);
   /*snav.style.width = '470px';*/
   login.style.display = 'block';
}

var alreadyRun = false;
function ifnoinit() {
  if (alreadyRun) {return;}
  alreadyRun = true;
  LoginText();
}

/* Set up referer url and text on sucess page
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   */

function loginReferrer(loginurl) {
source = loginurl.getAttribute("href");
var thisurl = document.URL;
var thistitle = document.title;
createCookie('loginurl',thisurl,1);
createCookie('logintitle',thistitle,1);
window.location = source;
}

function insertLoginUrl() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("s-login")) return false;
  var loginId = document.getElementById("s-login");
  var loginLink = loginId.getElementsByTagName("a");
  for ( var i=0; i < loginLink.length; i++) {
    loginLink[i].onclick = function() {
	  loginReferrer(this);
	  return false;
	}
  }
}

/* Set up CSS Text Switcher
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   */
  function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function setupStylesheet() {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

function writeStylesheet() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function insertTextSizeLink() {
  if (!document.getElementsByTagName) return false;
  if (!document.getElementById) return false;
  if (!document.getElementById("txtSize1")) return false;
  var textId1 = document.getElementById("txtSize1");
    textId1.onclick = function() {
	  setActiveStyleSheet('size1');
	  return false;
  }
  var textId2 = document.getElementById("txtSize2");
    textId2.onclick = function() {
	  setActiveStyleSheet('size2');
	  return false;
  }
  var textId3 = document.getElementById("txtSize3");
    textId3.onclick = function() {
	  setActiveStyleSheet('size3');
	  return false;
  }
}

function sendtofriend() {
currentURL = "http://www.technology-research.com/cgi-bin/friend.cgi?referrer=" + location.href
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(currentURL, '" + id + "', 'toolbar=0,scrollbars=1,location=1,statusbar=0,menubar=0,resizable=1,width=420,height=350');");
}

/* List of events that load when the page loads
	- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -	*/
addLoadEvent(ifnoinit);
addLoadEvent(insertLoginUrl);
addLoadEvent(insertTextSizeLink);
addLoadEvent(setupStylesheet);
addLoadEvent(prepareForms);
addLoadEvent(doPopups);
addLoadEvent(stripeTables);
addLoadEvent(highlightRows);
addUnloadEvent(writeStylesheet);

