
// Stores the reference to the XMLHttpRequest object.
var xmlHttp = createXmlHttpRequestObject();

// Retrieves the XMLHttpRequest object.
function createXmlHttpRequestObject()
{
   // Will store the reference to the XMLHttpRequest object.
   var xmlHttp;

   // If running Internet Explorer.
   if(window.ActiveXObject)
   {
      try
      {
         xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }
   // If running Mozilla or other browsers.
   else
   {
      try
      {
         xmlHttp = new XMLHttpRequest();
      }
      catch (e)
      {
         xmlHttp = false;
      }
   }

   // Return the created object or display an error message.
   if (!xmlHttp)
      alert("Error creating the XMLHttpRequest object.");
   else
      return xmlHttp;
}

var typeSelNode;

// Make an asynchronous HTTP request using the XMLHttpRequest object.
function getTypes(thisNode, typeNodeId)
{
   // Proceed only if the xmlHttp object isn't busy
   if(xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
   {
		typeSelNode = document.getElementById(typeNodeId);
		typeSelNode.disabled = 1;

		// Retrieve the name typed by the user on the form.
		areaValue = thisNode.value;

		area = encodeURIComponent(areaValue);

		// Execute the Eventola API page on the server.
		if(typeNodeId == 'type') cmd = 'getevtyps';
		else if(typeNodeId == 'locta') cmd = 'getloctypsads';
		else cmd = 'getloctyps';
		uri = "eventolaApi.php?cmd=" + cmd + ";loca=" + area;

		xmlHttp.open("GET", uri, true);

		// Define the method to handle server responses.
		xmlHttp.onreadystatechange = readTypes;

		// Make the server request.
		xmlHttp.send(null);
   }
}

// Executed automatically when a message is received from the server.
function readTypes()
{
	// Move forward only if the transaction has completed.
	if (xmlHttp.readyState == 4)
	{
		// status of 200 indicates the transaction completed successfully
		if (xmlHttp.status == 200)
		{
			// extract the XML retrieved from the server
			xmlResponse = xmlHttp.responseXML;
			// obtain the document element (the root element) of the XML structure
			xmlDocumentElement = xmlResponse.documentElement;

			// Remove all but the first option ("-- Select --").
			//typeSelNode = document.getElementById('type');
			nopts = typeSelNode.childNodes.length;
			for(i=1; i<nopts; i++)
				typeSelNode.removeChild(typeSelNode.lastChild);

			// Read the types for the given area code and create the option elements.
			for(i=0; i<xmlDocumentElement.childNodes.length; i++)
			{
				typeStr = xmlDocumentElement.childNodes[i].firstChild.data;
				typeKey = xmlDocumentElement.childNodes[i].getAttribute('name');
				var d=document.createElement("option");
				d.value=typeKey;
				d.appendChild(document.createTextNode(typeStr));
				typeSelNode.appendChild(d);
			}
			typeSelNode.disabled = 0;
		}
		// A HTTP status different than 200 signals an error.
		else
		{
			alert("There was a problem accessing the server: " + xmlHttp.statusText);
		}
	}
}

function hideMsgBox()
{
	$("#NormalContent").css({display:"none"});
}

// Function to help avoid inadvertent double-clicks on anchors.
function disableAnchor(node)
{
	if(node.hasBeenClicked == undefined)
	{
		node.hasBeenClicked = true;
		node.style.color = "gray";
		return(true);
	}
	else
		return(false);
}

function switchSearchBox(thisNode)
{
	value = 'events';
	for(i=0; i<thisNode.childNodes.length; i++)
		if(thisNode.childNodes[i].nodeType == 1 && thisNode.childNodes[i].selected == true)
			value = thisNode.childNodes[i].getAttribute('value');

	className = value + 'SearchBox';

	divNodes = document.getElementsByTagName('div');
	for(i=0; i<divNodes.length; i++)
	{
		if(divNodes[i].className == className)
			divNodes[i].style.display = 'block';
		else if(divNodes[i].className == 'adsSearchBox')
			divNodes[i].style.display = 'none';
		else if(divNodes[i].className == 'eventsSearchBox')
			divNodes[i].style.display = 'none';
		else if(divNodes[i].className == 'locsSearchBox')
			divNodes[i].style.display = 'none';
		else if(divNodes[i].className == 'acctsSearchBox')
			divNodes[i].style.display = 'none';
	}
}

var calMonth;
var calYear;

function updateCalendar(thisNode, dir, currentYear, currentMonth)
{
	if(calYear == undefined)
		calYear = currentYear;
	
	if(calMonth == undefined)
		calMonth = currentMonth;

	calMonth = calMonth + ((dir == 'next') ? 1 : -1);

	if(calMonth >= 12)
	{
		calMonth = 0;
		calYear++;
	}
	else if(calMonth < 0)
	{
		calMonth = 11;
		calYear--;
	}

	document.getElementById('monthYear').innerHTML = monthArrayShort[calMonth] + ' ' + calYear;
	
	var d = new Date();
	d.setFullYear(calYear);		
	d.setMonth(calMonth);
	d.setDate(1);		
	
	var dayStartOfMonth = d.getDay();

	var dd = new Date(calYear, calMonth+1, 0);
	var dayEndOfMonth = dd.getDate();
	
	var tables = document.getElementsByTagName('TABLE');
	for(i=0; i<tables.length; i++)
		if(tables[i].className == 'cal')
			break;

	var md;
	var dateCells = new Array();
	var cells = tables[i].getElementsByTagName('TD');
	for(i=0; i<cells.length; i++)
	{
		if(cells[i].className == 'calDate calToday')
			cells[i].className = 'calDate';

		if(cells[i].className != 'calDate')
			continue;

		if(md == undefined)
			md = 0;
			
		dotm = md - dayStartOfMonth + 1;
		if(dotm < 1 || dotm > dayEndOfMonth)
		{
			num = cells[i].childNodes.length;
			for(k=0; k<num; k++)
				cells[i].removeChild(cells[i].lastChild);
		}
		else
		{
			url = 'eventFinder.php?q=search;edst=op;edop=on;edyr=' + calYear + ';edmo=' + (calMonth+1) + ';eddy=' + dotm;
			html = '<a href=' + url + '>' + dotm + '</a>';
			cells[i].innerHTML = html;
		}

		md++;
	}

	if(dayStartOfMonth + dayEndOfMonth <= 35)
		document.getElementById('calLastRow').style.display = 'none';
	else
		document.getElementById('calLastRow').style.display = 'table-row';
}

/**************************************************************************************************\
DESCRIPTION:
	Display a box with Google Custom Search Results (i.e. search results limited to the Eventola
	site).  This routine is required by the addGoogleCSE() method in the Document class.
\**************************************************************************************************/

function doGoogleCSE(formNode)
{
    if(formNode.q.value)
	{
		tb_show('Google search', '#TB_inline?height=400&width=600');

		// Create a search control
		var searchControl = new GSearchControl();

		var webSearch = new GwebSearch();
		webSearch.setSiteRestriction('018121826762235466136:lke9lik5qdg');

		var searcherOptions = new GsearcherOptions;
		searcherOptions.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);

		// Add searcher
		searchControl.addSearcher(webSearch, searcherOptions);

		searchControl.setLinkTarget(GSearch.LINK_TARGET_SELF);
		searchControl.setResultSetSize(GSearch.LARGE_RESULTSET);
		//searchControl.setNoResultsString(GSearchControl.NO_RESULTS_DEFAULT_STRING);

		// tell the searcher to draw itself and tell it where to attach
		searchControl.draw(document.getElementById("TB_ajaxContent"));

		// execute an inital search
		searchControl.execute(formNode.q.value);
	}

	return(false);
}

