advancedSearch = ((navigator.userAgent.indexOf("Safari") == -1 || document.location.href.indexOf("forceadvsearch=true") != -1) ? new advancedSearch() : null);

function updateAdvancedSearchExpression() { /* Used by IE only */
	return advancedSearch.updateExpression();
}

function advancedSearch() {
	
    var str='';
    str+='            <fieldset class="search-builder">';
    str+='                <legend>Søkekriterier<\/legend>';
    str+='                <div id="clause1">';
    str+='                    <label for="word1">Finn dokumenter med ordet</label>';
    str+='                    <input type="text" id="word1" onChange="advancedSearch.updateExpression();" accesskey="d">';
    str+='                <\/div>';
    str+='';
    str+='                <button onclick="advancedSearch.removeClause();" type="button" id="removeClauseButton" style="display: none;" accesskey="æ">Færre valg<\/button>';
    str+='                <button onclick="advancedSearch.addClause();" type="button" id="addClauseButton" accesskey="v">Flere valg »<\/button>';
    str+='            <\/fieldset>';
    str+='';
    document.write(str);
    		
	function clause(num, operator, string) {
		this.create = function() {
			var div = document.createElement("div");
			div.id = "clause"+num;
			var input = document.createElement("input");
			input.setAttribute("type", "text");
			var selector = document.createElement("select");
			var andOption = document.createElement("option");
			andOption.setAttribute("value", "AND ");
			andOption.appendChild(document.createTextNode("og"));
			var orOption = document.createElement("option");
			orOption.setAttribute("value", "OR ");
			orOption.appendChild(document.createTextNode("eller"));
			var notOption = document.createElement("option");
			notOption.setAttribute("value", "-");
			notOption.appendChild(document.createTextNode("ikke"));

  		    selector.appendChild(andOption);
  		    selector.appendChild(orOption);
  		    selector.appendChild(notOption);
  		    
  		    div.appendChild(selector);
  		    div.appendChild(input);

            if (!msie) {
               div.setAttribute("onchange", "advancedSearch.updateExpression()");
            } else {
	          input.attachEvent("onchange", updateAdvancedSearchExpression);
	          selector.attachEvent("onchange", updateAdvancedSearchExpression);
            }
  		    
  		    this.element = div;
  		    return div;
			}
		this.pos = num;
		this.element = this.create();
		this.remove = function() {
			this.element.parentNode.removeChild(this.element);
		}
		this.getOperator = function() {
            var opEl = this.element.getElementsByTagName("select")[0];
			return (opEl ? opEl.value : "");
		}
		this.getWord = function() {
			return this.element.getElementsByTagName("input")[0].value;
		}
	}
	
	this.clauses = new Array();

	this.addClause = function() {
		var currentLastClauseElement = document.getElementById("clause"+(this.clauses.length + 1));
		var newClause = new clause(this.clauses.length + 2);
		var addClauseButton = document.getElementById("removeClauseButton");
		currentLastClauseElement.parentNode.insertBefore(newClause.element, addClauseButton);
		this.clauses.push(newClause);
		//alert("Added clause "+this.clauses[this.clauses.length - 1]);
	    this.updateButtonDisplay();
	}
	
    this.removeClause = function() {
	    var clause = this.clauses.pop();
	    clause.remove();
	    this.updateButtonDisplay();
    }
    
    this.updateButtonDisplay = function() {
	    var removeClauseButton = document.getElementById("removeClauseButton");
	    removeClauseButton.style.display = (this.clauses.length > 0 ? "inline" : "none");
	    var addClauseButton =  document.getElementById("addClauseButton");
	    var inputStarted = (document.getElementById("word1").value.length > 0);
	    addClauseButton.style.display = (this.clauses.length < 10 && inputStarted ? "inline" : "none");
	    documentLengthAdjust(); /* FOFO specific */
    }
    
    this.updateExpression = function() {
	    var s = "\"" + document.getElementById("clause1").getElementsByTagName("input")[0].value + "\"";
	    for (var i = 0; i < this.clauses.length; i++) {
		    s += " " + this.clauses[i].getOperator() + "\""+this.clauses[i].getWord()+"\"";
	    }
	    var field = document.getElementById("advancedSearchQuery");
	    field.value = s;
	    this.updateButtonDisplay();
	    return s;
    }
	
}
