var oXML;
var rawXML;
var allitems;
var reporttitle;
var reportlnk;
var reportdesc;
var reportcontent;
var htmlcopy;
var feedlnk;
var contentdepth;
var contentshort;
var divId;
var qkeys;
var qvals;

function XMLHTTP() {
	var oXMLHTTP;
	
	// code for Mozilla, etc.
	if ( window.XMLHttpRequest ) {
		oXMLHTTP = new XMLHttpRequest();
	}
	// code for IE
	else if ( window.ActiveXObject ) {
		try {
			oXMLHTTP = new ActiveXObject ( "Msxml2.XMLHTTP" );
		} 
		catch ( e ) {
			try {
				oXMLHTTP = new ActiveXObject ( "Microsoft.XMLHTTP" );
			}
			catch ( e ) {
				oXMLHTTP = false;
			}
		}
	}
	return oXMLHTTP;
}

function ajaxCall( method, url, onchange, aSynch ) {
	oXML = XMLHTTP();
	if ( oXML ) {
		oXML.onreadystatechange = onchange;
		oXML.open ( method, url, aSynch );
		oXML.send ( null );
		
		return true;
	}
	else {
		return false;
	}
}

function trim ( stringToTrim ) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function processReqChange() {
	var startIndex;
	var reportFound;
	
	if ( oXML.readyState == 4 ) {
		if ( oXML.status == 200 ) {
			rawXML = oXML.responseText;
			
			getAllItems();
			
			htmlcopy = '';
			
			//*************** Note: may have to change the order if Contactology changes the order of items in the feed *************
			//*************** currently they order it from earliest to latest *******************************************************
			startIndex = allitems.length - 1;
			for ( i = 0; i < contentdepth; i++ ) {
				reportFound = false;
				while ( !reportFound ) {
					reportFound = getReport ( allitems[startIndex] );
					if ( !reportFound ) {
						startIndex--
					}
				}
				
				if ( contentshort ) {
					htmlcopy = htmlcopy + '<p><a href="/view-report?rpt=' + reportlnk.substr(reportlnk.indexOf('g=') + 2) + '&title=' + reporttitle + '">' + reporttitle + '</a></p><div style="border-bottom: 1px solid rgb(211, 222, 225); margin-bottom: 10px;padding-bottom: 10px;">' + reportdesc.substr ( 0, 200 ) + '...</div>';
				}
				else {
					htmlcopy = htmlcopy + '<p><a href="/view-report?rpt=' + reportlnk.substr(reportlnk.indexOf('g=') + 2) + '&title=' + reporttitle + '">' + reporttitle + '</a></p><div style="border-bottom: 1px solid rgb(211, 222, 225); margin-bottom: 10px;padding-bottom: 10px;">' + reportcontent + '</div>';
				}
				
				startIndex--
			}
			
			document.getElementById ( divId ).innerHTML = htmlcopy;
		} else {
			htmlcopy = 'View the most recent reports at <a href="http://clicks.skem1.com/archive/rss.php?c=4027&l=' + feedlnk + '">clicks.skem1.com/archive/rss.php?c=4027&l=' + feedlnk + '</a>';
			document.getElementById ( divId ).innerHTML = htmlcopy;
		}
	}
}

function processReqChange_single() {
	var reportFound;
	if ( oXML.readyState == 4 ) {
		if ( oXML.status == 200 ) {
			rawXML = oXML.responseText;
			
			reportFound = getReportSingle ( rawXML );
			
			myTitle = getElementByClassName ( 'page_title' );
			myTitle.innerHTML = reporttitle;
			
			htmlcopy = '<div><p><a target="_blank" href="http://clicks.skem1.com/archive/view/?c=4027&g=' + feedlnk + '">If you are having trouble viewing the full report click here</a></p>' + reportcontent + '</div>';
			
			document.getElementById ( divId ).innerHTML = htmlcopy;
		} else {
			htmlcopy = 'View this report at <a href="http://clicks.skem1.com/archive/view/?c=4027&g=' + feedlnk + '">http://clicks.skem1.com/archive/view/?c=4027&g=' + feedlnk + '</a>';
			document.getElementById ( divId ).innerHTML = htmlcopy;
		}
	}
}

function getAllItems() {
	var tempXML;
	var srtPos;
	var endPos;
	var arrIndex;
	
	tempXML = rawXML;
	allitems = new Array();
	arrIndex = 0;
	
	if ( window.location.search.indexOf ( 'debug' ) > -1 ) {
		alert ( tempXML.substr ( tempXML.length - 5000 ) );
	}
	
	while ( tempXML.indexOf ( '<item>' ) > -1 ) {
		srtPos = tempXML.indexOf ( '<item>' ) + 6;
		endPos = tempXML.indexOf ( '</item>' );
		allitems[arrIndex] = tempXML.substring ( srtPos, endPos );
		tempXML = tempXML.substr ( endPos + 7 );
		arrIndex++;
	}
}

