// ======================================================================================
// --------------------------------------------------------------------------------------
// CSS Display Functions
//     - greenMouseover
//     - greenMouseout
//     - blueMouseover
//     - blueMouseout
// Random Functions
//     - getURLVar
//     - isUnique
//     - isIn
//     - BrowserDetect.init
//     - pausecomp
//     - getCurrDir
// XML Importing Functions
//     - importXML
//     - MWJ_checkXMLLoad
// Calendar Functions
//     - getMonthWord
//     - getMonthNo
//     - getMonthLong
//     - processCalXML
//     - createTable
//     - createBoldDates
//     - createXMLPar
//     - printEvents
//     - printCal
// News Feed Functions
//     - processFeed
//     - createTable2
//     - printRSS
// --------------------------------------------------------------------------------------
// ======================================================================================


// ======================================================================================
// --------------------------------------------------------------------------------------
// CSS DISPLAY FUNCTIONS
// --------------------------------------------------------------------------------------
// ======================================================================================

// --------------------------------------------------------------------------------------
// greenMouseover - mouseover effect in header.php (top menu)
// --------------------------------------------------------------------------------------
function greenMouseover(element0, element1, element2) {
    element0.className = 'header_top_over'
    element1.className = 'header_bottom_over'
    element2.className = 'header_top_over'
}

// --------------------------------------------------------------------------------------
// greenMouseout - mouseout effect in header.php (top menu)
// --------------------------------------------------------------------------------------
function greenMouseout(element0, element1, element2) {
    element0.className = 'header_top_out'
    element1.className = 'header_bottom_out'
    element2.className = 'header_top_out'
}

// --------------------------------------------------------------------------------------
// blueMouseover - mouseover effect in footer.php (bottom menu)
// --------------------------------------------------------------------------------------
function blueMouseover(element1, element2) {
    element1.className = 'gray_menu_over'
    element2.className = 'plain_white'
}

// --------------------------------------------------------------------------------------
// blueMouseout - mouseout effect in footer.php (bottom menu)
// --------------------------------------------------------------------------------------
function blueMouseout(element1, element2) {
    element1.className = 'gray_menu_out'
    element2.className = 'plain_black'
}


// ======================================================================================
// --------------------------------------------------------------------------------------
// RANDOM FUNCTIONS
// --------------------------------------------------------------------------------------
// ======================================================================================

// --------------------------------------------------------------------------------------
// getURLVar - retrieves a GET variable from the URL by the name of urlVarName
// --------------------------------------------------------------------------------------
function getURLVar(urlVarName) {
    //divide the URL in half at the '?'
    var urlHalves = String(document.location).split('?')
    var urlVarValue = ''
    if(urlHalves[1]) {
        //load all the name/value pairs into an array
        var urlVars = urlHalves[1].split('&')
        //loop over the list, and find the specified url variable
        for(i=0; i<=(urlVars.length); i++) {
            if(urlVars[i]) {
                //load the name/value pair into an array
                var urlVarPair = urlVars[i].split('=')
                if (urlVarPair[0] && urlVarPair[0] == urlVarName) {
                    // found a var that matches, load it's value into the return var
                    urlVarValue = urlVarPair[1];
                }
            }
        }
    }
    return urlVarValue;
}

// --------------------------------------------------------------------------------------
// isUnique - searches for >1 instance of an element in an array (compares the elements
//            as Strings
// --------------------------------------------------------------------------------------
function isUnique(arr, element) {
    var instances = 0
    for(k=0; k<arr.length; k++) {
        if(arr[k].toString() == element.toString()) {
            instances++
        }
    }
    return (instances <= 1)
}

// --------------------------------------------------------------------------------------
// isIn - checks for the existence of an element in the array
// --------------------------------------------------------------------------------------
function isIn(arr, element) {
    for(ii=0; ii<arr.length; ii++) {
        if(arr[ii] == element) {
            return true
        }
    }
    return false
}

