function searchAssistant() {

	var stdSAButtonAreaId = "saButtonArea";
	var stdSACloseButtonId = "saCloseButton";
	var stdSALayerId = "saLayer";
	var stdSAContentLayerId = "saContent";
	var oButtonArea = null;
	var oLayer = null;
	var oContentLayer = null;
	var oGeneralFunctions = new GeneralFunctions();
	var isVisible = false;
	
	
	this.init = function() {
		// check if external Id's are set.
		var saButtonAreaId = (window.saButtonAreaId?window.saButtonAreaId:stdSAButtonAreaId);
		var saCloseButtonId = (window.saCloseButtonId?window.saCloseButtonId:stdSACloseButtonId);
		var saLayerId = (window.saLayerId?window.saLayerId:stdSALayerId);
		var saContentLayerId = (window.saContentLayerId?window.saContentLayerId:stdSAContentLayerId);
				
		// get global objects
		oButtonArea = document.getElementById(saButtonAreaId);
		oCloseButtonL = document.getElementById(saCloseButtonId);
		oLayer = document.getElementById(saLayerId);
		oContentLayer = document.getElementById(saContentLayerId);
		oGeneralFunctions.checkBrowser();
		
		if (oButtonArea) this.prepareSAButtons(oButtonArea, (oGeneralFunctions.ie4 || oGeneralFunctions.ie5));
		if (oCloseButtonL) this.prepareSACloseButton(oCloseButtonL, (oGeneralFunctions.ie4 || oGeneralFunctions.ie5));
		
		// display first layer
		for (i=0;i<oContentLayer.childNodes.length;i++) {
			if (oContentLayer.childNodes[i].nodeName.toLowerCase() == "div") {
				oGeneralFunctions.showElement(oContentLayer.childNodes[i]);
				break;
			}
		}
	}
	
	this.prepareSAButtons = function(oButtonArea, IsIE) {
		var i;
		
		if (IsIE) {
			
			for (i=0;i<oButtonArea.childNodes.length;i++) {
				if (oButtonArea.childNodes[i].tagName && oButtonArea.childNodes[i].tagName.toLowerCase() == "a") oButtonArea.childNodes[i].attachEvent("onclick", oSearchAssistant.toggleSearchAssistant);
			}
		}
		else {
			for (i=0;i<oButtonArea.childNodes.length;i++) {
				if (oButtonArea.childNodes[i].tagName && oButtonArea.childNodes[i].tagName.toLowerCase() == "a") oButtonArea.childNodes[i].setAttribute("onclick", "oSearchAssistant.toggleSearchAssistant();");
			}
		}
			
		//oButton.style.cursor = "hand";
	}
	
	this.prepareSACloseButton = function(oButton, IsIE) {
		if (IsIE) 
			oButton.attachEvent("onclick", oSearchAssistant.toggleSearchAssistant);
		else
			oButton.setAttribute("onclick", "oSearchAssistant.toggleSearchAssistant();");
			
		oButton.style.cursor = "hand";
	}
	
	// Display or hide search assistant
	this.toggleSearchAssistant = function() {
		if (!isVisible) {
			oGeneralFunctions.showElement(oLayer);
			isVisible=true;
		}
		else {
			oGeneralFunctions.hideElement(oLayer);
			isVisible=false;
		}
	}
	
	// display a new contentlayer
	this.displayContent = function(ContentId) {
		var oNewContent = document.getElementById(ContentId);
		var i;
		
		if (oNewContent) {
			for (i=0;i<oContentLayer.childNodes.length;i++) {
				if (oContentLayer.childNodes[i].nodeName.toLowerCase() == "div") {
					if (oContentLayer.childNodes[i] != oNewContent) {
						oGeneralFunctions.hideElement(oContentLayer.childNodes[i]);
					}
					else {
						oGeneralFunctions.showElement(oContentLayer.childNodes[i]);
					}
				}
			}
		}
	}
}


