function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
      window.onload = fn;
    }
    else
    {
      window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}

function attachEventListener(target, eventType, functionRef, capture)
{
  if (typeof target.addEventListener != "undefined")
  {
    target.addEventListener(eventType, functionRef, capture);
  }
  else if (typeof target.attachEvent != "undefined")
  {
    target.attachEvent("on" + eventType, functionRef);
  }
  else
  {
    eventType = "on" + eventType;

    if (typeof target[eventType] == "function")
    {
      var oldListener = target[eventType];

      target[eventType] = function()
      {
        oldListener();

        return functionRef();
      }
    }
    else
    {
      target[eventType] = functionRef;
    }
  }

  return true;
}


function startList() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("dropdown");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
addLoadListener(startList);


function getEventTarget(event)
{
  var targetElement = null;

  if (typeof event.target != "undefined")
  {
    targetElement = event.target;
  }
  else
  {
    targetElement = event.srcElement;
  }

  while (targetElement.nodeType == 3 && targetElement.parentNode != null)
  {
    targetElement = targetElement.parentNode;
  }

  return targetElement;
}

var newwindow;
function poptastic(url)
{
	newwindow=window.open(url,'name','height=500,width=640,resizable=yes,scrollbars=yes,toolbar=no,status=no');
	if (window.focus) {newwindow.focus()}
}

function toggleDivOL( elemID )
{
	var elem = document.getElementById( elemID );
	if( elem.style.position != 'absolute' )
	{
		elem.style.position = 'absolute';
		elem.style.left = '-4000px';
	}
	else
	{
		elem.style.position = 'relative';
		elem.style.left = '0px';
	}
}

function divbox (box_id)
{
if (box_id.style.display == "block")
box_id.style.display = "none";
else if (box_id.style.display == "none")
box_id.style.display = "block";
else
return false;
}

var AJurl = "aj.pl?addbooking";
var AJwhat = "LoginStatus(req.responseText)";

function UnBookShift()
{
 var u_bdate = document.getElementById("u_date").value;
 var u_username = document.getElementById("u_username").value;
 var u_userlocation = document.getElementById("u_userlocation").value;
 var u_stime = document.getElementById("u_stime").value;
 var u_bookingid = document.getElementById("u_bookingid").value;
 DoCallback("username="+u_username+"&userlocation="+u_userlocation+"&bdate="+u_bdate+"&stime="+u_stime+"&booktype=unbook&bookingid="+u_bookingid);
}

function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;
    return false;
  }
  else return true;
}

function UnWatchThread()
{
 AJurl = "aj.pl?action=unwatchthread";
 AJwhat = "UnWatchStatus(req.responseText)";
 var threadID = document.getElementById("threadID").value;
 DoCallback("threadID="+threadID);
}

function WatchThread()
{
 AJurl = "aj.pl?action=watchthread";
 AJwhat = "WatchStatus(req.responseText)";
 var threadID = document.getElementById("threadID").value;
 DoCallback("threadID="+threadID);
}

function BookShift()
{
 var bdate = document.getElementById("date").value;
 var username = document.getElementById("username").value;
 var userlocation = document.getElementById("userlocation").value;
 var stime = document.getElementById("stime").value;
 var etime = document.getElementById("etime").value;
 var listener = document.getElementById("listen").value;
 DoCallback("username="+username+"&userlocation="+userlocation+"&bdate="+bdate+"&stime="+stime+"&etime="+etime+"&booktype=book&listner="+listener);
}

function WatchStatus(Status)
{
 if(Status == "error") {
    alert("Error, you may already be watching this thread, or don't have access to this thread, Please try re-freshing this page or try something else!");
 } else if (Status =="success") {
    alert("You are now watching this thread and will recieve Emails each time someone posts to it. To stop watching, please reload the browser and choose the stop watching option.");
    toggleDivOL('watchdiv');
    /*var watchthreadform = document.getElementById( 'watchthreadform' );
	watchthreadform.action = "aj.pl?action=watchthread";
    var unwatchthread = document.getElementById( 'unwatchthread' );
    unwatchthread.value = "Stop Watching This Thread";*/
 }
}

function UnWatchStatus(Status)
{
 if(Status == "error"){
    alert("Error, you may already not be watching this thread, or don't have access to this thread, Please try re-freshing this page or try something else!");
 } else if (Status =="success") {
    alert("You are no longer watching this thread and will not recieve Emails each time someone posts to it. To start watching again, please reload the browser and choose the stop watching option.");
 	toggleDivOL('watchdiv');
 }
}

function LoginStatus(Status)
{
 if(Status == "error")
 alert("Error, Please try again!");
 else if (Status =="doublebooked")
 alert("Error, It appears you have already made a booking to some of that time period. Please refresh the roster and check that you are not doublebooking!");
 else if (Status =="2manybuddy")
 alert("Error. There are too many buddys booked on this shift, only 2 buddys can be booked at once!");
 else if (Status =="time1")
 alert("Error, Please select an End time which is after the start time you wish to book!");
 else if (Status =="time2")
 alert("Error, Please select an End time which is not the same as the start time!");
 else if (Status =="success")
 alert("Done!");
 else if (Status =="unbooked")
 alert("Your shift is now Unbooked! \n Press refresh to see the booking.. ");

}
