/*
	A collection of useful javascript functions
*/

// Write an email mailto link into a document. Hopefully spam-bot proof.
function email(name, domain) {
	document.write("<a href='mailto:"+name+"@"+domain+"'>");
	document.write(name+"@"+domain);
	document.write("</a>");
}

// Shorthand for document.getElementById()
function get(id) {
	return document.getElementById(id);
}

function getByTagAndClass(tagName, clazz) {
	var allTags=document.getElementsByTagName(tagName);
	var theList = [];
	for (i=0; i<allTags.length; i++) { 
		if (allTags[i].className==clazz) {
			theList.push(allTags[i]);
		} 
	}
	return theList;
}

function setVisible(element, bool) {
	if (bool) element.style.display = 'block';
	else element.style.display = 'none';
}

/*
Create a cookie
   name - name of the cookie
   value - value of the cookie
These cookies will ask to stay alive for a year.
*/
function setCookie(name, value) {
   var largeExpDate = new Date ();
   largeExpDate.setTime(largeExpDate.getTime() + (365 * 24 * 3600 * 1000)); // Last for a year - note that the browser may not respect this.
   // Create the cookie string
   var curCookie = name + "=" + escape(value) + "; expires="+largeExpDate.toGMTString()+";";
   // Add it to the document
   document.cookie = curCookie; // Note that this *doesn't* re-assign document.cookie (which would delete all existing cookies).
   // It magically appends the new cookie to the list.
}

/*  
  name - name of the desired cookie
  Return a string containing the value of specified cookie, or null if cookie does not exist
*/
function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1) end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

// Convert a cookie into a number (otherwise it will be a string)
function getIntCookie(name) {
   return parseInt( getCookie(name) );
}

// This function takes an array as input, and returns a randomly chosen element.
function pickRandom(myarray) {
    x=Math.floor( Math.random() * myarray.length);
    return myarray[x];
}

// A nice readable date
function getDateString() {
    now = new Date();
    bits = now.toString().split(" ");
    // Arrays, which we use to convert numbers from the date object into strings
    days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
    months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
    mystring = days[now.getDay()-1] + ", " + now.getDate() + " " + months[now.getMonth()];
    return mystring;
}

// Parse url arguments
function getUrlVars() {
	var url = ""+window.location;
	var s = url.indexOf("?");
	if (s==-1 || s==url.length-1) return {};
	var varstr = url.substring(s+1);
	var kvs = varstr.split("&");
	var urlVars = {};
	for(i=0; i<kvs.length; i++) {
		var kv = kvs[i];
		var e = kv.indexOf("=");
		if (e!=-1 && e!=kv.length-1) {
			k = kv.substring(0,e);
			k = unescape(k);
			v = kv.substring(e+1);
			v = unescape(v);
			urlVars[k] = v;
		}
	}	
	return urlVars;
}


// Find x and y positions
// Public domain, by Peter-Paul Koch & Alex Tingle
function findPosX(obj) {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
}

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }



// Message handling
var shownFlag = false;
function showMessage() {
	if (shownFlag) return;
	shownFlag = true;
	var uvs = getUrlVars(); 
	if (uvs['msg']) {
		alert(uvs['msg']);
	}
	if (uvs['error']) {
		alert(uvs['error']);
	}
}
setTimeout("showMessage();", 500);
