/*  select pulldown emulator 
    all objects must be registred there before use
*/

function competer_proto() {
    this.SLRarr = [];
        this.lastSelected = null;

    this.add = function(formName, textElmName, classContainerName, selectedClassName, flags) {
                var alone = (flags != null && flags == 'alone');
        this.SLRarr[this.SLRarr.length] = Array(formName, textElmName, classContainerName, selectedClassName, alone);
    };
    
    this.addText = function(elmNode) {
                var txt = elmNode.innerHTML;
                curNode = elmNode.parentNode;
                var N = 5;
                while (N-- > 0 && curNode.className == '') curNode = curNode.parentNode;
                if (curNode.className != '') {
                        var k = 0;
                        var classContainerName = curNode.className;
                        while (k < this.SLRarr.length) {
                                if (this.SLRarr[k][2] == classContainerName) {
                                        var elm = document.forms[this.SLRarr[k][0]].elements[this.SLRarr[k][1]];
                                        if (this.SLRarr[k][4]) { //on alone sign
                                                elm.value = txt;
                                                if (this.lastSelected != null) this.lastSelected.className = "";
                                                elmNode.className = this.SLRarr[k][3];
                                                this.lastSelected = elmNode;
                                        } else { //multy select case
                                                var elmValue = elm.value;
                                                if (elmNode.className == this.SLRarr[k][3]) {
                                                        elmNode.className = '';
                                                        var getPos = elmValue.indexOf(txt);
                                                        if (getPos != -1) {
                                                                if (getPos == 0) 
                                                                        elm.value = elmValue.substring(txt.length + 2, elmValue.length);
                                                                else 
                                                                        elm.value = elmValue.substring(0, getPos - 2) + elmValue.substring(getPos + txt.length, elmValue.length);
                                                        }
                                                } else {
                                                        elmNode.className = this.SLRarr[k][3];
                                                        if (elmValue.length > 0)
                                                                elm.value = elmValue + ', ' + txt;
                                                        else 
                                                                elm.value = txt;
                                                }
                                        }
                                        
                                        break;
                                }
                                k++;
                        }
                }
    }
}

var completer_prototype = new competer_proto();

function selector_proto() {
    this.SLRarr = [];

    this.add = function(formName, textElmName, layerID) {
        this.SLRarr[this.SLRarr.length] = Array(formName, textElmName, layerID);
    };
    
    this.open = function(layerID) {
        var k = 0;
        while (k < this.SLRarr.length) {
            if (this.SLRarr[k][2] == layerID) {
                document.getElementById(layerID).style.display = 'block';
            } else {
				try {
                document.getElementById(this.SLRarr[k][2]).style.display = 'none';
				}
				catch (e) {
					//alert(this.SLRarr[k][2]);
				}
            }
            k++;
        }
                return false;
    };
    
    this.closeAll = function() {
        this.open('');
    }
    
    this.selPos = function(elmNode) {
                var txt = elmNode.innerHTML;
                var N = 5;
                while (N-- > 0 && elmNode.id == '') elmNode = elmNode.parentNode;
                if (elmNode.id != '') {
                        var k = 0;
                        var layerID = elmNode.id;
                        while (k < this.SLRarr.length) {
                                if (this.SLRarr[k][2] == layerID) {
                                        document.forms[this.SLRarr[k][0]].elements[this.SLRarr[k][1]].value = txt;
                                        break;
                                }
                                k++;
                        }
                }
    }
}

var selector_prototype = new selector_proto();

/* category's container */
function CategFilter(containerName) {
  this.cat = null;
  this.objname = containerName;
  this.activeMark = 'active';
  this.ActiveClass = "cat " + this.activeMark;
  this.InactiveClass = "cat";
  
  /* return selected categories list codes as one string with ':' delemiter */
  this.getCategList = function() {
    return '0' + this._getCategList($(this.objname));
  }
  
  this._getCategList = function(elmnod) {
    var str = '', len = elmnod.childNodes.length;
    
    for (var i=0; i < len; i++) {
        Elm = elmnod.childNodes[i];
        if (Elm.tagName == 'TD' && Elm.hasClassName(this.activeMark)) str += ':' + Elm.getAttribute('value');
        else str += this._getCategList(Elm);
    }
    return str;
  }
  this.tgl = function(obj) 
  {
	  $(this.objname).select('td[class~="active"]').each( function(td) {
	      $(td).removeClassName('active');
	  });
	  $(obj).addClassName(this.activeMark);
  }
  this.resetAll = function()
  {
	  var cells = $(this.objname).select('td[value]');
	  for (var i = 0; i < cells.length; ++i) {
		  if (cells[i].hasClassName('root')) {
			  $(cells[i]).addClassName(this.activeMark);
		  }
		  else {
			  $(cells[i]).removeClassName(this.ActiveClass);
		  }
	  }
  }
}

document.onmouseup = function(e) {
    selector_prototype.closeAll();
}