/**
 * Browser Sniffing
 */
 
 function its() {
	var n = navigator;
	var ua = ' ' + n.userAgent.toLowerCase();
	var pl = n.platform.toLowerCase(); 
	var an = n.appName.toLowerCase();

	this.version = n.appVersion;
    this.nn = ua.indexOf('mozilla') > 0;

	if(ua.indexOf('compatible') > 0) {
		this.nn = false;
	}

	this.opera = ua.indexOf('opera') > 0;
	this.webtv = ua.indexOf('webtv') > 0;
	this.ie = ua.indexOf('msie') > 0;
	this.aol = ua.indexOf('aol') > 0;
	
	this.major = parseInt( this.version );
	this.minor = parseFloat( this.version );
	
	this.mac = ua.indexOf('mac') > 0;
	this.mac68k = (ua.indexOf('68k') > 0 || ua.indexOf('68000') > 0);
	this.macppc = (ua.indexOf('ppc') > 0 || ua.indexOf('powerpc') > 0);
	this.win = ua.indexOf('win') > 0;
	this.win16 = (ua.indexOf('16') > 0 && ua.indexOf('win') > 0);
	this.win31 = this.win16;
	this.win95 = (ua.indexOf('95') > 0 && ua.indexOf('win') > 0);
	this.win98 = (ua.indexOf('98') > 0 && ua.indexOf('win') > 0);
	this.winnt = (ua.indexOf('nt') > 0 && ua.indexOf('win') > 0);
	this.os2 = ua.indexOf('os/2') > 0;
	this.unix = ua.indexOf("x11") > 0;

	// workarounds
	// - IE5/Mac reports itself as version 4.0
	if(this.ie && this.mac) {
		if(ua.indexOf("msie 5")) {
			this.major = 5;
			var actual_index = ua.indexOf("msie 5");
			var actual_major = ua.substring(actual_index + 5, actual_index + 8);
			this.minor = parseFloat(actual_major);
		}
	}

	return this;
}

var its = new its()

/**
 * Flash Detection
 */
 
 
 /**
  * IE5+ PNG Hack
  */
  if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	window.attachEvent("onload", alphaBackgrounds);
}

function alphaBackgrounds(){
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
	for (i=0; i<document.all.length; i++){
		var bg = document.all[i].currentStyle.backgroundImage;
		if (itsAllGood && bg){
			if (bg.match(/\.png/i) != null){
				var mypng = bg.substring(5,bg.length-2);
				document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
				document.all[i].style.backgroundImage = "url('images/x.gif')";
			}
		}
	}
}

/**
 * Netscape 4 workarounds
 */

function NS4_initResizeBugfix() {
	if (document.layers) {
	   window.loadWidth = window.innerWidth
	   window.loadHeight = window.innerHeight
	   window.onresize = NS4_restore
	}
}

function NS4_restore() {
   if (window.loadWidth != window.innerWidth || window.loadHeight != window.innerHeight) {
      history.go(0) //reload page
   }
}

function NS4_setBodyMargins(marginLeft, marginTop) {
	if (document.tags) {
		document.tags.BODY.margins(marginLeft, marginTop)
	}
}

function NS4_removeBodyMargins() {
	if (document.layers)  {
		var margins = { 'mac':[-8, -8],
					    'win':[-10, -16],
					    'nix':[-20, -22] }
		var platform = navigator.platform.toLowerCase()
		var isMac = platform.indexOf('mac') != -1
		var isWin = platform.indexOf('win') != -1

		if (isMac) {
			NS4_setBodyMargins(margins['mac'][0], margins['mac'][1])
		} else if (isWin) {
			NS4_setBodyMargins(margins['win'][0], margins['mac'][1])
		} else {
			NS4_setBodyMargins(margins['nix'][0], margins['nix'][1])
		}
	}
	return true
}

NS4_removeBodyMargins()

/**
 * xbDOM functions
 */

