/* 	event-common.js
	These are common event functions used by more than one HTML page 
 		getEventURL():		Returns the Program URL for a given public event
		openDefaultProgram();	open a default program window if no program .html file exists
		openProgramWindow();	open the concert program .html if it exists else calls openDefaultProgram()

	External Files:
		None
*/

function getEventURL(month,day,year) {
/* 	Return the URL for the selected event
	Pattern for ConcertProgURL = "progYYYY_MMDD.html"
*/
	var concertProgURL = new String();

	concertProgURL = glblConcertProgURLPrefix + year.toString() + "_"  + twocharMonth(month) + twocharDay(day) + ".html";
	return concertProgURL;
}

function openDefaultProgram(month, day, year, DOW, desc) {
/*	Opens a window for events with no program listing yet
	It builds some basic HTML and populates event information in a small window
*/
	var windowHTML = new String();
	var imageDirectory = new String();

	if (location.pathname.indexOf("index")!=-1) {			//What directory are we in?
		imageDirectory = "Images/PAS_logo2.gif";		//Root
	} else 	{
		imageDirectory = "../Images/PAS_logo2.gif";		//Need to come up a level
	}

	if (window.defaultWindow) defaultWindow.close();		//If already open, close it first

	defaultWindow = window.open('','Default','height=230,width=400');
	if (defaultWindow != null) {
		windowHTML += '<html><head><title>PAS Program Unavailable</title>';
		windowHTML += '<style type="text/css">';
		windowHTML += '.defprogtext {font-family: Verdana, Arial, Helvetica, sans-serif; font-size: 13px; font-style: normal; font-weight: normal; color: #333333; line-height: 15px;}';
		windowHTML += '</style></head><body BGCOLOR=#F3F3E8>';
		windowHTML += '<IMG SRC="'+imageDirectory+'" ALT="" WIDTH=286 HEIGHT=100 border="0"><br>';
		windowHTML += '<p class=defprogtext>Program details are not yet available for the <br>';
		windowHTML += desc+' on '+DOW+', '+wordMonth[month-1]+' '+day+', '+year+'.';
		windowHTML += '</body></html>';
		
		defaultWindow.document.write(windowHTML);
		defaultWindow.focus();
	}

}

function openProgramWindow(month, day, year, DOW, desc) {
/*	Opens a new window for the requested event program given the month and day
	It will open an HTTP connection and issue a GET on the program URL to see if it exists on the web server
	If it exists, it opens the HTML document in a new window, if not, it calls a default page passing the details
*/
	var concertProgURL = new String();
	var windowName = 'Program';
	var windowReplace = false;
	var windowFeatures = 'height=800,width=650,scrollbars=yes,directories=no,hotkeys=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no'

	if (document.all) 						//Is this IE?
		var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");	//Yup
	else
		var xmlhttp = new XMLHttpRequest(); 			//Nope

	concertProgURL = getEventURL(month,day,year);			//Get the program URL suffix

	if (test == true) {						//If running locally
		try {
			//Check to see if the .html file exists
			xmlhttp.open("GET", concertProgURL, false);
			xmlhttp.send("");
			window.open(concertProgURL,windowName,windowFeatures);				//get .html locally
		} catch (anException) {									//We do not get here in Production
//			alert("Error on GET: "+anException.name+anException.message);			//If you want to see the message generated
			openDefaultProgram(month, day, year, DOW, desc);				//Display default program window
		}
	} else {
		xmlhttp.open("GET", glblWebSiteURLPrefix+concertProgURL, false);			//Found status=200 || Not Found=404
		xmlhttp.send("");
		if ((xmlhttp.status == "200") || (xmlhttp.status == "0")) {				//If running on the webserver - in production
			//Program .html found
			window.open(glblWebSiteURLPrefix+concertProgURL,windowName,windowFeatures);	//get from server 
			return;	
		}	 
		else 	openDefaultProgram(month, day, year, DOW, desc);				//Not found (status=404) so open default program window
	}
}