<!--
// Primary Javascript file
var IE = $.browser.msie; //deprecated, but need to know for css parsing //document.all ? true : false;
var IE6 = IE && $.browser.version.substr(0,1)<=6; //navigator.userAgent.toLowerCase().indexOf("msie 6") > -1;

// backwards compatibility for getElementById
if(document.all && !document.getElementById) 
{
    document.getElementById = function(id){ return document.all[id]; }
}

// universal getElement(s) function
// todo: replace all instances of this with $. calls
function $0() 
{
	var elements = new Array();
	for (var i = 0; i < arguments.length; ++i)
	{
		var element = arguments[i];
		if (typeof element == 'string')
		{
		    element = document.getElementById(element);
		}
		if (arguments.length == 1)
		{
			return element;
		}
		elements.push(element);
	}
	return elements;
}

if(!IE) //Firefox 3 event handler fix
{
    (function()
    {
        var mouseEvt;
        if (typeof document.createEvent !== 'undefined')
        {
            mouseEvt = document.createEvent('MouseEvents');
        }
        if (mouseEvt && mouseEvt.__proto__ && mouseEvt.__proto__.__defineGetter__)
        {
            mouseEvt.__proto__.__defineGetter__('pageX', function()
            {
                return this.clientX + window.pageXOffset;
            });
            mouseEvt.__proto__.__defineGetter__('pageY', function()
            {
                return this.clientY + window.pageYOffset;
            });
        }
    })();
}

function isEmpty(s)
{
    return (s == null || s == '');
}

function isNumeric(s)
{
    return !isEmpty(s) && !isNaN(s);
}

var includedScripts = new Array();
function includeScript(path)
{
    if(includedScripts[path] == null)
    {
        var script = document.createElement('script');
        script.setAttribute('type', 'text/javascript');
        script.setAttribute('src', path);
        document.getElementsByTagName('head')[0].appendChild(script);
        includedScripts[path] = true;
    }
}

var includedStyles = new Array();
function includeStyle(path, title)
{
    if(includedStyles[path] == null)
    {
        var style = document.createElement('link');
        style.setAttribute('rel', 'stylesheet');
        style.setAttribute('type', 'text/css');
        style.setAttribute('href', path);
        if(title != null)
            style.setAttribute('title', title);
            
        document.getElementsByTagName('head')[0].appendChild(style);
        includedStyles[path] = true;
    }
}

var defaultButtonID = '';
var defaultButtonEnabled = true;
var lastKeyCode = -1;
var isLastKeyInput = false;
var isLastKeyTab = false;
var isLastKeyTrigger = false;
var isLastKeyShift = false;

function keyDownHandler(keyEvent)
{
    keyEvent = (keyEvent != null) ? keyEvent : event;
    lastKeyCode = keyEvent.keyCode || event.which;
    isLastKeyTrigger = lastKeyCode < 37 || lastKeyCode > 40;
    isLastKeyTab = lastKeyCode == 9;
    isLastKeyShift = keyEvent.shiftKey;
    
    // process only the "Enter" key
    if (lastKeyCode == 13
        && defaultButtonID != ''
        && defaultButtonEnabled)
    {
        // cancel the default submit (if any)
        keyEvent.returnValue    = false;
        keyEvent.cancel         = true;
        $0(defaultButtonID).click();        
    }    
    else if(keyEvent.ctrlKey && keyEvent.shiftKey)
    {
        //  Ctrl + Shift + "D" - keystroke combination for clearing cookies
        if(lastKeyCode == 68 && confirm("Are you sure you want to reset all your PrimeAccess cookies?"))
        {
            deleteAllCollapsibleCookies();
        }
        //  Ctrl + Shift + "X" - keystroke combination for clearing for current page
        else if(lastKeyCode == 88 && confirm("Are you sure you want to reset your cookie settings for this PrimeAccess page?"))
        {
            clearCookieSetting();
        }
    }
}
document.onkeydown = keyDownHandler;

function setDefaultButton(buttonID)
{
    defaultButtonID = buttonID;
}

function __onLoad()
{
}

function addOnLoadEvent(newEvent) 
{
    var oldOnLoad = window.onload;
    if (typeof(window.onload) != 'function')
    {
        window.onload = newEvent;
    } 
    else 
    {
        window.onload = function() 
        {
            oldOnLoad();
            newEvent();
        }
    }
}

