//************************************************************************ global variables
var util_isMozilla=util_isMozillaBrowser();
var util_isMicrosoft=util_isMicrosoftBrowser();

var util_rootHttp="/";
var util_rootFile="cosmoSite/";
var util_relRoot=
  util_getRelativeRoot((document.location.protocol=="file:")?util_rootFile:util_rootHttp);

//***************************************************************** util_getBrowserCompany()
function util_getBrowserCompany()
{
var appName=navigator.appName;
var spaceLoc=appName.indexOf(" "); if (spaceLoc==-1) spaceLoc=appName.length;
return appName.substring(0, spaceLoc);
}

//***************************************************************** util_getBrowserVersion()
function util_getBrowserVersion()
{
var company=util_getBrowserCompany();
if (company=="Netscape")  //appVersion: "5.0 (Windows; en-US)"
  return parseFloat(navigator.appVersion);
else if (company=="Microsoft") //appVersion: "4.0 (compatible; MSIE 6.0; Windows NT 5.1)"
  return navigator.appVersion.substring(22,25);
else if (company=="Konqueror") //appVersion: "5.0 (compatible; Konqueror/3; Linux)"
  return navigator.appVersion.substring(27,30);
else if (company=="Opera") //appVersion: "9.00 (Windows NT 5.1; U; en)"
  return navigator.appVersion.substring(0,3);
else
  return 0;
}

//**************************************************************** util_getBrowserRevision()
function util_getBrowserRevision()
{
var userAgent=navigator.userAgent;
//like: "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.2.1) Gecko/20021130"
//or  : "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.6) Gecko/20040206 Firefox/0.8"
if (!userAgent) return 0;
var matches=new RegExp("rv:([0-9]+\.?[0-9]*)").exec(userAgent);
if ((matches==null) || (matches.length<2)) return 0;
return parseFloat(matches[1]); //0th element is the entire string
}

//****************************************************************** util_isMozillaBrowser()
function util_isMozillaBrowser()
{
var company=util_getBrowserCompany();
var version=util_getBrowserVersion();
var revision=util_getBrowserRevision();
return ((company=="Netscape")&&(version>=5.0)&&(revision>=1.2)) ||
       ((company=="Konqueror")&&(version>=3.3));
}

//**************************************************************** util_isMicrosoftBrowser()
function util_isMicrosoftBrowser()
{
var company=util_getBrowserCompany();
var version=util_getBrowserVersion();
var revision=util_getBrowserRevision();
return ((company=="Microsoft")&&(version>=5.0));
}

//****************************************************************** util_extractParameter()
function util_extractParameter(paramName, href)
{
if (!(paramName && href)) return null;
var reStr="[?&]+"+paramName+"=([^&$]+)[&$]?";
var matches=new RegExp(reStr, "i").exec(href);
if ((matches==null) || (matches.length<2)) return null;
return matches[1]; //0th element is the entire string
}

//*****************************************************************************************
// a utility function, mimicking the the dirname cmdline program
//************************************************************************** util_dirname()
function util_dirname(filePath)
{
var result=filePath.replace(/\\/g, '/');
if (result.charAt(result.length-1)=='\/') result=result.substr(0, result.length-1);
var pos=result.lastIndexOf('/');
if (pos!=-1) result=result.substring(0, pos);
return result;
}

//******************************************************************************************
// gives the relative "up-dir" part that "returns" a url to the site root
//******************************************************************* util_getRelativeRoot()
function util_getRelativeRoot(root)
{
var path=document.location.pathname.replace(/\\/g, '/');
if (path.charAt(path.length-1)=='\/') path+="[DIR-INDEX]";
var pos=path.indexOf(root);
if (pos!=-1) path=path.substring(pos+root.length);
var parts=path.split(/[\/\\]/);
path="";
for (var i=0; i<parts.length-1; i++) if (parts[i].length) path+="../"; 
return path;
}

//*****************************************************************************************
// gets e.g "/scripts" and returns "file:///d:/projects/html/cosmoModel/scripts", if the 
// currentprotocol is "file:". Else returns argument unchanged
//********************************************************************* util_absoluteFile()
function util_absoluteFile(ref)
{
if (document.location.protocol!="file:") return ref;
if (ref.charAt(0)!='\/') return ref;   // not-absolute path; can't handle
var path=document.location.href.replace(/\\/g, '/');
var pos=path.indexOf(util_rootFile);
return path.substr(0, pos+util_rootFile.length-1)+ref;
}

