function searchFreeText() {
	if( !document.frmSearch.search_string.value ) {
		alert("Sökord saknas.");
		return false;
	}
}

String.prototype.trim = function() {
	var x=this;
	x=x.replace(/^\s*(.*)/, "$1");
	x=x.replace(/(.*?)\s*$/, "$1");
	return x;
}

var luceneSearchString = "";


// -- param value = string
// -- param and = boolean (if false, use OR)
// -- param similar = boolean (if true adds a ~after each word)
function buildBoolean( prefix, values, and, similar ) {
	
	var tmp = "";
	var bCount = 0;
	
	if(!values)
		return "";
	values.toString();
	values = values.trim();
	if( !values.length )
		return "";
	values = values.split(" ");
		
	while( bCount < values.length ) {
		if( bCount ) {
			tmp += (and?" AND ":" OR ");
		} 
		tmp += values[bCount];
		tmp += (similar?"~":"");
		bCount++;
	}
	tmp = tmp.trim();
	if( tmp.length ) {
		if( values.length > 1 ) {
			tmp = "("+tmp+")";
		}
		if( prefix.length ) {
			tmp = prefix+":"+tmp;
		}
	}
	return tmp;
}

function buildDate(prefix,fy,fm,fd,ty,tm,td) {
	var tmp = "";
	
	if( ty - fy > 30 ) {
		fm = (parseInt(fm)>0)?fm:"01";
		fd = (parseInt(fd)>0)?fd:"01";
		tm = (parseInt(tm)>0)?tm:"12";
		td = (parseInt(td)>0)?td:"31";
		
	
		tmp += "(";
		tmp += "(date_year:"+fy+" AND date_month:"+fm+" AND date_day:["+fd+" TO 31])";
		tmp += " OR ";
		tmp += "(date_year:["+(parseInt(fy)+1)+" TO "+(ty-1)+"])";
		tmp += " OR ";
		tmp += "(date_year:"+ty+" AND date_month:"+tm+" AND date_day:[01 TO "+td+"])";
		tmp += ")";
	} else {
		tmp+=prefix+":[";
		tmp+=((fy>1000)?fy:1000);
		tmp+=(fm?fm:"01");
		tmp+=(fd?fd:"01");			
		tmp+=" TO ";
		if( ty ) {
			tmp+=ty;
			tmp+=tm;
			tmp+=td;						
		} else {
			tmp+="20010101";
		}
		tmp+="]";
	}
	return tmp;
}

function addLucene(str) {
	if( luceneSearchString.length && str ) {
		luceneSearchString += " AND ";
	}
	luceneSearchString+=str;
}

function getLucene() {
	return luceneSearchString;
}

function clearLucene() {
	luceneSearchString="";
}

function buildLucene() {
	
	// -- Create variables
	var obj,str,strAnd=false;
	var daterange;
	var fromYear,fromMonth,fromDay,toYear,toMonth,toDay;
	var subject;//subject
	var county,countyId; //region
	var municipality,municipalityId; //fylke_new
	var origin,originId; // fylke_old
	var persons,personsSimilar;
	var places,placesSimilar;
	var count;	
	
	// -- Initiate variables
	clearLucene();
	count=0;
	obj = document.frmSearch;
	
	if( obj.search_string ) {
		str = obj.search_string.value;
		if( obj["boolean"] ) {
			strAnd = obj["boolean"][1].checked;
		}
	}
	if( obj.daterange ) {
		daterange = obj.daterange.options[obj.daterange.options.selectedIndex].value;
	} else if( obj.startyear ) {
		fromYear = obj.startyear.value;
		fromMonth = obj.startmonth.value;
		fromDay = obj.startday.value;
		toYear = obj.stopyear.value;
		toMonth = obj.stopmonth.value;
		toDay = obj.stopday.value;
	}
	if( obj.region ) {
		if(obj.region.type=="hidden") {
			county=obj.region.value;
			countyId=obj.region_id.value;
		} else {
			county = obj.region.options[obj.region.options.selectedIndex].text;
			countyId=obj.region.options[obj.region.options.selectedIndex].value;
		}
	}
	if( obj.fylke_new ) {
		if(obj.fylke_new.type=="hidden") {
			municipality=obj.fylke_new.value;
			municipalityId=obj.fylke_new_id.value;
		} else {
			municipality = obj.fylke_new.options[obj.fylke_new.options.selectedIndex].text;
			municipalityId=obj.fylke_new.options[obj.fylke_new.options.selectedIndex].value;
		}
	}
	if( obj.fylke_old ) {
		if(obj.fylke_old.type=="hidden") {
			origin=obj.fylke_old.value;
			originId=obj.fylke_old_id.value;
		} else {
			origin = obj.fylke_old.options[obj.fylke_old.options.selectedIndex].text;
			originId=obj.fylke_old.options[obj.fylke_old.options.selectedIndex].value;
		}
	}
	if( obj.persons ) {
		persons = obj.persons.value;
		personsSimilar = obj.persons_regexp.checked;
	}
	if( obj.places ) {
		places = obj.places.value;
		placesSimilar = obj.places_regexp.checked;
	}
	if( obj.subject ) {
		subject="";
		if(obj.subject.type=="hidden") {
			subject=obj.subject.value;
		} else {
			while(count < obj.subject.length) {
				if( obj.subject[count].checked && obj.subject[count].value ) {
					if(subject.length)
						subject+= " ";
					subject+=obj.subject[count].value;
				}
				
				count++;
			}
		}
	}
	
	// -- Search free text
	addLucene(buildBoolean("",str,strAnd,false));
	// -- Search date
	if( fromYear ) {
		addLucene(buildDate("date",fromYear,fromMonth,fromDay,toYear,toMonth,toDay));
	}
	if( daterange ) {
		addLucene("date:["+daterange+"]");
	}
	// --Search county
	if( countyId > 0 ) {
		addLucene(buildBoolean("county",county,true,false));
		addLucene(buildBoolean("county_id",countyId,false,false));
	}
	// --Search municipality
	if( municipalityId > 0 ) {
		addLucene(buildBoolean("municipality",municipality,true,false));
		addLucene(buildBoolean("municipality_id",municipalityId,true,false));
	}
	// --Search origin
	if( originId > 0 ) {
		//addLucene(buildBoolean("origin",origin,true,false));
		addLucene(buildBoolean("origin_id",originId,true,false));
	}
	else if( originId != '' ) {
		addLucene(originId);
	}
	// -- Search name
	if( persons ) {
		addLucene(buildBoolean("name",persons,true,personsSimilar));
	}
	// -- Search locality
	if( places ) {
		addLucene(buildBoolean("locality",places,true,placesSimilar));
	}
	// -- Search subject
	if( subject ) {
		addLucene(buildBoolean("subject",subject,false,false));
	}
}

function copyForm() {
	buildLucene();
	var str = getLucene();
	if( str.length ) {
		document.luceneSearch.search_string.value=str;
		document.luceneSearch.returnurl.value=document.location.href;
		document.luceneSearch.submit();
	}
	return false;
}
