// -------------------------------------------------------------
// cross-browser helper functions
// -------------------------------------------------------------

// Global variables
var isCSS 			= false;
var isW3C 			= false;
var isIE4 			= false;
var isNN4 			= false;
var isIE6 			= false;
var isGecko 		= false;
var isOpera 		= false;
var isDHTML 		= false;
var suppressMenus	= false;
var legacyMode		= false;
var timerID			= null;
var subtimerID		= null;
var lastPopup		= null;

// initialize upon load to let all browsers establish content objects
function autoconfig()
{
	if(document && document.images)
	{
		isCSS		= (document.body && document.body.style) ? true : false;
		isW3C		= (isCSS && document.getElementById) ? true : false;
		isIE4		= (isCSS && document.all && readIEVer() >= 4.0) ? true : false;
		isNN4		= (document.layers) ? true : false;
		isGecko		= (isCSS && navigator && navigator.product && navigator.product == "Gecko");
		isOpera		= (isCSS && navigator.userAgent.indexOf( "Opera") != -1 );
		isIE6CSS	= (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
		isIE6		= ( isIE6CSS && readIEVer() >= 6.0 );
		isDHTML		= isCSS && ( isIE4 || isGecko || isOpera );

		if( suppressMenus )
		{
			// Netscape 6.2 puts the menus in the wrong place...
			// Safari, the menus don't go away... problem in ResetMenu
			isDHTML	= false;
		}
		else if( isOpera && readOperaVer() < 7 )
		{
			// Opera 6.x doesn't seem to like the DHTML...
			isDHTML	= false;
		}
		else if( isGecko && navigator.productSub <= 20011022 )
		{
			// Netscape 6.2 puts the menus in the wrong place...
			isDHTML	= false;
		}
		else if( isGecko && navigator.productSub == 20030107 )
		{
			// Safari, the menus don't go away... problem in ResetMenu
			isDHTML	= false;
		}
	}
}

function readIEVer()
{
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "MSIE" );
	if( offset < 0 )
	{
		return 0;
	}
	return parseFloat( agent.substring( offset + 5, agent.indexOf( ";", offset ) ) );
}

function readOperaVer()
{
	var agent	= navigator.userAgent;
	var offset	= agent.indexOf( "Opera" );
	if( offset < 0 )
	{
		return 0;
	}
	return parseFloat( agent.substring( offset + 6 ) );
}

// Seek nested NN4 layer from string name
function seekLayer(doc, name)
{
    var theObj;
    for (var i = 0; i < doc.layers.length; i++)
    {
        if (doc.layers[i].name == name)
		{
            theObj = doc.layers[i];
            break;
        }

        // dive into nested layers if necessary
        if (doc.layers[i].document.layers.length > 0)
		{
            theObj = seekLayer(document.layers[i].document, name);
        }
    }
    return theObj;
}

function parentNode(elem)
{
	if( elem.parentElement )
	{
		return elem.parentElement;
	}
	
	if( elem.parentNode )
	{
		return elem.parentNode;
	}

	return null;
}

