
function GetXMLdoc(xmlFile){
xmlDoc.async = false;
xmlDoc.load(xmlFile);
}

function loadXML(xmlFile){
if (window.ActiveXObject) {// code for IE
try {xmlDoc = new ActiveXObject("Msxml2.XMLHTTP"); // newer IE
GetXMLdoc(xmlFile);}
catch(e){
try{xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); // older IE
GetXMLdoc(xmlFile);}
catch(e){}}} // exit without xmlDoc}
else if (window.XMLHttpRequest) { // Mozilla, Safari,...
xmlHTTP = new XMLHttpRequest();
if (xmlHTTP.overrideMimeType) {xmlHTTP.overrideMimeType('text/xml')}
xmlHTTP.open('get', xmlFile, false);
xmlHTTP.send('');
xmlDoc = xmlHTTP.responseXML;}
else if (document.implementation.createDocument) { // code for other
xmlDoc = document.implementation.createDocument('','',null) ;
GetXMLdoc(xmlFile);}
else{
alert('Your browser JavaScript is enabled, but cannot handle this script');// if all else fails display message
}
}

var xmlDoc = null;
loadXML('advent11_drabbles.xml');
if (xmlDoc != null){
var drabdata=xmlDoc.getElementsByTagName("drab");
} // end if regarding valid xmlDoc

xmlDoc = null
loadXML('advent11_profiles.xml');
if (xmlDoc !=null){
var profdata=xmlDoc.getElementsByTagName("writer");
} // end if for second xmlDoc

xmlDoc = null
loadXML('advent11_picturedata.xml');
if (xmlDoc !=null){
var picdata=xmlDoc.getElementsByTagName("picture");
} // end if for third xmlDoc

var d = new Date();
var t = d.getTime();	// time since Jan 1 1970 in milliseconds
var tdays = (t/86400000);	// time since Jan 1 1970 in days
var zoneoff = ( (d.getTimezoneOffset()) / (-1440) );	// negative offset in days
var advdate = ((tdays + zoneoff) - (15309)) 	//	use this for December 1 equal to zero!**
//	var advdate = ((tdays + zoneoff) - (15279))	//	*** use this for November 1 equal zero (2011)
//	var advdate = ((tdays + zoneoff) - (15248))	// ** use this to show everything no matter what, Oct 1 or so equals zero




var writerlink = new Array(drabdata.length)
	for (wl=0; wl<writerlink.length; wl++)
		{
		var checkprof = 0;
		var matched = "false";
		writerlink[wl] = "writers.html";
		while ( (checkprof < profdata.length) && (matched=="false") )
			{
			if ( (drabdata[wl].getElementsByTagName("author")[0].childNodes[0].nodeValue) == (profdata[checkprof].getElementsByTagName("person")[0].childNodes[0].nodeValue) )
				{writerlink[wl] = profdata[checkprof].getElementsByTagName("link1")[0].childNodes[0].nodeValue
				matched="true"}
			checkprof++
			}
		}


var paintername = new Array(drabdata.length)
var painterlink = new Array(drabdata.length)
var imagelocation = new Array(drabdata.length)
var thumblocation = new Array(drabdata.length)
	for (pn=0; pn<paintername.length; pn++)
		{
		var checkpaint = 0;
		var matched = "false";
		paintername[pn] = "no artist data";
		painterlink[pn] = "no artist link";
		imagelocation[pn] = "no image link";
		thumblocation[pn] = "no thumb link";
		while ( (checkpaint < picdata.length) && (matched=="false") )
			{
			if ( (drabdata[pn].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue) == (picdata[checkpaint].getElementsByTagName("picname")[0].childNodes[0].nodeValue) )
				{paintername[pn] = picdata[checkpaint].getElementsByTagName("picartist")[0].childNodes[0].nodeValue
				painterlink[pn] = picdata[checkpaint].getElementsByTagName("picartistlink")[0].childNodes[0].nodeValue
				imagelocation[pn] = picdata[checkpaint].getElementsByTagName("imagelink")[0].childNodes[0].nodeValue
				thumblocation[pn] = picdata[checkpaint].getElementsByTagName("thumblink")[0].childNodes[0].nodeValue
				matched="true"}
			checkpaint++
			}
		}


