// JScript source code
// Script to request next show (performance) data from server
// and display date and venue.  Make the entire entry a link to
// the provided URL.
//
// Format:  (xml)
//  <xml>
//      <show>
//          <date> </date>
//			<name> </name>
//          <venue>   </venue>
//          <city>  </city>
//          <url>    </url>
//      </show>
//  </xml>

// Define container var for all other variables (prevent name clash)
var NextShow = {};

// Define the data source
NextShow.DataSrc = rootFlashLocation + 'calendar/nextShow.php';
// Global used for document request.
NextShow.req = null;


// Use XMLHttpRequest to fetch the data file from the server
if (window.XMLHttpRequest) { // code for IE7, Firefox, Opera, Safari, etc.
    NextShow.req = new XMLHttpRequest();
} else if (window.ActiveXObject) { // code for IE6, IE5
    NextShow.req = new ActiveXObject("Microsoft.XMLHTTP");
}
if (null !== NextShow.req) {
    NextShow.req.onreadystatechange = NextShowProcessReqChange;
    NextShow.req.open("GET", NextShow.DataSrc, true);
    NextShow.req.send(null);
} else {
    alert('Your browser cannot handle this script');
}

// handle onreadystatechange event of req object
function NextShowProcessReqChange() {
    // only if req shows "loaded"
    if (NextShow.req.readyState == 4) {
        // only if "OK"
        if (NextShow.req.status == 200) {
            NextShowDisplayData();  // Call display function.
        } else {
            alert("There was a problem retrieving the XML data:\n" + NextShow.req.statusText);
        } // readyState == 4 but status not 200
    }
}

// Function to parse returned data and construct output text, then write that text.
function NextShowDisplayData() {
    var OutputStr = 'Sorry, we are unable to display next show data.<br />Please visit the <a href="../../calendar.php">Calendar</a> page instead.';

    var showDate = null;
    var showName = null;
    var showVenue = null;
    var showCity = null;
    var showUrl = null;
    var showTmpStr = null;

    var xmlDoc = NextShow.req.responseXML.documentElement;
    x = xmlDoc.getElementsByTagName("show");    // We only care about the first one if there are more.
    if (x) {
        // Date.
        if (x[0].getElementsByTagName("date")[0]) {
            showDate = x[0].getElementsByTagName("date")[0].childNodes[0].nodeValue;
        }
        // Name
        if (x[0].getElementsByTagName("name")[0]) {
            showName = x[0].getElementsByTagName("name")[0].childNodes[0].nodeValue;
        }
        // Venue 
        if (x[0].getElementsByTagName("venue")[0]) {
            showVenue = x[0].getElementsByTagName("venue")[0].childNodes[0].nodeValue;
        }
        // City
        if (x[0].getElementsByTagName("city")[0]) {
            showCity = x[0].getElementsByTagName("city")[0].childNodes[0].nodeValue;
        }
        // URL
        if (x[0].getElementsByTagName("url")[0]) {
            showUrl = x[0].getElementsByTagName("url")[0].childNodes[0].nodeValue;
        }
    
        // Construct output string.  Should be a 2 line 1 column table (2 cells)
        if (showDate) {
            tmpString = '<table><tr><td><span class="showDate">';
        if ( showUrl) {
           tmpString +='<a href="' + showUrl + '">';
        }
        tmpString += showDate;
        if ( showUrl) {
           tmpString += '</a>'; 
        }
        tmpString += '</span></td></tr>';
        
        if (showName) {
            tmpString += '<tr><td><span class="showname">' + showName + '</span></td></tr>';
        }
        if (showVenue) {
                tmpString += '<tr><td><span class="showVenue">' + showVenue + '</span></td></tr>';
        		if (showCity) {
                    tmpString += '<tr><td><span class="showAddress">' + showCity + '</span></tr></td>';
                } // end city
            } // end venue
            tmpString += '</table>';
        }// end date.
    
        if (tmpString) {
            OutputStr = tmpString;
        }
    }
    // Get handle to location for data.
    dest = document.getElementById("NextShowFeed");
    // Update inplace. 
    dest.innerHTML = OutputStr;
} //displayNextShowData