// --------------------------------------------------------------------------------------
// BrowserDetect variable - detects browser make/version when BrowserDetect.init() run
// --------------------------------------------------------------------------------------
var BrowserDetect = {
    init: function () {
	this.browser = this.searchString(this.dataBrowser) || "An unknown browser"
	this.version = this.searchVersion(navigator.userAgent)
		|| this.searchVersion(navigator.appVersion)
		|| "an unknown version"
	this.OS = this.searchString(this.dataOS) || "an unknown OS"
    },
    searchString: function (data) {
	for (var i=0;i<data.length;i++)	{
	    var dataString = data[i].string
            var dataProp = data[i].prop
            this.versionSearchString = data[i].versionSearch || data[i].identity
	    if (dataString) {
		if (dataString.indexOf(data[i].subString) != -1) return data[i].identity
	    }
	    else if (dataProp) return data[i].identity
        }
    },
    searchVersion: function (dataString) {
	var index = dataString.indexOf(this.versionSearchString);
	if (index == -1) return;
	return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
    },
    dataBrowser: [
	{ 	string: navigator.userAgent,
		subString: "OmniWeb",
		versionSearch: "OmniWeb/",
		identity: "OmniWeb"
	},
	{
		string: navigator.vendor,
		subString: "Apple",
		identity: "Safari"
	},
	{
		prop: window.opera,
		identity: "Opera"
	},
	{
		string: navigator.vendor,
		subString: "iCab",
		identity: "iCab"
	},
	{
		string: navigator.vendor,
		subString: "KDE",
		identity: "Konqueror"
	},
	{
		string: navigator.userAgent,
		subString: "Firefox",
		identity: "Firefox"
	},
	{
		string: navigator.vendor,
		subString: "Camino",
		identity: "Camino"
	},
	{		// for newer Netscapes (6+)
		string: navigator.userAgent,
		subString: "Netscape",
		identity: "Netscape"
	},
	{
		string: navigator.userAgent,
		subString: "MSIE",
		identity: "Explorer",
		versionSearch: "MSIE"
	},
	{
		string: navigator.userAgent,
		subString: "Gecko",
		identity: "Mozilla",
		versionSearch: "rv"
	},
	{ 		// for older Netscapes (4-)
		string: navigator.userAgent,
		subString: "Mozilla",
		identity: "Netscape",
		versionSearch: "Mozilla"
	}
	],
    dataOS : [
	{
		string: navigator.platform,
		subString: "Win",
		identity: "Windows"
	},
	{
		string: navigator.platform,
		subString: "Mac",
		identity: "Mac"
	},
	{
		string: navigator.platform,
		subString: "Linux",
		identity: "Linux"
	}
    ]
};
BrowserDetect.init();

// --------------------------------------------------------------------------------------
// pausecomp - a wait function
// --------------------------------------------------------------------------------------
function pausecomp(millis) {
    var date = new Date();
    var curDate = null;
    do { curDate = new Date(); }
    while(curDate-date < millis);
}

// --------------------------------------------------------------------------------------
// getCurrDir - get directory of the current file
// --------------------------------------------------------------------------------------
function getCurrDir() {
    var myloc = window.location.href
    var locarray = myloc.split("/")
    delete locarray[(locarray.length-1)]
    return locarray.join("/")
}



// ======================================================================================
// --------------------------------------------------------------------------------------
// XML IMPORTING FUNCTIONS
// --------------------------------------------------------------------------------------
// ======================================================================================

// --------------------------------------------------------------------------------------
// importXML - reads the XML doc using an XMLHttpRequest
// --------------------------------------------------------------------------------------
function importXML( oURL, oFunct, oNoRand, oDelay, toggle) {
    var MWJ_ldD = window.MWJ_ldD
    // note: in XML importing event handlers, 'this' refers to window
    if(!oNoRand) { oURL += ( ( oURL.indexOf('?') + 1 ) ? '&' : '?' ) + ( new Date() ).getTime() } //prevent cache
    if(window.XMLHttpRequest) {
        //alternate XMLHTTP request - Gecko, Safari 1.2+ and Opera 7.6+
        MWJ_ldD[MWJ_ldD.length] = new XMLHttpRequest()
        MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 && MWJ_ldD['+(MWJ_ldD.length-1)+'].status < 300 ) { '+oFunct+'(MWJ_ldD['+(MWJ_ldD.length-1)+'].responseXML); }' )
        MWJ_ldD[MWJ_ldD.length-1].open("GET", oURL, toggle)
        MWJ_ldD[MWJ_ldD.length-1].send(null)
        return true
    }
    if(!navigator.__ice_version && window.ActiveXObject) {
        //the Microsoft way - IE 5+/Win (ICE produces errors and fails to use try-catch correctly)
        var activexlist = ['Microsoft.XMLHTTP','Microsoft.XMLDOM'], tho; //add extra progids if you need specifics
        for(var i = 0; !tho && i < activexlist.length; i++) {
            try { tho = new ActiveXObject( activexlist[i] ); } catch(e) { }
        }
        if(tho) {
            MWJ_ldD[MWJ_ldD.length] = tho
            MWJ_ldD[MWJ_ldD.length-1].onreadystatechange = new Function( 'if( MWJ_ldD['+(MWJ_ldD.length-1)+'].readyState == 4 ) { '+oFunct+'(MWJ_ldD['+(MWJ_ldD.length-1)+'].load?MWJ_ldD['+(MWJ_ldD.length-1)+']:MWJ_ldD['+(MWJ_ldD.length-1)+'].responseXML); }' )
            if(MWJ_ldD[MWJ_ldD.length-1].load) {
                MWJ_ldD[MWJ_ldD.length-1].load(oURL)
            } else {
                MWJ_ldD[MWJ_ldD.length-1].open('GET', oURL, true)
//                MWJ_ldD[MWJ_ldD.length-1].open('GET', oURL, toggle)
                MWJ_ldD[MWJ_ldD.length-1].send(null)
            }
            return true
        }
    }
    if(document.createElement && document.childNodes) {
        // load the XML in an iframe
        var ifr = document.createElement('DIV')
        ifr.style.visibility = 'hidden'; ifr.style.position = 'absolute'; ifr.style.top = '0px'; ifr.style.left = '0px'
        // onload only fires in Opera so I use a timer for all
        if(!window.MWJ_XML_timer) { window.MWJ_XML_timer = window.setInterval('MWJ_checkXMLLoad();',100); }
        ifr.innerHTML = '<iframe src="'+oURL+'" name="MWJ_XML_loader_'+MWJ_ldD.length+'" height="0" width="0"><\/iframe>'
        MWJ_ldD[MWJ_ldD.length] = oFunct+'MWJ_SPLIT'+(oDelay?oDelay:1)+''
        document.body.appendChild(ifr)
        return true
    }
    return false
}