function QueryBuilder() {
	
	this.UpdateValue = function(sourceField, targetField) {
		
		var SourceFieldType = sourceField.type.toLowerCase();
		var targetValue;
		
		switch (SourceFieldType) {
			case "select-one" : targetValue = sourceField[sourceField.selectedIndex].value; break;
			case "checkbox" : targetValue = (sourceField.checked?sourceField.value:""); break;
			case "radio" : targetValue = (sourceField.checked?sourceField.value:""); break;
			case "select-multiple" : UpdateMultiValues(sourceField, targetField, 0); return;
			default : targetValue = (sourceField.value?sourceField.value:""); break;
		}
		targetField.value = targetValue;
	}
	
	this.UpdateMultiValues = function(sourceField, targetField, handleType) {
		// handleType : 0 --> Build string, 1 --> Sum values
		var i;
		var oSourceField;
		var oSourceForm = sourceField.form;
		var targetValue;
		var singleTargetValue;
		var SourceFieldType;
		
		oSourceField = oSourceForm[sourceField.name];
		
		if (handleType == 0) targetValue = "";
		else targetValue = 0;	
		
		for (i = 0; i < oSourceField.length; i++) {
			
			try {
				SourceFieldType = oSourceField[i].type.toLowerCase();
			}
			catch(e) {
				SourceFieldType = oSourceField[i].tagName.toLowerCase();	
			}
			
			switch (SourceFieldType) {
				case "checkbox" : singleTargetValue = (oSourceField[i].checked?oSourceField[i].value:""); break;
				case "radio" : singleTargetValue = (oSourceField[i].checked?oSourceField[i].value:""); break;
				case "option" : singleTargetValue = (oSourceField[i].selected?oSourceField[i].value:""); break;
				default : singleTargetValue = (oSourceField[i].value?oSourceField[i].value:""); break;
			}
			
			if (handleType == 0) {
				if (singleTargetValue != "") {
					if ((targetValue + "") != "") targetValue += ",";
					targetValue += singleTargetValue;
				}
			}
			else {
				if (!isNaN(parseInt(singleTargetValue)))	targetValue += parseInt(singleTargetValue);
			}
		}
		if ((handleType == 0) && (targetValue == "")) targetValue = "-";
		
		targetField.value = targetValue;
	}
	
	this.removeAllFilters = function (fldFilterInformation) {
		var strCommands = fldFilterInformation.value;
		var arrCommands = strCommands.split(";");
		var i;
		
		for (i=0;i<arrCommands.length;i++) {
			try {
				eval(arrCommands[i] + ";");
			}
			catch(e) { }
		}
	}
	
	/* resets the value of a single field */
	this.removeSingleFilter = function(objFld, fldValue) {
		var i, a, f, arrValues;
		arrValues = fldValue.split("#");
		if (objFld) {
			switch (objFld.type) {
				case "select-multiple" :
				case "select-one" :
					for (f=0;f<objFld.options.length;f++) {
						for (a=0; a<arrValues.length;a++) {
							if (objFld.type == "select-one") {
								if (arrValues[a] == objFld.options[f].value) { objFld.selectedIndex = f; break;	}
							}
							else {
								if (arrValues[a] == objFld.options[f].value) { objFld.options[f].selected = true; break; }
								else objFld.options[f].selected = false;
							}
						}
					}
					break;
				case "text": 
				case "hidden" :	objFld.value = fldValue; break;
				default :
					for (i=0;i<objFld.length;i++) {
						switch (objFld[i].type) {
							case "checkbox":
								for (a=0; a<arrValues.length;a++) {
									if (arrValues[a] == objFld[i].value) { objFld[i].checked = true; break; }
									else objFld[i].checked = false;
								}
								break;
							case "radio" :
								for (a=0; a<arrValues.length;a++) {
									if (arrValues[a] == objFld[i].value) { objFld[i].checked = true; break;	}
								}
								break;
							default : break;	
						}
					}					
					break;
			}
		}	
	}
}


function GeneralFunctions() {

	this.checkBrowser = function() {
		this.appName=navigator.appName;
		this.ver=navigator.appVersion;
		this.dom=(document.getElementById);
		this.ie5=(document.all && this.dom);
		this.ie4=(document.all && !this.dom);
		this.ns5=((parseInt(this.ver) >= 5) && this.dom && !document.all);
		this.ns4=(document.layers && !this.dom);
		return this;
	 }
	 
	 this.showElement = function(oElement) {
	 	oElement.style.display = "block";
	 }
	 
	 this.hideElement = function (oElement) {
 		oElement.style.display = "none";
	 }
}


var oSearchAssistant = new searchAssistant();
var oQueryBuilder = new QueryBuilder();

function SA_DC(ContentId) { oSearchAssistant.displayContent(ContentId); }


function QB_UMV(sourceField, targetField, handleType) { oQueryBuilder.UpdateMultiValues(sourceField, targetField, handleType); }
function QB_UV(sourceField, targetField) { oQueryBuilder.UpdateValue(sourceField, targetField); }
function QB_RAF(fldFilterInformation) { oQueryBuilder.removeAllFilters(fldFilterInformation); }
function QB_RSF(objFld, fldValue) { oQueryBuilder.removeSingleFilter(objFld, fldValue); }




