/**
* Dynamic components.
* @author Vladan Antic.
* @date 10.2006.
* @version 1.1.
* @date 11.2006.
* @version 1.2.
*/

var isSafari = false;
var isMoz    = false;
var isIE     = false;
var isOpera  = false;

if (navigator.userAgent.indexOf("Safari") > 0) {
    isSafari = true;
} else if (navigator.product == "Gecko") {
    isMoz = true;
} else if (navigator.userAgent.indexOf('Opera') >= 0) {
    isOpera = true;
} else if (navigator.userAgent.indexOf('MSIE') >= 0 && navigator.userAgent.indexOf('Opera') < 0) {
    isIE = true;
}

elmIframe = '<iframe id="dummyFrame" src="/blank.html"></iframe>';

/*
 * Helpful functions
 **************************/
function setElement(elm, prm) {
    if (elm == null || prm == null) return false;
    if (!(e = $(elm)))
        e = elm;
    
    for (var i=0; i<prm.length; i++){
        if (prm[i][0] == null || prm[i][1] == null)
            return false;
            
        k = prm[i][0];
        v = prm[i][1];
        
        eval("e."+k+" = v");
    }
    return true;
}

function statusMessage(msg) {
    msg = msg || '';
    if ($('status')) $('status').innerHTML = msg;
}

function iniLayer(layerName, act) {
  act = act || '';
  if (!$(layerName)) {
    var body = document.getElementsByTagName('body');
    var div  = '<div id="'+layerName+'"';
    if (act != '') div += 'onmousedown="'+act+'"';
    div += '></div>';
    new Insertion.Bottom(body[0], div);
  }
  return $(layerName);
}

function closeLayer(dBox) {
    Element.remove(dBox);
    if (--blurInd == 0 && $('blurLayer')) Element.remove('blurLayer');
}

function blurLayer() {
    var blurC = iniLayer('blurLayer');
    blurC.style.width  = getDocumentWidth() + 'px';
    blurC.style.height = getDocumentHeight() + 'px';
    if (isIE) blurC.innerHTML = elmIframe;
}

function dummyLayer() {
    var px_corr = 16;
    var dmy = iniLayer('dummyLayer');
    dmy.style.width  = getWindowWidth() + 'px';
    dmy.style.height = getDocumentHeight() - px_corr + 'px';
}
function removeDummyLayer() {
    Element.remove('dummyLayer');
}

function getElementWidth(element) {
    element = $(element);
    return element.offsetWidth;
}

function getWindowWidth() {
  var w = 0;
  if (self.innerWidth)
    w = self.innerWidth;
  else if (document.documentElement && document.documentElement.clientWidth)
    w = document.documentElement.clientWidth;
  else if (document.body)
    w = document.body.clientWidth;
  return w;
}

function getWindowHeight() {
  var h = 0;
  if (self.innerHeight)
    h = self.innerHeight;
  else if (document.documentElement && document.documentElement.clientHeight)
    h = document.documentElement.clientHeight;
  else if (document.body)
    h = document.body.clientHeight;
  return h;
}

function getDocumentWidth() {
  var w = getWindowWidth();
  if(document.width && w < document.width) w = document.width;
  else if(document.body.offsetWidth && w < document.body.offsetWidth) w = document.body.offsetWidth;
  return w;
}

function getDocumentHeight() {
  var h = getWindowHeight();
  if(document.height && h < document.height) h = document.height;
  else if(document.body.offsetHeight && h < document.body.offsetHeight) h = document.body.offsetHeight;
  return h;
}

function getWindowYOffset() {
  var y = 0;
  if(window.pageYOffset)
    y = window.pageYOffset;
  else if (document.documentElement && document.documentElement.scrollTop)
    y = document.documentElement.scrollTop;
  return y;
}

function objAlpha(element, nOpacity) {
  var obj = $(element);
  obj.style.opacity = (nOpacity/100);
  obj.style.MozOpacity = (nOpacity/100);
  obj.style.KhtmlOpacity = (nOpacity/100);
  obj.style.filter = "alpha(opacity=" + nOpacity + ")";
}

/* ----------------------- */

function isAlien(a) {
  return isObject(a) && typeof a.constructor != 'function';
}

