var my_base_url='http://brainstorm.bullhorn.com';
var my_pligg_base='';



function xmlhttpPostPre(strURL) {
	var strURL = "ajaxsearch.php";
	var xmlHttpReq = false;
	var self = this;
	if (window.XMLHttpRequest) { // Mozilla/Safari
		self.xmlHttpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) { // IE
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open('POST', strURL, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			updatepagePre(self.xmlHttpReq.responseText);
		}
	}

	var params = getstandardargsPre().concat(getquerystringPre());
	var strData = params.join('&');
	if(getquerystringPre() != 'q=') {
		self.xmlHttpReq.send(strData);
	}
}

function getstandardargsPre() {
	var params = [
		'wt=json'
		, 'indent=on'
		, 'q.op=OR'
		];
	return params;
}

function getquerystringPre() {
  //var form = document.forms['thisform'];
  //alert(form.title.value);
  //var query = form.title.value;
  var query = document.getElementById("search").value;
  qstr = 'q=source:Brainstorm ' + escape(query);
  return qstr;
}

// this function does all the work of parsing the solr response and updating the page.
function updatepagePre(str){

  var rsp = eval("("+str+")"); // use eval to parse Solr's JSON response
  var outformat = "";
  var ivotes = "";
  var ititle = "";
  var maxdisplay = 5;
  var todisplay = 5;

  outformat = "<ul id='searchList'>";
  for(i=0;i < rsp.response.numFound && i < maxdisplay ; i++) {

	ivotes = rsp.response.docs[i].votesFor;
	if(ivotes == 1){
		ivotes = " ("+ivotes+" vote)";
	} else {
		ivotes = " ("+ivotes+" votes)";
	}
	ititle = rsp.response.docs[i].title;
	iid= rsp.response.docs[i].id;
	ibody = rsp.response.docs[i].bodySummary;
	ibody = ibody.substr(0,255);
	ibody = ibody.replace(/\'/g,'&rsquo;');
	ibody = ibody.replace(/\n|<br \/>/g,'  ');
	outformat += "<li><a href="+my_pligg_base+"/story.php?id=" + iid + " title='" + ibody + ivotes + "'>" + ititle + "</a></li>";

  }
  outformat += "</ul>";
  if(i>0) {
	document.getElementById("searchPullDownChoices").innerHTML = outformat;
	document.getElementById('searchList').style.left='auto'
  }
}


// Function to add event listener to t
function loadsearchPre() {
	var el = document.getElementById('search');
	if (el.addEventListener){
		el.addEventListener('keyup', xmlhttpPostPre, false);
	} else if (el.attachEvent){
		el.attachEvent('onkeyup', xmlhttpPostPre);
	}
}

function setHide() {
	document.getElementById('searchList').style.left='-99999px';
}