function nav4FindLayer(doc, id)
{
  var i;
  var subdoc;
  var obj;
  
  for (i = 0; i < doc.layers.length; ++i)
  {
    if (doc.layers[i].id && id == doc.layers[i].id)
      return doc.layers[i];
      
    subdoc = doc.layers[i].document;
    obj    = nav4FindLayer(subdoc, id);
    if (obj != null)
      return obj;
  }
  return null;
}

if (document.layers)
{
  xbGetElementById = function (id, windowRef)
  {
    if (!windowRef)
      windowRef = window;

    return nav4FindLayer(windowRef.document, id);
  };

}
else if (document.all)
{
  xbGetElementById = function (id, windowRef) { if (!windowRef) windowRef = window; var elm = windowRef.document.all[id]; if (!elm) elm = null; return elm; };
}
else if (document.getElementById)
{
  xbGetElementById = function (id, windowRef) { if (!windowRef) windowRef = window; return windowRef.document.getElementById(id); };
}
else 
{
  xbGetElementById = function (id, windowRef) { return null; }
}

function xbGetElementsByTagName(elt, tag) {
	if ( (!elt) || (typeof tag != 'string') ) {
		return null
	}
	tag = tag.toUpperCase()
	if (elt.getElementsByTagName) {
		return elt.getElementsByTagName(tag)
	}
	else if (elt.tags) {
		return elt.tags(tags)
	}
	else {
		return null
	}
}

function setVisibility (elt, vis) {
	(elt.style) ? elt.style.visibility = vis : elt.visibility = vis
}

function setBackgroundColor(elt, clr) {
	(elt.style) ? elt.style.background = clr : elt.bgcolor = clr
}



function setColor(elt, clr) {
	(elt.style) ? elt.style.color = clr : elt.document.fgColor = clr;
}

function setBackgroundImg(elt, img) {
	
	(elt.style) ? elt.style.backgroundImage = "url(" + img + ")": elt.background.src = img;

}
/*
*function setBackgroundImg(elt, img) {
*	(elt.style) ? elt.style.background-image = img : elt.background = img;
*}
*/
/**
 * Rollover functions
 */

function preloadImages() {
	if (document.images) {
		for (var imgName in dynImages) {
			cachedImages[imgName] = new Array()
			for (var i = 0; i < dynImages[imgName].length; i++) {
				cachedImages[imgName][i] = new Image
				cachedImages[imgName][i].src = imagePath + dynImages[imgName][i]
			}
		}	
	}
	return true
}

function roll_over(imgName) {
	var img, rollSrc, blankSrc
	if (document.images && imagesCached) {
		img = document.images[imgName]
		rollSrc = cachedImages[imgName][1].src
		img.src = rollSrc;
	}

	return true
}