function isArray(a) {
  return isObject(a) && a.constructor == Array;
}

function isBoolean(a) {
  return typeof a == 'boolean';
}

function isEmpty(o) {
  var i, v;
  if (isObject(o)) {
    for (i in o) {
      v = o[i];
      if (isUndefined(v) && isFunction(v)) {
        return false;
      }
    }
  }
  return true;
}

function isFunction(a) {
  return typeof a == 'function';
}

function isNull(a) {
  return typeof a == 'object' && !a;
}

function isNumber(a) {
  return typeof a == 'number' && isFinite(a);
}

function isObject(a) {
  return (a && typeof a == 'object') || isFunction(a);
}

function isString(a) {
  return typeof a == 'string';
}

function isUndefined(a) {
  return typeof a == 'undefined';
}

function getPropertyByName(node, nodeName) {
  var arr = new Array();
  var items = node.getElementsByTagName("name");
  for (var i=0; i<items.length; i++) {
    if (nodeName == items[i].firstChild.nodeValue) {
      arr[0] = items[i].firstChild.nodeValue;
      arr[1] = items[i].parentNode.getElementsByTagName("value")[0].firstChild.nodeValue;
      return arr;
    }
  }
  return null;
}

function getValueForNode(node, nodeName) {
  var arr = getPropertyByName(node, nodeName);
  return arr != null ? arr[1] : null;
}

function evalBoolean(value, defaultValue) {
  if (!isNull(value) && isString(value)) {
    return ("true" == value.toLowerCase() || "yes" == value.toLowerCase()) ? "true" : "false";
  } else {
    return defaultValue == true ? "true" : "false";
  }
}

function replaceWithValue(sourceString, regExp, element) {
  var retString = "";
  switch (element.type) {
    case 'checkbox':
    case 'radio':
    case 'text':
    case 'textarea':
    case 'password':
    case 'hidden':
    case 'select-one':
    case 'select-multiple':
      retString = sourceString.replace(regExp, encodeEscChr($F(element)));
      break;
    default:
      retString = sourceString.replace(regExp, encodeEscChr(element.innerHTML));
      break;
  }
  return retString;
}

function arrayToParameterString(array) {
  var str = '';
  for (var i=0; i<array.length; i++) {
    // escape parameter values
    var pair = array[i].split('=');
    if (pair.length == 2) {
      str += (i >= 0 ? '&' : '') + pair[0] + '=' + escape(pair[1]);
    } else {
      str += (i >= 0 ? '&' : '') + array[i];
    }
  }
  return str;
}

function decodeHtml(str) {
    str = str.replace(/&amp;/, "&");
    str = str.replace(/&lt;/, "<");
    str = str.replace(/&gt;/, ">");
    return str;
}

