// Test functions to make sure it worked
//function runTest() {
//  queryString = "Allen=12";
//  var pageUrl = "allenTest2.cfm";
//  httpRequest("POST",pageUrl,testGetBack,true);
//}

//function testGetBack() {
//      document.getElementById('allen').innerHTML = request.responseText;
//}


var globalLinkID = "";
var globalChartLocation = "";
function trimString (str) {
  str = this != window ? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function showReportEdit(reportId) {
 		document.getElementById("report"+ reportId).style.display = "none";
 		document.getElementById("reportName"+ reportId).style.display = "inline";
 		document.getElementById("renameButton"+ reportId).style.display = "none";
 		document.getElementById("updateButton"+ reportId).style.display = "inline";
 		document.getElementById("cancelButton"+ reportId).style.display = "inline";
}
function cancelReportName(reportId) {
	
	document.getElementById("report"+ reportId).style.display = "inline";
	document.getElementById("reportName"+ reportId).value = document.getElementById("oldReportName"+ reportId).value;
	document.getElementById("reportName"+ reportId).style.display = "none";
	document.getElementById("renameButton"+ reportId).style.display = "inline";
	document.getElementById("updateButton"+ reportId).style.display = "none";
	document.getElementById("cancelButton"+ reportId).style.display = "none";
}
function updateReportName(reportId) {
  if(document.getElementById("oldReportName"+ reportId).value != document.getElementById("reportName"+ reportId).value )
  {
  	queryString = "reportId=" + reportId;
  	queryString += "&submitRequest=1";
  	queryString += "&oldReportName=" + document.getElementById("oldReportName" + reportId).value;
  	queryString += "&reportName=" + document.getElementById("reportName" + reportId).value;
    document.getElementById("oldReportName"+ reportId).value = document.getElementById("reportName"+ reportId).value;
    document.getElementById("reportLink"+ reportId).innerHTML = document.getElementById("reportName"+ reportId).value;
    var pageUrl = "/inputforms/reportNameUpdate.cfm";
    httpRequest("POST",pageUrl,returnReportName,true);
  }
  else
  {
 		document.getElementById("report"+ reportId).style.display = "inline";
 		document.getElementById("reportName"+ reportId).value = document.getElementById("oldReportName"+ reportId).value;
 		document.getElementById("reportName"+ reportId).style.display = "none";
 		document.getElementById("renameButton"+ reportId).style.display = "inline";
 		document.getElementById("updateButton"+ reportId).style.display = "none";
 		document.getElementById("cancelButton"+ reportId).style.display = "none";
  }
}

function returnReportName() {
  updateStatus();
	if (request.readyState == 4)
	{
    var reportId = trimString(request.responseText);
 		document.getElementById("report"+ reportId).style.display = "inline";
 		document.getElementById("reportName"+ reportId).style.display = "none";
 		document.getElementById("renameButton"+ reportId).style.display = "inline";
 		document.getElementById("updateButton"+ reportId).style.display = "none";
 		document.getElementById("cancelButton"+ reportId).style.display = "none";
  }

}
function deleteReport(reportId) {
// Need to have scheduledEmailsAttached return some logic so it won't display if there are not scheduled reports attached
	var scheduledMails = document.getElementById("scheduledEmailsAttached"+ reportId).value;
	var confirmationMessage = "Are you sure you want to delete " + document.getElementById("reportName"+ reportId).value;
	var isAre = " is ";
	if(scheduledMails != null && scheduledMails != ''){
		if(scheduledMails.indexOf(",") > 0){
			isAre = " are ";
		}
		confirmationMessage += " that (" + document.getElementById("scheduledEmailsAttached"+ reportId).value + ")" + isAre + "using?";
	} else {
		confirmationMessage += "?";
	}
//	if(confirm("Are you sure you want to delete " + document.getElementById("reportName"+ reportId).value + " that (" + document.getElementById("scheduledEmailsAttached"+ reportId).value + ") are using?"))
	if(confirm(confirmationMessage))
	{
  	queryString = "reportId=" + reportId;
  	queryString += "&submitRequest=1";
    var pageUrl = "/inputforms/reportDelete.cfm";
    httpRequest("POST",pageUrl,returnDeleteReport,true);
	}
}
function returnDeleteReport() {
  updateStatus();
	if (request.readyState == 4)
	{
    var reportId = trimString(request.responseText);
    document.getElementById("reportRow" + reportId).style.display = "none";
  }
}
function toggleRegion (action) {
  queryString = "byRegion=" + action;
  var pageUrl = "/AgileWire/getRegion.cfm";
  httpRequest("POST",pageUrl,returnRegion,true);
}

function returnRegion(){
  updateStatus();
	if (request.readyState == 4)
	{
    document.getElementById("regionDD").innerHTML = request.responseText;
  }
}
function showReportDropDown() {
  queryString = "";
  var pageUrl = "/Ajax/getReportList.cfm";
  httpRequest("POST",pageUrl,returnShowReportDropDown,true);
}
function returnShowReportDropDown () {
  updateStatus();
	if (request.readyState == 4)
	{
    document.getElementById("reportName").innerHTML = request.responseText;
  }
}
function updateReport() {
  var reportId = document.getElementById('top10clipReport').value;
  queryString = "reportId=" + reportId;
  var pageUrl = "/Ajax/updateTop10Report.cfm";
  httpRequest("POST",pageUrl,returnUpdateReport,true);
}
function returnUpdateReport () {
  updateStatus();
	if (request.readyState == 4)
	{
    ajaxSend('index_clipbox.cfm?dumpcache=1', DisplayClipBoxOne);
  }
}
function showChartDropDown(elemToDisplay) {
  globalChartLocation = elemToDisplay;
  queryString = "chart=" + elemToDisplay;
  var pageUrl = "/Ajax/getChartList.cfm";
  httpRequest("POST",pageUrl,returnShowChartDropDown,true);
}
function returnShowChartDropDown () {
  updateStatus();
	if (request.readyState == 4)
	{
	  if(document.getElementById(globalChartLocation + "Header") != null){
      document.getElementById(globalChartLocation + "Header").innerHTML = request.responseText;
    }
  }
}
function updateChart(chart) {
  var reportId = document.getElementById(chart).value;
  queryString = "reportId=" + reportId;
  queryString += "&chartName=" + chart;
  var pageUrl = "/Ajax/updateChart.cfm";
  httpRequest("POST",pageUrl,returnUpdateChart,true);
}
function returnUpdateChart () {
  updateStatus();
	if (request.readyState == 4)
	{
    var xmlResponse = request.responseXML;
    var chart = xmlResponse.getElementsByTagName("Chart")[0].firstChild.nodeValue;
    var chartLocation = xmlResponse.getElementsByTagName("Location")[0].firstChild.nodeValue;
    var ajaxPage = xmlResponse.getElementsByTagName("ajaxPage")[0].firstChild.nodeValue;
    var displayName = xmlResponse.getElementsByTagName("DisplayName")[0].firstChild.nodeValue;
    document.getElementById(chart + "Header").innerHTML = displayName;
    ajaxPage = unescape(ajaxPage);
    httpRequest("GET",ajaxPage,returnChart,true);
  }
}
function returnChart() {
  updateStatus();
	if (request.readyState == 4)
	{
    document.getElementById(globalChartLocation + "Body").innerHTML = request.responseText;
  }
}
function showISClipCount(response) {
  var totalCount = response.substring(response.indexOf('<TotalCount>') + 12, response.indexOf('</TotalCount>'));
  var startEnd = response.substring(response.indexOf("<StartEnd>") + 10, response.indexOf("</StartEnd>"));
  var displayNext = response.substring(response.indexOf("<DisplayNext>") + 13, response.indexOf("</DisplayNext>"));
  var linkList = response.substring(response.indexOf("<LinkList>") + 10, response.indexOf("</LinkList>"));
  if(totalCount == 1)
    textToShow = " clip found.";
  else
    textToShow = " clips found.";


	document.getElementById("TotalArticleCount").innerHTML = totalCount + textToShow;
	document.getElementById("startEnd").innerHTML = startEnd;
	if(displayNext == 1) {
	  document.getElementById("nextButton1").style.display = "";
	  document.getElementById("nextButton2").style.display = "";
	}
	if(document.getElementById("AllLinkID") != null)
	  document.getElementById("AllLinkID").value = linkList;
}
