// Common library for PL
/* $Id: PortalLibrary.Common.js,v 1.23 2005/03/30 16:53:12 asd Exp $ */   
var intervalShowInputs = null;
var plMenusShown = 0;

var bName = navigator.appName;
var bVer = parseInt(navigator.appVersion);
var NS4 = (bName == "Netscape" && bVer >= 4);
var IE4 = (bName == "Microsoft Internet Explorer" && bVer >= 4);
var isNS = (bName == "Netscape" );

	if (isNS && !document.getElementById){ // mozilla does not have this
		document.classes.desc.all.fontsize='8pt';
	}
var urlInChange = false;

function plCenterWindow(w,h) {
var l = Math.round((window.screen.width - w) / 2);
var t = Math.round((window.screen.height - h) / 2) - 30;
window.moveTo(l,t);
}
function plResizeWindowToFit(id, mW, mH, xW, xH) {
      this.w=0;
      this.h=0;
      var e = document.getElementById(id);
      var de = document.documentElement;
      window.resizeTo(mW,mH);
      var w = e.offsetWidth;
      var dw = de.offsetWidth;
      w = w>dw?w:dw;
      
      var h = e.offsetHeight;
      var dh = de.offsetHeight;
      h=h>dh?h:dh;
      
      if ( xW > w) {
         if (xH > h) {
            window.resizeTo(w+30,h+38);
         }
      }
      this.w = w;
      this.h = h;
      return this;
}

function chgc()
{
	var loc_c='/admin/chgc.jsp';
	var w=window.open(loc_c,"select_company", "top=30,left=30,scrollbars=yes,width=650,height=470,resizable=yes");
	w.focus();
	return false;
}

function chgr()
{
	var loc_c='/admin/chgr.jsp';
	var w=window.open(loc_c,	 "select_company_role", "top=30,left=30,scrollbars=yes,width=450,height=250,resizable=yes");
	w.focus();
	return false;
}

function doHelp()
{
var loc_help='/help/index.jsp?PAGE_URL='+thisPage+'&amp;ISO_LANG=en';
var hw=window.open(loc_help,"it4help", "top=30,left=30,scrollbars=yes,width=450,height=250,resizable=yes");
hw.focus();
return false;
}

