/*---------------------------------------------------------------
  Trim operation
---------------------------------------------------------------*/
String.prototype.trim = function(){
    return this.replace(/(^\s*)|(\s*$)/g, "");
}

/*---------------------------------------------------------------
  Tests to see if the navigator is Microsoft Internet Explorer
---------------------------------------------------------------*/
function isIE(){
    var sUserAgent = navigator.userAgent;
    return sUserAgent.indexOf("MSIE") >= 0 && sUserAgent.indexOf("Opera") < 0;
}


/*---------------------------------------------------------------
  Tests to see if the navigator is Mozilla
---------------------------------------------------------------*/
function isMozilla(){
    var sUserAgent = navigator.userAgent;
    return sUserAgent.indexOf("MSIE") < 0 && sUserAgent.indexOf("Mozilla") >= 0;
}

/*---------------------------------------------------------------
  Tests to see if the navigator is Opera
---------------------------------------------------------------*/
function isOpera(){
    var sUserAgent = navigator.userAgent;
    return sUserAgent.indexOf("Opera") >= 0;
}

/*---------------------------------------------------------------
  It opens a new browser window
---------------------------------------------------------------*/
function MM_openBrWindow(theURL,winName,features, referer) {
  var bIsOpera = isOpera(), 
      i, w = 0, h = 0, 
      aFet = features.split(","), 
      buff = "";
  
  for(i = 0; i < aFet.length; i++){
    aFet[i] = aFet[i].trim();

    if(0 == aFet[i].indexOf("width=")){
      w = parseInt(aFet[i].split("=")[1]);
      if(bIsOpera){
        w += 10;
        buff += "width=" + w + ",";
      }
    }

    else if(0 == aFet[i].indexOf("height=")){
      h = parseInt(aFet[i].split("=")[1]);
      if(bIsOpera){
        h += 10;
        buff += "height=" + h + ",";
      }
    }

    else{
      if(bIsOpera){
        buff += aFet[i] + ",";
      }
    }
  }

  if(bIsOpera){
    features = buff.substring(0, buff.length - 1)
  }

  if(!referer || (!isIE() && !isMozilla())){
      if(w > 0 && h > 0){
        if(window.screen){
            features = "left=" + ((screen.availWidth - w)/2) + "," +
                       "top=" + ((screen.availHeight - h)/2) + "," + features
        }
      }
  }
  else{
    var t = getOffsetY(referer), //referer.offsetParent.offsetTop 
        l = getOffsetX(referer); //referer.offsetParent.offsetLeft; 

    if(isIE()){
        l += window.screenLeft;
        t += window.screenTop;
    }
    else if(isMozilla()){
        l += mozilla_getScreenX(window);
        t += mozilla_getScreenY(window) - 10;
    }
    features = "left=" + l + ",top=" + t + "," + features;
  }
  
  wnd = window.open(theURL,winName,features);
  wnd.focus();
  return wnd;
}

/*---------------------------------------------------------------
  Special function for Mozilla to get correct window.screenX
---------------------------------------------------------------*/
function mozilla_getScreenX(wnd){
    try{
        if(wnd.top == wnd){
             return wnd.screenX;
        }
        var i, w = 0, pr = wnd.parent, fr = pr.frames, ln = pr.length;
        
        //frames on vertical:
        if(fr[0].innerWidth == wnd.innerWidth && fr[0].innerHeight != wnd.innerHeight){
            w = 0;
        }
        else {
            //frames on horizontal:
            for(var i = 0; i < ln && wnd != fr[i]; i++){
                w += fr[i].innerWidth;
            }
        }
        return w + + mozilla_getScreenX(pr);
    }
    catch(e){
        alert("mozilla_getScreenX" + e.message);
    }
}

/*---------------------------------------------------------------
  Special function for Mozilla to get correct window.screenY
---------------------------------------------------------------*/
function mozilla_getScreenY(wnd){
    try{
        if(wnd.top == wnd) {
            return wnd.screenY + wnd.outerHeight - wnd.innerHeight - 10;// - wnd.outerWidth + wnd.innerWidth;
        }
        var i, w = 0, pr = wnd.parent, fr = pr.frames, ln = pr.length;
        
        //frames on horizontal:
        if(fr[0].innerWidth != wnd.innerWidth && fr[0].innerHeight == wnd.innerHeight){
            w = 0;
        }
        else {
            //frames on vertical:
            for(var i = 0; i < ln && wnd != fr[i]; i++){
                w += fr[i].innerHeight;
            }
        }
        return w + + mozilla_getScreenY(pr);
    }
    catch(e){
        alert("mozilla_getScreenY" + e.message);
    }
}

/*---------------------------------------------------------------
  Do reload a form with special corrections for Mozilla
---------------------------------------------------------------*/
function reload(frm){
    try{
        if(isIE() || isOpera()) {
            frm.submit();
        }
        else {
            //window.location.reload();
            frm.submit();
        }
    }
    catch(e){
      alert("reload - " + e.message);
    }
}

/*---------------------------------------------------------------
  Adds all offsets:
---------------------------------------------------------------*/
function getOffsetY(obj) {
  try {
    if(obj.tagName == "BODY") {
        return 0;
    }
    
    return obj.offsetParent.offsetTop + getOffsetY(obj.parentNode)
  }
  catch(e) {
    alert("getOffsetY - " + e.message);
  }
}

function getOffsetX(obj) {
  try {
    if(obj.tagName == "BODY") {
        return 0;
    }
    return obj.offsetParent.offsetLeft + getOffsetX(obj.parentNode)
  }
  catch(e) {
    alert("getOffsetX - " + e.message);
  }
}