// --------------------------------------------------------------------------------------
// MWJ_checkXMLLoad - check to see that the XML loaded properly
// --------------------------------------------------------------------------------------
function MWJ_checkXMLLoad() {
    // check if each imported file is available (huge files may not have loaded completely - nothing I can do - use the delay to help)
    for(var x = 0; x < MWJ_ldD.length; x++) {
        if( MWJ_ldD[x] && window.frames['MWJ_XML_loader_'+x] ) {
            setTimeout( MWJ_ldD[x].split('MWJ_SPLIT')[0] + '(window.frames.MWJ_XML_loader_'+x+'.window.document);', parseInt(MWJ_ldD[x].split('MWJ_SPLIT')[1]) )
	    MWJ_ldD[x] = false
        }
    }
}



// ======================================================================================
// --------------------------------------------------------------------------------------
// CALENDAR FUNCTIONS
// --------------------------------------------------------------------------------------
// ======================================================================================

// --------------------------------------------------------------------------------------
// getMonthWord - converts a month in numeric form to its abbreviation
// --------------------------------------------------------------------------------------
function getMonthWord(monthx) {
    if(monthx == 1) { return "jan" }
    if(monthx == 2) { return "feb" }
    if(monthx == 3) { return "mar" }
    if(monthx == 4) { return "apr" }
    if(monthx == 5) { return "may" }
    if(monthx == 6) { return "jun" }
    if(monthx == 7) { return "jul" }
    if(monthx == 8) { return "aug" }
    if(monthx == 9) { return "sep" }
    if(monthx == 10) { return "oct" }
    if(monthx == 11) { return "nov" }
    if(monthx == 12) { return "dec" }
}

// --------------------------------------------------------------------------------------
// getMonthNo - converts a month in abbreviated text form to its numeric equivalent
// --------------------------------------------------------------------------------------
function getMonthNo(monthy) {
    if(monthy == "jan") { return 1 }
    if(monthy == "feb") { return 2 }
    if(monthy == "mar") { return 3 }
    if(monthy == "apr") { return 4 }
    if(monthy == "may") { return 5 }
    if(monthy == "jun") { return 6 }
    if(monthy == "jul") { return 7 }
    if(monthy == "aug") { return 8 }
    if(monthy == "sep") { return 9 }
    if(monthy == "oct") { return 10 }
    if(monthy == "nov") { return 11 }
    if(monthy == "dec") { return 12 }
}

// --------------------------------------------------------------------------------------
// getMonthLong - given a month as a number or abbreviation, returns the full name
// --------------------------------------------------------------------------------------
function getMonthLong(month) {
    if(month == 1 || month == "jan") return "January"
    if(month == 2 || month == "feb") return "February"
    if(month == 3 || month == "mar") return "March"
    if(month == 4 || month == "apr") return "April"
    if(month == 5 || month == "may") return "May"
    if(month == 6 || month == "jun") return "June"
    if(month == 7 || month == "jul") return "July"
    if(month == 8 || month == "aug") return "August"
    if(month == 9 || month == "sep") return "September"
    if(month == 10 || month == "oct") return "October"
    if(month == 11 || month == "nov") return "November"
    if(month == 12 || month == "dec") return "December"
}   

