function $(x){return document.getElementById(x);}

//following script code is from http://www.anyexample.com/webdev/javascript/ie7_javascript_prompt()_alternative.xml
// This is variable for storing callback function
var ae_cb = null;

// This is a main ae_prompt function
// it saves function callback 
// and sets up dialog
function ae_prompt(itemNum,cb) {
//function ae_prompt(q, a, cb) {
//	ae_cb = cb;
//	$('aep_t').innerHTML = document.domain + ' question:';
//	$('aep_prompt').innerHTML = q;
				var onePoem=buildPoem(poemArray[itemNum-1].itemNum.toString(),false);//alert(onePoem.toSource());
				var rdDiv=$('aep_prompt');
				while (rdDiv.hasChildNodes()){
					rdDiv.removeChild(rdDiv.childNodes[0]);//alert('deleted child node');
				}
				$('aep_prompt').appendChild(onePoem);
//	$('aep_prompt').innerHTML = buildOnePoem(itemNum);
//	$('aep_text').value = a;
	$('aep_ovrl').style.display = $('aep_ww').style.display = '';
	$('aep_text').focus();
//	$('aep_text').select();
}
 
// This function is called when user presses OK(m=0) or Cancel(m=1) button
// in the dialog. You should not call this function directly.
function ae_clk(m) {
	// hide dialog layers 
	$('aep_ovrl').style.display = $('aep_ww').style.display = 'none';
/*
	if (!m)  
		ae_cb(null);  // user pressed cancel, call callback with null
	else
		ae_cb($('aep_text').value); // user pressed OK 
*/
}
// preceding script code is from http://www.anyexample.com/webdev/javascript/ie7_javascript_prompt()_alternative.xml ...

function getXmlHttp(){
	if (typeof XMLHttpRequest != "undefined") return new XMLHttpRequest();
	if (typeof ActiveXObject != "undefined") return new ActiveXObject("MSXML2.XmlHttp");
	return false;
}
function PoemObj(itemNum,title,by){
	this.itemNum=itemNum;
	this.title=title;
	this.by=by;
}
var poemArray=[];
var maxWords=50;

function doPoemsOnload(){
	var poemStanzas=[];
	var stanzaLines=[];
	var xmlRqst=getXmlHttp();//alert("got XmlRqst");
	if(!xmlRqst){
		alert("No XMLHttpRequest object available. This functionality will not work.");
		return;
	}
	var sURL = "poetry.xml";//alert("0 I'm OK");
	xmlRqst.open("get", sURL , true);//alert("1 I'm OK");
	xmlRqst.onreadystatechange = function () {
		if (xmlRqst.readyState == 4) {
			var xmlDoc = xmlRqst.responseXML;//alert("2 I'm OK");
			var xmlPoems = xmlDoc.documentElement.getElementsByTagName("poem");
			var poemHTML="";
			var br;var nxtLn="";var wordCtr;var i;//alert(xmlPoems.length);
			for (var p=0;p<xmlPoems.length;p++) {
				br="";
				wordCtr=0;
				poemItemNum=xmlPoems[p].getAttribute("item");
				poemTitle=xmlPoems[p].getAttribute("title");
				poemBy=xmlPoems[p].getAttribute("by");
				poem=new PoemObj(poemItemNum,poemTitle,poemBy);
				var xmlPoemStanzas=xmlPoems[p].getElementsByTagName("stanza");//alert(xmlPoemStanzas.length);
				for (var ps=0;ps<xmlPoemStanzas.length;ps++){
					var xmlPoemStanzaLineItems=xmlPoemStanzas[ps].getElementsByTagName("sli");
					for (var psli=0;psli<xmlPoemStanzaLineItems.length;psli++){//alert(xmlPoemStanzaLineItems.length);
						var tmp=xmlPoemStanzaLineItems[psli].childNodes[0].nodeValue;
						tmp=tmp.replace(/\n|\t/g, " ");
						var ctr=0;
						var pttrn=new RegExp(/  /g);
						while((pttrn.test(tmp)) && (ctr<20))tmp=tmp.replace(/  /g," ");
						stanzaLines.push(tmp);
					}
					poemStanzas.push(stanzaLines);
					stanzaLines=[];
				}
				poem.poemText=poemStanzas;
				poemArray.push(poem);
				poemArray[poemItemNum]=poem;
				poemStanzas=[];
			}
			for (var i=0;i<poemArray.length;i++){
				var onePoem=buildPoem(poemArray[i].itemNum.toString(),true);
				$('poemZone').appendChild(onePoem);
			}//alert("3 I'm OK");
		}
	}
	xmlRqst.send(null);//alert("4 I'm OK");
}
function buildPoem(itemNum,partial){
	var myPoemObj=poemArray[itemNum.toString()];
	var poemDiv, poemStanza, poemStanzaLine, poemBr, clickBtn;
	poemDiv=document.createElement('div');
	if(partial)	poemDiv.className=('poemPart');
	else poemDiv.className=('poem');
	if (partial){
		clickBtn=document.createElement('a');
		clickBtn.setAttribute("href","javascript:ae_prompt('"+itemNum.toString()+"')");
		clickBtn.setAttribute("title","Click here to expand the full text of this poem");
		clickBtn.className="expandLink";
		clickBtnText=document.createTextNode('expand');
		clickBtn.appendChild(clickBtnText);
		poemDiv.appendChild(clickBtn);
	}
	poemDivH3=document.createElement('h3');
	poemDivH3.style.marginTop='-.5em';
	poemDivH4=document.createElement('h4');
	poemDivH3Text=document.createTextNode(myPoemObj.title);
	poemDivH4Text=document.createTextNode('\u2014\u00a0by '+myPoemObj.by);//\u escapes unicode 2014=&mdash, 00a0=&nbsp
	poemDivH3.appendChild(poemDivH3Text);
	poemDivH4.appendChild(poemDivH4Text);
	poemDiv.appendChild(poemDivH3);
	poemDiv.appendChild(poemDivH4);
	poemTextDiv=document.createElement('div');
	poemTextDiv.setAttribute('id','poem'+itemNum.toString());
	poemTextDiv.className=('poemText');
	for (var i=0;i<myPoemObj.poemText.length;i++){
		poemStanza=myPoemObj.poemText[i];
		poemStanzaP=document.createElement('p');
		for (var j=0;j<poemStanza.length;j++){
			if (j>0){
				poemBr=document.createElement('br');
				poemStanzaP.appendChild(poemBr);
			}
			poemStanzaLine=document.createTextNode(poemStanza[j]);
			poemStanzaP.appendChild(poemStanzaLine);
		}
		poemDiv.appendChild(poemStanzaP);
	}//alert(poemDiv.toSource());
return poemDiv;

	var rdDiv=$('timer');
	while (rdDiv.hasChildNodes()){
		rdDiv.removeChild(rdDiv.childNodes[0]);//alert('deleted child node');
	}
	$('timer').appendChild(timerBox);
}

//window.onload(doPoemsOnload());
