//This is the JavaScript file for the Quote of the Day widget.
// Copyright (c) 2011 by Matthew S. Harris

/* This code is expected to be referenced from a document containing the Quote of the Day (QotD) widget.
		For the widget to work, the data file (qod.xml) must be in the root folder of the domain, and the 
		parent/container document must have these elements:
		
		1)	The <body> tag must call showQOTD() for the onload event: <body onload="showQOTD();">
		
		2)	The <head> section must contain these two references (the first reference is this code, the second is the widget's style sheet):
				
						<script src="qotd.js" type="text/javascript"></script>        
						<link rel="stylesheet" href="qotd.css" type="text/css">
		
		3)	QotD widget needs a place to insert the quote; use this HTML to make widget container:
		
						<div id="qotdbox" class=qotd>
							Quote of the Day widget container
						</div>
						<div class=qotd style="text-align:center;border:0;padding:0;">
							<a onclick="randomQOTD();" href="#nowhere">Random Quote</a>
						</div>
	
	XML records are expected to be formatted thus (id attribute is a unique identifier for each record); <qd> is the day the quote
	belongs to, <qt> is the text of the quote, and <by> is the attribution to the quote:
		<qtr id="1">
			<qd>Jan 1</qd>
			<qt>the text of the quote</qt>
			<by>attribution of the quote</by>
		</qtr>
	
	For quotes which are a list, just put the list in the <qt> node (note that all text must be inside the list):
		<qtr id="1">
			<qd>Jan 1</qd>
			<qt>
				<ol>
					<li>list item one</li>
					<li>list item two</li>
				</ol>
			</qt>
			<by>attribution of the quote</by>
		</qtr>
*/		

// global variables
	var maxQ = 366;
// end global variables

//-------------------------------------------------------------------------------------
function openQOTDXML() {				// Open the qod.xml file and return a handle
	if (window.XMLHttpRequest) { xmlhttp=new XMLHttpRequest(); }   // code for IE7+, Firefox, Chrome, Opera, Safari
		else { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); }  // code for IE6, IE5

	xmlhttp.open("GET","http://www.didjiman.com/qod.xml",false);
	xmlhttp.send();
	return xmlhttp.responseXML;
}
//-------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
function getDateSelector() {
	// get today's date in the format "mmm nn"
	var month=new Array(12);
		month[0]="Jan"; month[1]="Feb"; month[2]="Mar";	month[3]="Apr";	month[4]="May";	month[5]="Jun";	month[6]="Jul";	month[7]="Aug";
		month[8]="Sep";	month[9]="Oct";	month[10]="Nov";	month[11]="Dec";
	var d = new Date();
	
	txt = d.getDate();
	txt = month[d.getMonth()] + " " + txt;
	return txt;
}
//-------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
function showQOTD() {			// show the quote for today
	ds = getDateSelector();																		// get the date selector
	xmlDoc = openQOTDXML();																		// open the quotes xml

	x = xmlDoc.getElementsByTagName("qd");
	var txt="";
	for (i=0;i<x.length;i++) {		// find the quote for today
		if (x[i].childNodes[0].nodeValue==ds) { // this is our match
			xtxt = xmlDoc.getElementsByTagName("qt")[i].childNodes[0].nodeValue;
			if (xtxt == null) { //it's an ol or ul list
				y = xmlDoc.getElementsByTagName("qt")[i].childNodes[0];
				txt = "<" + y.nodeName + " class=qotd>";			// insert the nodeName as an opening HTML tag
				for (k=0;k<y.childNodes.length;k++) {					// show the child nodes, they should be <li> elements			
					txt = txt + "<" + y.childNodes[k].nodeName	+ " class=qotd>";
					txt = txt + y.childNodes[k].childNodes[0].nodeValue;
					txt = txt + "</" + y.childNodes[k].nodeName	+ ">";
					}
				txt = txt + "</" + y.nodeName + ">";			// close outermost tag
			} else {
				txt = txt + xtxt;
			}	
			if (xmlDoc.getElementsByTagName("by")[i].childNodes[0] != null) { // there is attribution value to get
				txt = txt + "<p class=qotd>&ndash; <i>"  + 
				      xmlDoc.getElementsByTagName("by")[i].childNodes[0].nodeValue + "</i></p>"; }
			}
		}
	document.getElementById('qotdbox').innerHTML = txt + "<p style='margin:0;padding:0;text-align:right;font-size:7pt;'>" + ds + "</p>";
}
//-------------------------------------------------------------------------------------

//-------------------------------------------------------------------------------------
function randomQOTD() {			// pick a quote at random
	var qs = Math.floor(Math.random()* maxQ);  // get a random number
	xmlDoc = openQOTDXML();										 // open the quotes xml

	x = xmlDoc.getElementsByTagName("qtr");
	var txt="";
	for (i=0;i<x.length;i++) {		// find the random quote by id attribute
		if (x[i].getAttribute("id")==qs) { // this is our match
			xtxt = xmlDoc.getElementsByTagName("qt")[i].childNodes[0].nodeValue;
			if (xtxt == null) { //it's an ol or ul list
				y = xmlDoc.getElementsByTagName("qt")[i].childNodes[0];
				txt = "<" + y.nodeName + " class=qotd>";			// insert the nodeName as an opening HTML tag
				for (k=0;k<y.childNodes.length;k++) {					// show the child nodes, they should be <li> elements			
					txt = txt + "<" + y.childNodes[k].nodeName	+ " class=qotd>";
					txt = txt + y.childNodes[k].childNodes[0].nodeValue;
					txt = txt + "</" + y.childNodes[k].nodeName	+ ">";
					}
				txt = txt + "</" + y.nodeName + ">";			// close outermost tag
			} else {
				txt = txt + xtxt;
			}	
			if (xmlDoc.getElementsByTagName("by")[i].childNodes[0] != null) { // there is attribution value to get
				txt = txt + "<p class=qotd>&ndash; <i>"  + 
				      xmlDoc.getElementsByTagName("by")[i].childNodes[0].nodeValue + "</i></p>"; }
			}
		}
	document.getElementById('qotdbox').innerHTML = txt;
}
//-------------------------------------------------------------------------------------