// --------------------------------------------------------------------------------------
// processCalXML - loads the XML calendar (specified by month/year) and stores its data
//                 on the window
// --------------------------------------------------------------------------------------
function processCalXML(togglePreset) {
//alert("processCalXML")
    window.dates = new Array()
    window.titles = new Array()
    window.descriptions = new Array()
    window.MWJ_ldD = []
    var month = window.month
    var year = window.year
    if(month != null && year != null) {
        var toggle = true
        if(BrowserDetect.browser == "Safari" || BrowserDetect.browser == "IE") toggle = false
        else toggle = togglePreset
        importXML(getCurrDir()+'calendar/'+month+year+'.xml','createTable', false, 2000, toggle)
        // hack to prevent some inconsistencies when loading in IE (caching?)
        pausecomp(500)
    }
}

// --------------------------------------------------------------------------------------
// createTable - called by processCalXML; breaks down data in calendar XML doc and stores
//               its data on the window
// --------------------------------------------------------------------------------------
function createTable(xmlDoc) {
//alert("createTable")
    var dates = window.dates
    var titles = window.titles
    var descriptions = window.descriptions
    var category = window.category
    var keepers = new Array()
    var keepersInc = 0
    var newDates = new Array()
    var newTitles = new Array()
    var newDescriptions = new Array()
    /* Konqueror insists that the timer can keep running until there are 20 alerts (the file is still in the list of files to
    be checked until at least one alert is dismissed), so I use a timer to delay the alert and allow my script to stop
    checking the file */
    if( xmlDoc.documentElement && xmlDoc.documentElement.tagName && xmlDoc.documentElement.tagName.toUpperCase() == 'HTML' ) {
        setTimeout('alert(\'Script aborted.\');',50)
        return
    }
    /* the rest of this function is taken from PPK's script */
    var x = xmlDoc.getElementsByTagName('event')
    // Pocket IE fails here because the ActiveX control provides very basic DOM for the reponseXML, but the native DOM support is non-existent

    for(i=0; i<x.length; i++) {
    for(j=0; j<x[i].childNodes.length; j++) {
    if(x[i].childNodes[j].nodeType != 1) continue
        if(x[i].childNodes[j].nodeName == "date") {
            dates[i] = x[i].childNodes[j].firstChild.nodeValue
        }
        else if(x[i].childNodes[j].nodeName == "title") {
            titles[i] = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue)
        }
        else if(x[i].childNodes[j].nodeName == "description") {
            // pull each paragraph
            var parArray = new Array()
            var index = 0
            // for each paragraph
            for(k=0; k<x[i].childNodes[j].childNodes.length; k++) {
                if(x[i].childNodes[j].childNodes[k].nodeType != 1) continue
                if(x[i].childNodes[j].childNodes[k].nodeName == "dates") {
                    var copy = createBoldDates(x[i].childNodes[j].childNodes[k])
                    if(k == x[i].childNodes[j].childNodes.length) {
                        parArray[index++] = createXMLPar(2, copy, true)
                    }
                    else {
                        parArray[index++] = createXMLPar(2, copy, false)
                    }
                }
                else if(x[i].childNodes[j].childNodes[k].nodeName == "textpar") {
                    if(k == x[i].childNodes[j].childNodes.length) {
                        parArray[index++] = createXMLPar(1, x[i].childNodes[j].childNodes[k].firstChild.nodeValue, true)
                    }
                    else {
                        parArray[index++] = createXMLPar(1, x[i].childNodes[j].childNodes[k].firstChild.nodeValue, false)
                    }
                }
                else if(x[i].childNodes[j].childNodes[k].nodeName == "htmlpar") {
                    if(k == x[i].childNodes[j].childNodes.length) {
                        parArray[index++] = createXMLPar(0, x[i].childNodes[j].childNodes[k].firstChild.nodeValue, true)
                    }
                    else {
                        parArray[index++] = createXMLPar(0, x[i].childNodes[j].childNodes[k].firstChild.nodeValue, false)
                    }
                }
            }
            descriptions[i] = parArray
        }
        else if(x[i].childNodes[j].nodeName == "category") {
            if(category != null && x[i].childNodes[j].firstChild.nodeValue == category) {
                keepers[keepersInc++] = i
            }
        }
    }
    }
    // clean up array, that was sloppy
    var keepIndex = 0
    for(m = 0; m<keepersInc; m++) {
        keepIndex = keepers[m]
        newDates[m] = dates[keepIndex]
        newTitles[m] = titles[keepIndex]
        newDescriptions[m] = descriptions[keepIndex]
    }
    if(window.category != null) {
        window.dates = newDates
        window.titles = newTitles
        window.descriptions = newDescriptions
    }
    else {
        window.dates = dates
        window.titles = titles
        window.descriptions = descriptions
    }
    window.done = "true"
    printCal()
    printEvents()
}

