/* Documentation/Log: seach.xml. */

function searchCode(request,operator,records,Bfunction){
	this.request = request;	//This is the actual search text
	this.operator = operator;	//This is the search operator, for instance AND or OR
	this.records = records;	//This is the record that is to be searched in the XML file
	if (typeof(this.records) == "string"){
		this.records = [this.records];	
	}
	this.Bfunction = Bfunction;	//This is the search function (if one is specificed other than basic)
	this.mustThis = [];	//This is populated by regEx. It is the regular expression(s) that must be satisfied.
	
	this.regEx = function(splitChar){	
		request = request.split(splitChar);
		if (operator == "OR"){ this.mustThis.push(".*("+request.join('|')+").*"); } //OR regular expression
		else if (operator == "AND"){
			for (k = 0; k < request.length; k++){ this.mustThis.push(".*"+request[k]+".*"); } //AND regular expression
		}
	}
	
	this.makeSearch = function(record){
		if (this.Bfunction){ return this.Bfunction(record); }
		else {
			for (var i = 0; i < this.records.length; i++){
				rC = xml.xpSingle(record, this.records[i]);
				if (rC){
					rC = xml.getVAL(rC);
					var valid = true;
					for (var e = 0; e < this.mustThis.length; e++){
						if (!(new RegExp(this.mustThis[e], "i")).test(rC)){ valid = false; break; }
					}	
					if (valid){ return true; }
				}
			}
			return false;
			/*
			rC = xml.getTAG(record, this.records[0], 0);
			if (rC){
				rC = xml.getVAL(rC);
				for (i = 0; i < this.mustThis.length; i++){
					if (!(new RegExp(this.mustThis[i], "i")).test(rC)){ return false; } }			
			}
			else{ return false; }
			return true;*/
		}
	}
}

function makeSearch(sC){
	removals = [];
	for (e = 0; e < list.length; e++){
		tokeep = true;
		
		for (s = 0; s < sC.length; s++){
			tokeep = sC[s].makeSearch(list[e]);
			if (!tokeep){ removals.push(e); break; }
		}
	}
	
	for (m = 0; m < removals.length; m++){ list.splice(removals[m]-m,1); }
}