function doxhr(url){
	if(!document.getElementById || !document.createTextNode){return;}		
	var request;
	try{
		request = new XMLHttpRequest();
	}catch(error){
		try{
			request = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(error){
			document.getElementById("tipContent").innerHTML = "Please upgrade your browser";
			return;
		}
	}
		
	request.open('get',url,true);
	request.onreadystatechange=function(){			
		if(request.readyState == 4){
			if (request.status && /200|304/.test(request.status))
			{
				retrieved(request);
			} else{
				failed(request);
			}
		}
	}
	request.setRequestHeader('If-Modified-Since','Wed, 05 Apr 2006 00:00:00 GMT');
	request.send(null);		
}

function retrieved(requester) {		
	var data=requester.responseXML;
	var tip = data.getElementsByTagName("snippet");
	tipsCount = tip.length - 1;
	for(var i = 0; i < tip.length; i++) {
		tips[i] = tip[i].firstChild.nodeValue;
		titles[i] = tip[i].getAttribute("title");
		authors[i] = tip[i].getAttribute("author");
	}
	obj = document.getElementById("tipContent");
	var text ='<h3>' + titles[0] + '</h3>';
	 
	if (authors[0] != "")
	{
		text += '<p class="submitted_by">Submitted by ' + authors[0] + '</p>';
	}
    text += '<p>' + tips[0] + '</p>';
	obj.innerHTML = text;
		
}
function failed(requester){
	//alert('The XMLHttpRequest failed. Status: '+requester.status);  	
}


var tips = new Array();
var titles = new Array();
var authors = new Array();
var tipsCount;

// Set initial variables

tipsLink = 0;

function getNextTip() {
	++tipsLink;
	if (tipsLink > tipsCount) tipsLink = 0;
	obj = document.getElementById("tipContent");
	var text ='<h3>' + titles[tipsLink] + '</h3>';
	 
	if (authors[tipsLink] != "" && authors[tipsLink] != null)
	{
		text += '<p class="submitted_by">Submitted by ' + authors[tipsLink] + '</p>';
	}
    text += '<p>' + tips[tipsLink] + '</p>';
	obj.innerHTML = text;	
}

function getPrevTip() {
	--tipsLink;
	if (tipsLink < 0) tipsLink = tipsCount;
	obj = document.getElementById("tipContent");
	var text ='<h3>' + titles[tipsLink] + '</h3>';
	 
	if (authors[tipsLink] != "" && authors[tipsLink] != null)
	{
		text += '<p class="submitted_by">Submitted by ' + authors[tipsLink] + '</p>';
	}
    text += '<p>' + tips[tipsLink] + '</p>';
	obj.innerHTML = text;
}

function init() {
	doxhr("/content/xml_snippets/tips.xml");
}

//window.onload = init;
addLoadEvent(init);
