
function getText() {  
  /*The form data is sent to the given URL using a POST method, 
  /*rather than a GET by using the dojo.xhrPost function.*/
  updateStatus("Gathering results...");
  dojo.xhrPost({
    url: "query.php",
    load: function(response){
      document.getElementById("resultsDiv").innerHTML = response;
      releaseStatus();
      return response;
    },
    error: function(response){
      updateStatus("An error occurred, with response: " + response);
      document.getElementById("progressField").style.display = "none";
      return response;
    },
    
    /*Setting the 'form' parameter to the ID of a form on the page
    /*submits that form to the specified URL*/
    form:"myForm",
    handleAs: "text"
  });
}

function getNews() {
      dojo.xhrGet( {
        url: "newsfeeds.php", 
        handleAs: "text",

        timeout: 19000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { // ?
          dojo.byId("rssfeed").innerHTML = response;
          return response;
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) {
          console.error("HTTP status code: ", ioArgs.xhr.status);
          dojo.byId("rssfeed").innerHTML = response;
          return response;
          }
        });
}


function termQuery() {  
  updateStatus("Gathering results...");
  document.getElementById("submitquerybutton").innerHTML = "Please Wait...";
  dojo.xhrPost({
    url: "termquery.php",
    timeout: 300000,
    load: function(response){
      document.getElementById("resultsDiv").innerHTML = response;
      releaseStatus();
      document.getElementById("submitquerybutton").innerHTML = "Submit";
      return response;
    },
    error: function(response){
      updateStatus("An error occurred, with response: " + response);
      //document.getElementById("progressField").style.display = "none";
      document.getElementById("submitquerybutton").innerHTML = "Submit";
      return response;
    },
    
    /*Setting the 'form' parameter to the ID of a form on the page
    /*submits that form to the specified URL*/
    form:"termcountform",
    handleAs: "text"
  });

}

function submitMessage() {  
  updateStatus("Sending...");
  document.getElementById('submitbuttonspan').style.display = "none";
  dojo.xhrPost({
    url: "sendmessage.php",
    load: function(response){
      document.getElementById("statusDiv").innerHTML = response;
      document.getElementById('submitbuttonspan').style.display = "block";
      document.getElementById("progressField").style.display = "none";
      return response;
    },
    error: function(response){
      updateStatus("An error occurred, with response: " + response);
      document.getElementById("progressField").style.display = "none";
      document.getElementById('submitbuttonspan').style.display = "block";
      return response;
    },
    
    /*Setting the 'form' parameter to the ID of a form on the page
    /*submits that form to the specified URL*/
    form:"feedbackform",
    handleAs: "text"
  });
}

function newSearch(){
  document.getElementById("start").value = 0;
  getText();
}

function gotoPage(rowIndex){
  document.getElementById("start").value = rowIndex;
  getText();
}

function next(){
  var start = document.getElementById("start").value;
  if (start == null || start == ''){
    start = 0;
  }
  start = parseInt(start) + 10;
  document.getElementById("start").value = start;
  getText();
}

function previous(){
  var start = document.getElementById("start").value;
  if (start == null || start == ''){
    start = 0;
  }
  start = parseInt(start) - 10;
  document.getElementById("start").value = start;
  getText();
}


  /*  Retrieves the data for the agencies charts.*/
function getAgencyChart() {  

  updateStatus("Generating Chart Window (may take a few moments, please disable pop-up blockers)...");

  dojo.xhrPost({
    url: "chartquery.php",
    load: function(response){
      releaseStatus();
      try{
       chartWindow(response); /* redraw chart*/
      } catch (e){
        updateStatus("Unable to display the chart window.  Please make sure that pop-up blockers are disabled for cclme.org.");
        document.getElementById("progressField").style.display = "none";
      }
      return response;
    },
    error: function(response){
     /*The request was canceled by some other JavaScript code.*/
     if(response.dojoType == "cancel"){
       updateStatus("Request canceled.");
     /*The request took over 5 seconds to complete.*/
     }else if(response.dojoType == "timeout"){
       updateStatus("Request timed out.");
     /*Some other error happened.*/
     }else{
       updateStatus("An error occurred, with response: " + response);
     }
      document.getElementById("progressField").style.display = "none";
      return response;
    },
    
    /*Setting the 'form' parameter to the ID of a form on the page
    /*submits that form to the specified URL*/
    form:"myForm",
    handleAs: 'text'
  });
}


  /*  Displays the agencies charts.*/