var adventindex = new Array(drabdata.length)	// variable which finds ADVENT-status-drabbles and assigns a 0-whatever number to the drabble number
	var manualindex = 0;		// the number from 0-whatever which will be assigned
	for (checkdrab=0; checkdrab<drabdata.length; checkdrab++)	// check every drabble
		{
		if ( (drabdata[checkdrab].getElementsByTagName("status")[0].childNodes[0].nodeValue) == ("advent") )
			{adventindex[manualindex] = checkdrab;
			manualindex++}
		}



var sharedindex = new Array(31)		// the shared drabble value associated with each main drabble, -1 being no shared drabble
	for (shix=0; shix<sharedindex.length; shix++)
		{sharedindex[shix] = -1;}

var sh_check = 0;		// a manually incremented variable to check each Drabble in the database
var found_shared = "false";	// an indicator that a match has been found


//	The following sequences assigns shared drabbles, in the order appearing, to calendar drabble 2
//	NOTE: This is irrelevant to the 2010 calendar, where all drabbles should be 'advent'
//	The sharedadvent status applies to a second drabble appearing in the database, and immediately following another drabble
//	...that uses the same image.
while ( (sh_check < drabdata.length) && (found_shared == "false") )
	{
	if (drabdata[sh_check].getElementsByTagName("status")[0].childNodes[0].nodeValue == "sharedadvent")
		{sharedindex[1] = sh_check
		found_shared = "true"}
	sh_check++
	}
	found_shared = "false"



var adix = 0;	// a dynamic variable which will be set to the index number, so that the next Advent Drabble will be shown and not just any Drabble
var shndix = 0;	// a dynamic variable which will be set to the shared drabble's index number

var cellid = new Array(31)	// create id names for table data cells
for (tci=0; tci<31; tci++)	
	{
	cellid[tci] = "cell";
	cellid[tci] = cellid[tci]+tci
	}

var crcookie = GetCookie("user11data");

var cellrecord = new Array(31)		// record of cells viewed or not, n=no, y=yes
	for (rec=0; rec<cellrecord.length; rec++)
		{
		cellrecord[rec]="n";
		}

if (crcookie != null)
	{
	var userrecord=crcookie.split("**");
	for (cr=0; cr<userrecord.length; cr++)
		{
		cellrecord[cr] = userrecord[cr]
		}
	}


var wholecookie = " ";	//	the datastring to be built




function makecookie()	{
wholecookie = cellrecord[0]	// the first bit
for (bc=1; bc<(cellrecord.length); bc++)	// leaving out the first
	{
	wholecookie = wholecookie + "**" + cellrecord[bc]
	}	// end for loop
	DeleteCookie("user11data", "/");
	var expdate = new Date ();
	FixCookieDate (expdate);
	expdate.setTime (expdate.getTime() + (120 * 24 * 60 * 60 * 1000) ); // four months from now
	SetCookie("user11data", wholecookie, expdate, "/");

}	//end function





