
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('vd10_drabbles.xml');
if (xmlDoc != null){
var drabdata=xmlDoc.getElementsByTagName("drab");
} // end if regarding valid xmlDoc

xmlDoc = null
loadXML('profiles.xml');
if (xmlDoc !=null){
var profdata=xmlDoc.getElementsByTagName("writer");
} // end if for second xmlDoc

xmlDoc = null
loadXML('vd10_picdata.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) - (14641))	// the normal version
//	var advdate = ((tdays + zoneoff) - (14610))	// the test version

//	var advdate = ((tdays + zoneoff) - (14276))	** the 2009 version**
//	var advdate = ((tdays + zoneoff) - (14245)) 	//	use this for January 1 equal to zero!**
//	var advdate = ((tdays + zoneoff) - (14194))	*** use this to go to December proper



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 VALENTINE-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) == ("valentine") )
			{adventindex[manualindex] = checkdrab;
			manualindex++}
		}

var groupindex = new Array(drabdata.length)	// variable which finds VDGROUP-status drabbles and assigns a 0-whatever number to the drabble number
	var gmanindex = 0;	// the number from 0-whatever which will be assigned, the highest will equal the num of group drabs minus one
				// gmanindex will get incremented again, though, so this variable tells how many there are
				// groupindex 0, 1, 2 etc will have numbers, the array will have lots of blanks
	for (checkgd=0; checkgd<drabdata.length; checkgd++)	// check every drabble
		{
		if ( (drabdata[checkgd].getElementsByTagName("status")[0].childNodes[0].nodeValue) == ("vdgroup") )
			{groupindex[gmanindex] = checkgd;
			gmanindex++}
		}

var featuredex = 0;	// need a universal variable for the function which scrolls through the Drabs on the 14th


//	var sharedindex = new Array(24)		// 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


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(28)	// create id names for table data cells
for (tci=0; tci<28; tci++)	
	{
	cellid[tci] = "cell";
	cellid[tci] = cellid[tci]+tci
	}

var vdcookie = GetCookie("vd10_data");

var cellrecord = new Array(28)		// record of cells viewed or not, n=no, y=yes
	for (rec=0; rec<cellrecord.length; rec++)
		{
		cellrecord[rec]="n";
		}

if (vdcookie != null)
	{
	var userrecord=vdcookie.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("vd10_data", "/");
	var expdate = new Date ();
	FixCookieDate (expdate);
	expdate.setTime (expdate.getTime() + (120 * 24 * 60 * 60 * 1000) ); // four months from now
	SetCookie("vd10_data", wholecookie, expdate, "/");

}	//end function





function showdailydrabble(drabnumber) {

if (advdate < drabnumber)
	{alert('Peeking ahead?  This Drabble will be available soon!');}
else
{
adix = adventindex[drabnumber]

document.dr_painting.src=imagelocation[adix]
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>");

if (paintername[adix] == "no info")
	{
	document.getElementById('theartist').innerHTML=("&ldquo;") + ("<i><a href=\"") + painterlink[adix] + ("\" target=\"_blank\">") + drabdata[adix].getElementsByTagName("arttitle")[0].childNodes[0].nodeValue + ("</a></i>") + ("&rdquo;");
	}

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>");
	}

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="24px"
document.getElementById(cellid[drabnumber]).style.backgroundImage="url(" + thumblocation[adix] + ")"
cellrecord[drabnumber] = "y"
makecookie();
} // end else for legal date
} // end function



