/*********************************************************************************************/
function Validate(f) {
	
	// 0-0-0
	if (IsEmpty(f.searchTerm1.value) && IsEmpty(f.searchTerm2.value) && IsEmpty(f.searchTerm3.value)) {
	   	f.searchTerm1.style.backgroundColor='#EAEAEA';
			f.searchTerm1.focus();
      alert("U moet uw zoekterm(en) nog invullen!");
      return false;
    } 
	else {
		var q1a, q1b, q2a, q2b, q3, str1, str2, str3, key1, key2, key3, type1, type2, strQuery, undefined;
		// Get the input values from the form for the query
		str1=f.searchTerm1.value; key1=f.searchKey1.value; type1=f.searchType1.value;
		str2=f.searchTerm2.value; key2=f.searchKey2.value; type2=f.searchType2.value;
		str3=f.searchTerm3.value; key3=f.searchKey3.value;
		var strGrouping = f.grouping.value; // material type, period, year of publication etc.
		
		if(key1=="pica.pao" || key1=="pica.aut"){str1=trimCommaSpace(str1)};
		if(key2=="pica.pao" || key2=="pica.aut"){str2=trimCommaSpace(str2)};
		if(key3=="pica.pao" || key3=="pica.aut"){str3=trimCommaSpace(str3)};
		
		// dev only : check what's filled in
		//alert(str1 +"\n"+key1+"\n"+type1+"\n\n"+str2 +"\n"+key2+"\n"+type2+"\n\n"+str3 +"\n"+key3+"\n");
	
		// Build the individual querystring.

		// Without quotes
		//q1a = key1 + "%3d" + escape(str1);
		//q1b = key1 + "%3d" + escape(str1) + "\+" + type1 + "\+"; 
		//q2a = key2 + "%3d" + escape(str2);
		//q2b = key2 + "%3d" + escape(str2) + "\+" + type2 + "\+"; 
		//q3 = 	key3 + "%3d" + escape(str3); 
				
		// With Quotes
		//q1a = key1 + "\%3d\"" + escape(str1) +"\"";
		//q1b = key1 + "\%3d\"" + escape(str1) +"\"" + "\+" + type1 + "\+"; 
		//q2a = key2 + "\%3d\"" + escape(str2) +"\"";
		//q2b = key2 + "\%3d\"" + escape(str2) +"\""+ "\+" + type2 + "\+"; 
		//q3 = key3 + "\%3d\"" + escape(str3) +"\""; 

		q1a = key1 + "=" + escape(str1);
		q1b = key1 + "=" + escape(str1) + "\+" + type1 + "\+"; 
		q2a = key2 + "=" + escape(str2);
		q2b = key2 + "=" + escape(str2) + "\+" + type2 + "\+"; 
		q3 = 	key3 + "=" + escape(str3); 
		
		// Check which fields are filled in and build up the complete query string accordingly.
		
		// 1-0-0
		if ((!IsEmpty(str1)) && (IsEmpty(str2)) && (IsEmpty(str3))){
			strQuery = q1a;
		}
		// 0-1-0
		else if ((IsEmpty(str1)) && (!IsEmpty(str2)) && (IsEmpty(str3))){
			strQuery = q2a;
		}
		// 0-0-1
		else if ((IsEmpty(str1)) && (IsEmpty(str2)) && (!IsEmpty(str3))){
			strQuery = q3;
		}		
		// 1-1-0
		else if ((!IsEmpty(str1)) && (!IsEmpty(str2)) && (IsEmpty(str3))){
			strQuery = q1b + q2a;
		}
		// 0-1-1
		else if ((IsEmpty(str1)) && (!IsEmpty(str2)) && (!IsEmpty(str3))){
			strQuery = q2b + q3;
		}
		// 1-0-1
		else if ((!IsEmpty(str1)) && (IsEmpty(str2)) && (!IsEmpty(str3))){
			strQuery = q1b + q3;
		}	
		// 1-1-1
		else if ((!IsEmpty(str1)) && (!IsEmpty(str2)) && (!IsEmpty(str3))){
			strQuery = q1b + q2b + q3;
		} 
		// Check if grouping parameter was selected. 
		// Check if scanning operation requested.

		if (strGrouping != "0") {
			f.stylesheet.value = "groupPresentation.xslt";
		} else if (f.searchType1.value == "scan") {
			f.stylesheet.value = "scanPresentation.xslt";		
			f.tab.value="tref";
		}	else {
			f.stylesheet.value = "shortTitlePresentation.xslt";
			f.tab.value="list";
		}
		// set "addHistory" to "1" to write query to history cookie
		// set "holdQuery" to "1" to maintain form state across pages
		f.addHistory.value = "1";
		f.frmQuery.value = strQuery;
		f.startRecord.value = "1";
		f.maximumRecords.value = "10";
		f.holdQuery.value="1";
		//return true;		
	}
}

/*********************************************************************************************/
function TrimString(strInput){
// Match whitespaces at beginning and end of text and replace with null strings 
		return strInput.replace(/^\s+/,'').replace(/\s+$/,'');
}

function trimCommaSpace(strValue) {
	return strValue.replace(/,\s*/g,",");
}

/*********************************************************************************************/
function CheckMultiple(strInput){
// Check if more than one term filled in. 
// If so split it up and place logical 'and' in between.
	if (strInput.indexOf(' ') != -1){
		var ar = strInput.split(' ');
		var tempie = '';
		for (var i=0; i<ar.length; i++){
			// attach 'and' to all except the last one
			if (i != ar.length -1){
				tempie += '\"' + ar[i] + '\"' + 'and';
			} else {
				tempie += '\"' + ar[i] + '\"';
			}
		}
		strInput = tempie;
	}
	return strInput;
}

/*********************************************************************************************/
function clearForm(fId) {
	// first crumble the cookie
	document.cookie = "queryHistory; expires=Thu, 01-Jan-70	00:00:01 GMT";
	// clear the form and form elements
	var f = document.getElementById(fId);
	var nrOfElements = f.elements.length;
	if (nrOfElements != null) {
		var j = 0;
		while ( j < nrOfElements ) {
			var cur = f.elements[j];
			if (cur.type == "select-one") {
				cur.selectedIndex = "0";
				cur.disabled = false;
			}
			if (cur.type == "text") {
				cur.value = "";
				cur.style.backgroundColor =  "";
			}
			if (cur.type == "hidden") {
				cur.value = "";
				cur.style.backgroundColor =  "";
			}
			j++;
		}

	}
	if(frames['timeline'])
	{
			for (var i = 0; i < frForm.elements.length; i++) {
				var cur = frForm.elements[i];
				if (cur.type == "checkbox") {
					cur.checked = false;
					var frNr = 'div_'+cur.value;
					fr.document.getElementById(frNr).style.display='none';
				} 

		}
	}
}
/*********************************************************************************************/
// Submit query from history list
function HistorySubmit(oQuery) {
	x.frmQuery.value=oQuery;
	x.frmStyle.value='formatShortTitleList.xslt';
	x.addHistory.value='0';
	x.submit();
}
/*********************************************************************************************/


