function OpenEvent (id){
	document.location = "/immp/calendar/editevent.cfm?EventID="+id;
}
function OpenWindow (url){
	window.open(url,null,
    "height=600,width=960,status=no,toolbar=no,menubar=no,location=no");
}
// JavaScript Document
function GetHTTPRequest (){
	//Mozilla and Safari
	var req;
	if (window.XMLHttpRequest) {
	   req = new XMLHttpRequest();

	} else if (window.ActiveXObject) {
	   req = new ActiveXObject("Microsoft.XMLHTTP");

	} else { //Using an old browser
		return null;
	}
	return req;
}

function get (name){return document.getElementById (name);}


///STYLE SWITCHER///
var currentStyle = "original";
function ToggleColorScheme (){
	if (currentStyle == "original")
		currentStyle = "inverted";
	else
		currentStyle = "original";

	var setstyles = new XHTTP();
	setstyles.load ("/immp/_ajax/toggleStyles.cfm");
	setActiveStyleSheet(currentStyle);
    return false;
}
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 DisplayEvents(atHome){
	ClearAgenda();
	var req = GetHTTPRequest();
    req.open ("GET", "/immp/_ajax/fetchCalendarData.cfm"+(atHome?"?FullMonth=no":""), true);

	req.onreadystatechange = function (){
		if (req.readyState == 4){
			MSG_Alert (null);
			var response = req.responseXML.documentElement;
            var hasEvents=false;
			ClearAgenda();
            if(response) { // IE gets a null response; FF gets non-null but empty
                var i=0;
                var events=response.getElementsByTagName('eventID');
                for (; i<events.length && i<10; i++) {
                    if (!events[i]) break;

                    var eventID= events[i].firstChild.data;
                    var facilityID = response.getElementsByTagName('facilityID')[i].firstChild.data;
                    var eventName = response.getElementsByTagName('eventName')[i].firstChild.data;
                    var startDate = response.getElementsByTagName('startDate')[i].firstChild.data;
                    var endDate = response.getElementsByTagName('endDate')[i].firstChild.data;
                    var description = "";

                    var ptr_description = response.getElementsByTagName('description')[i];
                    if (ptr_description.firstChild)
                        description = ptr_description.firstChild.data;

                    AddEvent (eventID, facilityID, eventName, startDate, endDate, description);
                }
                hasEvents=i>0;
            }

			if (!hasEvents) ShowNoEvents();

			init_startDate = new Array();
		}else{
			MSG_Alert ("Loading events...");
		}
	}
	req.send (null);
}

function AddEvent (eventID, facilityID, eventName, startDate, endDate, description){
	var eventsStartDate = new Date(startDate);
	var eventsEndDate = new Date(endDate);

	var ptr = get("Agenda");
	var appended = "<div class='Event'>";
	appended += "<div class='link' onClick='OpenEvent("+eventID+")'>&nbsp;</div>";
	appended += "<div class='Name'>"+eventName+"</div>";
	if (endDate != startDate){
		appended += "<div class='Date'><strong>Start date:</strong> "+startDate;
		appended += " <br/> <strong>End date:</strong> "+endDate+"</div>";
	}else{
		appended += "<div class='Date'><strong>Date:</strong> "+startDate+"</div>";
	}
	if (description != undefined)
		appended += "<div class='Description'>"+description+"</div>";
	appended += "</div>";
	ptr.innerHTML = ptr.innerHTML + appended;
}
function ClearAgenda(){
	if (get("Agenda")) get ("Agenda").innerHTML = "";
}
function ShowNoEvents(){
	if (get("Agenda")) get ("Agenda").innerHTML = "There are no current events for this month.";
}

function CleanState (){
	var ptr = get ("ServerResponse");
	if (ptr) ptr.className = "Hidden";
}
function Include (src){
	document.writeln ("<script src='"+src+"' type='text/javascript'></script>")
}
function goToForm (form,step){
	document.location = "/immp/plans/form.cfm?FormID="+form+"&StepID="+step;
}