// --------------------------------------------------------------------------------------
// createBoldDates - called by createTable; creates a DOM 'P' element containing the
//                date/dateline info from the XML doc (and formats)
// --------------------------------------------------------------------------------------
function createBoldDates(grabFromNode) {
    var xx
    var boldText = document.createElement('B')
    for(xx = 0; xx < grabFromNode.childNodes.length; xx++) {
        if(grabFromNode.childNodes[xx].nodeType != 1) continue
        boldText.appendChild(document.createTextNode(grabFromNode.childNodes[xx].firstChild.nodeValue))
        if(xx != grabFromNode.childNodes.length-1) {
            boldText.appendChild(document.createElement('BR'))
        }
    }
    return boldText
}


// --------------------------------------------------------------------------------------
// createXMLPar - called by createTable; creates a DOM 'P' element containing the
//                paragraph passed in
// --------------------------------------------------------------------------------------
function createXMLPar(autoflag, node, last) {
    var description_p = document.createElement('P')
    if(last) {
        description_p.setAttribute('className', 'body bottom_margin')
        description_p.className = 'body bottom_margin'
    }
    else {
        description_p.setAttribute('className', 'body')
        description_p.className = 'body'
    }
    if(autoflag == 0) {
        description_p.innerHTML = node
    }
    else if(autoflag == 1) {
        description_p.appendChild(document.createTextNode(node))
    }
    else {
        description_p.appendChild(node)
    }
    return description_p
}

// --------------------------------------------------------------------------------------
// printEvents - displays the current events with their descriptions
// --------------------------------------------------------------------------------------
function printEvents() {
    var dates = window.dates
    var titles = window.titles
    var descriptions = window.descriptions
    var other = 2
    var news = document.createElement('DIV')
    for(i=0; i<titles.length; i++) {
        if(descriptions[i] != null) {
            var headline_div = document.createElement('DIV')
            if(isUnique(dates, dates[i])) {
                headline_div.setAttribute('id', month+dates[i])
            }
            else {
                headline_div.setAttribute('id', month+dates[i]+'_'+other++)
            }
            headline_div.setAttribute('className', 'headline')
            headline_div.className = 'headline'
            var h2 = document.createElement('H2')
            h2.appendChild(titles[i])
            headline_div.appendChild(h2)
            news.appendChild(headline_div)
            // description paragraphs
            var description_div = document.createElement('DIV')
            for(j=0; j<descriptions[i].length; j++) {
                var temp = descriptions[i]
                description_div.appendChild(temp[j])
            }
            news.appendChild(description_div)
        }
    }
    if(titles.length == 0) {
        var dax = new Date()
        var yx = dax.getFullYear()
        var mx = getMonthLong(dax.getMonth()+1)
        var noItems = document.createTextNode("  There are no current items in this category.")
        news.appendChild(noItems)
    }
    document.getElementById('writeroot').appendChild(news)
}

