
// From http://www.quirksmode.org/js/detect.html
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
BrowserDetect.init();


function changeImages( id, file )
{
	document.getElementById( id ).src = file;
}

function CheckLogoFrame( url )
{
	var CurrentURL = parent.Logo.location.href;

	if( CurrentURL.search( url ) == -1 )
	{
		parent.Logo.location.href = url;
	}
}

function expirationError( accountName )
{
	alert( 'Since the data for "' + accountName + '" is out-of-date, only limited service is available for it.  Order a SourceBook, Site Survey, or GuardianService to update our data.' );
}

function expirationWarning( accountName, expirationDate )
{
	alert( 'The data for "' + accountName + '" will be classified as out-of-date unless a SourceBook, Site Survey, or GuardianService is requested for this account by ' + expirationDate + ".  Out-of-date accounts receive limited service through the database." );
}

function isPercentage( value )
{
	// percentages are numbers between 0 and 100.
	return !( isNaN( value ) || value < 0 || value > 100 );
}

function onClickHelpImage()
{
	var help = document.getElementById( "help" );
	var helpImage = document.getElementById( "helpImage" );
	var helpImageText = document.getElementById( "helpImageText" );

	if( helpImage && help && helpImageText )
	{
		if( helpImage.src.search( "stock_hide_help.gif" ) == -1 )
		{
			helpImage.src = "/4dcgi/images/stock_hide_help.gif";
			help.style.display = "block";
			helpImageText.style.textDecoration = "line-through";
		}
		else
		{
			helpImage.src = "/4dcgi/images/stock_help.gif";
			help.style.display = "none";
			helpImageText.style.textDecoration = "none";
		}
	}
}

function hideShowDetails( elementId )
{
	var object = document.getElementById( elementId );
	var objectLink = document.getElementById( elementId+"actionLink" );
	if( object.style.display == "none" )
	{
		objectLink.innerHTML = "<font color = #a8292a>Hide Details &amp; Tips</font>";
		object.style.display = "";
	}
	else
	{
		objectLink.innerHTML = "<font color = #a8292a>Show Details &amp; Tips</font>";
		object.style.display = "none";
	}
}



function mouseX( evt )
{
	if (evt.pageX) 
		return evt.pageX;
	else if (evt.clientX)
		return evt.clientX + (document.documentElement.scrollLeft ?
								document.documentElement.scrollLeft :
								document.body.scrollLeft);
	else return null;
}

function mouseY( evt )
{
	if (evt.pageY) 
		return evt.pageY;
	else if (evt.clientY)
		return evt.clientY + (document.documentElement.scrollTop ?
								document.documentElement.scrollTop :
								document.body.scrollTop);
	else return null;
}
		

function showTip( e, item, elementId )
{
	// This is the element that we want to position and have appear
	var popUp = document.getElementById( elementId );
	
	// determine the coordinates of the element being hovered over
	var pos = elementPosition( item );

	// set a default width for the hover-over
	popUp.style.width = '200px';

	// calculate the Y coordinate for the hover-over
	// var popUpY = (mouseY(e) - popUp.offsetHeight - 10);
	var popUpY = (pos.y - popUp.offsetHeight - 5);

	if (e.clientY < (popUp.offsetHeight+item.clientHeight + 5)) // Make sure it is drawn on the screen
		popUpY = pos.y + item.clientHeight + 5;

	// calculate the X coordinate for the hover-over
	var popUpX = mouseX(e);
	var windowWidth = window.innerWidth;
	if( windowWidth == null )
		windowWidth = document.body.offsetWidth;

	if (windowWidth < (popUpX + 230)) //  Make sure it is drawn on the screen
		popUpX = popUpX - 200;

	// Set position of hover-over popup
	popUp.style.top = popUpY + 'px';
	popUp.style.left = popUpX + 'px';

	var useIFrameHack = ((BrowserDetect.browser == "Explorer") && (BrowserDetect.version < 8.0))
	if (useIFrameHack)
	{
		var iFrame = document.getElementById( "tipiFrame" );
		if (! iFrame)
		{
			// Non-dynamic attributes
			iFrame = document.createElement("IFRAME");
			iFrame.setAttribute("src", "blank.html");
			iFrame.setAttribute('id', "tipiFrame");
			iFrame.setAttribute('scrolling', "no");
			iFrame.style.position="absolute";
			iFrame.style.zIndex = '5000';
			iFrame.className = "transparentIFrameForIE";
			document.body.appendChild( iFrame );
		}
		iFrame.style.left = popUp.style.left;
		iFrame.style.top = popUp.style.top;
		iFrame.style.height = popUp.clientHeight;
		iFrame.style.width  = "200px";
		
		popUp.style.zIndex = '5001';
		iFrame.visibility = "visible";
	}

	// Set popup to visible
	popUp.style.visibility = "visible";
}

function hideTip( elementId )
{
	var popUp = document.getElementById( elementId );
	popUp.style.visibility = "hidden";
	var tipiFrame = document.getElementById( "tipiFrame");
	if (tipiFrame)
	{
		tipiFrame.visibility = "hidden";
		tipiFrame.style.width = "0px";
		tipiFrame.style.height = "0px";
		tipiFrame.style.left = 0;
		tipiFrame.style.top = 0;
		
	}
}

/*
 * elementPosition will determine the page coordinates of the element obj
 *
 * return: an object containing the x and y coordinates of obj
 */
function elementPosition( obj ) 
{
	var curleft = 0, curtop = 0;

	if (obj.offsetParent) 
	{
		curleft = obj.offsetLeft;
		curtop = obj.offsetTop;
		while (obj = obj.offsetParent) 
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
	}

	return { x: curleft, y: curtop };
}