function addOnClickEvent(elem, newOnClickEvent) 
{
    var oldOnClickEvent = elem.onclick;
    if (typeof(oldOnClickEvent) != 'function') 
    {
        elem.onclick = newOnClickEvent;
    } 
    else 
    {
        elem.onclick = function() 
        {
            oldOnClickEvent();
            newOnClickEvent();
        }
    }
}

// for onmouseover on/off image replacement
function imgOnOff(id)
{
    var img = $0(id);
    if(img != null)
    {
        if (img.src.indexOf('_off') != -1)  
        {
            img.src = img.src.replace('_off','_on');
        }
        else
        {
            img.src = img.src.replace('_on','_off');
        }    
    }
}

// show/hide elements by ID
function showHideConfirm(id, isVisible, doNotSaveSetting)
{	
	var elem = $0(id);
	if(elem == null)
	{
	    return null;
	}
	var img = $0('swivel_' + id); // if [+]/[-] image is associated with show/hide function
	if((elem.style.display == 'none' && isVisible == null) || isVisible==true)
	{
		elem.style.display = 'block';
		if(img != null)
		{ 
		    img.src = img.src.replace('_show', '_hide'); 
		}
		isVisible = true;
	}
	else if(!isVisible)
	{
		elem.style.display = 'none';
		if(img != null)
		{ 
		    img.src = img.src.replace('_hide', '_show'); 
		}
		isVisible = false;
	}
	
	if(doNotSaveSetting != true)
    {
        modifyCollapsibleId(id, isVisible);
    }
	return isVisible;
}

function showHide(id, isVisible, doNotSaveSetting)
{
//alert('showHide:' + id + ', ' + isVisible + ', ' + doNotSaveSetting);
    // hide individual element
    if(showHideConfirm(id, isVisible, doNotSaveSetting) == null)
    {
        var dx = 0;
        if(doNotSaveSetting != true)
        {
            var isSubElementVisible = showHideConfirm(id + '1', isVisible, true); //hardcoded subelement ids: 'id' + dx
            isVisible = isSubElementVisible != null ? isSubElementVisible : isVisible;            
            modifyCollapsibleId(id, isVisible);
        }
        // hide subelements (if any), element1, element2, etc...        
        while(showHideConfirm(id + (++dx), isVisible, true) != null);
    }
}

function insertElementsAfter(elementAfterID, elementIDList)
{
    var elems = elementIDList.split(';');
    var elementAfter = $0(elementAfterID);
    
    for(dx=0; dx<elems.length; ++dx)
    {
        var elem = $0(elems[dx]);
        if(elem != null && elementAfter != null)
        {
            elementAfter.parentNode.insertBefore($0(elems[dx]), elementAfter);
        }
    }
}

function setFocus(elemID)
{
    var elem = $0(elemID);
    if(elem != null && isVisible(elemID)) /* && typeof(elem.focus)=='function'*/ 
    {
        elem.focus();
    }
}

function isVisible(elemID)
{
    var elem = $0(elemID);
    while(elem != null)
    {
        if(elem.style && (elem.style.display == "none" || elem.style.visibility == "hidden"))
        {
            return false;
        }
        elem = elem.parentNode;
    }
    return true;
}

//var collapsiblesIDs = new Array();
function initCollapsibles()
{
    var collapsibleIDs = getCookieKeyValue(window.location.pathname, 'collapsibles');
    var anchors = document.getElementsByTagName('a');
    var pattern = new RegExp(/collapsible\[([^\:]+):([^\]]+)\]/); //collapsible[<elementId>:<defaultsToOpen>]
    for (var i = 0; i < anchors.length; i++)
    {
        var rel = anchors[i].getAttribute('rel');
        if(rel != '' && pattern.test(rel))
        {
            var match = pattern.exec(rel);
            if(match.length > 0)
            {                
                var isCollapsed = eval(match[2]); //default
                if(typeof collapsibleIDs != 'undefined' && typeof collapsibleIDs[match[1]] != 'undefined') 
                {
                    isCollapsed = collapsibleIDs[match[1]];
                }
                showHide(match[1], isCollapsed);
            }
        }
    }
}

$(document).ready(function(){initCollapsibles();});

