﻿function GetFeed(element) {
	$.ajax({ type: "GET",
		url: "http://feeds.feedburner.com/DailyLivingMinistries-Weblog?format=sigpro",
		datatype: "text",
		success: function (text) {
			var response = $(text.responseText).text();

			//remove the unnecessary text from the end
			var index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);

			// remove everything before what is now the last document.write();
			index = response.lastIndexOf('document.write');
			response = response.substring(index);

			// get rid of the document.write(""); that encases the desirable text
			response = response.substring(16);
			index = response.lastIndexOf('");');
			response = response.substring(0, index);

			// replace the hex characters
			response = response.replace(/\\x3c/ig, "<");
			response = response.replace(/\\x3e/ig, ">");
			response = response.replace(/\\x3d/ig, "=");
			response = response.replace(/\\x22/ig, "\"");
			response = response.replace(/\\x26/ig, "&");
			response = response.replace(/\\x27/ig, "'");
			response = response.replace(/\\xa9/ig, "©");
			response = response.replace(/\\n/ig, "");

			// replace the unicode characters
			var indexU = response.lastIndexOf('\\u');
			while (indexU != -1) 
			{
				//alert(response.lastIndexOf('\\u'));
				
				var i = response.lastIndexOf('\\u');
				var replaceOrig = response.slice(i+2, i + 6);				
				var replacement = escape(replaceOrig);
				
				//alert(replacement);
				
				replacement = unescape("%u" + replacement);
				
				//alert(replacement);
				
				var old = "\\u" + replaceOrig;
				response = response.replace(old, replacement);
				indexU = response.lastIndexOf('\\u');
			};

			// find the last image tag - it and everything after it needs to stay
			var lastIndex = response.lastIndexOf('<img');
			// keep the last image tag and closing div to append after we remove the 3 networking links
			var lastText = response.substring(lastIndex);

			// find the index of the starting social networking links and remove them (there are 7 links)
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);
			index = response.lastIndexOf('<a');
			response = response.substring(0, index);

			// append the save text from earlier back to the end of the cleaned text
			response = response.concat(lastText);
			
			$(element).append(response);
		},
		error: function () {
			//alert("Error retrieving feed.");
		}
	});
};

function GetTitle(element) {
	$.ajax({ type: "GET",
		url: "http://feeds.feedburner.com/DailyLivingMinistries-Weblog?format=sigpro",
		datatype: "text",
		success: function (text) {
			var response = $(text.responseText).text();

			//remove the unnecessary text from the end
			var index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);
			index = response.lastIndexOf('document.write');
			response = response.substring(0, index);

			// remove everything before what is now the last document.write();
			index = response.lastIndexOf('document.write');
			response = response.substring(index);

			// get rid of the document.write(""); that encases the desirable text
			response = response.substring(16);
			index = response.lastIndexOf('");');
			response = response.substring(0, index);

			// replace the hex characters
			response = response.replace(/\\x3c/ig, "<");
			response = response.replace(/\\x3e/ig, ">");
			response = response.replace(/\\x3d/ig, "=");
			response = response.replace(/\\x22/ig, "\"");
			response = response.replace(/\\x26/ig, "&");
			response = response.replace(/\\x27/ig, "'");
			response = response.replace(/\\xa9/ig, "©");
			response = response.replace(/\\n/ig, "");

			// replace the unicode characters
			var indexU = response.lastIndexOf('\\u');
			while (indexU != -1) 
			{
				//alert(response.lastIndexOf('\\u'));
				
				var i = response.lastIndexOf('\\u');
				var replaceOrig = response.slice(i+2, i + 6);				
				var replacement = escape(replaceOrig);
				
				//alert(replacement);
				
				replacement = unescape("%u" + replacement);
				
				//alert(replacement);
				
				var old = "\\u" + replaceOrig;
				response = response.replace(old, replacement);
				indexU = response.lastIndexOf('\\u');
			};

			// find the index of the link text and remove everything except the link text		
			index = response.indexOf('</a>');	
			response = response.substring(0, index);
				
			index = response.lastIndexOf('>');
			response = response.substring(index + 1);
			
			//alert(response);
						
			$(element).append(response);
		},
		error: function () {
			//alert("Error retrieving feed.");
		}
	});
};
