String.prototype.encodeHTML = function() {
	var s = this;
	s = s.replace(/</g,'&lt;');
	s = s.replace(/>/g,'&gt;');
	return s;
}

String.prototype.escapeQuotes = function() {
	var s = this;
	s = s.replace(/\'/g, "&#39;");
	s = s.replace(/\"/g, "&#34;");
	return s;
}

function dom_getElements(node, elt) {
    var list = node.getElementsByTagName(elt);
    return (list.length) ? list : node.getElementsByTagNameNS("*", elt); 
}

function new_getElementsByTagName(tagName, ns, prefix, scope){
		var elementListForReturn = scope.getElementsByTagName(prefix+':'+tagName);
		if(elementListForReturn.length == 0){
			elementListForReturn = scope.getElementsByTagName(tagName);
			if(elementListForReturn.length == 0){
				elementListForReturn = scope.getElementsByTagName('ns:'+tagName);
				if(elementListForReturn.length == 0 && document.getElementsByTagNameNS){
					elementListForReturn = scope.getElementsByTagNameNS(ns, tagName);
				}
			}
		}     
 
		return elementListForReturn;
    }

/*    
oDoc.setProperty("SelectionNamespaces", "xmlns:whatever='http://example.com/'");
var oDocEl = oDoc.documentElement;
var correctForCase3A = oDocEl.selectNodes('whatever:child');
var correctForCase3B = oDocEl.selectSingleNode('whatever:child');
*/    

function showDebug() {
        if(dbg) document.getElementById("debugDiv").innerHTML = document.getElementById("chan").innerHTML.encodeHTML();
}

/*
function removeDish(elem,from) {
  var d = document.getElementById(from);
  var olddiv = document.getElementById(elem);
  d.removeChild(olddiv);
}
*/
function renumber() {
  var d = document.getElementById('chan_items');
  var r1 = new RegExp("this,[0-9]{1,}","g");
  var r2 = new RegExp("[0-9]{1,}wrest","g");
  for (var i=0;i<d.childNodes.length;i++) {
  	d.childNodes[i].innerHTML = d.childNodes[i].innerHTML.replace(r1,'this,'+i);
  	d.childNodes[i].innerHTML = d.childNodes[i].innerHTML.replace(r2,i+'wrest');
  }
}

var dishClip;
function removeDish(o) {
  var d = o.parentNode.parentNode;
  var olddiv = o.parentNode;
  dishClip=d.removeChild(olddiv);
  showDebug();
  //alert(dishClip.childNodes[0].value);
}

var categoryClip;
function removeCategory(o) {
  var d = document.getElementById('chan_items');
  var olddiv = o.parentNode.parentNode;
  categoryClip=d.removeChild(olddiv);
  renumber();
  showDebug();
  //alert(clipboard.childNodes[0].value);
}

function insertDish(o) {
	if(dishClip==null) dishClip=newDish();
    var d = o.parentNode.parentNode;
    var newdiv = o.parentNode;
  	d.insertBefore(dishClip,newdiv);
  	dishClip=null;
  	renumber();
    showDebug();
}

function newCategory() {
  var myDiv = document.createElement('div');
  myDiv.setAttribute('id','item');
  myDiv.innerHTML = startTitle(0) + "" + endTitle(0);
  myDiv.appendChild(newDish());
  myDiv.innerHTML += endItemTag;
  return myDiv;
}

function insertCategory(o) {
	if(categoryClip==null) {
		categoryClip=newCategory();
	}
    var d = document.getElementById('chan_items');
    var newdiv = o.parentNode.parentNode;
  	d.insertBefore(categoryClip,newdiv);
  	categoryClip=null;
  	renumber();
    showDebug();
}

function addDish(title,description,i,j) {
        var my_html='';
        //var RSS=o;
        //my_html += "<div id='dish" + j + "'>";
        //my_html += "<div id='dish'>";
        my_html += (title == null) ? "" : startDishTitle(i) + title + endDishTitle;
        my_html += (description == null) ? "" : startDishDescription(i) + description + endDescription;
        my_html += deleteButton ;
        my_html += insertButton;
        //my_html += "</div>";
        return my_html;
}
function newDish() {
  var myDiv = document.createElement('div');
  myDiv.setAttribute('id','dish');
  myDiv.innerHTML = addDish('','',i,0);
  return myDiv;
}
function addNewDish(o,i) {
  o.parentNode.insertBefore(newDish(),o.parentNode.lastChild);
  showDebug();
}
function clearClips() {
	dishClip=null;
	categoryClip=null;
	alert("Clipboard Cleared!");
}
function revert(m) {
	categoryClip=null;
	dishClip=null;
	getRSS(m);
	alert("Menu Reverted!");
}

// example rss feed: http://www.onthesnow.com/colorado/snow.rss
//OBJECTS

//objects inside the RSS2Item object
function RSS2Enclosure(encElement)
{
	if (encElement == null)
	{
		this.url = null;
		this.length = null;
		this.type = null;
	}
	else
	{
		this.url = encElement.getAttribute("url");
		this.length = encElement.getAttribute("length");
		this.type = encElement.getAttribute("type");
	}
}

function RSS2Guid(guidElement)
{
	if (guidElement == null)
	{
		this.isPermaLink = null;
		this.value = null;
	}
	else
	{
		this.isPermaLink = guidElement.getAttribute("isPermaLink");
		this.value = guidElement.childNodes[0].nodeValue;
	}
}

function RSS2Source(souElement)
{
	if (souElement == null)
	{
		this.url = null;
		this.value = null;
	}
	else
	{
		this.url = souElement.getAttribute("url");
		this.value = souElement.childNodes[0].nodeValue;
	}
}

//object containing the RSS 2.0 item
function RSS2Item(itemxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//optional vars
	this.author;
	this.comments;
	this.pubDate;

	//optional objects
	this.category;
	this.enclosure;
	this.guid;
	this.source;

	this.dish = new Array();
	//var dishes = dom_getElements(itemxml, 'wrest:dish')
	//var dishes = itemxml.getElementsByTagName('wrest:dish');
	var dishes = new_getElementsByTagName('dish', "http://www.wilshirerestaurant.com/wrest_rss_namespace/", 'wrest', itemxml);
//alert("DL " +dishes.length);	
//alert("ItemXML " + itemxml.textContent);	
	for (var i=0; i<dishes.length; i++)
	{
		var dishTitle='';
		var dishDescription='';
		if (!window.ActiveXObject) {
			if(dishes[i].childNodes[0].nextSibling) { 
				dishTitle=dishes[i].childNodes[0].nextSibling.textContent.escapeQuotes();
			}
			//dishDescription=dishes[i].childNodes[1].nextSibling.textContent;
			if(dishes[i].childNodes[1].nextSibling)  {
				dishDescription=dishes[i].childNodes[1].nextSibling.nextSibling.textContent;
			}
//if(i==0) alert('Dish Title: ' + dishTitle + ' Desc: ' + dishDescription);
		} else {
			dishTitle=dishes[i].childNodes[0].childNodes[0].nodeValue.escapeQuotes();
			dishDescription=dishes[i].childNodes[1].childNodes[0].nodeValue;
		}
		this.dish.push({ "title":dishTitle,"description":dishDescription });
	}
	var properties = new Array("title", "link", "description", "author", "comments", "pubDate");
	var tmpElement = null;
	//alert("Item: " + itemxml.getElementsByTagName("ots:resort_rss_link")[0].childNodes[0].nodeValue);
	for (var i=0; i<properties.length; i++)
	{
		eval("this."+properties[i]+"=''");      // make sure something is there!
		tmpElement = itemxml.getElementsByTagName(properties[i])[0];
		if (tmpElement != null && tmpElement.childNodes[0] != null)
//alert(tmpElement.childNodes[0].nodeValue.replace(/\'/g, "&#39;"));		
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
	}

	//alert("Item: " + itemxml.getElementsByTagName("ots:resort_rss_link")[0].childNodes[0].nodeValue);
	//this.link = itemxml.getElementsByTagName("ots:resort_rss_link")[0].childNodes[0].nodeValue;
	//this.link = new RSS2SpecialLink(itemxml.getElementsByTagName("ots:resort_rss_link")[0]);
	this.category = new RSS2Category(itemxml.getElementsByTagName("category")[0]);
	this.enclosure = new RSS2Enclosure(itemxml.getElementsByTagName("enclosure")[0]);
	this.guid = new RSS2Guid(itemxml.getElementsByTagName("guid")[0]);
	this.source = new RSS2Source(itemxml.getElementsByTagName("source")[0]);
}

//objects inside the RSS2Channel object
function RSS2SpecialLink(linkElement)
{
	if (linkElement == null)
	{
		this.link = null;
		this.value = null;
	}
	else
	{
		this.link = linkElement.getAttribute("domain");
		this.value = linkElement.childNodes[0].nodeValue;
	}
}

function RSS2Category(catElement)
{
	if (catElement == null)
	{
		this.domain = null;
		this.value = null;
	}
	else
	{
		this.domain = catElement.getAttribute("domain");
		this.value = catElement.childNodes[0].nodeValue;
	}
}

//object containing RSS image tag info
function RSS2Image(imgElement)
{
	if (imgElement == null)
	{
	this.url = null;
	this.link = null;
	this.width = null;
	this.height = null;
	this.description = null;
	}
	else
	{
		imgAttribs = new Array("url","title","link","width","height","description");
		for (var i=0; i<imgAttribs.length; i++)
			if (imgElement.getAttribute(imgAttribs[i]) != null)
				eval("this."+imgAttribs[i]+"=imgElement.getAttribute("+imgAttribs[i]+")");
	}
}

//object containing the parsed RSS 2.0 channel
function RSS2Channel(rssxml)
{
	//required
	this.title;
	this.link;
	this.description;

	//array of RSS2Item objects
	this.items = new Array();

	//optional vars
	this.language;
	this.copyright;
	this.managingEditor;
	this.webMaster;
	this.pubDate;
	this.lastBuildDate;
	this.generator;
	this.docs;
	this.ttl;
	this.rating;

	//optional objects
	this.category;
	this.image;

	var chanElement = rssxml.getElementsByTagName("channel")[0];
	var itemElements = rssxml.getElementsByTagName("item");
	for (var i=0; i<itemElements.length; i++)
	{
		Item = new RSS2Item(itemElements[i]);
		this.items.push(Item);
		//chanElement.removeChild(itemElements[i]);
	}

	var properties = new Array("title", "link", "description", "language", "copyright", "managingEditor", "webMaster", "pubDate", "lastBuildDate", "generator", "docs", "ttl", "rating");
	var tmpElement = null;
	for (var i=0; i<properties.length; i++)
	{
		tmpElement = chanElement.getElementsByTagName(properties[i])[0];
		if (tmpElement!= null) {
			eval("this."+properties[i]+"=tmpElement.childNodes[0].nodeValue");
		}
	}

	this.category = new RSS2Category(chanElement.getElementsByTagName("category")[0]);
	this.image = new RSS2Image(chanElement.getElementsByTagName("image")[0]);
}

//PROCESSES
var printit=false;
//uses xmlhttpreq to get the raw rss xml
function getRSS(s)
{
	var r = s.toLowerCase();
	r = r.replace(/ /,"-");
	//call the right constructor for the browser being used
	if (window.ActiveXObject)
		xhr = new ActiveXObject("Microsoft.XMLHTTP");
	else if (window.XMLHttpRequest)
		xhr = new XMLHttpRequest();
	else
		alert("not supported");
	//prepare the xmlhttprequest object
	//xhr.open("GET","http://www.greenunixtest.com/wr/rss/" + s,true);
	xhr.open("GET","../rss/" + s + "?date=" + Math.random(),true);
	//xhr.open("GET",document.rssform.rssurl.value,true);
	//alert("xdomain.php?state="+s);
	//xhr.open("GET","http://www.greenlightdevelopment.com/monsterhutch/xdomain.php?state="+r,true);
	xhr.setRequestHeader("Cache-Control", "no-cache");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4)
		{
			if (xhr.status == 200)
			{
				if (xhr.responseText != null) {
					processRSS(xhr.responseXML);
					if(printit) window.print();
				}
				else
				{
					alert("Failed to receive RSS file from the server - file not found.");
					return false;
				}
			}
			else
				alert("Error code " + xhr.status + " received: " + xhr.statusText);
		}
	}

	//send the request
	xhr.send(null);
}