function contains(lookWhere,lookFor)
{
	if( lookWhere == null || lookFor == null )
	{
		return false;
	}
	
/*	if( lookWhere.contains )
	{
		return lookWhere.contains( lookFor );
	}
	else	*/
	{
		var parent = parentNode( lookFor );
		
		while( parent )
		{
			if( parent == lookWhere )
			{
				return true;
			}
			
			parent = parentNode( parent );
		}
	}
	
	return false;
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj)
{
    var theObj;
    if (typeof obj == "string")
    {
    	if (isW3C)
			{
				theObj = document.getElementById(obj);
	
				if( !theObj )
				{
					theObj = document.getElementByName(obj);
				}
			}
			else if (isIE4)
			{
				theObj = document.all(obj);
			}
			else if (isNN4)
			{
				theObj = seekLayer(document, obj);
			}
    } 
    else
    {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

// Convert object name string or object reference
// into a valid style (or NN4 layer) reference
function getObject(obj)
{
    var theObj = getRawObject(obj);
    if (theObj && isCSS)
    {
        theObj = theObj.style;
    }
    return theObj;
}

function getObjectsByTag(tag)
{
	if( document.getElementsByTagName )
	{
		return document.getElementsByTagName(tag);
	}
	else if( document.all )
	{
		return document.all.tags(tag);
	}

	return null;
}

// get the element an event refers to
function eventToElement( evt )
{
	var elem = null;
	
	if( evt.target )
	{
		elem = evt.target;
	}
	else if( evt.toElement )
	{
		elem = evt.toElement;
	}
	
	if( elem && elem.nodeName == "#text" )
	{
		elem = elem.parentNode;
	}
	
	return elem;
}

// Set the visibility of an object to visible
function show(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.visibility = "visible";
    }
}

// Set the visibility of an object to hidden
function hide(obj)
{
    var theObj = getObject(obj);
    if (theObj)
    {
        theObj.visibility = "hidden";
    }
}

// Set the visibility of an object to hidden
function isVisible(obj)
{
    var theObj = getObject(obj);
    return (theObj) ? ( theObj.visibility == "visible" ) : false;
}

// Position an object at a specific pixel coordinate
function shiftTo(obj, x, y) 
{
    var theObj = getObject(obj);
    if (theObj) 
    {
        if (isCSS) 
        {
            // equalize incorrect numeric value type
            var units = (typeof theObj.left == "string") ? "px" : 0 
            theObj.left = x + units;
            theObj.top = y + units;
        }
        else if (isNN4) 
        {
            theObj.moveTo(x,y)
        }
    }
}

// Retrieve the x coordinate of a positionable object
function getObjectLeft(obj) 
{
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView)
    {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("left");
    }
    else if (elem.currentStyle)
    {
        result = elem.currentStyle.left;
    }
    else if (elem.style)
    {
        result = elem.style.left;
    }
    else if (isNN4)
    {
        result = elem.left;
    }
    return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function getObjectTop(obj) 
{
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView)
    {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("top");
    }
    else if (elem.currentStyle)
    {
        result = elem.currentStyle.top;
    }
    else if (elem.style)
    {
        result = elem.style.top;
    }
    else if (isNN4)
    {
        result = elem.top;
    }
    return parseInt(result);
}

// Retrieve the rendered width of an element
function getObjectWidth(obj) 
{
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetWidth)
    {
        result = elem.offsetWidth;
    }
    else if (elem.clip && elem.clip.width)
    {
        result = elem.clip.width;
    }
    else if (elem.style && elem.style.pixelWidth)
    {
        result = elem.style.pixelWidth;
    }
    return parseInt(result);
}

// Retrieve the rendered height of an element
function getObjectHeight(obj) 
{
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetHeight)
    {
        result = elem.offsetHeight;
    }
    else if (elem.clip && elem.clip.height)
    {
        result = elem.clip.height;
    }
    else if (elem.style && elem.style.pixelHeight)
    {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}

// Return the available content width space in browser window
function getInsideWindowWidth() 
{
    if (window.innerWidth)
    {
        return window.innerWidth;
    }
    else if ( isIE6CSS )
    {
        // measure the html element's clientWidth
        return document.body.parentElement.clientWidth
    }
    else if (document.body && document.body.clientWidth)
    {
        return document.body.clientWidth;
    }
    return 0;
}

// Return the available content height space in browser window
function getInsideWindowHeight() 
{
    if( window.innerHeight )
    {
        return window.innerHeight;
    }
    else if( isIE6CSS )
    {
        // measure the html element's clientHeight
        return document.body.parentElement.clientHeight
    }
    else if (document.body && document.body.clientHeight)
    {
        return document.body.clientHeight;
    }
    return 0;
}

// Open a popup window
function winopen(url,stuff,morestuff) 
{
	var popwin = window.open(url,stuff,morestuff);
	
	// you may get "undefined" if a popup blocker did its thing...
	if( typeof(popwin) != "undefined" && popwin )
	{
		popwin.focus();
	}
	
	lastPopup = popwin;
}

// -------------------------------------------------------------
// cookie handling functions
// -------------------------------------------------------------

function getCookie(NameOfCookie) 
{
	if (document.cookie.length > 0) 
	{
		begin = document.cookie.indexOf(NameOfCookie+"="); 
		if (begin != -1) 
		{
			begin += NameOfCookie.length + 1; 
			end = document.cookie.indexOf(";", begin);
			if (end == -1) 
			{
				end = document.cookie.length;
			}
			return unescape( document.cookie.substring(begin, end)); 
		} 
	}

	return ""; 
}

