
//Javascript functions used for toggling signup
//and getting data from the drop down menu

var req; // Httpxml request object, used to make signup status requests.
var sid; // Global variable for holding the id of the calendar-event that was clicked.
var classAttribute = "class"; // class attribute called different names between firefox / IE
var mouseInAttribute = "mouseIn"; // Attribute for determining whether event is in focus or not.

function submitform(signid)
{

  document.dropdownform.signid.value=signid;
	var dropdownValue = document.dropdownform.dropdown.value;
	var date = new Date();
	sid = signid;
	var IE = false;

	var targetUrl = "index.php?action=toggle_signup&signid=" + signid + "&dropdown=" + dropdownValue;
	
	//branch for IE / Firefox compatability
	if(window.XMLHttpRequest)
	{
		try
		{
			req = new XMLHttpRequest();
		}
	 	catch(e) 
		{
			req = false;
		}
 	}
	// IE uses ActiveXObject rather than XMLHttpRequest
	else if(window.ActiveXObject)
	{
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
			IE = true;
		}
		catch(e) 
		{
			try
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
				IE = true;
			}
			catch(e)
			{
			req = false;
			}
		}
	}
	// Otherwise XMLHTTPrequest object is not supported.
	// Redirect to event-specific page.
	else
	{
		window.location = "index.php?action=display&id="+signid;
	}
	if(IE)
		classAttribute = "className";
	
	// If the request object has been instantiated, send a request via index.php to toggle_signup.php
	if(req)
	{
		req.onreadystatechange = reqStatusChange;
		req.open("GET", targetUrl, true);
		req.send("");		
	}

}

// Function to get called by the readyStateChange event handler for
// the request object, req.  Purpose is to color cell and update tooltip appropriately.
function reqStatusChange()
{
	var loaded = (req.readyState == 4);

	// LI element corresponding to the 
	var clickedEvent = document.getElementById(sid);	

	if(loaded && (req.statusText == "OK"))
	{

		// Hacky way of taking the first chunk of the responseText
		// since script information and other things are placed below it due to index.php.
		// <tooltipdivider> divides the tooltip, from the signed in status, from the garbage at the end.
		// Garbage is the result of relying on an 'action' from index.php.  We should get around this soon.
		var responseArray = req.responseText.split("<tooltipdivider>");
	
		// Toggle the signedup/notsignedup status of the event's class (visually controlled in style.php).
	if((responseArray[1] == "signedup"))
	{
			clickedEvent.setAttribute(classAttribute, "signedup");
	}
	else if(responseArray[1] == "notsignedup")
	{	
			clickedEvent.setAttribute(classAttribute, "notsignedup");  
	}
	
		// Update the tooltip attribute, to be used in tooltipwrapper(...) to generate tooltip.
		clickedEvent.setAttribute("tooltip", responseArray[0]);
		var min;
		if((min = clickedEvent.getAttribute(mouseInAttribute)) == "true")
		{	
			tooltipwrapper(clickedEvent);


		}


	}
	// If the request is not fully loaded, or did not complete "OK", set the event to "toggling" class.
	else 
	{
		// set the tooltip and class to toggling status if necessary.
		if(clickedEvent.getAttribute(classAttribute) != "toggling")
		{
			var oldTip = clickedEvent.getAttribute("tooltip");
			clickedEvent.setAttribute(classAttribute, "toggling");
			clickedEvent.setAttribute("tooltip", oldTip + "<br><b>Updating the Calendar. Please wait</b>");

				tooltipwrapper(clickedEvent);
			
		}
	}
}

function mouseInEvent(calEvent)
{ 
	calEvent.setAttribute(mouseInAttribute, "true");

	tooltipwrapper(calEvent);
}

function mouseOutEvent(calEvent)
{
	calEvent.setAttribute(mouseInAttribute, "false");
	hideddrivetip();	
}

// Wrapper function for ddrive tip.
// This is necessary, since to change eventHandlers for an element, only parameterless functions
// can be assigned.
function tooltipwrapper(calEvent)
{
	var ttip = calEvent.getAttribute("tooltip");
	//alert(ttip);
	ddrivetip(ttip);
}

// Sets the focus on the first text input in the first form encountered.
function setInputFocus()
{
	var elements = document.forms[0].elements;
	var i;
	for(i = 0; i < elements.length; i++)
	{
		if(elements[i].type == "text")
		{
			elements[i].focus();
			return;
		}
	}
}

// Toggle event color when admin selects checkbox.
function adminEventSelect(eventID)
{

	var event = document.getElementById(eventID);

	// Quick check to see if we're using IE
	if(!event.getAttribute(classAttribute))
		classAttribute = "className";
	
	var eventClass = event.getAttribute(classAttribute);

	if(eventClass == "notselected")
		event.setAttribute(classAttribute, "selected");
	else
		event.setAttribute(classAttribute, "notselected");

}