// =============================================================================
// Generate HTML for all Shows stored in the SHOWS.XML file...
// Display <table> with shows in DIV "show_list" or "show_list_all"
// -----------------------------------------------------------------------------

var oXmlHttp;
var sShowDIV  = "";
var sShowHTML = "";

String.prototype.reverse = function(){
	splitext = this.split("");
	revertext = splitext.reverse();
	reversed = revertext.join("");
	return reversed;
}

// =============================================================================
// padZero(?)
// -----------------------------------------------------------------------------
function padZero(sVal, totalDigits) 
{ 
	var sPad = '', nOfs; 
	sVal = sVal.toString(); 

	if (totalDigits > sVal.length) { 
		for (nOfs=0; nOfs < (totalDigits-sVal.length); nOfs++) { 
			sPad += '0'; 
		} 
	} 
	return sPad + sVal;
} 

// =============================================================================
// DateFmt(?) - input DD.MM.YYYY return YYYYMMDD
// -----------------------------------------------------------------------------
function DateFmt(sDate) { 
	var sNewDate;
	sDate = sDate.toString();
	sNewDate = sDate.split('.')[2]+sDate.split('.')[1]+sDate.split('.')[0];
	return sNewDate;
}

// =============================================================================
// generateShowsAll()
// -----------------------------------------------------------------------------
function generateShowsAll()
{
	if (getHttpRequestObj()) {
		var sDatum, sCompDatum, sLocation, sZeit, sStadt;
		var oDoc, oItem, nItem, nTotalRows, nDone, sShowsNew, sShowsOld;

		// Use this DIV for ALL shows...
		sShowDIV = "show_list_all";
		sShowsNew = ""
		sShowsOld = ""

		try {
			oXmlHttp.open("GET","http://www.fransandisco.com/shows.xml",false);
			oXmlHttp.send("");
			oDoc  = oXmlHttp.responseXML;
			oItem = oDoc.getElementsByTagName("show");
			// get actual show count from XML
			nTotalRows = oItem.length;
			//-------------------------------------------------------
			var sToday, sDay, sMonth, sYear, oDate = new Date();
			sDay   = padZero(oDate.getDate(), 2);
			sMonth = padZero(oDate.getMonth() + 1, 2);  // '+1' = WTF...
			sYear  = oDate.getFullYear();
			//YYYYMMDD
			sToday  = sYear + sMonth + sDay;
			//-------------------------------------------------------
			// Build the HTML buffer with values fron the XML file...
			for (nItem = nTotalRows-1; nItem >= 0; nItem--) {

				sDatum    = oItem[nItem].getElementsByTagName("datum")[0].childNodes[0].nodeValue;
				sLocation = oItem[nItem].getElementsByTagName("location")[0].childNodes[0].nodeValue;
				sZeit     = oItem[nItem].getElementsByTagName("zeit")[0].childNodes[0].nodeValue;
				sStadt    = oItem[nItem].getElementsByTagName("stadt")[0].childNodes[0].nodeValue;

				//YYYYMMDD
				sCompDatum = DateFmt(sDatum);
				if (sCompDatum >= sToday) {
					//Upcoming Shows >= Heute() OBEN
					sShowsNew += generateRowsAll(sDatum, sLocation, sZeit, sStadt)
				}
				else {
					//Past Shows 	 <  Heute()	UNTEN
					sShowsOld += generateRowsAll(sDatum, sLocation, sZeit, sStadt)
				}
			}
			//-------------------------------------------------------
			var sNewHeader = "<tr><td  ColSpan=4><h3>UPCOMING SHOWS:</h3></td></tr>";
			if (sShowsNew.length == 0) sShowsNew = "<tr><td ColSpan=4>keine neue Shows verfügbar...</td></tr>"
			sShowsNew = sNewHeader + sShowsNew;
			//-------------------------------------------------------
			var sOldHeader = "<tr><td ColSpan=4 ><h3>PAST SHOWS:</h3></td></tr>";
			if (sShowsOld.length == 0) sShowsOld = "<tr><td ColSpan=4>keine alte Shows verfügbar...</td></tr>"
			sShowsOld =  "<tr><td ColSpan=4>&nbsp;</td></tr>" + sOldHeader + sShowsOld;
			//-------------------------------------------------------
			document.getElementById(sShowDIV).innerHTML = "<table>"+ sShowsNew + sShowsOld +"</table>";
		}
		catch (ex) { 
			HttpXmlError(true);
		}
	}
	else {
		HttpRequestError(true);
	}
}

// Display ALL Shows on the "SHOWS" page
function generateRowsAll(sDatum, sLocation, sZeit, sStadt)
{
	var sHtml = "";
	sHtml  += "<tr>";
	sHtml  += "<td class=shows_all width= 40>"+ sDatum    +"</td>";
	sHtml  += "<td class=shows_all width=390>"+ sLocation +"</td>";
	sHtml  += "<td class=shows_all width= 40>"+ sZeit     +"</td>";
	sHtml  += "<td class=shows_all width=220>"+ sStadt    +"</td>";
	sHtml  += "</tr>";
	return sHtml;
}

