/**
 * Javascript for AJAX Support
 * Calgary Callies.com
 */

   function checkdate(checkyear,checkmonth,checkday){
			var myDate=new Date();
			myDate.setFullYear(checkyear,checkmonth,checkday);
			var today = new Date();
			
			if (myDate>today)
				{
				return(true);
				}
			else
				{
				return(false);
				}
			}

    function makeRequest(url) {
        var httpRequest;
		var ifModifiedSince = new Date(0);
		
        if (window.XMLHttpRequest) { // Mozilla, Safari, ...
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
                // See note below about this line
            }
        } 
        else if (window.ActiveXObject) { // IE
            try {
                httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
                } 
                catch (e) {
                           try {
                                httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                               } 
                             catch (e) {}
                          }
                                       }

        if (!httpRequest) {
            alert('Giving up:( Cannot create an XMLHTTP instance');
            return false;
        }

        httpRequest.open('GET', url, true);
		httpRequest.onreadystatechange = function() { alertContents(url,httpRequest); };
		httpRequest.setRequestHeader("If-Modified-Since", ifModifiedSince);
        httpRequest.send('');

    }

    function alertContents(typeurl, httpRequest){
		if (typeurl == 'sked.xml') {
		
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
					var xmlDoc = httpRequest.responseXML;
					var gameList = xmlDoc.getElementsByTagName('game');
					var Sched = "";
					
					if (gameList.length <= 0) {
						document.getElementById("sked").innerHTML = "No Games";
					}
					else {
						for (i = 0; i < gameList.length; i++) {
						
							gamemonth = gameList[i].getElementsByTagName('month').item(0).firstChild.data;
							gameyear = gameList[i].getElementsByTagName('year').item(0).firstChild.data;
							gameday = gameList[i].getElementsByTagName('day').item(0).firstChild.data;
							gamedatestring = gameList[i].getElementsByTagName('datestring').item(0).firstChild.data;
							gameopp = gameList[i].getElementsByTagName('opp').item(0).firstChild.data;
							
							if (checkdate(gameyear, gamemonth, gameday) == true) {
								document.getElementById("sked").innerHTML = "VS: " + gameopp + " on " + gamedatestring;
								break;
							}
							else {
								document.getElementById("sked").innerHTML = "No Games";
							}
						}
					}
				}
				else {
					document.getElementById("sked").innerHTML = "<p>There was a problem with the request.</p><p>Check XML File</p>";
				}
			}
			
		}
		else {
			if (httpRequest.readyState == 4) {
				if (httpRequest.status == 200) {
				
					var xmldoc = httpRequest.responseXML;
					var root_node = xmldoc.getElementsByTagName('story')[0];
					var story = root_node.getElementsByTagName('content').item(0).firstChild.data;
					var storydate = root_node.getElementsByTagName('date').item(0).firstChild.data;
					var headline = root_node.getElementsByTagName('headline').item(0).firstChild.data;
					document.getElementById("topstorydate").innerHTML = storydate;
					document.getElementById("topstory").innerHTML = story;
					document.getElementById("topstoryheadline").innerHTML = headline;
							
				}
				else {
					document.getElementById("topstory").innerHTML = "<p>There was a problem with the request.</p><p>Check XML File</p>";
				}
			}
		}
		}
  
         