/****									 ****/
/****        Javascript file that contains all JS stuff that I use       ****/
/****									 ****/

// I'm putting my email address in here, in an attempt to stop it being visible 
// to spiders on the web
var emailAddr= "raj" + "&#64;" + "lordofthemoon." + "com";

/*
 * Toggles visibility of the given element.  This should be a block-level element
 * from the DOM node.  When 'invisible', it is removed from the DOM tree (ie
 * display: none).
 *
 * @param elementID a block-level DOM element
 * @param currentVisibility whether the element is currently visible or not
 * @return the inverse of <code>currentVisibility</code>
 */
function toggleElementVisibility(elementID, currentVisibility)
{
  elementID.style.display= (!currentVisibility) ? "block" : "none";
  return !currentVisibility;
} // end function toggleElementVisibility()

function writeEmail()
{
    document.write('<a href="mailto:' + emailAddr + '">' + emailAddr + '</a>');
} // end function writeEmail()

/*
The code to change the font size (the block in if (document.getElementByID) {}, 
setSizes(), changeType(), in/decreaseSize() ), was written by Eric Costello,
and you are free to use, modify and distribute it. Please leave this 
comment block intact so that people can find the original unmodified
version at: http://www.glish.com/css/blogger/

Also see:
http://www.schwa.com
http://www.glish.com
*/
if (document.getElementById) 
{
	// read the cookie to get the proper font size, set to default 12 if a first timer
	fs= parseFloat(getCookie('fontSize'));
	if (!fs>0) fs=1.0;

	// set the appropriate font sizes for the relevant HTML elements
	// any other elements that you want to have a changeable font size must be added to this list
	// in the form "element_fs" and then also added to the setSizes and changeType functions
	var body_fs; //, h1_fs, h2_fs, h3_fs, h4_fs, h5_fs, h6_fs;
	
	setSizes();

	// lets write out styles with the changeable values that we get from the cookies
	// This will make the page display
	document.writeln('<style type="text\/css">');
	document.writeln('body {font-size:'+body_fs+'em;}');
/* this stuff is commented out, since everything can be taken relative to the body size
	document.writeln('h1{font-size:'+h1_fs+'px;}');
	document.writeln('h2{font-size:'+h2_fs+'px;}');
	document.writeln('h3{font-size:'+h3_fs+'px;}');
	document.writeln('h4{font-size:'+h4_fs+'px;}');
	document.writeln('h5{font-size:'+h5_fs+'px;}');
	document.writeln('h6{font-size:'+h6_fs+'px;}');
*/
	document.writeln('<\/style>');
}

// this changes the sizes of the various elements relative to the fs
function setSizes() 
{
  body_fs = fs
/* this stuff is commented out, since everything can be taken relative to the body size
  h1_fs = body_fs+6;
  h2_fs = body_fs+5;
  h3_fs = body_fs+4;
  h4_fs = body_fs+3;
  h5_fs = body_fs+2;
  h6_fs = body_fs+1;
*/
} // end function setSizes()

function changeType() 
{
  if (!document.getElementsByTagName) {return false;} // unclean! unclean!
  // because NS6 seems to freak out on abs. positionined divs
  // when you change a style property of the body, we have to
  // set the fontFamily and fontSize on div elements and not the body
  setStyleByTag('div','fontSize',body_fs+'em');

/* this stuff is commented out, since everything can be taken relative to the body size
  setStyleByTag('h1','fontSize',h1_fs+'px');
  setStyleByTag('h2','fontSize',h2_fs+'px');
  setStyleByTag('h3','fontSize',h3_fs+'px');
  setStyleByTag('h4','fontSize',h4_fs+'px');
  setStyleByTag('h5','fontSize',h5_fs+'px');
  setStyleByTag('h6','fontSize',h6_fs+'px');
*/

  var expires= new Date();
  expires.setTime(expires.getTime() + 63072000000);
  // store these values in cookies for subsequent page loads (set expiration for 2 years in future)
  setCookie('fontSize',fs, expires, "/");
}

