﻿
function catchEnter(){
	if(event.keyCode == 13){
	    submitFormWithButton();
	}
}

function filterAndSubmit() {
    document.getElementById(action).value = "filter";
    document.getElementById("cleanList").value = "true";
    document.forms[0].action = "Search.aspx";
    UpdateCheckedList();
    document.forms[0].submit();
}

function setSortAndSubmit(exp)
{
    document.getElementById(action).value = "sort";
    document.getElementById(sortExp).value = exp;
    document.forms[0].action = "Search.aspx";
    UpdateCheckedList();
    document.forms[0].submit();
}

function isNumber (o) {
  return ! isNaN (o-0);
}

function submitFormWithButton() {
    var freeText = document.getElementById('SearchTable1_freeText').getAttribute('value');
    if (freeText != null && freeText != '' && isNumber(freeText))
    {
        ShowProperty(freeText, '', true);
        return;
    }
    ClearCheckedList();
    document.getElementById(action).value = "search";
    document.getElementById(sortExp).value = "";
    document.forms[0].action = "Search.aspx";
    document.forms[0].submit();
}

function nextPage()
{
    document.getElementById(action).value = "page";
    var currPageValue;
    if (document.getElementById(currPage).value == "") {
        currPageValue = 1;
    }
    else {
        currPageValue = parseInt(document.getElementById(currPage).value) + 1;
    } 
    document.getElementById(currPage).value = currPageValue;

    document.forms[0].action = "Search.aspx";
    UpdateCheckedList();
    document.forms[0].submit();
}

function prevPage()
{
    document.getElementById(action).value = "page";
    var currPageValue;

    if (document.getElementById(currPage).value == "") {
        currPageValue = 0;
    }
    else {
        currPageValue = parseInt(document.getElementById(currPage).value) - 1;
    }
    document.getElementById(currPage).value = currPageValue;
    
    document.forms[0].action = "Search.aspx";
    UpdateCheckedList();
    document.forms[0].submit();
}

function setPage(page)
{
    document.getElementById(action).value = "page";
    document.getElementById(currPage).value = page;
    //document.forms[0].action = "search.aspx";
    UpdateCheckedList();
    document.forms[0].submit();
}

function UpdateCheckedList() {
    document.getElementById("checkedList").value = filtered_list.get_list_as_string();
}

function ClearCheckedList() {
    if (document.getElementById("checkedList") != null) {
        document.getElementById("checkedList").value = "";
    }
    if (filtered_list != null)
    {
        filtered_list.clearList();
    }
}

var filtered_list;

function LoadList(checkedListString) {
    filtered_list = new ObjectList();
    if (document.getElementById("cleanList").value != "true")
    {
        document.getElementById("checkedList").value = checkedListString;
        if (checkedListString != undefined)
        {
            var tmpList = checkedListString.split(", ");
            for (i = 0; i < tmpList.length; i++) {
                if (tmpList[i] != undefined && tmpList[i] != null && tmpList[i] != "" && tmpList[i] != "undefined") {
                    add_value(tmpList[i]);
                }
            }
        }
    }
}

// define a new class
// actually there is no 'real' classes in JavaScript. Instead we use a function here.
var ObjectList = function () {
    // internal array that we will store items
    this._obj_list = [];
    // item counter    
    this._obj_cnt = 0;
    // pre defined variable. this value is returned if a given item is not in the list    
    this.NOT_FOUND = -1;

    // add an object to list    
    this.addToList = function (obj) {
        if (obj == null) {
            return false;
        }
        pos = this.findPos(obj);
        if (pos == this.NOT_FOUND) {
            // simply append to the list        
            this._obj_list[this._obj_cnt++] = obj;
            return true;
        }
        return false;
    }

    // remove the given object from the list    
    this.removeFromList = function (obj) {
        if (this._obj_cnt == 0) {
            return false;
        }
        if (obj == null) {
            return false;
        }
        // find index of the object        
        pos = this.findPos(obj);
        if (pos == this.NOT_FOUND) {
            // obj not found in the list            
            return false;
        }
        // remove object from the list and shrink the array        
        this._obj_list.splice(pos, 1);
        this._obj_cnt--;
    }

    // returns the position of an object in the list    
    this.findPos = function (obj) {
        if (obj == null) {
            return this.NOT_FOUND;
        }
        for (i = 0; i < this._obj_cnt; i++) {
            if (obj == this._obj_list[i]) {
                return i;
            }
        }
        return this.NOT_FOUND;
    }

    this.clearList = function () {
        this._obj_list = [];
        this._obj_cnt = 0;
    }

    // this function is for debugging purposes. simple prints all items.    
    // not so useful, I know =)    
    this.print = function () {
        document.write("Total Items: " + this._obj_cnt + "<br>");
        for (i = 0; i < this._obj_cnt; i++) {
            document.write(this._obj_list[i] + "<br>");
        }
    }

    this.get_list_as_string = function () {
        var list_string = ""
        for (i = 0; i < this._obj_cnt; i++) {
            list_string += this._obj_list[i] + ", ";
        }
        return list_string;
    }

    this.get_list = function () {
        return this._obj_list;
    }
}

function add_remove_value(value, checked) {
    if (checked) {
        add_value(value);
    }
    else {
        remove_value(value);
    }
}

function find_pos(value) {
    return filtered_list.findPos(value);
}

function clear(value) {
    return filtered_list.clearList();
}

function add_value(value) {
    filtered_list.addToList(value);
}

function remove_value(value) {
    filtered_list.removeFromList(value);
}

function get_list() {
    return filtered_list.get_list();
}

function print_list() {
    if (filtered_list != undefined && filtered_list != null) {
        alert(filtered_list.get_list_as_string());
    }
    else {
        alert("List is empty");
    }
}