//processes the received rss xml
function processRSS(rssxml)
{
	RSS = new RSS2Channel(rssxml);
	showRSS(RSS);
}

//shows the RSS content in the browser
function showRSS(RSS)
{
	//populate channel data
	//var properties = new Array("title","link","description","pubDate","copyright");
	var properties = new Array("title","description","copyright");
	for (var i=0; i<properties.length; i++)
	{
		eval("document.getElementById('chan_"+properties[i]+"').innerHTML = ''");
		curProp = startTag(properties[i]) + eval("RSS."+properties[i]) + endTag();
		if (curProp != null)
			eval("document.getElementById('chan_"+properties[i]+"').innerHTML = curProp");
	}

	//show the image
	document.getElementById("chan_image_link").innerHTML = "";
	if (RSS.image.src != null)
	{
		document.getElementById("chan_image_link").href = RSS.image.link;
		document.getElementById("chan_image_link").innerHTML = imageTag
			+" alt='"+RSS.image.description
			+"' width='"+RSS.image.width
			+"' height='"+RSS.image.height
			+"' src='"+RSS.image.url
			+"' "+"/>";
	}

	//populate the items
	document.getElementById("chan_items").innerHTML = "";
	for (var i=0; i<RSS.items.length; i++)
	{
		item_html = startItemTag;
		item_html += (RSS.items[i].title == null) ? "" : startTitle(i) + RSS.items[i].title + endTitle(i);
		//item_html += (RSS.items[i].description == null) ? "" : startDescription(i) + RSS.items[i].description + endDescription;
		//item_html +='<br>';
		for(var j=0;j<RSS.items[i].dish.length;j++) {
		       // item_html += "<div id='dish" + j + "'>";
		        item_html += "<div id='dish'>";
		        item_html += addDish(RSS.items[i].dish[j].title,RSS.items[i].dish[j].description,i,j);
		        item_html += "</div>";
		}
		//item_html += (RSS.items[i].link == null) ? "" : startLink + RSS.items[i].link + endLink;
		item_html += endItemTag;
		document.getElementById("chan_items").innerHTML += item_html;
		item_html='';
		//if(RSS.items[i].dish) alert("dish: " + RSS.items[i].dish[2]);
	}
	document.getElementById("chan_items").innerHTML += endCategoryTag;
	showDebug();
	//we're done
	//document.getElementById("chan").style.visibility = "visible";
	return true;
}

var xhr;