var cookieID = '';
var noncollapsedElements = '_';
function initializeCollapsibles(cookieIDIn, noncollapsedElementsIn)
{
    cookieID = cookieIDIn;
    noncollapsedElements = getCookie(cookieID);
    
    if(noncollapsedElements == null)
    {
        // if no elements are listed yet, set the default elements as uncollapsed
        noncollapsedElements = '_' + (noncollapsedElementsIn != null ? noncollapsedElementsIn : '');
        setCookie(cookieID, noncollapsedElements);
    }
    var elems = noncollapsedElements.split(';');
    for(dx=0; dx<elems.length; ++dx)
    {
        showHide(elems[dx], true, true);
    }
}

function modifyCollapsibleId(id, addMe)
{
    var collapsibleIDs = getCookieKeyValue(window.location.pathname, 'collapsibles');
    //alert(collapsibleIDs);
    if(typeof collapsibleIDs  == 'undefined')
    {
        collapsibleIDs = new Object();
    }
    collapsibleIDs[id] = addMe;
    setCookieKeyValue(window.location.pathname, 'collapsibles', collapsibleIDs); //, function(key,value){return value}))
}

function clearCookieSetting()
{
    deleteCookie(cookieID);
}

// Application Popup functions
var currentAPUID = null;
function loadAPU(clickElem, id)
{   
//    if(currentAPUID != null)
//    {
//        closeAPU(currentAPUID);
//    } 
    
    showHide(id, true, true);
    var elem = $0(id);
    var coords = findPos(clickElem);
    elem.style.position = 'absolute';
    elem.style.left = (coords[0] - 170) + 'px'; //(elem.style.width*0.5); 
    elem.style.top = (coords[1] - 100) + 'px'; //(elem.style.height*0.5);
    currentAPUID = id;
}

function closeAPU(id)
{
    showHide(id, false, true);
//    if(currentAPUID == id)
//    {
//        currentAPUID = null;
//    }
}

function findPos(obj) 
{
	var curleft = curtop = 0;
	if(obj.style.position == 'absolute')
	{
	    curleft = obj.style.left;
	    curtop = obj.style.top;
	}
	else if (obj.offsetParent)
	{
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function getWindowHeight() 
{
    var windowHeight=0;
    if (typeof(window.innerHeight)=='number') 
    {
        windowHeight=window.innerHeight;
    }
    else 
    {
        if (document.documentElement&&
            document.documentElement.clientHeight) 
        {
        windowHeight = document.documentElement.clientHeight;
        }
        else 
        {
            if (document.body&&document.body.clientHeight) 
            {
                windowHeight=document.body.clientHeight;
            }
        }
    }
    return windowHeight;
}

function getWindowWidth() 
{
    var windowWidth=0;
    if (typeof(window.innerWidth)=='number') 
    {
        windowWidth=window.innerWidth;
    }
    else 
    {
        if (document.documentElement&&
            document.documentElement.clientWidth) 
        {
            windowWidth = document.documentElement.clientWidth;
        }
        else
        {
            if (document.body&&document.body.clientWidth) 
            {
                windowWidth=document.body.clientWidth;
            }
        }
    }
    return windowWidth;
}

function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function NewWindow(url, w, h) 
{
	try 
	{	    
	    var frameDim = '';
	    if(w == null)
	        w = 800;
	    if(h == null)
	        h = 550;
	    
	    if (w!=0 && h!=0)
	     var frameDim = ',width=' + w + ',height=' + h;
	           
		newWin = window.open(url, "_blank", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no" + frameDim);
		if (newWin != null)
			newWin.focus();
	} catch (e) {/* User probably has a popup blocker. */}
	return false;
}

// misc functions
function clearElemValue(elemID)
{
    var elem = $0(elemID);
    if(elem != null)
    {
        if(elem.type == "checkbox")
            elem.checked = false;
        else if(elem.type == "dropdownlist")
            elem.selectedIndex = 0;
        else
            elem.value = '';
        
        elem.disabled = false;
    }
}

function clearExclusiveFields(changingFieldID, clearFieldIDList)
{
    //if($0(changingFieldID).value != '')
    {
        var clearFieldIDs = clearFieldIDList.split(',');
        for(var i=0; i < clearFieldIDs.length; ++i)
        {
            clearElemValue(clearFieldIDs[i]);            
        }
    }
}

function hasSL3() { return Silverlight.isInstalled('3.0'); }

function hasSL4() { return Silverlight.isInstalled('4.0'); }

$.extend({
  requestParms: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  requestParm: function(name){
    return $.requestParms()[name];
  }
});

//-->