// --------------------------------------------------------------------------------------
// printCal - prints the calendar on the events page
// --------------------------------------------------------------------------------------
function printCal() {
    var dates = window.dates
    var titles = window.titles
    var descriptions = window.descriptions
    var month = window.month
    var year = window.year
    var new_month = getMonthNo(month)
    var new_year = parseFloat(year) + 2000
    var curmonth = new_month
    var curyear = new_year
    var mainTable = document.createElement('TABLE')
    var mainTableBody = document.createElement('TBODY')
    mainTable.border="1"
    mainTable.width = "525"
    mainTable.height = "100"
    mainTable.cellPadding = "0"
    mainTable.cellSpacing = "0"
    mainTable.border = "0"
    var mainTr = document.createElement('TR')
    mainTr.vAlign = "top"
    var mainTd1 = document.createElement('TD')
    mainTd1.style.background = "#eeeeee"
    mainTd1.width = "350"
    var table1 = document.createElement('TABLE')
    var tbody1 = document.createElement('TBODY')
    table1.cellPadding = "5"
    table1.cellSpacing = "0"
    table1.border = "0"
    table1.width = "350"
    var longMonth = getMonthLong(month)
    var other = 2
	
	
	// Insert a new row at the top of the calendar list.
	/*if((month == "jan" || month == "feb" || month == "mar") && (year == "09")) {
        var newTR1 = document.createElement('TR')
        newTR1.style.background = "#bbbbbb";
        var newTD1 = document.createElement('TD')
        newTD1.width = "350"
        var content1 = document.createElement('B')
        content1.appendChild(document.createTextNode("NOW TAKING RESERVATIONS"))
        newTD1.appendChild(content1)
        newTR1.appendChild(newTD1)
        tbody1.appendChild(newTR1)
        var newTR2 = document.createElement('TR')
        newTR2.style.background = "#eeeeee"
        var newTD2 = document.createElement('TD')
        newTD2.appendChild(document.createTextNode("Nasher Travel: Chicago"))
        var newP = document.createElement('P')
        newP.className = "calendar_item"
        var newA = document.createElement('A')
        newA.className = "plain_gray"
        newA.setAttribute("href", "http://www.NasherTravel.com")
        var moreLink = document.createTextNode("> more info")
        newA.appendChild(moreLink)
        newP.appendChild(newA)
        newTD2.appendChild(newP)
        newTR2.appendChild(newTD2)
        tbody1.appendChild(newTR2)
        table1.appendChild(tbody1)
    } */
	
    for(i=0; i<titles.length; i++) {
        var newTR1 = document.createElement('TR')
        newTR1.style.background = "#bbbbbb";
        var newTD1 = document.createElement('TD')
        newTD1.width = "350"
        var content1 = document.createElement('B')
        content1.appendChild(document.createTextNode(longMonth+" "+dates[i]))
        newTD1.appendChild(content1)
        newTR1.appendChild(newTD1)
        tbody1.appendChild(newTR1)
        var newTR2 = document.createElement('TR')
        newTR2.style.background = "#eeeeee"
        var newTD2 = document.createElement('TD')
        newTD2.appendChild(document.createTextNode(titles[i].nodeValue))
        if(descriptions[i] != null) {
            var newP = document.createElement('P')
            newP.className = "calendar_item"
            var newA = document.createElement('A')
            newA.className = "plain_gray"
            if(isUnique(dates, dates[i])) {
                newA.setAttribute("href", "#"+month+dates[i])
            }
            else {
                newA.setAttribute("href", "#"+month+dates[i]+'_'+other++)
            }
            var moreLink = document.createTextNode("> more info")
            newA.appendChild(moreLink)
            newP.appendChild(newA)
            newTD2.appendChild(newP)
        }
        newTR2.appendChild(newTD2)
        tbody1.appendChild(newTR2)
        table1.appendChild(tbody1)
    }

    mainTd1.appendChild(table1)
    mainTr.appendChild(mainTd1)
    var mainTd2 = document.createElement('TD')
    var spacerImg = document.createElement('IMG')
    spacerImg.src = getCurrDir()+"images/spacer.gif"
    spacerImg.height = "1"
    spacerImg.width = "10"
    mainTd2.appendChild(spacerImg)
    mainTr.appendChild(mainTd2)

    var mainTd3 = document.createElement('TD')
    var m = curmonth
    var y = curyear
    var cM = "calendar"
    var cH = "calendar_header"
    var cDW = "calendar_rows"
    var cD = "calendar_cols"
    var brdr = 0

    var mn=['January','February','March','April','May','June','July','August','September','October','November','December'];
    var dim=[31,0,31,30,31,30,31,31,30,31,30,31];

    var oD = new Date(y, m-1, 1); //DD replaced line to fix date bug when current day is 31st
    oD.od=oD.getDay()+1; //DD replaced line to fix date bug when current day is 31st

    var todaydate=new Date() //DD added
    var scanfortoday=(y==todaydate.getFullYear() && m==todaydate.getMonth()+1)? todaydate.getDate() : 0 //DD added

    var last_month = ""
    var next_month = ""
    if(m == 1) {
        last_month = "dec"
        next_month = "feb"
        last_year = y-2001
        next_year = y-2000
    }
    else if(m == 12) {
        last_month = "nov"
        next_month = "jan"
        last_year = y-2000
        next_year = y-1999
    }
    else {
        last_month = getMonthWord(m-1)
        next_month = getMonthWord(m+1)
        last_year = y-2000
        next_year = y-2000
    }

    if(last_year < 10) { last_year = "0" + last_year }
    if(next_year < 10) { next_year = "0" + next_year }

    var back_link = ""
    var front_link = ""

    //special case
    if(m == 12 && y == 2007) {
        back_link = "events_nov07.php";
        front_link = "events.php?m=jan&y=08"
    }
    else {
        back_link = "events.php?m="+last_month+"&y="+last_year;
        front_link = "events.php?m="+next_month+"&y="+next_year;
    }

    dim[1]=(((oD.getFullYear()%100!=0)&&(oD.getFullYear()%4==0))||(oD.getFullYear()%400==0))?29:28;

    var mainDiv_F = document.createElement('DIV')
    mainDiv_F.className = cM
    var mainTable_F = document.createElement('TABLE')
    mainTable_F.className = cM
    mainTable_F.cellPadding = "3"
    mainTable_F.border = brdr
    mainTable_F.cellSpacing = "0"
    mainTable_F.style.backgroundColor = "#eeeeee"
    var mainTbody_F = document.createElement('TBODY')
    var tr1 = document.createElement('TR')
    tr1.align = "center"
    var td1 = document.createElement('TD')
    td1.className = cH
    var div1 = document.createElement('DIV')
    div1.style.fontSize = "11pt"
    div1.style.lineHeight = "12pt"
    var a1 = document.createElement('A')
    a1.className = "plain_black"
    a1.href = back_link
    var backText = document.createTextNode("<<")
    a1.appendChild(backText)
    div1.appendChild(a1)
    td1.appendChild(div1)
    tr1.appendChild(td1)
    var td2 = document.createElement('TD')
    td2.colSpan = "5"
    td2.align = "center"
    td2.className = cH
    var middleText = document.createTextNode(mn[m-1]+' '+y)
    td2.appendChild(middleText)
    tr1.appendChild(td2)
    var td3 = document.createElement('TD')
    td3.className = cH
    var div2 = document.createElement('DIV')
    div2.style.fontSize = "11pt"
    div2.style.lineHeight = "12pt"
    var a2 = document.createElement('A')
    a2.className = "plain_black"
    a2.href = front_link
    var frontText = document.createTextNode(">>")
    a2.appendChild(frontText)
    div2.appendChild(a2)
    td3.appendChild(div2)
    tr1.appendChild(td3)
    mainTbody_F.appendChild(tr1)
    var tr2 = document.createElement('TR')
    tr2.align = "center"
    for(s=0;s<14;s+=2) {
        var tempTd = document.createElement('TD')
        tempTd.className = cDW
        var tempB = document.createElement('B')
        var tempText = document.createTextNode("SuMoTuWeThFrSa".substr(s,2))
        tempB.appendChild(tempText)
        tempTd.appendChild(tempB)
        tr2.appendChild(tempTd)
    }
    mainTbody_F.appendChild(tr2)
    var daysWritten = 0
    var firstDay = new Date()
    firstDay.setMonth(m-1)
    firstDay.setDate(1)
    firstDay.setFullYear(y)
    var blankDays = firstDay.getDay()
    for(i=0; (i<7 && daysWritten<dim[m-1]); i++) {
        var tempTr = document.createElement('TR')
        if(i == 0) {
            for(j=0; j<blankDays; j++) {
                var tempTd = document.createElement('TD')
                tempTd.className = cD
                tempTr.appendChild(tempTd)
            }
            for(j=0; j<(7-blankDays); j++) {
                var tempTd = document.createElement('TD')
                tempTd.className = cD
                tempTd.align = "center"
                if(isIn(dates, daysWritten+1)) {
                    var tempB = document.createElement('B')
                    var tempText = document.createTextNode(++daysWritten)
                    tempB.appendChild(tempText)
                    tempTd.appendChild(tempB)
                }
                else {
                    var tempText = document.createTextNode(++daysWritten)
                    tempTd.appendChild(tempText)
                }
                tempTr.appendChild(tempTd)
            }
        }
        else {
            // 2nd, 3rd, 4th, etc. week in the month, making sure not to go over dim[m-1]
            for(j=0; j<7; j++) {
                if(daysWritten == dim[m-1]) {
                    // blank TD
                    var tempTd = document.createElement('TD')
                    tempTd.className = cD
                    tempTr.appendChild(tempTd)
                }
                else {
                    var tempTd = document.createElement('TD')
                    tempTd.className = cD
                    tempTd.align = "center"
                    if(isIn(dates, daysWritten+1)) {
                        var tempB = document.createElement('B')
                        var tempText = document.createTextNode(++daysWritten)
                        tempB.appendChild(tempText)
                        tempTd.appendChild(tempB)
                    }
                    else {
                        var tempText = document.createTextNode(++daysWritten)
                        tempTd.appendChild(tempText)
                    }
                    tempTr.appendChild(tempTd)
                }
            }
        }
        mainTbody_F.appendChild(tempTr)
    }

    var lastTr = document.createElement('TR')
    var lastTd = document.createElement('TD')
    lastTd.colSpan = "7"
    lastTd.className = cH
    var lastP = document.createElement('P')
    lastP.className = "short"
    var lastText = document.createTextNode("Please note: All events at the Nasher Museum of Art are free with admission and open to the public, unless otherwise noted.")
    lastP.appendChild(lastText)
    lastTd.appendChild(lastP)
    lastTr.appendChild(lastTd)
    mainTbody_F.appendChild(lastTr)
    mainTable_F.appendChild(mainTbody_F)
    mainDiv_F.appendChild(mainTable_F)
    mainTd3.appendChild(mainDiv_F)
    mainTr.appendChild(mainTd3)
    mainTableBody.appendChild(mainTr)
    mainTable.appendChild(mainTableBody)

    document.getElementById('writeroot0').appendChild(mainTable)
}