function chartWindow(inHTML){
    //document.getElementById("dropincontent").innerHTML = inHTML;
    //document.getElementById("dropin").style.display = "block";
    ChartWindow=window.open('', 'ChartWindow', "width=850,height=700,scrollbars=yes,resizable=yes");
    ChartWindow.document.write(inHTML);
    ChartWindow.document.close();
    if (ChartWindow.focus()){
        ChartWindow.focus();
    }
    var browser=navigator.appName;
    /* have to reload in IE for some reason.*/
    if(browser=="Microsoft Internet Explorer"){
      ChartWindow.location.reload();
    }
}

function updateStatus(in_string){
    //document.getElementById("progressField").style.display = "inline";
    document.getElementById("statusDiv").innerHTML = in_string;
    /*document.getElementById("dialog1").innerHTML = " <img src=\"images/fish.gif\" /> <br />Retrieving results...";*/
    /*dijit.byId('dialog1').show();*/
    /*clearResults();*/
}

function releaseStatus(){
    dojo.byId("statusDiv").innerHTML = "";
    document.getElementById("progressField").style.display = "none";
    /*dojo.byId("statusDiv").style.display = "none";*/
    /*dijit.byId('dialog1').innerHTML = "";*/
    /*dijit.byId('dialog1').hide();*/
}
function clearResults(){
    dojo.byId("resultsDiv").innerHTML = "";
}


function showOptions(){
    var s = document.getElementById("searchOptions");
    if (s.style.display == "none" || s.style.display == "") {
    	s.style.display = "inline";
    } else {
    	s.style.display = "none";
    }
}
function helpwindow(){
    window.open("help.htm", "HelpWindow", "width=350,height=600,scrollbars=yes,resizable=yes");
}
function relevancewindow(){
    window.open("relevance.htm", "RelevanceWindow", "width=800,height=220,scrollbars=yes,resizable=yes");
}

function toggleLocations(){
	var c = document.getElementById("all");
	if (c.checked == true){
		document.getElementById("all").checked = true;
		document.getElementById("ca").checked = true;
		document.getElementById("or").checked = true;
		document.getElementById("wa").checked = true;
		document.getElementById("us").checked = true;
		document.getElementById("mexico").checked = true;
	} else {
		document.getElementById("all").checked = false;
		document.getElementById("ca").checked = false;
		document.getElementById("or").checked = false;
		document.getElementById("wa").checked = false;
		document.getElementById("us").checked = false;
		document.getElementById("mexico").checked = false;
	}

}

function addtextbox(element){
	if (element.value != "" && element.value != null){
		var inputarea = document.getElementById("inputboxes");
		var inputs = inputarea.getElementsByTagName("input");
		var count = inputs.length;
		var count2 = count+1;
		var newid = "term" + count; 

		// don't create the next text box if it exists already
		element.setAttribute('onblur','');

		var br = document.createElement('br');
		inputarea.appendChild(br);

		var newelement = document.createElement('input');
		newelement.setAttribute('id',newid);
		newelement.setAttribute('name',newid);
		newelement.setAttribute('type', 'text');
		newelement.setAttribute('size', '80');
		newelement.setAttribute('onblur', 'addtextbox(this);');

		var checkboxelement = document.createElement('input');
		checkboxelement.setAttribute('type','checkbox');
		checkboxelement.setAttribute('onclick', 'makeword('+"'"+newid+"'"+', this);');

		inputarea.appendChild(newelement);
		inputarea.appendChild(checkboxelement);

		newelement.focus();
	}

}

// Adds [\W] to the front and back of the search term, or removes if it exists.
function makeword(element, checkboxthis){
  var inputelement = document.getElementById(element);
  var v = inputelement.value;
  if (v != null && v != ""){
    if (checkboxthis.checked) {
       var newvalue = '[\\W]' + v + '[\\W]';
       inputelement.value = newvalue;
    } else{
        var newvalue = v.replace(/\[\\W]/gi,"");
        inputelement.value = newvalue;
    } 
  }

}

function dismissbox(){
 document.getElementById("dropincontent").innerHTML = "";
 document.getElementById("dropin").style.display = "none";
}


