function pageInit(){
	
	//var search = queryString('search');
	//if(search != "false"){
		//autoFill(search);
	//}
	/*
	if((document.getElementById("search_text").value != "") ||
		(if(document.getElementById("search_format")) && document.getElementById("search_format").value != "")) {
		liveSearch(1);
	}
	*/
	
	if(document.getElementById("search_format")){
		
		liveSearch(1);
		
		if(document.getElementById("search_text").value != "" || document.getElementById("search_format").value != ""){
			//liveSearch(1);
		}
	
	}else{
		liveSearch(1);
	}
	
	

}

function checkDate(obj){
	
	//
	//alert(obj.value + "___" + obj.value.length);
	
	if(obj.value.length == 4){
		liveSearch();
	}
	
}

function autoFill(data){
	
	//need to reset form or accept more values
	
	//
	document.getElementById("search_text").value = data;
	liveSearch(1);	
	
	//

}

function popFill(obj){
	
	autoFill(obj.childNodes[0].data);
	
}



	
function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}

	this.getKeyValuePairs = function() { return this.keyValuePairs; }

	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
			}
		return false;
	}

	this.getParameters = function() {
		var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
				a[j] = this.keyValuePairs[j].split("=")[0];
			}
		return a;
	}
	
	this.getLength = function() { return this.keyValuePairs.length; } 
}

function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}


function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

var http = createRequestObject(); 

var searchInterval = undefined;

function liveSearch(force){	
	//do the delay and set timeout
	if(force != undefined){
		submitSearch();
	}else{
		window.clearInterval(searchInterval);
		searchInterval = setInterval("submitSearch()",500);
			
	}
}

function padZero(t){
	if(t.length < 2){
		return "0" + t;
	}else{
		return t;
	}
}

function hoverRow(obj,t){

	//get parent
	//var tParent = obj.parentNode;	
	//do all children
	for(var i=0;i<obj.childNodes.length;i++){
		var row = obj.childNodes[i];
		if(t == "on"){
			row.style.background = "#EEEEEE";
		}		
		if(t == 1){
			row.style.background = "#FFFFFF";
		}
		if(t == 0){
			row.style.background = "#FFFFFF";
		}
	}
	
	
}


function resetSearch(){

	document.getElementById("search_year_start").value = "";
	document.getElementById("search_year_end").value = "";
	document.getElementById("search_text").value = "";
	document.getElementById("search_results").innerHTML = "";
	if(document.getElementById("search_format")){
		document.getElementById("search_format").selectedIndex = 0;
	}
	
	
}

function formatPost(obj){
	var ret = "";
	var c = 0;
	for(var i in obj){
		if(c > 0){
			ret += "&";
		}		
		ret += i + "=" + obj[i];
		c++;
	}
	return ret;
}


function viewPublication(id){

	window.location = '/publications/details/' + id + '/';

}

function submitSearch(){
	
	window.clearInterval(searchInterval);
	
	var sendVars = new Object();
	sendVars.year_start = encodeURI(document.getElementById("search_year_start").value);
	sendVars.year_end = encodeURI(document.getElementById("search_year_end").value);
	sendVars.s = encodeURI(document.getElementById("search_text").value);
	if(document.getElementById("search_format")){
		sendVars.format = encodeURI(document.getElementById("search_format").value);
	}
		
	var params = formatPost(sendVars);
	
	http.open('post',ajax_data_src,true);
	http.onreadystatechange = displayResults;
	//if(http.overrideMimeType) {
      //     http.overrideMimeType('text/xml');
    //}else{
    	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");    //}
	
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");
	
	http.send(params);
			
}


/* Function called to handle the list that was returned from the internal_request.php file.. */
function displayResults(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 1){
		document.getElementById('search_results').innerHTML = "Loading...";
	}
		
	if(http.readyState == 4){ //Finished loading the response
		if(http.status == 200){
			document.getElementById('search_results').innerHTML = http.responseText;
		}
	}
}