function roll_out(imgName) {
	var img, defaultSrc
	if (document.images && imagesCached) {
		img = document.images[imgName]
		defaultSrc = cachedImages[imgName][0].src
		img.src = defaultSrc
	}
	fixPngImages();
	return true
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

/**
 * Popop Windows
 */

/* window data */

var popupData = {
	'AutoCalcWin': {w:645, h:600},
	'MtgCalcWin': {w:645, h:700},
	'PqlCalcWin': {w:645, h:660},
	'CsmrCalcWin': {w:645, h:550},
	'LoanCalcWin': {w:645, h:860},
	'SvngCalcWin': {w:645, h:660},
	'RtrCalcWin': {w:645, h:700},
	'smisWin': {w:640, h:410},
	'verisignWin': {w:660, h:410},
	'memberAlertWin': {w:320, h:292},
	'flashMovieWin': {w:340, h:300},
	'NxtStpWin': {w:600, h:480}
}

function popUp(target, href) {
	var winData = popupData[target]
	if (!winData) {
		return true  // in case of missing target, return true to activate link
	}
	var x = 2; var y = 2
	var feat = ['scroll', 'resize', 'status']
	var retval = (href) ? false : true
	var nuWin = openWin(href, target, winData.w, winData.h, x, y, feat)
	return retval;
}



function openWin(url, name, width, height, top, left, features) {
	var nuwin, options = ''
	var key = {'dir':'directories', 'loc':'location', 
	           'menu':'menubar', 'resize':'resizable', 
	           'scroll':'scrollbars', 'status':'status', 
	           'tool':'toolbar'};
	options += (width) ? ('width=' + width + ',') : ''
	options += (height) ? ('height=' + height + ',') : ''
	options += (top) ? ('top=' + top + ',') : ''
	options += (left) ? ('left=' + left + ',') : ''
	
	if (features) {
		for (var i = 0; i < features.length; i++) {
			if ( key[features[i]] ) {	    // first, try to look up shorthand
				options += key[features[i]]
			}
			else {                          // otherwise, just add property
				options += features[i]           
			}
			options += '=yes,'
		}
	}
	
	for (prop in key) {
		var found = false
		if (features) {
			for (var i = 0; i < features.length; i++) {
				if ( (key[features[i]] == key[prop]) || (features[i] == key[prop]) ) {  // found in the features array
					found = true
				}
			}
		}
		if (!found) {
			options += key[prop] + '=no,'
		}
	}
	options = options.substring(0, options.length-1)
	nuwin = window.open(url, name, options)
	return nuwin
}


/*******************
  Browser Detection
********************/

// Browser Detect Lite  v2.1.4
// http://www.dithered.com/javascript/browser_detect/index.html
// modified by Chris Nott (chris@NOSPAMdithered.com - remove NOSPAM)


function BrowserDetectLite() {
   var ua = navigator.userAgent.toLowerCase();

   // browser name
   this.isGecko     = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isMozilla   = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isNS        = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );
   this.isIE        = ( (ua.indexOf('msie') != -1) && (ua.indexOf('opera') == -1) && (ua.indexOf('webtv') == -1) );
   this.isSafari    = (ua.indexOf('safari') != - 1);
   this.isOpera     = (ua.indexOf('opera') != -1);
   this.isKonqueror = (ua.indexOf('konqueror') != -1 && !this.isSafari);
   this.isIcab      = (ua.indexOf('icab') != -1);
   this.isAol       = (ua.indexOf('aol') != -1);

   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);

   // browser version
   this.versionMinor = parseFloat(navigator.appVersion);

   // correct version number
   if (this.isNS && this.isGecko) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('/') + 1 ) );
   }
   else if (this.isOpera) {
      if (ua.indexOf('opera/') != -1) {
         this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera/') + 6 ) );
      }
      else {
         this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera ') + 6 ) );
      }
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isIcab) {
      if (ua.indexOf('icab/') != -1) {
         this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab/') + 6 ) );
      }
      else {
         this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab ') + 6 ) );
      }
   }

   this.versionMajor = parseInt(this.versionMinor);
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );

   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);

   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin   = (ua.indexOf('win') != -1);
   this.isWin32 = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac   = (ua.indexOf('mac') != -1);
   this.isUnix  = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux = (ua.indexOf('linux') != -1);

   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);

   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);

   this.isIE4xMac = (this.isIE4x && this.isMac);
}
var browser = new BrowserDetectLite();

// browser detection variables
// uses dithered.com browserdetect_lite script
// http://www.dithered.com/javascript/browser_detect/index.html
var isIE55Up = (browser.isWin && browser.isIE && browser.versionMinor >= 5.5);
var isIE55Down = (browser.isWin && browser.isIE && browser.versionMinor < 5.5);
var spacerGif = '../images/gui/spacer.gif';

// recurse through linked and imported stylesheets
function fixPngBackgrounds() {
	if (isIE55Up) {
		walkStyleRules(applyAlphaLoader);
	}
	else if (isIE55Down) {
		walkStyleRules(substituteGif);
	}
}