function nextfeaturedaydrab() {
featuredex++
if (featuredex >= gmanindex) {featuredex=0}	// reset the count when it passes the number of available Drabbles
adix = groupindex[featuredex]
document.getElementById('navigaterow').innerHTML=("<input type=\"button\" class=\"another\" value=\"See another Drabble for this image\" onclick=\"nextfeaturedaydrab()\" />");
//	document.dr_painting.src=imagelocation[adix]
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>");
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 function




function showfeaturedrabble(drabnumber) {
if (advdate < drabnumber)
	{alert('Peeking ahead?  This Drabble will be available soon!');}
else
{

var ranfearaw = Math.random()*gmanindex;
var ranfeature = Math.floor(ranfearaw);
featuredex = ranfeature

adix = groupindex[featuredex]

document.getElementById('navigaterow').innerHTML=("<input type=\"button\" class=\"another\" value=\"See another Drabble for this image\" onclick=\"nextfeaturedaydrab()\" />");

document.getElementById('hidden_drabble').style.height="660px";
document.dr_painting.src=imagelocation[adix]
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>");
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>");
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:showfeaturedrabble(" + drabnumber + ")\">" + displaydate + "</a></td>");
document.getElementById(cellid[drabnumber]).style.fontSize="24px"
document.getElementById(cellid[drabnumber]).style.backgroundImage="url(" + thumblocation[13] + ")"

cellrecord[drabnumber] = "y"
makecookie();

} // 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";
document.getElementById('navigaterow').innerHTML="";
} // end function




function createdatecell(datenumber) {

adix = adventindex[datenumber]

if (datenumber==13)
	{
	document.write("<td class=\"spread\" rowspan=\"2\" colspan=\"3\" id=\"" + cellid[datenumber] + "\" background=\"desatthumbnails/" + thumblocation[adix] + "\">");
	var displaydate=(datenumber + 1);
	//	add displaydate minus one for diagnostic
	document.write("<a href=\"JavaScript:showfeaturedrabble(" + datenumber + ")\">" + displaydate + "</a></td>");
	}

else	{
	document.write("<td class=\"vdcell\" id=\"" + cellid[datenumber] + "\" background=\"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 blankspacer(){
document.write("<td class=\"spacercell\"></td>");
} // end function

function displaycalendar() {

document.write("<table id=\"adventrow_one\" class=\"treepart\">");
document.write("<tr>");
createdatecell(20);
createdatecell(15);
blankspacer();
blankspacer();
createdatecell(12);
createdatecell(23);
document.write("</tr></table>");

document.write("<table id=\"adventrow_two\" class=\"treepart\"><tr>");
createdatecell(1);
createdatecell(5);
createdatecell(10);
blankspacer();
createdatecell(16);
createdatecell(11);
createdatecell(21);
document.write("</tr></table>");

document.write("<table id=\"adventrow_three\" class=\"treepart\"><tr>");
createdatecell(14);
createdatecell(18);
createdatecell(4);
createdatecell(8);
createdatecell(0);
createdatecell(19);
createdatecell(24);
document.write("</tr></table>");


document.write("<table id=\"adventrow_four\" class=\"cntrpiece\"><tr>");
createdatecell(7);
createdatecell(25);
createdatecell(13);
createdatecell(26);
createdatecell(3);
document.write("</tr><tr>");
blankspacer();
createdatecell(27);
createdatecell(9);
blankspacer();
document.write("</tr></table>");

document.write("<table id=\"adventrow_five\" class=\"treepart\"><tr>");
createdatecell(6);
createdatecell(2);
createdatecell(22);
document.write("</tr></table>");

document.write("<table id=\"adventrow_six\" class=\"treepart\"><tr>");
createdatecell(17);
document.write("</tr></table>");



//	document.write("<img id=\"backtree\" name=\"backtree\" src=\"images/heartback.gif\" />");

//	document.write("<img src=\"images/twinkle.gif\" name=\"littlestar\" id=\"littlestar\" style=\"left:0px; top:-780px;\" />");

adix = adventindex[0]

document.write("<table id=\"hidden_drabble\" cellspacing=\"12\">");
document.write("<tr><td class=\"underpaint\" colspan=\"2\" id=\"navigaterow\"></td></tr>");
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("vduserdata", "/");
alert('refresh browser to see changes');
}


function showventdate() {
	alert(advdate);
}

function showmycookie() {
	alert(wholecookie);
}

function revealshindex() {
document.write("<table>");
for (rsx=0; rsx<24; rsx++)
	{document.write("<tr><td>" + rsx + "</td><td>" + sharedindex[rsx] + "</td></tr>");}
document.write("</table>");
}	// end function



