// Method to get HTTP request
function createAgrowXMLHttpRequest() {
	var httpRequest;
	if (window.XMLHttpRequest) {
		httpRequest = new XMLHttpRequest();
	} else {
		if (window.ActiveXObject) {
			httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return httpRequest;
}
function getAgrowAutonomyResponse(XMLHttpRec, title, autnQuery, conSize, relatedLink, recency, source, refineBy, refineValue) {

   // Abort any currently pending requests for this XMLHttpRequest Object
	XMLHttpRec.onreadystatechange = function () {
	};
	XMLHttpRec.abort();
	var urlMapping = "";
	

   // Get variables from form and build URL to call
	if (urlMapping == "") {
		urlMapping = "";
	} else {
		urlMapping = "/" + urlMapping;
	}
	url = urlMapping + "/sectorDataServlet?autnQuery=" + autnQuery + "&contentSize=" + conSize + "&isReltated=" + relatedLink + "&recency=" + recency + "&refineBy=" + refineBy + "&refineValue=" + refineValue + "&action=sectorData";

	// Tell the XMLHttpRequest object what the url is and that you will use the "GET" response not "POST" to call the external url
	XMLHttpRec.open("GET", url, true);



   // Assign the name of the JavaScript function which will be called when the request completed then send the request
	XMLHttpRec.onreadystatechange = function () {
		callback(XMLHttpRec, title, source);
	};
	XMLHttpRec.send(null);
}
function callback(httpRequest, sectionDiv, source) {
	if (httpRequest.readyState == 4) {
		if (httpRequest.status == 200) {
			if (window.ActiveXObject) {
				populateData(httpRequest, sectionDiv, source);
			} else {
				if (window.XMLHttpRequest) {
					populateData(httpRequest, sectionDiv, source);
				}
			}
		}
	}
}
function populateData(httpRequest, sectionDiv, source) {
	var httpResponse = httpRequest.responseText;
	var jsonRespObject = eval("(" + httpResponse + ")");
	displayAgrowData(jsonRespObject, sectionDiv, source);
}
function displayAgrowData(agrowInfoObj, sectionDiv, source) {
	var i;
	var flag = false;
	var responseHTML = "";
	var moreID = "more" + sectionDiv;
	if (typeof (agrowInfoObj.autnResponse) == "undefined" || agrowInfoObj.autnResponse.length == 0) {
		if (document.getElementById(moreID) != null) {
			//document.getElementById(moreID).style.display = "none";
			if (source == "EVENTS") {
				document.getElementById("EVENTS").innerHTML = "<div class='rightCommon'><span class='headlineTitle'>Expecting data for this section.</span></div>";
			} else {
				document.getElementById(sectionDiv).innerHTML = "<div class='middleCommon'><span class='headlineTitle'>Expecting data for this section.</span></div>";
			}
		}
		return;
	}
	for (i = 0; i < agrowInfoObj.autnResponse.length; i++) {
		var agrowData = agrowInfoObj.autnResponse[i];
		if (source == "PEOPLE") {
			responseHTML += "<div class='peopleContainer'><span class='peopleTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span>";
			responseHTML += "<span class='peopleTxt marginTop10'>" + agrowData.articleContent + "</span></div>";
		} else {
			if (source == "BOTTOM ANALYSIS") {
				if (typeof (agrowData.imgSrc) != "undefined" && agrowData.imgSrc != null) {
					responseHTML += "<div class='middleCommon'><div class='middleRight'><span class='interviewImg'><img src='" + agrowData.imgSrc + "' border='0'/></span></div>";
					responseHTML += "<div class='middleLeft'><span class='headlineTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span></div>";
					responseHTML += "<div class='middleLeft'><span class='italicText'><i>" + agrowData.articleDate + "</i></span></div>";
					responseHTML += "<div class='middleLeft'><span class='italicText'><i>" + agrowData.articleAuthor + "</i></span></div>";
					responseHTML += "<p>" + agrowData.articleContent + "</p></div>";
				} else {
					responseHTML += "<div class='middleCommon'><span class='headlineTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span></div>";
					responseHTML += "<div class='middleCommon'><span class='italicText'><i>" + agrowData.articleDate + "</i></span></div>";
					responseHTML += "<div class='middleCommon'><span class='italicText'><i>" + agrowData.articleAuthor + "</i></span></div>";
					responseHTML += "<div class='middleCommon'><span class='middleTxt'>" + agrowData.articleContent + "</span></div>";
				}
			} else {
				if (source == "ANALYSIS") {
					responseHTML += "<div class='middleCommon'><span class='headlineTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span></div>";
					responseHTML += "<div class='middleCommon'><span class='italicText'><i>" + agrowData.articleDate + "</i></span></div>";
					responseHTML += "<div class='middleCommon'><span class='middleTxt'>" + agrowData.articleContent + "</span></div>";
					if (typeof (agrowData.relatedLink1) != "undefined") {
						responseHTML += "<div class='middleCommon'><span class='middleArrow'><img src='/template/ver1-0/images/arrowImg.gif' height='7' width='5' border='0'/></span>";
						responseHTML += "<span class='middleInnerArrowtxt'> <a href='" + agrowData.relatedUrl1 + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.relatedLink1.substring(0, 60) + "...</a></span></div>";
					}
					if (typeof (agrowData.relatedLink2) != "undefined") {
						responseHTML += "<div class='middleCommon'><span class='middleArrow'><img src='/template/ver1-0/images/arrowImg.gif' height='7' width='5' border='0'/></span>";
						responseHTML += "<span class='middleInnerArrowtxt'> <a href='" + agrowData.relatedUrl2 + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.relatedLink2.substring(0, 60) + "...</a></span></div>";
					}
				} else {
					if (source == "EVENTS") {
						responseHTML += "<div class='rightCommon'><span class='headlineTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span></div>";
						responseHTML += "<div class='rightCommon'><span class='italicText'><i>" + agrowData.eventDate + "</i></span></div>";
						responseHTML += "<div class='rightCommon'><span class='newsContent'>" + agrowData.eventLocation + "</span></div>";
					} else {
						if (source == "REFINEMENT") {
							responseHTML += "<div class='middleCommon'><span class='headlineTitle'>";
							responseHTML += "<a href='#' onclick='setParametric(\"" + sectionDiv + "\",\"" + encodeURIComponent(agrowData.refineBy) + "\")'>" + agrowData.refineBy + "</a></span></div>";
							document.getElementById("hideRefineBy").style.display = "block";
						} else {
							if (source == "MULTIMEDIAREFINEMENT") {
								responseHTML += "<div class='middleCommon'><span class='headlineTitle'>";
								responseHTML += "<a href='#' onclick='viewListing(\"" + sectionDiv + "\",\"" + encodeURIComponent(agrowData.refineBy) + "\")'>" + agrowData.refineBy + "</a></span></div>";
								document.getElementById("hideRefineBy").style.display = "block";
							} else {
								if (source == "MULTIMEDIA") {
								if (typeof (agrowData.imgSrc) != "undefined" && agrowData.imgSrc != null) {
									responseHTML += "<div class=leftHeading><div class=leftImage><img src='" + agrowData.imgSrc + "' border='0'/></div>";
									responseHTML += "<div class='leftContent'><span class='headlineSubTitle'><a href='#P25_337' onclick='playVideo(\"" + agrowData.mediaUrl + "\")'>" + agrowData.articleTitle + "</a></span>";
									responseHTML += "<span class='italicTextAlt'><i>" + agrowData.articleDate + "</i></span><div class='blogContentTxt top10 '>" + agrowData.articleContent + "</div></div>";
								}else{
									responseHTML += "<div class='leftHeading'><span class='headlineTitle'><a href='#P25_337' onclick='playVideo(\"" + agrowData.mediaUrl + "\")'>" + agrowData.articleTitle + "</a></span></div>";
									responseHTML += "<div class='leftHeading'><span class='italicText'><i>" + agrowData.articleDate + "</i></span></div>";
									responseHTML += "<div class='leftHeading marginTop10'><p>" + agrowData.articleContent + "</p></div>";
								}

								} else {
									if (typeof (agrowData.imgSrc) != "undefined" && agrowData.imgSrc != null) {
										responseHTML += "<div class=leftHeading><div class=leftImage><img src='" + agrowData.imgSrc + "' border='0'/></div>";
										responseHTML += "<div class='leftContent'><span class='headlineSubTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span>";
										responseHTML += "<span class='italicTextAlt'><i>" + agrowData.articleDate + "</i></span></div><div class='leftContentTxt top10'>" + agrowData.articleContent + "</div></div>";
									} else {
										responseHTML += "<div class='leftHeading'><span class='headlineTitle'><a href='" + agrowData.articleUrl + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.articleTitle + "</a></span></div>";
										responseHTML += "<div class='leftHeading'><span class='italicText'><i>" + agrowData.articleDate + "</i></span></div>";
										responseHTML += "<div class='leftHeading marginTop10'><p>" + agrowData.articleContent + "</p></div>";
									}
									if (typeof (agrowData.relatedLink1) != "undefined") {
										responseHTML += "<div class='leftHeading'><span class='leftArrow'><img src='/template/ver1-0/images/arrowImg.gif' height='7' width='5' border='0' /></span>";
										responseHTML += "<span class='leftInnerTxt'><a href='" + agrowData.relatedUrl1 + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.relatedLink1.substring(0, 60) + "...</a></span></div>";
									}
									if (typeof (agrowData.relatedLink2) != "undefined") {
										responseHTML += "<div class='leftHeading'><span class='leftArrow'><img src='/template/ver1-0/images/arrowImg.gif' height='7' width='5' border='0' /></span>";
										responseHTML += "<span class='leftInnerTxt'><a href='" + agrowData.relatedUrl2 + "?autnRef=" + agrowData.articleReference + "'>" + agrowData.relatedLink2.substring(0, 60) + "...</a></span></div>";
									}
								}
							}
						}
					}
				}
			}
		}
	}
	document.getElementById(sectionDiv).innerHTML = responseHTML;
}
function getAgrowEventsResponse(XMLHttpRec, source) {
	var urlMapping = "";
   
   // Get variables from form and build URL to call
	if (urlMapping == "") {
		urlMapping = "";
	} else {
		urlMapping = "/" + urlMapping;
	}
	
   // Abort any currently pending requests for this XMLHttpRequest Object
	XMLHttpRec.onreadystatechange = function () {
	};
	XMLHttpRec.abort();

	// Get variables from form and build URL to call
	var url = urlMapping + "/sectorDataServlet?action=eventData";

   // you will use the "GET" response not "POST" to call the external url
	XMLHttpRec.open("GET", url, true);

   // Assign the name of the JavaScript function which will be called

   // when the request completed then send the request
	XMLHttpRec.onreadystatechange = function () {
		callback(XMLHttpRec, source, source);
	};
	XMLHttpRec.send(null);
}
function getAgrowRefinementData(XMLHttpRec, title, query, source) {
	var urlMapping = "";
   
   // Get variables from form and build URL to call
	if (urlMapping == "") {
		urlMapping = "";
	} else {
		urlMapping = "/" + urlMapping;
	}
	
   // Abort any currently pending requests for this XMLHttpRequest Object
	XMLHttpRec.onreadystatechange = function () {
	};
	XMLHttpRec.abort();
   // Get variables from form and build URL to call
	var url = urlMapping + "/sectorDataServlet?autnQuery=" + query + "&action=refineData";

   // Tell the XMLHttpRequest object what the url is and that
   // you will use the "GET" response not "POST" to call the external url
	XMLHttpRec.open("GET", url, true);

   // Assign the name of the JavaScript function which will be called
   // when the request completed then send the request
	XMLHttpRec.onreadystatechange = function () {
		callback(XMLHttpRec, title, source);
	};
	XMLHttpRec.send(null);
}