function showdailydrabble(drabnumber) {

if (advdate < drabnumber)
	{alert('Peeking ahead?  This Drabble will be available soon!');}
else
{
adix = adventindex[drabnumber]
shndix = sharedindex[drabnumber]

if (drabnumber==24) {
		cellrecord[drabnumber] = "y"
		makecookie();
		window.location="cmas11.html#cmas11";}

else {
document.dr_painting.src=imagelocation[adix]
if ( (sharedindex[drabnumber] == -1) || (drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue != drabdata[shndix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue) )
	{document.getElementById('astory').innerHTML=drabdata[adix].getElementsByTagName("drabtext")[0].childNodes[0].nodeValue + ("<p class=\"writertag\"> - <i><a href=\"" + writerlink[adix] + "\">" + drabdata[adix].getElementsByTagName("author")[0].childNodes[0].nodeValue + "</a></i></p>");}
else
	{document.getElementById('astory').innerHTML=drabdata[adix].getElementsByTagName("drabtext")[0].childNodes[0].nodeValue + ("<p class=\"writertag\"> - <i><a href=\"" + writerlink[adix] + "\">" + drabdata[adix].getElementsByTagName("author")[0].childNodes[0].nodeValue + "</a></i></p><hr /><br />" + drabdata[shndix].getElementsByTagName("drabtext")[0].childNodes[0].nodeValue + "<p class=\"writertag\"> - <i><a href=\"../../" + writerlink[shndix] + "\">" + drabdata[shndix].getElementsByTagName("author")[0].childNodes[0].nodeValue + "</a></i></p>");}

if (paintername[adix] == "shownotitle")
	{
	document.getElementById('theartist').innerHTML=("&nbsp;");
	}	// end if for show-no-title option.  NOTE THAT THIS OPTION MUST BE INDICATED WITH THE ARTIST NAME

else if (paintername[adix] == "none")
	{
	document.getElementById('theartist').innerHTML=("&nbsp;") + ("&ldquo;") + ("<a href=\"") + painterlink[adix] + ("\" target=\"_blank\">") + drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue + ("</a>") + ("&rdquo;");
	}	// end if for no artist with title serving as link


else if (painterlink[adix] == "none")
	{
	document.getElementById('theartist').innerHTML=("&ldquo;") + drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue + ("&rdquo;<br />") + ("<i>") + paintername[adix] + ("</i>");
	}	// end if for artist with no link

else
	{
	document.getElementById('theartist').innerHTML=("&ldquo;") + drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue + ("&rdquo;<br />") + ("<i><a href=\"") + painterlink[adix] + ("\" target=\"_blank\">") + paintername[adix] + ("</a></i>");
	}	// end if for artist with link


document.getElementById('hidden_drabble').style.zIndex="4";

var displaydate=(drabnumber + 1);	//	simple correction because java counts from zero
document.getElementById(cellid[drabnumber]).innerHTML=("<a href=\"JavaScript:showdailydrabble(" + drabnumber + ")\">" + displaydate + "</a></td>");
document.getElementById(cellid[drabnumber]).style.fontSize="16px"
document.getElementById(cellid[drabnumber]).style.backgroundImage="url(" + thumblocation[adix] + ")"
cellrecord[drabnumber] = "y"
makecookie();
} // end else for not-a-shared-drabble
} // end else for legal date
} // end function



function hidedaily() {
document.dr_painting.src="paintings/loading.jpg";
document.getElementById('hidden_drabble').style.zIndex="-1";
document.getElementById('astory').innerHTML="blank";
document.getElementById('hidden_drabble').style.height="400px";
} // end function




function createdatecell(datenumber) {

adix = adventindex[datenumber]

if (Math.floor((datenumber+1)/3) == (datenumber/3) )
	{document.write("<td class=\"one\" id=\"" + cellid[datenumber] + "\" background=\"advent11_desatthumbnails/" + thumblocation[adix] + "\">");}

if (Math.floor((datenumber+1)/3) < (datenumber/3) )
	{document.write("<td class=\"two\" id=\"" + cellid[datenumber] + "\" background=\"advent11_desatthumbnails/" + thumblocation[adix] + "\">");}

if (Math.floor((datenumber+1)/3) > (datenumber/3) )
	{document.write("<td class=\"three\" id=\"" + cellid[datenumber] + "\" background=\"advent11_desatthumbnails/" + thumblocation[adix] + "\">");}

var displaydate=(datenumber + 1);
//	add displaydate minus one for diagnostic

document.write("<a href=\"JavaScript:showdailydrabble(" + datenumber + ")\">" + displaydate + "</a></td>");

if ( (cellrecord[datenumber]) == "y" )
	{
	document.getElementById(cellid[datenumber]).style.fontSize="16px"
	document.getElementById(cellid[datenumber]).style.backgroundImage="url(" + thumblocation[adix] + ")"
	}

} // end function



function displaycalendar() {

document.write("<table id=\"adventrow_one\" class=\"treepart\">");
document.write("<tr>");
createdatecell(20);
document.write("</tr></table>");

document.write("<table id=\"adventrow_two\" class=\"treepart\"><tr>");
createdatecell(12);
createdatecell(1);
document.write("</tr></table>");

document.write("<table id=\"adventrow_three\" class=\"treepart\"><tr>");
createdatecell(5);
createdatecell(23);
createdatecell(8);
document.write("</tr></table>");

document.write("<table id=\"adventrow_four\" class=\"treepart\"><tr>");
createdatecell(16);
createdatecell(11);
createdatecell(21);
createdatecell(7);
document.write("</tr></table>");

document.write("<table id=\"adventrow_five\" class=\"treepart\"><tr>");
createdatecell(14);
createdatecell(18);
createdatecell(24);
createdatecell(4);
createdatecell(28);
document.write("</tr></table>");

document.write("<table id=\"adventrow_six\" class=\"treepart\"><tr>");
createdatecell(0);
createdatecell(19);
createdatecell(15);
createdatecell(26);
createdatecell(3);
createdatecell(9);
document.write("</tr></table>");

document.write("<table id=\"adventrow_seven\" class=\"treepart\"><tr>");
createdatecell(10);
createdatecell(25);
createdatecell(30);
createdatecell(27);
createdatecell(2);
createdatecell(29);
createdatecell(17);
document.write("</tr></table>");

document.write("<table id=\"adventrow_eight\" class=\"treepart\"><tr>");
createdatecell(22);
createdatecell(6);
createdatecell(13);
document.write("</tr></table>");

//	document.write("<img id=\"backtree\" name=\"backtree\" src=\"images/treeback.gif\" />");

document.write("<img src=\"images/advent10_images/twinkle.gif\" name=\"littlestar\" id=\"littlestar\" style=\"left:0px; top:-881px;\" />");

adix = adventindex[0]

document.write("<table id=\"hidden_drabble\" cellspacing=\"12\">");

document.write("<tr><td class=\"painting\">");
document.write("<img src=\"paintings/loading.jpg\" id=\"dr_painting\" name=\"dr_painting\" alt=\"drabble picture\" />");
document.write("</td>");

document.write("<td class=\"story\" rowspan=\"2\" id=\"astory\">");
document.write(drabdata[adix].getElementsByTagName("drabtext")[0].childNodes[0].nodeValue);
document.write("<p class=\"writertag\"> - <i><a href=\"../../" + writerlink[adix] + "\">" + drabdata[adix].getElementsByTagName("author")[0].childNodes[0].nodeValue + "</a></i></p>");
document.write("</td></tr>");

document.write("<tr><td class=\"underpaint\" id=\"theartist\">");
document.write("&ldquo;" + drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue + "&rdquo;<br />");
document.write("<i><a href=\"" + painterlink[adix] + "\" target=\"_blank\">" + paintername[adix] + "</a></i>");
document.write("</td></tr>");

document.write("<tr><td colspan=\"2\"><a href=\"JavaScript:hidedaily()\">click to close</a></td></tr>");

document.write("</table>");

}

//	test-value functions below

function undomycookie() {
	DeleteCookie("user11data", "/");
alert('refresh browser to see changes');
}


function showventdate() {
	alert(advdate);
}

function showmycookie() {
	alert(wholecookie);
}

function revealshindex() {
document.write("<table>");
for (rsx=0; rsx<31; rsx++)
	{document.write("<tr><td>" + rsx + "</td><td>" + sharedindex[rsx] + "</td></tr>");}
document.write("</table>");
}	// end function