function applyAlphaLoader(rule) {
	var sBgImage = rule.style.backgroundImage; // url(/path/to/image)
   // alert(sBgImage);
	if (sBgImage) {
		var imgSrc = sBgImage.substring(4,rule.style.backgroundImage.length-1);
	//	alert(imgSrc);
		if ( imgSrc.match(/\.png$/i) ) {
			rule.style.backgroundImage = 'none';
			imgSrc = imgSrc.substring(3,imgSrc.length);
			rule.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + imgSrc + "')";
			//alert(imgSrc);
		
		}
	}
}

function substituteGif(rule) {
	var sBgImage = rule.style.backgroundImage; // url(/path/to/image)
	if (sBgImage) {
		var imgSrc = sBgImage.substring(4,rule.style.backgroundImage.length-1);
		if ( imgSrc.match(/\.png$/i) ) {
			rule.style.backgroundImage = 'url(' + imgSrc.replace(/(.+)\.png$/i, "$1.gif") + ')';
		}
	}
}

// modified version of youngpup.net's "sleight" code snippet
function fixPngImages() {
	if (browser.isIE && browser.isWin && window.attachEvent) {
		window.attachEvent("onload", fnLoadPngs);
	}
}

// call onload
function fnLoadPngs() {
	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (isIE55Up && img.src.match(/\.png$/i) != null) {
			var src = img.src;
			img.style.width = img.width + "px";
			img.style.height = img.height + "px";
			img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
			img.src = spacerGif;
		}
	}
}

// recurse through linked and imported stylesheets, applying fn to each rule
function walkStyleRules(fn) {
	var sCol = (walkStyleRules.arguments[1]) ? walkStyleRules.arguments[1] : document.styleSheets;
	for (var i = 0; i < sCol.length; i++) {
		for (var j = 0; j < sCol[i].rules.length; j++) {
   fn(sCol[i].rules[j]);
		}
		if (sCol[i].imports.length > 0) {
			walkStyleRules(fn, sCol[i].imports);
		}
	}
}

fixPngBackgrounds();
fixPngImages();

if (navigator.platform == "Win32" && navigator.appName == "Microsoft Internet Explorer" && window.attachEvent) {
	document.writeln('<style type="text/css">img, input.image { visibility:hidden; } </style>');
	window.attachEvent("onload", fnLoadPngs);
}

function fnLoadPngs() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);

	for (var i = document.images.length - 1, img = null; (img = document.images[i]); i--) {
		if (itsAllGood && img.src.match(/\.png$/i) != null) {
			fnFixPng(img);
			img.attachEvent("onpropertychange", fnPropertyChanged);
		}
		img.style.visibility = "visible";
	}

	var nl = document.getElementsByTagName("INPUT");
	for (var i = nl.length - 1, e = null; (e = nl[i]); i--) {
		if (e.className && e.className.match(/\bimage\b/i) != null) {
			if (e.src.match(/\.png$/i) != null) {
				fnFixPng(e);
				e.attachEvent("onpropertychange", fnPropertyChanged);
			}
			e.style.visibility = "visible";
		}
	}
}

function fnPropertyChanged() {
	if (window.event.propertyName == "src") {
		var el = window.event.srcElement;
		if (!el.src.match(/x\.gif$/i)) {
			el.filters.item(0).src = el.src;
			el.src = "/images/x.gif";
		}
	}
}

function dbg(o) {
	var s = "";
	var i = 0;
	for (var p in o) {
		s += p + ": " + o[p] + "\n";
		if (++i % 10 == 0) {
			alert(s);
			s = "";
		}
	}
	alert(s);
}

function fnFixPng(img) {
	var src = img.src;
	img.style.width = img.width + "px";
	img.style.height = img.height + "px";
	img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')"
	img.src = "/images/x.gif";
}
