/* 	SSSSSS	EEEEEE	AAAAAA	RRRRRR	  CCCC	HH  HH
	ss	EE	AA  AA	RR  RR	CC	HH  HH
	SSSSSS	EEEEEE	AAAAAA	RRRRRR	CC	HHHHHH
	    SS	EE	AA  AA	RRRR	CC	HH  HH
	SSSSSS	EEEEEE	AA  AA	RR  RR	  CCCC	HH  HH

	An JavaScript SQL search function for arrays.
	This is free to use.
	Copyright 2001 - Siteam AB, Sweden
			 http://www.siteam.net

	-----------------------------------------------

	How to use it?
	--------------

	With this JavaScript function you can search in a data
	array design as follow:
		testArray = new Array();
		testArray[0]=new Array("ID","Name","Phone";
		testArray[1]=new Array("1","Lars","+46-8-4556451");
		testArray[2]=new Array("2","Stina","+46-40-4578546");
		testArray[3]=new Array("3","Lars-olov","+47-8-4556471");
		testArray[4]=new Array("4","Helena","+46-8-4556456");

	The first post in the array has the names of the fields.

	To use this function you should call it as follow:
		select(data array,output format,search arg1,search arg2,...)

	Different search arguements you can use:
		=	equal with
		>	bigger then
		<	smaller then
		>=	bigger or equal then
		<=	smaller or equal then
		!=	not equal with
		partof	is a part of the string

	How to specify a search arguement - do like this:
		<Fieldname> <search arguement> <searched value>
		Some examples: 	Name=Lars
					will result in the first field
				Name partof rs
					will result in the first field
					- "rs" is a part of "Lars"

	Declaration of the "output format":
		Here you specify how you want the result from the search
		designed - which fields, etc.
		An example:	"'Result: '+ID+' '+Name+'<br />\n'"
				(with the search arg "Name partOf Lars")
			This will return a list with the following design:
				Result: 1. Lars
				Result: 3. Lars-olov

	An example of a search in the array on the top with the
	search-function:
		searchResult = search(testArray,
			"'Swede in Stockholm: '+ID+' '+Name+'<br />\n'",
			"Phone partOf +46","Phone partOf 8-");
		This example will give "searchResult" the value:
			Swede in Stockholm: 1 Lars<br />\n
			Swede in Stockholm: 4 Helena<br />\n
			(in one line, \n=linebreak)
*/
		

function select() {
   	var i,j,search="",searchString="",out="",args = select.arguments,operator;
	var where=new Array();
	if(args[0].length<1 || args.length<3) return "";
	t=eval(args[0]);
	// stoppar fältnamnen från 1:a posten "t" i 1:a posten i "where"
	for(i=0;i<(t[0].length);i++) {
		where[i]=new Array;
		where[i][0]=t[0][i];
	}
	/* sorterar variablerna i vektorn "where". 1:a värdet i varje post
	   innehåller det aktuella fältnamnet. */
	for(i=2;i<(args.length);i++) {
		var splitArray=new Array();
		if(args[i].length>2) {
			if(args[i].search("=")!=-1) { splitArray=args[i].split("="); operator="=="; }
			if(args[i].search(">")!=-1) { splitArray=args[i].split(">"); operator=">"; }
			if(args[i].search("<")!=-1) { splitArray=args[i].split("<"); operator="<"; }
			if(args[i].search(">=")!=-1) { splitArray=args[i].split(">="); operator=">="; }
			if(args[i].search("<=")!=-1) { splitArray=args[i].split("<="); operator="<="; }
			if(args[i].search("!=")!=-1) { splitArray=args[i].split("!="); operator="!="; }
			if(args[i].search(" partOf ")!=-1) { splitArray=args[i].split(" partOf "); operator=".toLowerCase().search("; }
			for(j=0;j<(t[0].length);j++)
				if(splitArray[0]==where[j][0]) where[j][where[j].length]=operator+"\""+splitArray[1]+"\""+(operator==".toLowerCase().search("?")!=-1":"");
		}
	}


	// make a select
	for(i=0;i<(t[0].length);i++)
		if(where[i].length>=2) {
			if(searchString.length==0) searchString="("; else searchString+=" && (";
			for(j=1;j<(where[i].length);j++) {
				if(j>1) searchString+=" || ";
				searchString+=t[0][i]+where[i][j];
			}
			searchString+=")";
		}
	if(searchString=="") searchString="true";
	for(i=1;i<(t.length);i++) {
		if(t[i]) {
			for(j=0;j<(t[0].length);j++) {
				eval(t[0][j]+"=\""+t[i][j]+"\"");
			}
			// eval("if("+searchString+") out+=eval(args[1]);");
			// Nedan är en specialversion för denna partnersite
			eval("if("+searchString+") { out+=eval(args[1]); i=t.length; }");
		}
	}
	return out;
}