////////////////////////////////////////////////////////////////////////////////
// Special objects that allow outside routines to tap into the tabs code
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
// The following section is a duplicate of code in ssf_support.js
// The code included here should be removed from here post 6.0
// This code was placed here to make the 6.0 language translations work with the 6.0 patch kits

var isNSN = (navigator.appName == "Netscape");
var isNSN6 = ((navigator.userAgent.indexOf("Netscape6") > -1));
var isMacIE = ((navigator.userAgent.indexOf("IE ") > -1) && (navigator.userAgent.indexOf("Mac") > -1));

// general functions

function getAnchorTop(anchorName) {
    var top = 0;
    if (isNSN6) {
        var obj = document.anchors[anchorName]
        while (1) {
            if (!obj || obj.offsetTop == 0) {break}
            top += obj.offsetTop
            obj = obj.offsetParent
        }
    } else if (isNSN) {
        top = document.anchors[anchorName].y
    } else {
        var obj = document.all[anchorName]
        while (1) {
            if (!obj || obj.offsetTop == 0) {break}
            top += obj.offsetTop
            obj = obj.offsetParent
        }
    }
    return parseInt(top);
}

function getAnchorLeft(anchorName) {
    var left = 0;
    if (isNSN6) {
        var obj = document.anchors[anchorName]
        while (1) {
            if (!obj || obj.offsetLeft == 0) {break}
            left += obj.offsetLeft
            obj = obj.offsetParent
        }
    } else if (isNSN) {
        left = document.anchors[anchorName].x
    } else {
        var obj = document.all[anchorName]
        while (1) {
            if (!obj || obj.offsetLeft == 0) {break}
            left += obj.offsetLeft
            obj = obj.offsetParent
        }
    }
    return parseInt(left);
}

function getImageTop(imageName) {
    var top = 0;
    if (isNSN6) {
        var obj = document.images[imageName]
        while (1) {
            if (!obj || obj.offsetTop == 0) {break}
            top += obj.offsetTop
            obj = obj.offsetParent
        }
    } else if (isNSN) {
        top = document.images[imageName].y
    } else {
        var obj = document.all[imageName]
        while (1) {
            if (!obj || obj.offsetTop == 0) {break}
            top += obj.offsetTop
            obj = obj.offsetParent
        }
    }
    return parseInt(top);
}

function getImageLeft(imageName) {
    var left = 0;
    if (isNSN6) {
        var obj = document.images[imageName]
        while (1) {
            if (!obj || obj.offsetLeft == 0) {break}
            left += obj.offsetLeft
            obj = obj.offsetParent
        }
    } else if (isNSN) {
        left = document.images[imageName].x
    } else {
        var obj = document.all[imageName]
        while (1) {
            if (!obj || obj.offsetLeft == 0) {break}
            left += obj.offsetLeft
            obj = obj.offsetParent
        }
    }
    return parseInt(left);
}

function getWindowWidth() {
    if (isNSN) {
        return window.innerWidth
    } else {
        return document.body.clientWidth
    }
}

function getWindowHeight() {
    if (isNSN) {
        return window.innerHeight
    } else {
        return document.body.clientHeight
    }
}

// End of duplicated code. 
// The above section should be removed post 6.0
////////////////////////////////////////////////////////////////////////////////

//Define the "list" function for older browsers which know no better
function list() {}

var onLoadList = new list;

//Routine to create a new "onLoadObj" object
//onLoadObj objects are set up whenever you want to call something at onLoad time.
function createOnLoadObj(name, initName) {
    if (onLoadList[name] == null) {
        onLoadList[name] = new onLoadObj(name);
        onLoadList[name].setInitRoutine(initName);
    }
}
function onLoadObj(name) {
    this.name = name;
    this.initRoutine = null;
    this.setInitRoutine = m_setInitRoutine;
}
function m_setInitRoutine(initRoutine) {
    this.initRoutine = initRoutine;
}

// activate_menulayer tests this flag to make sure the page is
// loaded before the pulldown menus are clicked.
var layerFlag = 0;
function setLayerFlag() {
    layerFlag = 1;
}

createOnLoadObj('layerFlag', setLayerFlag);