// =============================================================================
// generateShowsTop()
// -----------------------------------------------------------------------------
function generateShowsTop()
{
	if (getHttpRequestObj()) {
		var sDatum, sLocation, sZeit, sStadt;
		var oDoc, oItem, nItem, nTotalRows, nDone, sShowDIV;
		// Use this DIV for ALL shows...
		sShowDIV = "show_list"

		try {
			oXmlHttp.open("GET","http://www.fransandisco.com/shows.xml",false);
			oXmlHttp.send("");
			oDoc  = oXmlHttp.responseXML;
			oItem = oDoc.getElementsByTagName("show");

			// set show Max needed...
			nTotalRows = 2;
			if (nTotalRows > oItem.length) nTotalRows = oItem.length;

			// Build the HTML buffer with values fron the XML file...
			sShowHTML = "<table>";

			for (nItem = oItem.length-1, nDone = 0; nDone < nTotalRows; nItem--) {
				sDatum    = oItem[nItem].getElementsByTagName("datum")[0].childNodes[0].nodeValue;
				sLocation = oItem[nItem].getElementsByTagName("location")[0].childNodes[0].nodeValue;
				sZeit     = oItem[nItem].getElementsByTagName("zeit")[0].childNodes[0].nodeValue;
				sStadt    = oItem[nItem].getElementsByTagName("stadt")[0].childNodes[0].nodeValue;

				generateRowsTop(sDatum, sLocation, sZeit, sStadt);
				nDone++;
			}

			sShowHTML += "</table>";
			// Now insert the HTML buffer into specified DIV...
			document.getElementById(sShowDIV).innerHTML = sShowHTML;
		}
		catch (ex) { 
			//ex.
			HttpXmlError(false);
		}
	}
	else {
		HttpRequestError(false);
	}
}

// Display Shows on the main page
function generateRowsTop(sDatum, sLocation, sZeit, sStadt)
{
	// Row 1
	sShowHTML += "<tr>";
	sShowHTML += "<td class=top_left>"+  sDatum    +"</td>";
	sShowHTML += "<td class=top_right>"+ sLocation +"</td>";
	sShowHTML += "</tr>";
	// Row 2
	sShowHTML += "<tr>";
	sShowHTML += "<td class=bottom_left>"+  sZeit  +"</td>";
	sShowHTML += "<td class=bottom_right>"+ sStadt +"</td>";
	sShowHTML += "</tr>";
}

// =============================================================================
// Create the appropriate HttpRequest object for the browser...
// (MS: ve hav vays und mins tu mek u tok)
// -----------------------------------------------------------------------------
function getHttpRequestObj()
{
	var bSuccess = false;
	oXmlHttp = null;

	if (window.XMLHttpRequest) {
		oXmlHttp = new window.XMLHttpRequest();
		bSuccess = true;
		//alert("DEBUG: oXmlHttp = XMLHttpRequest");
	}
	else if (window.ActiveXObject) {
		// Define a list of Microsoft XML HTTP ProgIDs.
		var MS_PROGIDS = new Array(
			"Microsoft.XMLHTTP",
			"MSXML2.XMLHTTP",
			"Msxml2.XMLHTTP.7.0",
			"Msxml2.XMLHTTP.6.0",
			"Msxml2.XMLHTTP.5.0",
			"Msxml2.XMLHTTP.4.0",
			"MSXML2.XMLHTTP.3.0"
		);

		// Find a ActiveXObject that works...
		for (var i = 0; i < MS_PROGIDS.length && !bSuccess; i++) {
			try {
				oXmlHttp = new ActiveXObject(MS_PROGIDS[i]);
				bSuccess = true;
				//alert("DEBUG: oXmlHttp = ActiveXObject( "+ MS_PROGIDS[i] +" )");
			}
			catch (ex)
			{}
		}
	}

	// If we couldn't create a XMLHttpRequest, relax and have a beer...
	if (oXmlHttp == null) {
		//alert("DEBUG: Browser mag XML nicht():\n\nCannot create XMLHttpRequest object.");
	}
	return bSuccess;
}

// XML HtmlReqest Object nix gut!
function HttpRequestError(bShowALL)
{
	//alert("HttpRequestError! bShowALL:" + bShowALL)

	sShowHTML = "<table>"
	if (bShowALL) {
		generateRowsAll( "Leider", "derzeit", "nicht", "verfügbar");
	}
	else {
		generateRowsTop( "Leider", "derzeit", "nicht", "verfügbar");
	}
	sShowHTML += "</table>";

	document.getElementById(sShowDIV).innerHTML = sShowHTML;
}

// XML Datei Zugriff nix gut!
function HttpXmlError(bShowALL)
{
	//alert("HttpXmlError! bShowALL:" + bShowALL)

	sShowHTML = "<table>"
	if (bShowALL) {
		generateRowsAll( "Show-Daten", "derzeit", "nicht", "verfügbar");
	}
	else {
		generateRowsTop( "Show-Daten", "derzeit", "nicht", "verfügbar");
	}
	sShowHTML += "</table>";

	document.getElementById(sShowDIV).innerHTML = sShowHTML;
}

// =============================================================================

