/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// get num results class
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// this class is simply calls a php script with a search string, which returns the number of results found
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// written by scott jackman.  
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function numresults(){
	// constructor
	numresults.prototype._formid; // which form we are checking
	numresults.prototype._connection; // request object for ajax
	numresults.prototype._searchstring; // the search string we pass to php script
	numresults.prototype._loaderimgid; // the loader image to display / hide
	}
	
// methods
numresults.prototype.createRequestObject = function (){
	var request_o; 
	if(window.XMLHttpRequest){
		request_o = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(!request_o){
		request_o = new ActiveXObject("Msxml2.XMLHTTP");		
		}
	return request_o;
}
numresults.prototype.getResults = function(){
	this._searchstring = "";
	var fields = document.getElementById(this._formid).getElementsByTagName('input');
	for(var i=0; i<fields.length-1; i++){ // fields.length-1 to get rid of the 'search' button field
			var f = fields[i].name;
			var v = fields[i].value;
			// if input type is checkbox, only return value if it's ticked
			if(fields[i].type == "checkbox"){
				if(fields[i].checked){
					this._searchstring += f+'='+v+'&';
					}
				} else {
				this._searchstring += f+'='+v+'&';
				}
			}
	var fields = document.getElementById(this._formid).getElementsByTagName('select');
	for(var i=0; i<fields.length-1; i++){ // fields.length-1 to get rid of the 'search' button field
			// handle multiple selections, if allowed
			var f = fields[i].name;
			if(fields[i].multiple){
				//alert('multi baby');
				var allopts = fields[i].childNodes;
				for(var opt=0; opt < allopts.length; opt++){
					if(allopts[opt].value && allopts[opt].selected){
						this._searchstring += f+'='+allopts[opt].value+'&';
						}	
					}
				} else {
				var v = fields[i].value;
				this._searchstring += f+'='+v+'&';
				}
			}

//alert('checking!');
	this._connection = this.createRequestObject();
	document.getElementById(this._loaderimgid).style.visibility = 'visible';
	document.getElementById(this._resultdispid).innerHTML = '';
	this.sendQuery();
	}

numresults.prototype.sendQuery = function(){
	//alert(this._searchstring);
	this._connection.open('get', '/includes/ajax_get_numresults.php?search_string='+this._searchstring+'rand='+Math.random()*10,true);
	// Define a function to call once a response has been received. (using a curry because other wise 'this' gets confused and becomes related to the ajax request, rather than this instance of the class, which breaks our readyState 
	this._connection.onreadystatechange = curry(this.giveResult,this);
	this._connection.send(null);	
	}


numresults.prototype.giveResult = function(){
		//alert(that._obj);
		var result = this._connection.readyState;
		//alert(result);
		/* 
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished 
		*/
		if(result < 4){
			}
		if(result == 4){ //Finished loading the response
			// get response - will either return true or fail
			if(this._connection.responseText.substr(0,4) != 'true'){
				//alert('something went wrong! - '+ this._connection.responseText);
				} else {
				var tot = this._connection.responseText.substr(4);
				document.getElementById(this._loaderimgid).style.visibility = 'hidden';
				var txt = 'Your search will yield '+tot+' result';
				if(tot != 1){
						txt += 's';
						}
				document.getElementById(this._resultdispid).innerHTML = txt;
				//document.forms[0].submit();
				}
			} 
	}
function curry (fn, scope) {
    var scope = scope || window;
    var args = [];
    for (var i=2, len = arguments.length; i < len; ++i) {
        args.push(arguments[i]);
    };
    return function() {
	    fn.apply(scope, args);
    };
}
