//var XML_FEED_URL = "http://stablog.qa.wiley.com:85/staxml.php";
var xmlhttp=false;
var XML_FEED_URL ;

$(document).ready(function() {

    if($('#isJboss').val() == "yes"){
    XML_FEED_URL = "/STA1.0/blog_proxy.jsp";
}
else {
    XML_FEED_URL = "/sta/blog_proxy.jsp";
}

});





function retrieveData() {
	/*	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200||xmlhttp.status == 0) { */

	$.ajax({
		type: "GET",
		url: XML_FEED_URL,
		dataType: "xml",
		error: function(req,error) {
            alert("error:"+error);
        },
		success: function(xmldoc) {		
		var postlist = xmldoc.getElementsByTagName('postlist').item(0);
		//for the first item of postlist which is first <blogpost>
		for (var iNode = 0; iNode < postlist.childNodes.length; iNode++) {
			var blogpost = postlist.childNodes.item(iNode);

			if (blogpost.tagName == "blogpost")
			{
				var title = "";
				var quotation = "";
				var market_quotes = "";
				var trend = "";
				var link = "";
				var postdate = "";
				var posttitle = "";
				var gainstitle = "";
				
				for (var inNode = 0; inNode < blogpost.childNodes.length; inNode++) 
				{
					var childNode = blogpost.childNodes.item(inNode);

					if (childNode.tagName == "gainstitle")							
						gainstitle = childText(childNode);
					
					if (childNode.tagName == "posttitle") {				
						
						var postTitleNode = childNode;						
						
						 link = childText(postTitleNode.childNodes[0]);
						 title = childText(postTitleNode.childNodes[1]);						 
						
					}
					
					if (childNode.tagName == "postdate")							
						postdate = childText(childNode);

					if (childNode.tagName == "quote")
						quotation = childText(childNode);

					if (childNode.tagName == "gainsprobability")
						market_quotes = childText(childNode);

					if (childNode.tagName == "sentiment")
						trend = childText(childNode);					
					

				}

				$('#postsTable').append("<tr><td><h2>"+postdate+"</h2></td></tr>"+
						"<tr><td><a href='"+link+ "' target=_blank>"+title+"</a></td></tr>" +
						"<tr><td><b><a href='"+link+ "' target=_blank>"+gainstitle+"</a></b></td></tr>"+
						"<tr><td>&quot;<i>"+quotation+"</i>&quot;</td></tr>" +
						"<tr><td><font size='0.4pt' color='#950D1D'><b>"+market_quotes+"</b></font></td></tr>"+
						"<tr><td><img src='"+trend+"'></img></td></tr>");					
				
			//take only first blog post
			break;
			}
			

		}
	}
	});


}

function childText(child)
{
	// IE uses text property;
	if (window.ActiveXObject) {        	 
		return child.text;
	}
	// Other browsers use textContent
	else {
		return child.textContent;		

	}
}

$(document).ready(function() {  retrieveData(); } );