function SetCookie (NameOfCookie , value) 
{	
	document.cookie	= NameOfCookie + "=" + escape( value );
}

function AutoSubmit ( frm )
{
	var		cookieVal	= getCookie ( "autosubmit" );
	if ( cookieVal == document.location )
	{
		var 	submitForm	= window.confirm ( "Resubmit form?");
		if ( submitForm	)
		{
			frm.submit();
			return;
		}
		else
		{
			return;
		}	
		
	}
	SetCookie( "autosubmit" , document.location );
	frm.submit();
}


// -------------------------------------------------------------
// Flash handling
// -------------------------------------------------------------

var activeX = false;
var swf = false;

function writeFlash(swfFile,altFile,width,height,ver,params) {
var s = '';
bgcolor= '#FFFFFF';
  if(hasFlash(ver)) {
    if(!activeX) {
    s = '<embed src="'+swfFile+'" quality="high" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="'+width+'" height="'+height+'" bgcolor="'+bgcolor+'" wmode="opaque"></embed>\n';
    } else {
    var st = 0;

    s = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=4,0,2,0" width="'+width+'" height="'+height+'">\n';
    s += '\t<param name="movie" value="'+swfFile+'" />\n';
    s += '\t<param name="quality" value="high" />\n';
    s += '\t<param name="menu" value="false" />\n';
    s += '\t<param name="wmode" value="opaque" />\n';
    s += '\t<param name="bgcolor" value="'+bgcolor+'" />\n';

    if (params && params.length>0) {
        while (true) {
            var e = (params.indexOf('&',st)>0)?params.indexOf('&',st):params.length-1;
            var t = params.substr(st,params.indexOf('=',st));
            var v = params.substr(params.indexOf('=',st)+1,(e-(params.indexOf('=',st)+1)));
            s += '\t<param name="' + t + '" value="' + v + '" />\n';
            if (e >= params.length-1) break;
            params = params.substr(e+1);
        }
    }

    s += '</object>\n\n';
    }
  } else {
    if(altFile.search(/(.gif)|(.jpg)$/gi) != -1) {
    s = '<img src="'+altFile+'" width="'+width+'" height="'+height+'" border="0" alt="" />\n';
    } else {
    window.location.href = altFile;
    return;
    }
  }
  with(document) {
  open('text/html');
  write(s);
  close();
  }
}

function hasFlash(ver) {
if(!window.ua) ua = window;
  if(!ua.ver) {
  var av = navigator.appVersion;
  ua.mac = (av.indexOf("Mac") != -1);
  ua.ie = (av.indexOf("MSIE") != -1);
  }
if(!ver) ver = 0;
  if(!ua.mac && ua.ie) {
    for(var i=ver; i<=5&&i!=1&&swf!=true; i++) {
    execScript('on error resume next: swf=IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash'+((i==0)?'':'.'+i)+'"))','VBScript');
    }
  activeX = swf;
  }
  else if(navigator && navigator.plugins) {
  var n,m,t,d,v;
  n = navigator;
  m = n.mimeTypes;
  t = 'application/x-shockwave-flash';
    if(m && m[t] && m[t].enabledPlugin && m[t].enabledPlugin.description) {
    d = m[t].enabledPlugin.description;
    v = d.charAt(d.indexOf('.')-1);
      if(v >= ver) swf=true;
    }
  }
return swf;
}

// Netscape 4 resizing helper
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
	if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
		document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

// Post back page scrolling handling

var bodyTag;
function saveScrollTop(){
	var top;
	
	if( typeof(bodyTag) == "undefined" && !bodyTag){
		bodyTag = getObjectsByTag( "body" )[0];
	}
	
	if( isIE6 ){
		top = document.body.parentNode.scrollTop;
	}
	else if( isIE4 ){
		top = document.body.scrollTop;
	}
	else{
		top = window.scrollY;
	}
	
	document.forms[0].elements['ScrollTop'].value = top;
}

function loadScrollTop(top){
	if( isIE6 ){
		onscroll = saveScrollTop;
		document.body.parentNode.scrollTop = top;
	}
	else if( isIE4 ){
		onscroll = saveScrollTop;
		document.body.scrollTop = top;
	}
	else{
		window.scrollY = top;
	}
}

// -------------------------------------------------------------
// end of common.js
// -------------------------------------------------------------