//****************************************************************** util_makeRelativeLink()
function util_makeRelativeLink(ref)
{
if (ref.indexOf(':')!=-1) return ref;  // contains protocol; don't mess with it
if (ref.charAt(0)=='\/') return ref;   // absolute path; dont't mess it up
if (ref.charAt(0)=='.') return ref;    // explicitly relative path; dont't change it
if (ref.indexOf('/')==-1) return ref;  // special case: no dir included; same as above
return util_relRoot+ref;              
}

//*****************************************************************************************
// tries to locate the "nearest" parent containing elem, of a requested tag-type  
//************************************************************************* util_getParent()
function util_getParent(elem, parentTagName)
{
var parent=elem; parentTagName=parentTagName.toUpperCase();
while (parent.tagName.toUpperCase()!=parentTagName)
  {
  if (parent==document.documentElement) return null;
  parent=parent.parentNode;
  }
return parent;
}

//*****************************************************************************************
// returns the nearest element after elem with the requested tagName
//*************************************************************************** util_getNext()
function util_getNext(elem, nextTagName)
{
var nxt=elem.nextSibling; nextTagName=nextTagName.toUpperCase();
while (nxt)
  {
  if (nxt.nodeType==1)  // 1=ELEMENT_NODE
    {
    if (nxt.tagName==elem.tagName) return null;
    if (nxt.tagName.toUpperCase()==nextTagName) break;
    }
  nxt=nxt.nextSibling;
  }
return nxt;
}

//******************************************************************************************
// returns all declared css files as a comma separated list of absolute paths
//******************************************************************** util_getStyleSheets()
function util_getStyleSheets()
{
var result="";
var css, pos;
for (var i=0; i<document.styleSheets.length; i++)
  {
  pos=-1;
  css=document.styleSheets.item(i).href;
  if (css)pos=css.lastIndexOf("/styles");
  if (pos>=0) result+=((i)?",":"")+util_absoluteFile(css.substr(pos));
  }
return result;
}

//*****************************************************************************************
// returns the current date in a human readable format (e.g. for "last update" text)
//*************************************************************************** util_getDate()
function util_getDate()
{
var months=["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"]; 
var date=new Date();
return date.getDate()+" "+months[date.getMonth()]+" "+date.getFullYear();
}

//*****************************************************************************************
// accepts a float and returns a string trying to avoid X.YZ99999999999991 displays
//**************************************************************************** util_float()
function util_float(value, maxDecimals)
{
var multiplier=Math.pow(10, maxDecimals);
var intValue=Math.round(value*multiplier);
var result=intValue/multiplier;
return (result+"").replace(/00{4,}[^0]$/,"");
}

//*****************************************************************************************
// it will try first to load a new window with a given link. If this fails (e.g. if
// browser configuration blocks it), it will change the location of the current window 
//*********************************************************************** util_openWindow()
function util_openWindow(link, title)
{
var hwin=window.open(link, title, "");   
if (!hwin) window.location=link; else hwin.focus();
}

//**************************************************************** util_importExtraScripts()
function util_importExtraScripts()
{
var scripts=[];
if (util_isMozilla)
  scripts=[ "menuText.js", "menuConfig.js", 
            "menuObjects.mozilla.js", "logoAnimation.mozilla.js" ];
else if (util_isMicrosoft)
  scripts=[ "menuText.js", "menuConfig.js", 
            "menuObjects.ie5.js", "logoAnimation.ie5.js" ];
else
  scripts=[ "menuText.js", "frame.js" ]; 
for (var i=0; i<scripts.length; i++)
  document.writeln("<script src='"+util_relRoot+"/scripts/"+scripts[i]+"'></script>");
}

//*********************************************************************** util_displayMenu()
function util_displayMenu(hrzMenu, vertMenu)
{
var horizontalMenu=menu_dfltTop, verticalMenu=menu_dfltLeft; 
if (hrzMenu||vertMenu) { horizontalMenu=hrzMenu; verticalMenu=vertMenu; }
if (util_isMozilla || util_isMicrosoft)
  new PageControl(horizontalMenu, verticalMenu).node.PageControl_showPage();
else 
  frm_handleDoc(horizontalMenu, verticalMenu);
}