// ======================================================================================
// --------------------------------------------------------------------------------------
// NEWS FEED FUNCTIONS
// --------------------------------------------------------------------------------------
// ======================================================================================

// --------------------------------------------------------------------------------------
// processFeed - loads the news feed from an XML doc and stores its data on the window
// --------------------------------------------------------------------------------------
function processFeed() {
    window.titles = new Array()
    window.links = new Array()
    window.descriptions = new Array()
    window.MWJ_ldD = []
    var toggle = true
    if(BrowserDetect.browser == "Safari" || BrowserDetect.browser == "Opera" || BrowserDetect.browser == "IE") toggle = false
    importXML(getCurrDir()+'news.xml','createTable2', false, 2000, toggle)
    // hack to prevent some inconsistencies when loading in IE (caching?)
    pausecomp(1000)
}

// --------------------------------------------------------------------------------------
// createTable2 - called by processFeed; extract data from XML and attach to window
// --------------------------------------------------------------------------------------
function createTable2(xmlDoc) {
    var links = window.links
    var titles = window.titles
    var descriptions = window.descriptions
    /* Konqueror insists that the timer can keep running until there are 20 alerts (the file is still in the list of files to
    be checked until at least one alert is dismissed), so I use a timer to delay the alert and allow my script to stop
    checking the file */
    if( xmlDoc.documentElement && xmlDoc.documentElement.tagName && xmlDoc.documentElement.tagName.toUpperCase() == 'HTML' ) {
        setTimeout('alert(\'Script aborted.\');',50); return
    }
    /* the rest of this function is taken from PPK's script */
    var x = xmlDoc.getElementsByTagName('item')
    // Pocket IE fails here because the ActiveX control provides very basic DOM for the reponseXML, but the native DOM support is non-existent

    for(i=0; i<x.length; i++) {
        for(j=0; j<x[i].childNodes.length; j++) {
            if(x[i].childNodes[j].nodeType != 1) continue
            if(x[i].childNodes[j].nodeName == "title") {
                titles[i] = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue)
            }
            else if(x[i].childNodes[j].nodeName == "link") {
                links[i] = x[i].childNodes[j].firstChild.nodeValue
            }
            else if(x[i].childNodes[j].nodeName == "description") {
                descriptions[i] = document.createTextNode(x[i].childNodes[j].firstChild.nodeValue)
            }
        }
    }
    window.links = links
    window.titles = titles
    window.descriptions = descriptions
    printRSS()
}

