function SearchKey(firstLevel, operator, targetWindow) {
    // assigns an id and increments the global counter
    this.id = targetWindow.searchKeyCounter;
    this.targetWindow = targetWindow;
    targetWindow.searchKeyCounter++;
    this.status = 'on';
    this.firstLevel = firstLevel;
    if (this.firstLevel == "datiid") { 
        this.desFirstLevel = "Dati ID";
    } else if (this.firstLevel == "localita") { 
        this.desFirstLevel = "Localit\u00E0";
    } else if (this.firstLevel == "impexp") { 
        this.desFirstLevel = "Imp/Exp";
    } else if (this.firstLevel == "attivita") { 
        this.desFirstLevel = "Attivit\u00E0";
    } else if (this.firstLevel == "Match File") { 
        this.desFirstLevel = "Scontro File";
    } else {
        this.desFirstLevel = firstLevel;
    }
    this.secondLevel = '';
    this.userChoice = new Array();
    this.tipologia = '';
    this.operator = operator.toLowerCase();
    this.count = "";
    return this.id;
}

//cambia stile dei box a seconda dello stato impostato sopra
SearchKey.prototype.draw = function(){
    var stile = (this.status == "on") ? "query" : "query-off";
    var boxId = "q_" + this.id;
    var divBox = window.top.iframe.document.createElement("div");
    divBox.setAttribute("className", stile);
    divBox.setAttribute("class", stile);
    divBox.setAttribute("onfocus", "focusedQuery=this.id.substr(2)");
    divBox.setAttribute("id", boxId);
    box = window.top.iframe.document.getElementById(
        "drop-" + this.operator + "-queries").appendChild(divBox);
    
    // spezza la stringa di secondlevel per non creare problemi di layout 
    // nel box quando il nome inserito in subquery e' troppo lungo
    var strSecLev = this.secondLevel;
    var cuttedOutput = this.secondLevel;
    
    if (this.firstLevel == "subQuery" || this.firstLevel == "Match File") {
        cuttedOutput = '';
        for (i = 0; i < strSecLev.length; i += 15) {
            x = (i + 15);
            strSliced = strSecLev.slice(i, x);
            cuttedOutput += strSliced + " ";
        }
    }
    divBox.innerHTML = "<p class=\"pl\">" + this.desFirstLevel 
        + "</p><p class=\"sl\">" + cuttedOutput 
        + "</p><p class=\"op\">Operatori:<span id=\"count_" 
        + this.id + "\">" + this.count + "</span></p>" 
        + "<div class=\"toggle-box\"><img src=\"img/btn_box_" 
        + this.status + ".gif\" onclick=\"changeStatus(" + this.id + ")\" "  
        + "id=\"imgstato" + this.id + "\" /></div><div class=\"close-box\">" 
        + "<img src=\"img/btn_box_close.gif\" onclick=\"creamaschera(" 
        + this.id + ");\"/></div>";
}

SearchKey.prototype.remove = function(targetWindow){
    targetWindow.searchKeyCounter--;
    targetWindow.arrQueries.splice(this.id, 1);
    for (i = 0; i < arrQueries.length; i++) {
        arrQueries[i].id = i;
    }
}

SearchKey.prototype.toString = function() {
    this.string = "";
    if (this.id != undefined) {
        this.string += "Id: " + this.id + "\n";
    }
    if (this.firstLevel != undefined) {
        this.string += "FirstLevel: " + this.firstLevel + "\n";
    }
    if (this.secondLevel != undefined) {
        this.string += "SecondLevel: " + this.secondLevel + "\n";
    }
    if (this.operator != undefined) {
        this.string += "Operator: " + this.operator + "\n";
    }
    if (this.tipologia != undefined) {
        this.string += "tipologia: " + this.tipologia + "\n";
    }
    if (this.status != undefined) {
        this.string += "Status: " + this.status + "\n";
    }
    if (this.count != undefined) {
        this.string += "Count: " + this.count + "\n";
    }
    if (this.userChoice != undefined) {
        this.string += "userChoice: " + this.userChoice + "\n";
    }
    return this.string;
    
}