// this function is called from the A+ button
function increaseSize() 
{
  fs+=0.05;
  setSizes();
  changeType();
} // end function increaseSize()

// this function is called from the A- button
function decreaseSize() 
{
  if (body_fs>=0.6) 
    {
      fs-=0.05;
      setSizes();
      changeType();
    } // end if 
} // end function decreaseSize()



/* opens a smaller window with filename "file", window name "theName", width "wid"
 * and height "hei"
 */
function openDetails(file, theName, wid, hei) 
{
    dwWindow=window.open(file,theName,
			 'width=' + wid + ',height=' + hei +
			 ',toolbar=no,location=no,directories=no,status=yes' + 
			 ',menubar=yes,scrollbars=yes,resizable=yes');
} // end function openDetails()

// --------------------------------------------

/* function to swap images */
function swap(theNum, theName)
{
    if (document.images) 
	document.images[theNum].src= eval(theName + ".src");
} // end function swap()

// --------------------------------------------

/* decide whether or not to open links in new windows */
function targetLinks(boNew)
{
   // thanks to randomwalks.com for this code
   if (boNew) where = "_blank";
   else where = "_self";
   for (var i=0; i<=(document.links.length-1); i++)
   {
   document.links[i].target = where;
   }
}

// --------------------------------------------

/* Prints the contents of the cookie "hasVisited" to screen */
function putCookie()
{
    var temp = getCookie("hasVisited");
    document.write(temp);
} // end function putCookie()

/* sets a cookie called "hasVisited" containing the name of the visitor */
function setName() {
	// set expiration date for 2 years in the future in seconds
	var expiration = new Date();
	expiration.setTime(expiration.getTime() +  63072000000);
	setCookie("hasVisited", document.RajForm.signername.value, expiration, "/");
	document.RajForm.submit();
}

function writeName()
{
    var temp= getCookie("hasVisited");
    if (temp != null)
		document.write("Hi " + temp + ", welcome back ");
    else
		document.write("Welcome ");

    if (temp != null)
	{
	    // reset the cookie, to make sure that it never expires
	    var expiration = new Date();
	    expiration.setTime(expiration.getTime() + 630720000000);
	    //setCookie("hasVisited", temp, expiration);
	    setCookie("hasVisited", temp, expiration, "/");
	} // end if
}

// -----------------------------
//      cookie functions
// -----------------------------

// An adaptation of Dorcht's function for setting a cookie. 
function setCookie(name, value, expires, path, domain, secure) { 
	document.cookie = name + "=" + escape(value) + 
	((expires == null) ? "" : "; expires=" + expires.toGMTString()) + 
	((path == null) ? "" : "; path=" + path) + 
	((domain == null) ? "" : "; domain=" + domain) + 
	((secure == null) ? "" : "; secure"); 
} 

function getCookieVal(offset) { 
    var endstr = document.cookie.indexOf(";", offset); 
    if (endstr == -1) endstr = document.cookie.length;
	 var theCookie= document.cookie.substring(offset, endstr);
	 if (theCookie == "null")
		return null;
	 else
	   return unescape(theCookie); 
}

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; 
	} // end loop
    return null;
}

// -----------------------------
//      style functions
// -----------------------------

// These 2 setstyle functions were modified from code by Steven Champeon found at
// http://developer.apple.com/internet/_javascript/styles.html

// setStyleByTag: given an element type, style property and value
// args:
//  e - element type or id
//  p - property
//  v - value
function setStyleByTag(e, p, v) {
	var elements = document.getElementsByTagName(e);
	for(var i = 0; i < elements.length; i++) {
		elements.item(i).style[p] = v;
		}
	}

// setStyleById: given an element id, style property and 
// value, apply the style.
// args:
//  i - element id
//  p - property
//  v - value
// 
function setStyleById(i, p, v) {
	var n = document.getElementById(i);
	n.style[p] = v;
}