function encodeForURL(s) {
            return(escape(s).replace(/\//g, '%2F').replace(/\@/g, '%40').replace(/\+/g, '%2B'));
}
                    
function ReplaceCode(Location,cVarName,cNewValue)
{
		if  (Location.indexOf('?')<0)
				Location= Location+"?";
		var beg=Location.indexOf(cVarName);
		if (beg>0)
			{
			var end=Location.indexOf('&',beg);
			if (Location.charAt(beg-1)=='&') beg=beg-1;
			if (end>0)
			Location=Location.substring(0,beg)	+Location.substring(end);
			else
			Location=Location.substring(0,beg)
			}
		Location= Location+"&"+cVarName+"="+cNewValue;
	return Location
}

function rwd(s) {
    // 
}

function plIsExists(o) {
    if (typeof o == 'undefined') return false;
    if (typeof o == 'unknown') return false;
    if (o==null) return false;
    return true;
}

function plStringStartsWith(str1, str2) {
    if (typeof str2 == 'undefined' || str2 == null) return false;
    if (typeof str1 == 'undefined' || str1 == null) return false;
    if (str1.length == 0) return false;
    if (str1.length < str2.length) return false;
    if (str2.length == 0) return true;
    if (str1.substring(0,str2.length)==str2) return true;
    return false;
}

function closeOwnerRefresh() {
    if ( window.opener ) window.opener.window.location.reload(true);
    window.close();
}

function windowResize(w,h,s) {
    window.resizeTo(10,10);
    for ( var i = 0; i < s; i++ ) {
        window.resizeTo((w*i)/s, (h*i)/s);
    }
}

function beModal() {
    window.focus();
}

// 

function findUpperNode(nis, src) {
    //alert(src.tagName + ':' + src.id);
    if (src.id && src.id.substring(0,nis.length) == nis) {
        return src;
    } else {
        if (src.parentNode)
            return findUpperNode(nis, src.parentNode);
        else
            return null;
    }
    return null;
}
function findLowerNode(nis, src) {
    if (src.id && src.id.substring(0,nis.length) == nis) {
        return src;
    } else {
        var cn = src.childNodes;
        if (cn) {
            for (var i = 0; i < cn.length; i++) {
                var q = findLowerNode(nis, cn.item(i));
                if (q != null) {
                    return q;
                }
            }
        }
    }
    return null;
}
function findNextNode(nis, src) {
    if (src.id && src.id.substring(0,nis.length) == nis) {
        return src;
    } else {
        if (src.nextSibling)
            return findNextNode(nis, src.nextSibling);
        else
            return null;
    }
}
function findPreviousNode(nis, src) {
    if (src.id && src.id.substring(0,nis.length) == nis) {
        return src;
    } else {
        if (src.previousSibling)
            return findPreviousNode(nis, src.previuosSibling);
        else
            return null;
    }
}
// Select/Deselect all 
function selectElements(idstart, tagname, flag) {
    var elems = document.getElementsByTagName(tagname);
    for ( var i = 0; i<elems.length; i++ ) {
        var e=elems.item(i);
        if ( e.id.substring(0,idstart.length)==idstart ) {
            if ( flag ) {
                e.checked=true; e.selected=true;
            } else {
                e.checked=false; e.selected=false;
            }
        }
    }
}

// Layout

function Point(x,y) {
    this.x=x;this.y=y;
}
Point.prototype.add=function(ox,oy) {
    this.x+=ox;this.oy+=oy;
}
function Rect(el) {
    this.left=pl_getLeft(el);this.top=pl_getTop(el);this.width=pl_getWidth(el);this.height=pl_getHeight(el);
    rwd(el.tagName+'.'+el.id+': ' + this);
}
Rect.prototype.shift = function(ox,oy) {
    this.left+=ox;this.top+=oy;
}
Rect.prototype.moveTo = function(ow,oh) {
    this.left=ow;this.top=oh;
}
Rect.prototype.resize = function(ow,oh) {
    this.width=ow; this.height=oh;
}
Rect.prototype.toString=function() {
    return this.left+' :'+ this.top + '/'+ this.width+':'+this.height;
}
Rect.prototype.isInside=function(ox,oy) {
    return oy>this.top && this.top+this.height>oy && ox>this.left && this.left+this.width>ox?true:false;
}
function pl_getLeft(el) {
    if (el == document || el == document.body || el == document.getDocumentElement || (!el)) return 0;
    else return el.offsetLeft + pl_getLeft(el.offsetParent);
}
function pl_getTop(el) {
    if (el == document || el == document.body || el == document.getDocumentElement || (!el)) return 0;
    else return el.offsetTop + pl_getTop(el.offsetParent);
}
function pl_getWidth(el) {
    return el.offsetWidth;
}
function pl_getHeight(el) {
    return el.offsetHeight;
}


function plHideInputs() {
    var sl = document.getElementsByTagName('select');
    for (var i = 0 ; i < sl.length; i++) {
        sl.item(i).style.visibility = 'hidden';
    }
    sl = document.getElementsByTagName('iframe');
    for (var i = 0 ; i < sl.length; i++) {
        sl.item(i).style.visibility = 'hidden';
    }
}
function plShowInputs() {
    var sl = document.getElementsByTagName('select');
    for (var i = 0 ; i < sl.length; i++) {
        sl.item(i).style.visibility = 'inherit';
    }
    sl = document.getElementsByTagName('textarea');
    for (var i = 0 ; i < sl.length; i++) {
        sl.item(i).style.visibility = 'inherit';
    }
    sl = document.getElementsByTagName('iframe');
    for (var i = 0 ; i < sl.length; i++) {
        sl.item(i).style.visibility = 'inherit';
    }
}
function plSwitchInputs(dv) {
    if (dv) {
        window.clearTimeout(intervalShowInputs);
        plHideInputs();
    } else {
        window.clearTimeout(intervalShowInputs);
        intervalShowInputs = window.setTimeout('plShowInputs()', 300);
    }
}
// change class
function plChangeClassByTagName(tagName, newClass, oldClass) {
    var elts = document.getElementsByTagName(tagName);
    if (elts)
        for (var i = 0; i < elts.length; i++) {
            var e=elts.item(i);
            if (typeof e.className != 'undefined') if (e.className == oldClass || oldClass==null) e.className = newClass;
        }
}
function plSwitchClassById(id, newClass, oldClass) {
    var elt = document.getElementById(id); 
    if (elt)
        if (typeof elt.className != 'undefined')
            if (elt.className == oldClass ) {
                elt.className = newClass;
            } else {
                elt.className = oldClass;
            }
}
function plChangeClassById(id, newClass, oldClass) {
    var elt = document.getElementById(id); 
    if (elt)
        if (typeof elt.className != 'undefined')
            if (elt.className == oldClass || oldClass == null) {
                elt.className = newClass;
            }
}
function plChangeClassByIdPrefix(idPrefix, newClass, oldClass) {
    var elts = document.getElementsByTagName('*');
    var si = elts.length;
    if ( plIsExists(elts) )
        for ( var i = 0; i < si; i++ ) {
            if ( plIsExists(elts.item(i)) )
                if ( plIsExists(elts.item(i).id) )
                    if ( plStringStartsWith(elts.item(i).id, idPrefix) )
                        plChangeClassById(elts.item(i).id, newClass, oldClass);
        }
}

function plChangeContentsById(id, cnts) {
    var el = document.getElementById(id);
    if (plIsExists(el)) {
        if (el.innerHTML) {
            el.innerHTML = cnts;
        }
    }
}

function plSwitchContentsById(id, cnts1, cnts2) {
    var el = document.getElementById(id);
    if (plIsExists(el)) {
        if (el.innerHTML) {
            if (el.innerHTML == cnts1) {
                el.innerHTML = cnts2;
            } else {
                el.innerHTML = cnts1;
            }

        }
    }
}
// Transfers 
var _plTransfers = new Array();
function _plTransferStart(ds, idf, dsrc, handler) {
	var q = _plTransfers.length;
	_plTransfers[q] = new Object();
	_plTransfers[q].caller = ds;
	_plTransfers[q].handler = handler;
	_plTransfers[q].timeout = window.setTimeout('_plTransferCannotReceiveData('+q+', \''+ idf.id +'\')', 10000);
	idf.src= dsrc + (dsrc.indexOf('?')>0?('&_plTransferID='+q):('?_plTransferID='+q));
}
function _plTransferCannotReceiveData(n,wi,msg) {
	if (msg == null || typeof(msg) == 'undefined' || typeof(msg) == 'unknown') 
		alert('Cannot complete data transfer.\nPlease check your Internet connection.');
	else 
		alert(msg);
	window.clearTimeout(_plTransfers[n]);
}
function _plTransferEnd(n, rd) {
   var ct = parent._plTransfers[n];
   if (ct!=null) {
      parent.window.clearTimeout(ct.timeout);
      ct.handler(ct.caller, rd);
   }
}
function plQueryCheckboxes(prefix, checked) {
   var a = new Array();
   var swl = prefix.length;
   var q = document.getElementsByTagName('input');
   for (var i = 0; i < q.length; i++) {
      if (q.item(i).id.substring(0,swl) == prefix) {
         var el = q.item(i);
         if (checked == null) a[a.length] = el;
         else { if (checked == el.checked) a[a.length] = el; }
      }
   }
   return(a);
}

function plQueryElements(tagName, prefix) {
   return(plQueryElementsEx(tagName, prefix, null, null));
}

function plQueryElementsEx(tagName, prefix, an, av) {
   var a = new Array();
   var swl = prefix.length;
   var q = document.getElementsByTagName(tagName);
   for (var i = 0; i < q.length; i++) {
      if (q.item(i).id.substring(0,swl) == prefix) {
         var el = q.item(i);
         if (an != null) {
            var qe = el.getAttribute(an);
            if (av == null || av == qe) {
               a[a.length] = el;
            }
         } else {
            a[a.length] = el;
         }
      }
   }
   return(a);
}