function getReport ( currentItem ) {
	var curItem;
	var curItemSrt;
	var curItemEnd;
	
	curItem = currentItem;
	curItem = curItem.replace ( /\t/g, "" );
	
	curItemSrt = curItem.indexOf ( '<title>' ) + 7;
	curItemEnd = curItem.indexOf ( '</title>' );
	reporttitle = curItem.substring ( curItemSrt, curItemEnd );
	
	if ( reporttitle.indexOf ( 'EMT Express' ) >= 0 ) {
		return false;
	}
	else {
		curItemSrt = curItem.indexOf ( '<link>' ) + 6;
		curItemEnd = curItem.indexOf ( '</link>' );
		reportlnk = curItem.substring ( curItemSrt, curItemEnd );
		
		if ( contentshort ) {
			curItemSrt = curItem.indexOf ( '<description>' ) + 13;
			curItemEnd = curItem.indexOf ( '</description>' );
			reportdesc = curItem.substring ( curItemSrt, curItemEnd );
		}
		else {
			curItemSrt = curItem.indexOf ( '<content:encoded>' ) + 17;
			curItemEnd = curItem.indexOf ( '</content:encoded>' );
			reportcontent = curItem.substring ( curItemSrt, curItemEnd );
			if ( reportcontent.indexOf ( '<![CDATA[' ) > -1 ) {
				reportcontent = reportcontent.substring ( reportcontent.indexOf ( '<![CDATA[' ) + 9, reportcontent.indexOf ( ']]>' ) );
			}
			if ( reportcontent.indexOf ( '<body' ) > -1 ) {
				//remove header tags and content first
				reportcontent = reportcontent.substr ( reportcontent.indexOf ( '</head>' ) + 7 );
				//remove the body tag starting at the closing bracket for the body tag
				reportcontent = reportcontent.substring ( reportcontent.indexOf ( '>' ) + 1, reportcontent.indexOf ( '</body>' ) );
			}
			if ( reportcontent.indexOf ( 'If you are having trouble viewing this message' ) > -1 ) {
				//remove the view in browser div
				//*************** Note: may have to change this if Contactology changes the HTML code *************
				reportcontent = reportcontent.substr ( reportcontent.indexOf ( '</div>' ) + 6 );
			}
			if ( reportcontent.indexOf ( 'This message sent to <b>{EMAIL}</b>' ) > -1 ) {
				//remove the contactology crap
				//*************** Note: may have to change this if Contactology changes the HTML code *************
				reportcontent = reportcontent.substring ( 0, reportcontent.indexOf ( '<table align="center" style="margin-top: 5px;"><tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #585858">This message sent to <b>{EMAIL}</b>' ) );
			}
		}
		return true;
	}
}

function getReportSingle ( currentItem ) {
	var curItem;
	var curItemSrt;
	var curItemEnd;
	
	reportcontent = currentItem;
	
	if ( reportcontent.indexOf ( '<body' ) > -1 ) {
		//remove header tags and content first
		reportcontent = reportcontent.substr ( reportcontent.indexOf ( '</head>' ) + 7 );
		//remove the body tag starting at the closing bracket for the body tag
		reportcontent = reportcontent.substring ( reportcontent.indexOf ( '>' ) + 1, reportcontent.indexOf ( '</body>' ) );
	}
	if ( reportcontent.indexOf ( 'If you are having trouble viewing this message' ) > -1 ) {
		//remove the view in browser div
		//*************** Note: may have to change this if Contactology changes the HTML code *************
		reportcontent = reportcontent.substr ( reportcontent.indexOf ( '</div>' ) + 6 );
	}
	if ( reportcontent.indexOf ( 'This message sent to <b>{EMAIL}</b>' ) > -1 ) {
		//remove the contactology crap
		//*************** Note: may have to change this if Contactology changes the HTML code *************
		reportcontent = reportcontent.substring ( 0, reportcontent.indexOf ( '<table align="center" style="margin-top: 5px;"><tr><td style="font-family: Arial, Helvetica, sans-serif; font-size: 11px; color: #585858">This message sent to <b>{EMAIL}</b>' ) );
	}
	
	return true;
}

function getElementByClassName (cl) {
	var retnode;
	var elem;
	
	elem = document.getElementsByTagName('h1');
	
	for (var i = 0; i < elem.length; i++) {
		var classes;
		classes = elem[i].className;
		if (classes == cl)
			retnode = elem[i];
	}
	return retnode;
}

function getQueryString () {
	qkeys = new Array();
	qvals = new Array();
	loc = window.location.search.substring(window.location.search.indexOf('?') + 1);
	temp = '';
	i = 0;
	
	while ( loc.indexOf ( '=' ) > -1 ) {
		qkeys[i] = loc.substring(0,loc.indexOf ( '=' ));
		if ( loc.indexOf ( '&' ) > -1 ) {
			qvals[i] = loc.substring(loc.indexOf ( '=' ) + 1,loc.indexOf ( '&' ));
			loc = loc.substring ( loc.indexOf ( '&' ) + 1 );
		}
		else {
			qvals[i] = loc.substring(loc.indexOf ( '=' ) + 1);
			loc = '';
		}
		i++;
	}
}

function getFeed ( lnk, depth, excerptonly, id ) {
	feedlnk = lnk;
	contentdepth = depth;
	contentshort = excerptonly;
	divId = id;
	
	ajaxCall('get', 'http://www.exploreminnesota.com/assets/uploads/ReadFeed.asp?lnk=' + lnk, processReqChange, true);
}

function getFeedSingle ( id ) {
	divId = id;
	
	getQueryString();
	for ( i = 0; i < qkeys.length; i++ ) {
		if ( qkeys[i] == 'rpt' ) {
			feedlnk = qvals[i];
		}
		else {
			reporttitle = qvals[i].replace ( /%20/g, ' ' );
		}
	}
	ajaxCall('get', 'http://www.exploreminnesota.com/assets/uploads/ReadFeed.asp?rpt=' + feedlnk, processReqChange_single, true);
}