////////////////////////////////////////////////////////////////////////
//ImageElement class                                                  //
//Creates an image element (<img>) for any format under Firefox.      //
//If using Internet Explorer, it will create a div with the           //
//appropiate width and height (needs to be supplied) and apply the    //
//PNG by means of an IE style filter (AlphaImageLoader).              //
////////////////////////////////////////////////////////////////////////

//ImageElement - constructor
//Optional: src, the source file for the image
function ImageElement (src, code){
	if (!src)
		this.src = null;
	else
		this.src = src;

	this.container = null;
	this.isPNG = false;
	this.width = 0;
	this.height = 0;
	this.id = code;
}

//copy constructor
ImageElement.prototype.copy = function (){
	var copy = new ImageElement (this.src);
	copy.width = this.width;
	copy.height = this.height;
	copy.id = this.id;
	copy.isPNG = this.isPNG;
	return copy;
}

//getElement - gets the DOM element holding the image
//Required: isIE
ImageElement.prototype.getElement = function (isIE){
	if (this.src == null)
		throw "UIE-WEBDEV: An ImageElement has to have a src defined by means of obj.setSource('src') or the constructor.";
	if (this.container == null){
		if ((this.src).indexOf (".png")!= -1){
			this.isPNG = true;
		}
		if (this.isPNG && isIE){
			if (this.width == 0 && this.height == 0)
				throw "UIE-WEBDEV: An ImageElement for IE has to have a width (obj.setWidth()) and a height (obj.setHeight()).";
			this.container = document.createElement("div");
			this.container.style.width = this.width+"px";
			this.container.style.height = this.height+"px";
			this.container.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+this.src+"', sizingMethod='scale');";

			this.container.style.float = "left";


		}else{
			this.container = document.createElement ("img");
			this.container.src = this.src;

			if (this.width > 0)
				this.container.style.width = this.width+"px";
			if (this.height >0)
				this.container.style.height = this.height+"px";
		}
	}
	return this.container;
}
//setSource - sets the source of the image, required if not passed in the constructor.
ImageElement.prototype.setSource = function(src){
	this.src = src;
}

//setWidth - sets the width of the image, required for the IE + PNG case
ImageElement.prototype.setWidth = function(width){
	this.width = width;
}
//setHeight - sets the width of the image, required for the IE + PNG case
ImageElement.prototype.setHeight = function(height){
	this.height = height;
}

//getWidth - gets the width of the image, required for the IE + PNG case
ImageElement.prototype.getWidth = function(){
	return this.width;
}
//getHeight - gets the width of the image, required for the IE + PNG case
ImageElement.prototype.getHeight = function(){
	return this.height;
}

function MSG_Alert (msg){
    ptr = get ("Alert");
    if (msg != null){
        ptr.innerHTML = msg;
        ptr.className = "";
    }else{
        ptr.innerHTML ="";
        ptr.className = "Hidden";
    }
}

function gotoSection (id){
    document.location = "/immp/"+id;
}

if(window.attachEvent)
  window.attachEvent("onload",setListeners);

function setListeners(){
  inputList = document.getElementsByTagName("input");
  for(i=0;i<inputList.length;i++){
    inputList[i].attachEvent("onpropertychange",restoreStyles);
    inputList[i].style.backgroundColor = "";
  }
  selectList = document.getElementsByTagName("select");
  for(i=0;i<selectList.length;i++){
    selectList[i].attachEvent("onpropertychange",restoreStyles);
    selectList[i].style.backgroundColor = "";
  }
}

function restoreStyles(){
  if(event.srcElement.style.backgroundColor != "")
    event.srcElement.style.backgroundColor = "";
}

addLoadEvent(function() {
    get("switchstyles").onclick=ToggleColorScheme;
});

/**
 * Helper function to allow multiple functions to execute on page load.
 * http://simon.incutio.com/archive/2004/05/26/addLoadEvent
 */
function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {
                oldonload();
            }
            func();
        }
    }
}