// --------------------------------------------------------------------------------------
// printRSS - prints news feed items using data stored on window by createTable2
// --------------------------------------------------------------------------------------
function printRSS() {
    var links = window.links
    var titles = window.titles
    var descriptions = window.descriptions
    var news = document.createElement('DIV')
    for(i=2; i<titles.length; i++) {
        var paragraph = document.createElement('P')
        var boldDiv = document.createElement('DIV')
        var link = document.createElement('A')
        link.className = "blue"
        link.setAttribute('id', 'news'+i)
        link.setAttribute('href', links[i])
        var bold = document.createElement('B')
        bold.appendChild(titles[i])
        link.appendChild(bold)
        boldDiv.appendChild(link)
        paragraph.appendChild(boldDiv)
        // description
        if(descriptions[i] != null) {
            var italicDiv = document.createElement('DIV')
//            var italic = document.createElement('I')
//            italic.appendChild(descriptions[i])
//            italicDiv.appendChild(italic)
            italicDiv.appendChild(descriptions[i])
            paragraph.appendChild(italicDiv)
        }
        news.appendChild(paragraph)
    }
    document.getElementById('writeroot').appendChild(news)
    for(i=0; i<titles.length; i++) {
        // inconsistent results setting class name... make sure it is set
        document.getElementById('news'+i).className = 'plain_black'
    }
}