var spannedAreasList = new list;
//Routine to create a new "spannedArea" object
//spannedAreaObj objects are set up whenever you need some form elements to be 
//   blanked when showing the menus
function createSpannedAreaObj(name) {
    if (spannedAreasList[name] == null) {
        spannedAreasList[name] = new spannedAreaObj(name);
    }
    return spannedAreasList[name];
}
function spannedAreaObj(name) {
    this.name = name;
    this.showArgumentString = '';
    this.hideArgumentString = '';
    this.showRoutine = null;
    this.show = m_showSpannedArea;
    this.setShowRoutine = m_setShowRoutine;
    this.hideRoutine = null;
    this.hide = m_hideSpannedArea;
    this.setHideRoutine = m_setHideRoutine;
}
function m_setShowRoutine(showRoutine) {
    this.showRoutine = showRoutine;
    //See if there are any other arguments passed in
    //  These will get passed on to the show routine
    for (var i = 1; i < m_setShowRoutine.arguments.length; i++) {
        if (this.showArgumentString != '') {this.showArgumentString += ',';}
        this.showArgumentString += '"'+m_setShowRoutine.arguments[i]+'"';
    }
}
function m_setHideRoutine(hideRoutine) {
    this.hideRoutine = hideRoutine;
    //See if there are any other arguments passed in
    //  These will get passed on to the show routine
    for (var i = 1; i < m_setHideRoutine.arguments.length; i++) {
        if (this.hideArgumentString != '') {this.hideArgumentString += ',';}
        this.hideArgumentString += '"'+m_setHideRoutine.arguments[i]+'"';
    }
}
function m_showSpannedArea() {
    eval(this.showRoutine+'('+this.showArgumentString+');')
}
function m_hideSpannedArea() {
    eval(this.hideRoutine+'('+this.hideArgumentString+');')
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////


//browser detection
if (_version != '1.2') {
    window.onerror = new Function("return true;");
}
var isNSN = (navigator.appName == "Netscape");
var isNSN6 = (navigator.userAgent.indexOf("Netscape6") > -1);
var isMacIE = ((navigator.userAgent.indexOf("IE ") > -1) && (navigator.userAgent.indexOf("Mac") > -1));
//var isDynamic = ( ((document.layers && document.layers['test_menulayer']) || (document.all && document.all['test_menulayer'])) && !isMacIE );
var isDynamic = ( isNSN6 || (((document.layers && document.layers['test_menulayer']) || (document.all && document.all['test_menulayer']) )) );
var active_menulayer = '';
var lastActive_menulayer = '';
var form = 0;

if (isNSN6) {
    var layerRef = "document";
} else if (isNSN) {
    var layerRef = "document";
} else {
    var layerRef = "document.all";
}

if (isNSN6) {
    var styleRef = ".style";
} else if (isNSN) {
   var styleRef = "";
} else {
   var styleRef = ".style";
}

//  NOTE!  For this thing to work the anchor "barBottom" must be a *body-level* tag
//  or the menulayers will not render at the correct position   (or until Microsoft
//  provides another way of doing this...)
//  anchorId 
function activate_menulayer(menulayerId, type, anchorId) {  
    var maxWidth = 0;
    var left = 0;        
    var top = 0;

//   if (!layerFlag) {return};

    if (active_menulayer != menulayerId) {      
        if (lastActive_menulayer == menulayerId) {
            lastActive_menulayer = '';
            return;
        }  
        if (active_menulayer != '') hideMenu(active_menulayer);
        active_menulayer = menulayerId;   
        if (anchorId == null) { 
            anchorId = menulayerId + "_a";
        } 
        if (isNSN) {  
            var top = getAnchorTop(anchorId);
            var left =  getAnchorLeft(anchorId);
            maxWidth = window.innerWidth;    
            if (isNSN6) {    
                //left =  document.anchors[anchorId].offsetLeft + 5;
                //if (left + 330 > maxWidth) {left = maxWidth-330}
                if (left + 250 > maxWidth) {left = maxWidth-250}

                if (left < 0) {left = 0}
                //document.getElementById(menulayerId).style.top = document.anchors[anchorId].offsetTop + 23; 
                document.getElementById(menulayerId).style.top = top; 
                document.getElementById(menulayerId).style.left = left;
                if (form) { document.getElementById(menulayerId).style.width = 250; }  
            } else {
                left =  document.anchors[anchorId].x - 2;
                if (left + 330 > maxWidth) {left = maxWidth-330}
                //if (left + 150 > maxWidth) {left = maxWidth-150}
                if (left < 0) {left = 0}
                if (form) {
                    document[menulayerId].top = document.anchors[anchorId].y + 65;
                    document[menulayerId].left = document.anchors[anchorId].x + 60;
                } else {                
                    document[menulayerId].top = document.anchors[anchorId].y + 15;
                    document[menulayerId].left = left;
                }
            }
        } else {
            //anchorId = menulayerId + "_a";
            var isIE4 = (navigator.userAgent.indexOf("IE 4") > -1) 
            maxWidth = document.body.scrollWidth;
            if (anchorId == null) {
                left =  document.all[menulayerId+"_bottom"].offsetParent.offsetLeft + 7
            } else {
                left =  document.all[anchorId].offsetParent.offsetLeft
            }
            if (left <= 0) {left = 2}  
            if (isIE4) {
		        if (type == "context") {
			        document.all[menulayerId].style.pixelTop = document.all["barBottom"].offsetTop + 15; 
                    document.all[menulayerId].style.width = 150;
		        } else if (type == "tab")  {	
			        document.all[menulayerId].style.pixelTop = document.all["barBottom"].offsetTop + 40; 
		            document.all[menulayerId].style.width = 250;
                } else if (type == "toolbar") {                
                    document.all[menulayerId].style.width = 150;
                    document.all[menulayerId].style.pixelTop = document.all["barBottom"].offsetTop + 65;
                }
	        } else {                        
                if (form) {
                    document.all[menulayerId].style.pixelTop = getAnchorTop(anchorId);
                } else {
                    document.all[menulayerId].style.pixelTop = document.all[menulayerId+"_bottom"].offsetParent.offsetParent.offsetTop + 23;
                    //document.all[menulayerId].style.pixelTop = document.all[anchorId].offsetParent.offsetTop + 23;
    
                }
            }
            //if (left + 320 > maxWidth) {left = maxWidth-320}
            if (left + 250 > maxWidth) {left = maxWidth-250}
            document.all[menulayerId].style.pixelLeft = left;
        }
        showMenu(menulayerId);
    }
}

function activate_textlayer(menulayerId, type, anchorId) {
    form = 1;
    active_menulayer = '';
    activate_menulayer(menulayerId, type, anchorId);
    form = 0;
}

function showMenu(layerId) {
    if (isNSN6) {    
        eval(layerRef + '.getElementById("' + layerId + '")' + styleRef + '.visibility = "visible"'); 
    } else {
        eval(layerRef + '["' + layerId + '"]' + styleRef + '.visibility = "visible"');
    //Hide the various form elements used by the product while the menu is up
        toggleSpannedAreas('attrformspan','hidden')
        toggleSpannedAreas('attrform2span','hidden')
        toggleSpannedAreas('viewentry','hidden')
    //Hide any form elements that may be visible
        for (var name in spannedAreasList) {
            spannedAreasList[name].hide()
        }
    }
    if (form) document.onmouseup = null;
}

function hideMenu(layerId) {
    if (isNSN6) {
        eval(layerRef + '.getElementById("' + layerId + '")' + styleRef + '.visibility = "hidden"'); 
    } else {
        eval(layerRef + '["' + layerId + '"]' + styleRef + '.visibility = "hidden"');
    //Make the other form elements visible again
        toggleSpannedAreas('attrformspan','visible')
        toggleSpannedAreas('attrform2span','visible')
        toggleSpannedAreas('viewentry','visible')
    //Show any form elements that should be returned to the visible state
        for (var name in spannedAreasList) {
            spannedAreasList[name].show()
        }
    }
    // if we're in a form, don't do this
        if (!form) {
            document.onmouseup = clearActive_menulayer;
        }
}

function toggleSpannedAreas(spanName,newValue) {
    if (isNSN6) {
        if (document.getElementById(spanName) != null) {
            document.getElementById(spanName).style.visibility = newValue;
        }    
    } else {  
        if (self.document.layers && document.layers[spanName] != null) {
            self.document.layers[spanName].visibility = newValue;
        } else if (self.document.all && document.all[spanName] != null) {
            self.document.all[spanName].style.visibility = newValue;
        }
    }
}

// Clears (hides) the active menulayer (if any)
function clearActive_menulayer() {
    lastActive_menulayer = active_menulayer;
    if (active_menulayer != '') {
        menulayerId = active_menulayer;
        hideMenu(menulayerId);
        active_menulayer = '';
    }
    form = 0;     
    if (self.clearActiveMenu) {self.clearActiveMenu()}
}

//Routine called at "onLoad" time
function init() {
    if (isDynamic || isNSN6) {
        if (isNSN) {
	        document.captureEvents(Event.MOUSEUP);
        }
// if we're in a form, don't do this
        if (!form) {
            document.onmouseup = clearActive_menulayer;
        }
    }
    //Now, call any other routines that want to be called at onLoad time
    for (var name in onLoadList) {
        onLoadList[name].initRoutine();
    }
}

// Netscape Navigator requires a little push	
function reloadNSN() {	top.location.reload(); }	
if ((isNSN6) || (isDynamic && isNSN)) { onresize = reloadNSN; }

// this func is primarily for WFS, which is called in all contexts so this
// function is here rather than 3 or 4 other template files. 
function launchNew(url) {
    var win = self.window.open(url, 'new', 'width=600,height=650,resizable,scrollbars');
    if (self.window.focus && win) {win.focus()}
}


// this function takes a URL (typically from self.window.location.href) and
// tries to turn off portal mode by taking out the .portal piece of the URL
// note: it's argument must be a string
function turnOffPortalMode(url) {
  var loc = url.indexOf('.portal');
  var pre = url.substring(0,loc);
  var len = url.length;
  var post = url.substring(loc+7,len);
  return pre+post;
}


