var userAgent = navigator.userAgent;
var isMozilla = !isNS4 && userAgent.indexOf('Mozilla') == 0 && userAgent.indexOf('ompatible') < 0;
var isMSIE = !isOpera && userAgent.indexOf('MSIE') >= 0;
var isOpera = userAgent.indexOf('Opera') >= 0;
var isNS4 = userAgent.indexOf('Mozilla/4') == 0 && userAgent.indexOf('ompatible') < 0;

function dhGet(id)
{
    
   
    if (id.constructor != String) {
        return id;
    }
    

    var p = id.indexOf(':');
    var doc = document;

    if (p >= 0) {
        var frame = id.substr(0, p);
        id = id.substr(p+1);

        for (var i=0; i < parent.frames.length; i++) {
            if (parent.frames[i].name == frame) {
                doc = parent.frames[i].document;
                break;
            }
        }
    }

    if (doc.all) {
        return doc.all[id];
    }


    if (doc.getElementById) {
        if(doc.getElementById(id)){
            return doc.getElementById(id);
        }
    }

    if (doc.getElementsByName) {
        return doc.getElementsByName(id)[0];
    }

    if (doc.layers) {
        for (var i=0; i < doc.layers.length; i++) {
            if (doc.layers[i].id == id) {
                return doc.layers[i];
            }
        }
        return null;
    }
}

function dhAttr(id, cssAttr)
{
    var obj = dhGet(id);

    if (obj.currentStyle) {
        return obj.currentStyle[cssAttr];
    }

    if (document.defaultView) {
        return document.defaultView.getComputedStyle(obj, null).getPropertyValue(cssAttr);
    }

    res = eval('obj.style.' + cssAttr);
    if (!res) res = '';
    return res;
}

function dhLeft(id)
{
    var obj = dhGet(id);
    var res = 0;

    while (obj != null) {
        res += obj.offsetLeft;
        obj = obj.offsetParent;
    }

    return res;
}

function dhTop(id)
{
    var obj = dhGet(id);
    var res = 0;

    while (obj != null) {
        res += obj.offsetTop;
        obj = obj.offsetParent;
    }

    return res;
}

function dhMove(id, x, y)
{
    var obj = dhGet(id);

    if (!isNS4) {
        obj.style.position = 'absolute';
        obj.style.left = x + 'px';
        obj.style.top = y + 'px';
    }
    else {
        obj.pageX = x;
        obj.pageY = y;
    }
}

function dhMoveScreen(id, screenX, screenY)
{
    var obj = dhGet(id);

    dhMove(obj, dhScreenX() + screenX, dhScreenY() + screenY);
}

function dhVisible(id)
{
    var obj = dhGet(id);

    s = dhAttr(obj, 'visibility').toLowerCase();

    return s != 'hidden' && s != 'collapse' &&
            dhAttr(obj, 'display').toLowerCase() != 'none';
}

function dhHide(id)
{
    if (!isNS4) {
        dhGet(id).style.visibility = 'hidden';
    }
    else {
        dhGet(id).visibility = 'hidden';
    }
}

function dhRemove(id)
{
    if (isOpera || isNS4) {
        dhHide(id);
    }
    else {
        dhGet(id).style.display = 'none';
    }
}

function dhShow(id, display)
{
    var obj = dhGet(id);

    if (!isNS4) {
        s = dhAttr(obj, 'display').toLowerCase();
        if (s == 'none') {
            if (display == null) display = 'block';
            obj.style.display = display;
        }
        s = dhAttr(obj, 'visibility').toLowerCase();
        if (s == 'hidden' || s == 'collapse') {
            obj.style.visibility = 'visible';
        }
    }
    else {
        with (dhGet(id)) {
            visibility = 'visible';
            display = '';
        }
    }
}

function max(a, b)
{
    if (a > b) {
        return a;
    }
    return b;
}

function dhScreenWidth(doc)
{
    var a = 0, b = 0;

    if (!doc) {
        doc = document;
    }

    if (doc.body) {
        a = doc.body.clientWidth;
    }
    if (doc.documentElement) {
        b = doc.documentElement.clientWidth;
    }
    return max(a, b);
}

function dhScreenHeight(doc)
{
    var a = 0, b = 0;

    if (!doc) {
        doc = document;
    }

    if (doc.body) {
        a = doc.body.clientHeight;
    }
    if (doc.documentElement) {
        b = doc.documentElement.clientHeight;
    }
    return max(a, b);
}

function dhScreenX(doc)
{
    var a = 0, b = 0;

    if (!doc) {
        doc = document;
    }

    if (doc.body) {
        a = doc.body.scrollLeft;
    }
    if (doc.documentElement) {
        b = doc.documentElement.scrollLeft;
    }
    return max(a, b);
}

function dhScreenY(doc)
{
    var a = 0, b = 0;

    if (!doc) {
        doc = document;
    }

    if (doc.body) {
        a = doc.body.scrollTop;
    }
    if (doc.documentElement) {
        b = doc.documentElement.scrollTop;
    }
    return max(a, b);
}

function dhWidth(id)
{
    if (!isNS4) {
        return dhGet(id).offsetWidth;
    }

    return dhGet(id).clip.width;
}

function dhHeight(id)
{
    if (!isNS4) {
        return dhGet(id).offsetHeight;
    }

    return dhGet(id).clip.height;
}

function dhBottom(id)
{
    return dhTop(id) + dhHeight(id);
}

function dhRight(id)
{
    return dhLeft(id) + dhWidth(id);
}



/// todo:
function selectValue(ctl)
{
    return ctl.options[ctl.selectedIndex].value;
}

function selectName(ctl)
{
    return ctl.options[ctl.selectedIndex].name;
}

function selectClear(ctl)
{
    ctl.length = 0;
}

function selectAdd(ctl, text, value)
{
    if (arguments.length < 3) {
        value = text;
    }
    ctl.options[ctl.length] = new Option(text, value);
}

function selectAddNoDup(ctl, text, value)
{
    if (arguments.length < 3) {
        value = text;
    }
    for (var i=0; i < ctl.length; i++) {
        if (ctl.options[i].value == value) {
            return;
        }
    }
    selectAdd(ctl, text, value);
}

function radioValue(form, name)
{
    var n = form[name].length;

    for (var i=0; i < n; i++) {
        var r = form[name][i];
        if (r.checked) return r.value;
    }
    return 'unchecked';
}


function form_url(form){
// Retrieve action string from FORM elements
// Used with window_open() function
  var url='';
  if (form != null){
      url=form.action+'?';
      for (i=0; i < form.length; ++i)
         url+=(i!=0?'&':'')+encodeURI(form.elements(i).name)+'='+encodeURI(form.elements(i).value);
  }
  return url;
}

function window_open(URL,name,width,height,menu,resizable,scroll){
// Open window centered to users screen
    var screen_left=window.screenLeft;
    var screen_top=window.screenTop;
    var body_width=dhScreenWidth();
    var body_height=dhScreenHeight();

    var window_left=screen_left+Math.ceil((body_width-width)/2);
    var window_top=screen_top+Math.ceil((body_height-dhScreenY()-height)/2)-20;
//    var window_left = 0;
//    var window_top = 0;
		

    var window_width=width;
    var window_height=height;

    return window.open(URL,name,'height='+window_height+',left='+window_left+',menubar='+menu+',resizable='+resizable+',scrollbars='+scroll+',top='+window_top+',width='+window_width);

}

