/* get RSS Feed and return only the record matching the current week */
/* open RSS Feed */
function getWTip() {
	url = document.getElementById('weekly_tip').getAttribute('rss');
	sitepath = document.getElementById('weekly_tip').getAttribute('sitepath');
	viewallweekly = document.getElementById('weekly_tip').getAttribute('viewall');
	showtitle = (document.getElementById('weekly_tip').getAttribute("showtitle")) ? document.getElementById('weekly_tip').getAttribute("showtitle") : "yes";	
	http_request3 = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request3 = new XMLHttpRequest();
		if (http_request3.overrideMimeType) {
			http_request3.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request3 = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
			http_request3 = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request3) {
		alert('Giving up :( Cannot create an XMLHTTP instance');
		return false;
	}
	http_request3.onreadystatechange = displayWResources;
	http_request3.open('GET', url, true);
	http_request3.send(null);
}

/* process RSS Feed */
function displayWResources() {
	if (http_request3.readyState == 1) { // Not Loaded yet
		document.getElementById('weekly_tip').innerHTML = '<img src="'+sitepath+'/loading.gif" />';
	}
	if (http_request3.readyState == 4) { 
		if (http_request3.status == 200) { // Loaded
				var xmldoc = http_request3.responseXML;
				var root_node = xmldoc.getElementsByTagName('item');
				tipdataW = gWRSS(root_node);
				tipdataW.current = 0;
				returnWTip();
			} else {
				alert('There was a problem with the request.');
			}
		}
}

function returnWTip(){
	var filterDiv = '';
	var currentDate = new Date();
	var tipwritten = false;
	
	filterDiv = "<!-- Inline Content Listing Begin -->"
	filterDiv += '<div id="icl_container">'
	filterDiv += '<ul>'
	for (i=0;i<tipdataW.length;i++) {
		/* verify that tip week matches current week and that tip year matches current year */
		if ((tipdataW[i].date.getWeek()) == (currentDate.getWeek()) && tipdataW[i].date.getFullYear() == currentDate.getFullYear()) {
			if (showtitle == "yes")
				filterDiv += '<li class=icl_title>'+tipdataW[i].title.replace(/\\\"/gi,'"')+'</li>'
			filterDiv += '<li class=icl_summary><p>'+tipdataW[i].summary+'</p>'
			if (viewallweekly =='yes') filterDiv += '<div id="tipviewWeekly"><a href="javascript:returnWTipAll()">View All Weekly Tips...</a></div>'
			tipwritten = true;
			break;
		}
	}

	if (!tipwritten && tipdataW.length > 0) {
		var datediff = new Date() - new Date('1/1/1970');
		var randomtip = Math.floor((datediff/86400000) % tipdataW.length);
		if (showtitle == "yes")
			filterDiv += '<li class=icl_title>'+tipdataW[randomtip].title.replace(/\\\"/gi,'"')+'</li>'
		filterDiv += '<li class=icl_summary><p>'+tipdataW[randomtip].summary+'</p>'
		if (viewallweekly =='yes') filterDiv += '<div id="tipviewWeekly"><a href="javascript:returnWTipAll()">View All Weekly Tips...</a></div>'
	}
	filterDiv += '</li>'
	filterDiv += '</ul>'
	filterDiv += '</div>'
	filterDiv += '<!-- Inline Content Listing End -->'
	$('weekly_tip').innerHTML = filterDiv;
	if ($('readmoreWeekly')) filterDiv += ''
	if ($('readmoreWeekly')) $('readmoreWeekly').style.display = "none";
}

function returnWTipAll(){
	var filterDiv = '';

	filterDiv = "<!-- Inline Content Listing Begin -->"
	//filterDiv += "<div id='icl_container' style='position: absolute; padding-left: 20px; left: 50px; top: 100px; width: 300px; height: 300px; overflow: auto; border: 1px solid #000; background: #fff;z-index: 999999'>"
	filterDiv += "<div id='icl_container' style='width: 741px; background: #fff'>"
	filterDiv += '<table cellspacing=0 cellpadding=0>'
	filterDiv += '<tr><td><div style="position: absolute; left: 45px; top: 1px; color: #FFF; font-weight: bolder; font-size: 9px; 	font-family:Verdana, Arial, Helvetica, sans-serif;">'+document.title.substr(0,document.title.indexOf("-")-1).toUpperCase()+'</div><a href="javascript:hideWTipAll()"><img src="'+sitepath+'/TipAllHeader.gif" border=0></a></td></tr>'
	filterDiv += '<tr><td><div style="background: url('+sitepath+'/TipAllBackgroundg.jpg) no-repeat; width: 741px; height: 276px; margin-top: 0px; padding-top: 10px;">'
	filterDiv += '<div style="padding-top: 10px;margin: 0px 10px 20px 20px; height: 244px; overflow: auto; width: 704px;"><h2>Weekly Tips</h2><ul>';
	for (i=0;i<tipdataW.length;i++) {
		tipdataW[i].title = tipdataW[i].title.replace(/\\\"/gi,'"');
		tipdataW[i].summary = tipdataW[i].summary.replace('id="readmoreWeekly"', ''); /* removed duplicate div */
		tipdataW[i].summary = tipdataW[i].summary.replace('id="readmoreWLink"', 'style="display:none"'); /* change duplicate div */
		filterDiv += '<li class=icl_title>'+tipdataW[i].title+'</li>'
		filterDiv += '<li class=icl_summary><p>'+tipdataW[i].summary+'</p>'
		filterDiv += '</li>';
	}
	filterDiv += '</ul></div></div></td></tr></table>'
	filterDiv += '</div>'
	filterDiv += '<!-- Inline Content Listing End -->'
	$('divtip').innerHTML = filterDiv;
	$('divtip').style.display = "none";
	$('tipviewWeekly').innerHTML = '<a href="javascript:hideWTipAll()">Hide All Weekly Tips...</a>'
	$('divint').style.display = "none";
	Effect.toggle('divtip','Appear');
}
function hideWTipAll() {
	$('tipviewWeekly').innerHTML = '<a href="javascript:returnWTipAll()">View All Weekly Tips...</a>'
	$('divtip').style.display="none";
	$('divint').style.display = "block";
}

function gWRSS( XMLNode ) {
	var XMLItem = new Array(), j=0;
	for (i=0; XMLNode.length>i; i++)
	{
		 XMLItem[i] = new Array();
		 for (k=0; XMLNode[i].childNodes.length>k; k++)
		 {
			 if(XMLNode[i].childNodes[k].firstChild != null) {
				 switch(XMLNode[i].childNodes[k].tagName) {
					 case "title" : XMLItem[i]["title"] = XMLNode[i].childNodes[k].firstChild.data;
									break;
					 case "description" : XMLItem[i]["summary"] = XMLNode[i].childNodes[k].firstChild.data;
									break;
					 case "pubDate" : XMLItem[i]["date"] = new Date(XMLNode[i].childNodes[k].firstChild.data);
									break;
				 }
			 }
		 }
	}
	
	return XMLItem;
}

/* getweek function */
Date.prototype.getWeek = function() {
	var onejan = new Date(this.getFullYear(),0,1);
	return Math.ceil((((this - onejan) / 86400000) + onejan.getDay())/7);
} 

getWTip();