function encodeHtml(str) {
    str = str.replace(/&/g, "&amp;");
    str = str.replace(/</g, "&lt;");
    str = str.replace(/>/g, "&gt;");
    str = str.replace(/"/g, "&quot;");
    str = str.replace(/'/g, "&rsquo;");
    return str;
}

function encodeEscChr(inVar) {
    inVar = inVar.replace(/&/g, "%26");
    inVar = inVar.replace(/'/g, "%27");
    inVar = inVar.replace(/"/g, "%22");
    inVar = inVar.replace(/\?/g, "%3F");
    inVar = inVar.replace(/\$/g, "%24");
    inVar = inVar.replace(/>/g, "%3E");
    inVar = inVar.replace(/</g, "%3C");
    inVar = inVar.replace(/%/g, "%25");
    inVar = inVar.replace(/\+/g, "%2B");
    return inVar;
}  
 
function trimString(str) {
    return str.replace(/^\s+|\s+$/, '');
}

function truncateString(str, len, sfx, exa){
    sfx = sfx || '';
    exa = exa || false;
    var ls = len;
    if (str.length > len){
        str = str.substr(0, len);
        if (!exa) {
            var ls = str.lastIndexOf(' ');
            if (ls < 1) ls = len;
        }
        str = str.substr(0, ls) + sfx;
    }
    return str;
}


/* Dragging ----------------------- */

var dragElem = '';
var iDiffX   = 0;
var iDiffY   = 0;

function setDragElem(dElm) {
    dragElem = dElm;
}

function handleMouseDown(oEvent) {
    if ($(dragElem)) {
        var oDiv = $(dragElem);
        iDiffX = oEvent.clientX - oDiv.offsetLeft;
        iDiffY = oEvent.clientY - oDiv.offsetTop;
        $(dragElem).style.cursor = 'move';
        
        Event.observe(document.body, 'mousemove', handleMouseMove, false);
        Event.observe(document.body, 'mouseup',  handleMouseUp, false);
    }
}
function handleMouseMove(oEvent) {
    if ($(dragElem)) {
        var oDiv = $(dragElem);
        oDiv.style.left = oEvent.clientX - iDiffX + 'px';
        oDiv.style.top = oEvent.clientY - iDiffY + 'px';
    }
}
function handleMouseUp() {
    if ($(dragElem)) {
        $(dragElem).style.cursor = 'default';
        Event.stopObserving(document.body, 'mousemove', handleMouseMove, false);
        Event.stopObserving(document.body, 'mouseup',  handleMouseUp, false);
    }
}

/*
 * contextMenu
 **************************/
function contextMenu(evn, elm, wd, clsEvent, obj, orient) {
    if (elm == null) return false;
    wd = wd || 160;
    clsEvent = clsEvent || 'click';
    orient = orient || 'down';
    
    var top_c  = 0;
    var left_c = 0;
    
    if ($('contextMenu'))
        closeContextMenu();
        
    var cm_items = document.getElementsByName(elm);
    
    // create context menu if items exist...
    if (cm_items.length > 0) {
        var cm_div = iniLayer('contextMenu');
        var cm_main = document.createElement('div');
        cm_main.className   = 'main';
        cm_main.style.width = wd;
        
        for (i=0; i<cm_items.length; i++) {
            var div = document.createElement('div');
            cm_icon   = cm_items[i].getAttribute('icon');
            cm_text   = (cm_items[i].getAttribute('label')) ? cm_items[i].getAttribute('label') : "Context Menu";
            cm_action = cm_items[i].getAttribute('actions');
            cm_sep    = cm_items[i].getAttribute('sep');
            if (cm_action) {
                div.className   = 'item';
                div.setAttribute('onclick', "closeContextMenu();" + cm_action, 0);
                div.setAttribute('onmouseover', "this.className = 'over';", 0);
                div.setAttribute('onmouseout', "this.className = 'item';", 0);
                
                ico = new Image(16, 16); 
                if (cm_icon) {
                    ico.src   = "images/icons/" + cm_icon; 
                } else {
                    ico.src   = "images/icons/empty.gif"; 
                }
                ico.align = 'absmiddle';
                div.appendChild(ico);
            } else {
                div.className   = 'text';
            }
            div.innerHTML += "&nbsp;" + cm_text;
            cm_main.appendChild(div);
            
            if (cm_sep) {
                var sep = document.createElement('DIV');
                sep.className = 'sep';
                sep.innerHTML = "<img src=\"images/1.gif\">";
                cm_main.appendChild(sep);
            }
        }
        
        if (isIE && i>0) cm_main.innerHTML += elmIframe;

        cm_div.appendChild(cm_main);
        
        if (obj != null) {
            // get position of given element (obj)...
            var elmPos = getPosition(obj);
            
            // if dummyLayer is closed by mouseover event, mask given element...
            if (clsEvent == 'mouseover') {
                var om_div = iniLayer('objMask');
                om_div.style.top    = (elmPos.y - 1) + 'px';
                om_div.style.left   = (elmPos.x - 1) + 'px';
                om_div.style.width  = obj.offsetWidth + 2 + 'px';
                om_div.style.height = obj.offsetHeight + 2 + 'px';
                om_div.style.position = 'absolute';
                om_div.style.zIndex = 101;
            }
            if (orient == 'down') { 
                top_c  = elmPos.y + obj.offsetHeight + 1;
                left_c = elmPos.x;
            } else {
                top_c  = elmPos.y;
                left_c = elmPos.x + obj.offsetWidth + 1;
            }
        } else {
            top_c  = Event.pointerY(evn) - 2;
            left_c = Event.pointerX(evn) - 2;
        }
        
        oX = left_c + cm_div.offsetWidth;
        oW = getWindowWidth();
        
        cm_div.style.padding  = '1px';
        cm_div.style.top  = top_c + 'px';
        cm_div.style.left = left_c + 'px';
        // left orientation if cM is out of screen
        if (oX > oW)
            cm_div.style.left = left_c - (oX - oW) - 24 + 'px';
            
        cm_div.style.visibility = 'visible';
        
        // dummyLayer to close cM
        dummyLayer();
        Event.observe('dummyLayer', clsEvent, closeContextMenu, false);
    }
}

function closeContextMenu() {
    Event.stopObserving('dummyLayer', 'click', closeContextMenu, false);
    Element.remove('dummyLayer');
    Element.remove('contextMenu');
    if ($('objMask'))
        Element.remove('objMask');
}


/*
 * dialogBox
 **************************/
var dbIndex = 0;
var dbName  = 'dBox';
var blurInd = 0;

DialogBox = Class.create();
DialogBox.prototype = {
    initialize: function(blr, drg) {
        dbIndex++;
        this.id       = dbName + dbIndex;
        this.buttons  = Array();
        this.blur     = blr || false;
        this.drag     = drg || false;
        this.minimize = true;
        this.left     = null;
        this.top      = null;
        this.width    = null;
        this.height   = null;
    },

    setVar: function(vr, vl) {
        eval("this." + vr + "=vl");
    },

    addButton: function(lbl, act, wd) {
        wd = wd || 65;
        this.buttons[this.buttons.length] = '<input type="button" class="buttons" value="' + lbl + '" onclick="' + act + '" style="width:' + wd + 'px;" />';
    },
  
    dialogBox: function(ttl, main, lf, tp, wd, hi) {
        this.init();
        
        // make blurLayer...
        ++blurInd;
        if (this.blur && blurInd == 1) blurLayer();

        this.left   = lf || this.left;
        this.top    = tp || this.top;
        this.width  = wd || this.width;
        this.height = hi || this.height;
        
        $(this.id + 'Title').innerHTML = ttl;
        $(this.id + 'Main').innerHTML = main;
        
        var btns = '';
        for (i=0; i<this.buttons.length; i++)
            btns += this.buttons[i];
        $(this.id + 'Buttons').innerHTML = btns;
        
        var d_wd  = this.width || getElementWidth(this.id);
        var d_hi  = this.height || Element.getHeight(this.id);
        var y_new = Math.round(getWindowHeight()/2 + getWindowYOffset() - d_hi/2 - 30);
        if (y_new < 0) y_new = 1;
        
        $(this.id).style.left = (this.left || (getWindowWidth()/2 - d_wd/2)) + 'px';
        $(this.id).style.top  = (this.top || y_new) + 'px';
        $(this.id).style.visibility = 'visible';
        $(this.id + 'Table').style.width = d_wd + 'px';
        
        // start dragging...
        if (this.drag) {
            setDragElem(this.id);
            Event.observe(this.id + 'Head', 'mousedown', handleMouseDown, false);
            $(this.id + 'Title').style.cursor = 'move';
        }
        setActiveDialog(dbIndex);
    },
  
    init: function () {
        var act = "setActiveDialog(" + dbIndex + ");";
        var d = iniLayer(this.id, act);
        $(this.id).className = 'dbox';
        
        var drg = "setDragElem('" + this.id + "');";
        var mmb = "minimizeBox('" + this.id + "', this);";
        var cls = "closeLayer('" + this.id + "');";
        
        var out = '';
        out += '<table id="' + this.id + 'Table" width="120" border="0" cellPadding="2" cellSpacing="1">';
        out += '<tr>';
        out += '<td id="' + this.id + 'Head" height="19" class="dbox_head" nowrap ';
        if (this.drag)
            out += 'onmousedown="' + drg + '" style="padding-right: 2px;" ';
        out += '><div id="' + this.id + 'Title" class="dbox_title"></div>';
        out += '<div class="dbox_icons">';
        if (this.minimize)
            out += '<img src="images/icons/dbox_minimize.gif" onclick="' + mmb + '" style="padding-right: 2px;">';
        out += '<img src="images/icons/dbox_close.gif" onclick="' + cls + '"></div>';
        out += '</td>';
        out += '</tr>';
        out += '<tr>';
        out += '<td height="1" align="left" valign="top" class="dbox_main">';
        out += '<div id="' + this.id + 'Main"></div>';
        out += '<div id="' + this.id + 'Buttons" align="right" style="padding-top: 5px;"></div>';
        out += '</td>';
        out += '</tr>';
        out += '</table>';
        if (isIE) out += elmIframe;
        d.innerHTML = out;
    }
}

function minimizeBox(idBox, iObj) {
    if (iObj.src.indexOf('minimize') >= 0) {
        $(idBox + 'Main').style.display = "none";
        $(idBox + 'Buttons').style.display = "none";
        iObj.src = iObj.src.replace('minimize','maximize'); 
    } else {            
        $(idBox + 'Main').style.display = "block";
        $(idBox + 'Buttons').style.display = "block";
        iObj.src = iObj.src.replace('maximize','minimize'); 
    }
}

function setActiveDialog(idBox) {
    for (i=1; i<=dbIndex; i++){
        if ($(dbName + i)) {
            $(dbName + i + 'Head').className = "dbox_head_";
            $(dbName + i).style.zIndex    = "100";
        }
    }
    if ($(dbName + idBox)) {
        $(dbName + idBox + 'Head').className = "dbox_head";
        $(dbName + idBox).style.zIndex    = "101";
    }
}

/*
 * dynamicList
 **************************/
DynamicList = Class.create();
DynamicList.prototype = {

    initialize: function(url, options) {
        this.baseUrl = url;
        this.xml = null;
        this.currentIndex = 0;
        this.listLength   = 0;
        this.suggestList  = new Array();
        this.currentLetters = new Array();
        
        this.setOptions(options);
        this.createPopup();
        if (isIE) this.createIframe();

        this.options.sourceElem.setAttribute("autocomplete", "off");
        this.attachBehaviors(this.options.sourceElem, this.options.eventField, this.makeList, this);
        this.attachBehaviors(this.options.sourceElem, this.options.eventPopup, this.keyNavi, this);
        this.attachBehaviors(this.options.sourceElem, 'blur', this.emptyField, this);
    },
    
    setOptions: function(options) {
        this.options = Object.extend({
            sourceElem: $(options.source),
            targetElem: options.target ? $(options.target) : false,
            eventField: options.eventField ? options.eventField : "keyup",
            eventPopup: "keydown",
            appendSeparator: options.appendSeparator || "#",
            appendValue: evalBoolean(options.appendValue),
            fillSource: options.fillSource ? options.fillSource : "selection"
        }, options || {});
        
        this.popupElem = "dynamicList";
        this.listElem  = "dlOption";
        this.cssOption = this.options.className + "_option";
        this.cssOver   = this.options.className + "_over";
        this.maxHeight = 198;
    },
    
    makeList: function(e) {
        var key = 0;
        if (e.keyCode) { key = e.keyCode; }
        else if (typeof(e.which)!= 'undefined') { key = e.which; }

        var fieldLength = $F(this.options.sourceElem).length;

        if (fieldLength == 0 || fieldLength < this.options.minChars){
            $(this.popupElem).innerHTML = '';
            this.hidePopup();
            statusMessage();
                        
        } else if (key != 38 && key != 40 && key != 13 && key != 9 && key != 27 && key != 37 && key != 39 && key != 17 && key != 18) {
            this.setProgressStyle();
            
            // get ajax request...
            var ajax = new Ajax.Request(
                this.baseUrl, {
                    method: 'post', 
                    parameters: arrayToParameterString(this.buildParameterString(this.options.parameters).split(',')) + '&maxItems=' + this.options.maxItems + '&utfDecode=no',
                    onComplete: this.setResult.bind(this)
                });
        }
    },
    
    setResult: function(request) {
        this.xml = request.responseXML;
        $(this.popupElem).innerHTML = '';
        this.hidePopup();

        this.listLength   = 0;
        this.currentIndex = 0;
        
        // grab xml of options...
        var respNode = this.xml.getElementsByTagName("response")[0];
        var items = respNode.getElementsByTagName("item");
        
        for (var i=0; i<items.length; i++) {
            var div = document.createElement("div");
            div.id = this.listElem + i;
            div.index = i;
            div.className = this.cssOption;
            div.onmouseover = this.handleOver.bindAsEventListener(this);
            div.onclick = this.handleClick.bindAsEventListener(this);
            if (this.options.targetElem)
                div.value = items[i].getElementsByTagName("value")[0].firstChild.nodeValue;
            div.innerHTML = items[i].getElementsByTagName("name")[0].firstChild.nodeValue;
            $(this.popupElem).appendChild(div);
        }
        if (items.length >= this.options.maxItems) {
            var div = document.createElement("div");
            div.className = this.cssOption;
            div.innerHTML = "-- more...";
            $(this.popupElem).appendChild(div);
        }
        
        this.listLength = i;
        
        if (this.listLength > 0){
            $(this.listElem + this.currentIndex).className = this.cssOver;
            Element.show(this.popupElem);
            this.setPopupStyles();
        }
        
        statusMessage(respNode.getElementsByTagName("status")[0].firstChild.nodeValue);
    },
    
    keyNavi: function(e) {
        var key = 0;
        if (e.keyCode) { key = e.keyCode; }
        else if (typeof(e.which) != 'undefined') { key = e.which; }

        //up arrow
        if (key == 38) {
            if (this.currentIndex > 0) {
                $(this.listElem + this.currentIndex).className = this.cssOption;
                this.currentIndex--;
                $(this.listElem + this.currentIndex).className = this.cssOver        
                $(this.listElem + this.currentIndex).scrollIntoView(false);
            }
        
        //down arrow
        } else if (key == 40) {
            if (this.currentIndex < this.listLength - 1) {
                $(this.listElem + this.currentIndex).className = this.cssOption;
                this.currentIndex++;
                $(this.listElem + this.currentIndex).className = this.cssOver        
                $(this.listElem + this.currentIndex).scrollIntoView(false);
            }
        
        //enter
        } else if (key == 13 && $(this.popupElem).style.display != 'none') {
            this.fillField($(this.listElem + this.currentIndex));
            Event.stop(e);
            this.executePostFunction();
            
        //tab
        } else if (key == 9) {
            this.hidePopup();
            
        //escape
        } else if (key == 27) {
            this.hidePopup();
            Event.stop(e);
        }
    },
    
    executePostFunction: function() {
        if (this.options.postFunction) {
            this.options.postFunction(this.xml);
        }
    },
    
    createPopup: function() {
        new Insertion.Top(document.getElementsByTagName("body")[0],
            "<div id=\""+this.popupElem+"\" class=\""+this.options.className+"\"></div>");
        this.hidePopup();
    },
    
    hidePopup: function() {
        this.resetProgressStyle();
        Element.hide(this.popupElem);
        if (isIE && $('dummyFrame')) $('dummyFrame').style.display = "none";
        if ($('dummyLayer')) removeDummyLayer();
    },
    
    setProgressStyle: function() {
        if (this.options.progressStyle != null) {
          Element.addClassName(this.options.sourceElem, this.options.progressStyle);
        }
    },
    
    resetProgressStyle: function() {
        if (this.options.progressStyle != null) {
          Element.removeClassName(this.options.sourceElem, this.options.progressStyle);
        }
    },
    
    fillField: function(selection) {
        if (this.options.fillSource == "selection") this.options.sourceElem.value = decodeHtml(selection.innerHTML);
        if (this.options.targetElem) {
            // fill source with target value (id, ...) to make a search...
            if (this.options.fillSource == "target") this.options.sourceElem.value = selection.value;
            if (this.options.appendValue == "false") {
                this.options.targetElem.value = selection.value;
            } else {
                if (this.options.targetElem.value.length > 0)
                    this.options.targetElem.value += this.options.appendSeparator;
                this.options.targetElem.value += selection.value;
            }
        }
        this.hidePopup();
    },
    
    emptyField: function() {
        if (this.options.sourceElem.value == "" || this.listLength < 1) {
            if (this.options.targetElem) this.options.targetElem.value = "";
            statusMessage();
        }
    },
    
    handleClick: function(e) {
        this.fillField(Event.element(e));
        this.executePostFunction();
    },
    
    handleOver: function(e) {
        $(this.listElem + this.currentIndex).className = this.cssOption;
        this.currentIndex = Event.element(e).index;
        $(this.listElem + this.currentIndex).className = this.cssOver;
    },
    
    setPopupStyles: function() {
        var elmPos = getPosition(this.options.sourceElem);
        var pxTop   = isOpera ? 5 : 0;
        var pxLeft  = isOpera ? 2 : 0;
        
        if ($(this.popupElem).offsetHeight < this.maxHeight) {
            $(this.popupElem).style.overflow = 'hidden';
        } else if (isMoz) {
            $(this.popupElem).style.maxHeight = this.maxHeight + 'px';
            $(this.popupElem).style.overflow = '-moz-scrollbars-vertical';
        } else {
            $(this.popupElem).style.height = this.maxHeight + 'px';
            if (isOpera) {
                $(this.popupElem).style.overflow = 'auto';
            } else {
                $(this.popupElem).style.overflowY = 'auto';
            }
        }
        
        $(this.popupElem).scrollTop = 0;
        $(this.popupElem).style.top = (elmPos.y + this.options.sourceElem.offsetHeight + pxTop) + "px";
        $(this.popupElem).style.left = elmPos.x + pxLeft + "px";
        
        if (isIE) {
            $(this.popupElem).style.width = this.options.sourceElem.offsetWidth + "px";
        } else {
            $(this.popupElem).style.minWidth = this.options.sourceElem.offsetWidth + "px";
        }
        
        if (isIE) {
            dmyF = $('dummyFrame');
            dmyF.style.top = (elmPos.y + this.options.sourceElem.offsetHeight) + "px";
            dmyF.style.left = elmPos.x + "px";
            dmyF.style.width = $(this.popupElem).offsetWidth;
            dmyF.style.height = $(this.popupElem).offsetHeight;
            dmyF.style.display = "block";
            dmyF.style.zIndex = 90;
        }
        
        dummyLayer();
        this.attachBehaviors($('dummyLayer'), 'click', this.hidePopup, this);
    },
    
    createIframe: function() {
        new Insertion.Before($(this.popupElem), elmIframe);
        $('dummyFrame').style.width = 0;
        $('dummyFrame').style.height = 0;
    },
    
    buildParameterString: function(parameterList) {
        var ajaxParameters = parameterList || '';
        var re = new RegExp("(\\{[^,]*\\})", 'g'); // should retrieve each {} group
        var results = ajaxParameters.match(re);
        if (results != null) {
            for (var r=0; r<results.length; r++) {
                var nre = new RegExp(results[r], 'g');
                var field = $(results[r].substring(1, results[r].length-1));
                ajaxParameters = replaceWithValue(ajaxParameters, nre, field);
            }
        }
        return ajaxParameters;
    },
    
    attachBehaviors: function(element, event, listener, obj) {
        if (isArray(element)) {
            for (var i=0; i<element.length; i++) {
                eval("element[i].on"+event+" = listener.bindAsEventListener(obj)");
            }
        } else {
            eval("element.on"+event+" = listener.bindAsEventListener(obj)");
        }
    }
}


/*
 * tab functions
 **************************/
function displayTab(elm, idx){
    var spans = document.getElementsByTagName('span');
    for (i=0; i<spans.length; i++) {
        tn = spans[i].getAttribute("name");
        if (tn == elm) {
            spans[i].className = (i == idx) ? "tab_o" : "tab_c";
            $(elm + '_block_' + i).style.display = (i == idx) ? "block" : "none";
        }
    }
}
function setTab(elm, idx, ind){
    var spans = document.getElementsByTagName('span');
    for (i=0; i<spans.length; i++) {
        tn = spans[i].getAttribute("name");
        if (tn == elm) {
            if (i == idx) spans[i].className = (ind == 1) ? "tab_c" : "tab_d";
        }
    }
}

