// CHANGES:
//   7-9-2007:
//     *The SelectInitialValues function was added.
//   8-3-2007
//     *Added the Flash detection functions.
//   8-31-2007:
//     *Removed the proofing actions from rotation, and added the collages.net ad.
//   11-03-2007:
//     *Updated the ad pages on the page.
//   2008-03-13:
//     *Updated the OpenExternalLink() function to display windows correctly if the width and height are not specified.
//   2008-05-10:
//     *Updated the SetCookie function so that if no domain is specified to the function a domain will be set to ".ronnichols.com" (depending on the server).
//     *Removed the LucisArt add from the page.
//   2008-08-08:
//     *Updated the UseFlash() function to look at the flash version if the flash settins are not set.
//   2009-03-27:
//     *Removed the back up and burn add from being displayed.



if ( navigator.appName == 'Microsoft Internet Explorer' ) {
	document.write('<link href="/style-ie.css" type="text/css" rel="stylesheet">');
}

// Include the vbscript Flash test.
document.write(
'<script type="text/vbscript">\n'
	+ 'Function VBGetSwfVer(i)\n'
		+ 'on error resume next\n'
		+ 'Dim swControl, swVersion\n'
		+ 'swVersion = 0\n'
		
		+ 'set swControl = CreateObject("ShockwaveFlash.ShockwaveFlash." + CStr(i))\n'
		+ 'if (IsObject(swControl)) then\n'
			+ 'swVersion = swControl.GetVariable("$version")\n'
		+ 'end if\n'
		+ 'VBGetSwfVer = swVersion\n'
	+ 'End Function\n'
+ '</script>' );




function ExternalDocumentWrite( l_text ) {
	document.write( l_text );
}



function UseFlash() {
	// If cookies are disabled show the flash version because most people have flash or can install it.
	// Even if someone tries to use '?useflash=N' the setting will not stick so use flash.
	if ( !CookiesEnabled() ) {
		return true;
	}

	// Get a location bar key that is used to set flash
	useFlashGET = GetLocationBarData( 'useflash' );
	if ( useFlashGET  ) {
		// The user is trying to change whether they use flash or not.
		if ( useFlashGET == 'Y' ) {
			return true;
		}
		else {
			return false;
		}
	}

	// Get the cookie that tells me whether to use flash or not
	useFlashCookie = GetStoredFlashUse();
	
	// If the cookie is not set because the user didn't visit the home page recently then
	// use flash because most people have flash
	if ( !useFlashCookie ) {
		if ( 6 <= GetFlashVersion() ) {
			return true;
		} else {
			return false;
		}
	}

	// The user has the flash cookie set and we can check its value.
	if ( useFlashCookie == 'Y' ) {
		return true;
	}
	else {
		return false;
	}

}



// WriteActualFlashCode()
// Write the flash or html code based on whether flash is enabled.
//
function WriteLargeHeaderCode( UserName ) {
	var useFlash = UseFlash();
	SetStoredFlashUse( useFlash );
	if ( UseFlash() ) {
		WriteFlashCode( UserName );
	}
	else {
		WriteNonFlashCode();
	}
}



// WriteSmallHeaderCode( UserName )
// Write the small version of the flash or html code for pages that do not have the large version.
//
function WriteSmallHeaderCode( UserName ) {
	var useFlash = UseFlash();
	SetStoredFlashUse( useFlash );
	if ( UseFlash() ) {
		WriteSmallFlashCode( UserName );
	}
	else {
		WriteSmallNonFlashCode();
	}
}



//	WriteSwitchFlashCode()
//
//	Description: This funtion writes the code on a page that will allow the user to switch to the 
//		flash or non-flash version of the website.  It must refresh the page with new GET data
//		in the url.
//
function WriteSwitchFlashCode() {
	// people with cookies disabled don't get to switch.
	if ( !CookiesEnabled() ) {
		return;
	}
	var locationToGo = document.location.href;
	var useFlashCookie = GetStoredFlashUse();
	var useFlashGET = GetLocationBarData( 'useflash' );
	var useFlashText = "";
	if ( useFlashGET != false ) {
		// The location currently has a useFlash setting
		var useFlashStart = locationToGo.indexOf( "useflash=" );
		var useFlashEnd = useFlashStart + 10;
		var useFlashString = "";
		
		// Set whether flash will be used
		if ( useFlashGET == 'Y' ) {
			useFlashString = 'N';
			useFlashText = "Flash Free";
		}
		else {
			useFlashString = 'Y';
			useFlashText = "Flash";
		}

		// Set the location of the link.
		locationToGo = locationToGo.substr( 0, useFlashStart ) + locationToGo.substr( useFlashEnd );
		if ( locationToGo.indexOf( '?' ) == (locationToGo.length - 1) ) {
			locationToGo = locationToGo + "useflash=" + useFlashString;
		}
		else {
			locationToGo = locationToGo + "&useflash=" + useFlashString;
		}
		
		// Set the Text of the link.

	}
	else {
		var useFlashString = "";
		if ( ( useFlashCookie == false ) || ( useFlashCookie == 'Y' ) ) {
			// Flash is currently running so let users switch to NonFlash version.
			useFlashString = "N";
			useFlashText = "Flash Free";
		}
		else {
			// The NonFlash version is currently running so let users switch to the Flash version.
			useFlashString = "Y";
			useFlashText = "Flash";
		}

		// Set the location of the link.
		if ( locationToGo.indexOf('?') != -1) {
			locationToGo = locationToGo + "&useflash=" + useFlashString;
		}
		else {
			locationToGo = "?useflash=" + useFlashString;
		}
	}

	document.write("<br><a href=\"" + locationToGo + "\" class=\"stdLink small\">Switch To " + useFlashText + " Version</a>");
}



// SetStoredFlashUse( useFlash )
// Sets whether to use flash or not in a cookie to be stored.
//
// Arguments:
//		useFlash: true if flash should be used
//
function SetStoredFlashUse( useFlash ) {
	if ( useFlash ) {
		useFlashString = 'Y';
	}
	else {
		useFlashString = 'N';
	}
	SetCookie('useflash', useFlashString, 60 * 60 * 24 * 1000, '/', '', '');
}



// GetStoredFlashUse()
// Gets the stored value for using flash from a cookie.
//
// Return Values: Returns 'Y' for use flash, 'N' for don't use flash, and false for cookie not set or cookies disabled.
//
function GetStoredFlashUse() {
	if ( !CookiesEnabled() ) {
		return false;
	}

	useFlashCookie = GetCookie('useflash');
	if ( useFlashCookie == null ) {
		return false;
	}
	else if ( useFlashCookie == 'Y' ) {
		return 'Y';
	}
	else {
		return 'N';
	}
}



// WriteFlashCode()
// Writes the code neccessary to use flash on the page.
//
function WriteFlashCode( UserName ) {
	var protocol = document.location.protocol;
	var UserNameParam = '';
	var UserNameEmbed = ''
	if ( ( UserName == '' ) || ( UserName == null ) ) {
		UserName = '';
	}
	else {
		UserNameParam = "<param name=\"FlashVars\" value=\"UserName=" + UserName + "\">"
		UserNameEmbed = " FlashVars=\"UserName=" + UserName + "\""
	}
	
	var flashCode = "<object id=\"flashHead\" classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"" + protocol + "//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"781\" height=\"349\">";
	flashCode = flashCode + "<param name=\"movie\" value=\"/7848.swf\">";
	flashCode = flashCode + "<param name=\"quality\" value=\"high\">";
	flashCode = flashCode + "<param name=\"menu\" value=\"false\">";
	flashCode = flashCode + UserNameParam;
	flashCode = flashCode + "<embed src=\"/7848.swf\" id=\"flashHead\" name=\"flashHead\"" + UserNameEmbed + " quality=\"high\" pluginspage=\"" + protocol + "//www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"781\"  height=\"349\"></embed>";
	flashCode = flashCode + "</object>";

	document.write(flashCode);
}



// WriteNonFlashCode()
// Writes the non flash code on the page.
//
function WriteNonFlashCode() {
	var nonFlashCode = "<div style=\"background-image:url('/images/noflash.jpg');border:0px none; padding: 0; margin:0;\"><img width=782 height=349 border=0 src=\"/images/spacer.gif\" style=\"border:0px none; padding: 0; margin: 0;\" usemap=\"#largeflashMap\"></div>";

	document.write(nonFlashCode);
}



// WriteSmallFlashCode()
// Writes the code neccessary to use flash on the page.
//
function WriteSmallFlashCode( UserName ) {
	var protocol = document.location.protocol;
	var UserNameParam = '';
	var UserNameEmbed = ''
	if ( ( UserName == '' ) || ( UserName == null ) ) {
		UserName = '';
	}
	else {
		UserNameParam = "<param name=\"FlashVars\" value=\"UserName=" + UserName + "\">"
		UserNameEmbed = " FlashVars=\"UserName=" + UserName + "\""
	}

	var flashCode = "<object classid=\"clsid:D27CDB6E-AE6D-11cf-96B8-444553540000\" codebase=\"" + protocol + "//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0\" width=\"781\" height=\"162\">";
	flashCode = flashCode + "<param name=\"movie\" value=\"/flashheadsmall.swf\">";
	flashCode = flashCode + "<param name=\"quality\" value=\"high\">";
	flashCode = flashCode + "<param name=\"menu\" value=\"false\">";
	flashCode = flashCode + UserNameParam;
	flashCode = flashCode + "<embed src=\"/flashheadsmall.swf\" id=\"flashHead\" name=\"flashHead\"" + UserNameEmbed + " quality=\"high\" pluginspage=\"" + protocol + "//www.macromedia.com/go/getflashplayer\" type=\"application/x-shockwave-flash\" width=\"781\" height=\"162\"></embed>";
	flashCode = flashCode + "</object>";

	document.write(flashCode);
}



// WriteSmallNonFlashCode()
// Writes the non flash code on the page.
//
function WriteSmallNonFlashCode() {
	var nonFlashCode = "<div style=\"background-image:url(/images/smallflashhead.gif);\"><img width=782px height=162px border=0 src=\"/images/spacer.gif\" style=\"border:0px none;\" usemap=\"#smallflashMap\"></div>";
	
	document.write(nonFlashCode);
}



// JavaScript helper required to detect Flash Player PlugIn version information
function JSGetSwfVer(i){
	// NS/Opera version >= 3 check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
			var swVer2 = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
	  		var flashDescription = navigator.plugins["Shockwave Flash" + swVer2].description;
			descArray = flashDescription.split(" ");
			tempArrayMajor = descArray[2].split(".");
			versionMajor = tempArrayMajor[0];
			versionMinor = tempArrayMajor[1];
			if ( descArray[3] != "" ) {
				tempArrayMinor = descArray[3].split("r");
			} else {
				tempArrayMinor = descArray[4].split("r");
			}
	  		versionRevision = tempArrayMinor[1] > 0 ? tempArrayMinor[1] : 0;
			flashVer = versionMajor + "." + versionMinor + "." + versionRevision;
	  	} else {
			flashVer = -1;
		}
	}
	// MSN/WebTV 2.6 supports Flash 4
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) {
		flashVer = 4;
	}
	// WebTV 2.5 supports Flash 3
	else if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) {
		flashVer = 3;
	}
	// older WebTV supports Flash 2
	else if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) {
		flashVer = 2;
	}
	// Can't detect in all other cases
	else {
		flashVer = -1;
	}
	return flashVer;
}



// DESCRIPTION: Attempts to get the flash version that the user has.
//
// RETURNS: Returns a floating point value which should be the version of the Flash Player or 0.0.
//
// ex: Flash Player 7r14 returns 7.14
//
function GetFlashVersion() {
	// Detect Client Browser type
	var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
	var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
	var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

   	// loop backwards through the versions until we find the newest version	
	for ( i = 25; i > 0; i-- ) {
		var versionStr = -1;
		if (isIE && isWin && !isOpera) {
			versionStr = VBGetSwfVer(i);
		} else {
			versionStr = JSGetSwfVer(i);		
		}
		if (versionStr == -1 ) { 
			return 0.0;
		} else if (versionStr != 0) {
			if(isIE && isWin && !isOpera) {
				tempArray         = versionStr.split(" ");
				tempString        = tempArray[1];
				versionArray      = tempString.split(",");				
			} else {
				versionArray      = versionStr.split(".");
			}
			versionMajor      = versionArray[0];
			versionMinor      = versionArray[1];
			versionRevision   = versionArray[2];
			
			versionString     = versionMajor + "." + versionRevision;   // 7.0r24 == 7.24
			versionNum        = parseFloat(versionString);

			return versionNum;
		}
	}	
	return 0.0;
}



// CookiesEnabled()
// Determines if cookies are enabled
//
// Return Value:  Returns true if cookies are enabled.
//
function CookiesEnabled() {
	return true;
	SetCookie ('Test', 'T', 60, '/', '', '');
	return ( GetCookie('Test') == 'T' );
}



// SetCookie( name, value, expires, path, domain, secure )
// Creates a cookie and setes its value.
//
// Arguments:
//		name:  Name of the cookie.
//		value:  Value of the cookie.
//		expires:  Number of seconds till the cookie will expire.
//		path:  The folder name that the cookie is valid in (normally '/')
//		domain:  The site domain the cookie is valid on (normally '').  If not specified this will be set to ".ronnichols.com" (or whatever the case may be).
//		secure:  sets if the cookie is secure (normally '')
//
function SetCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	if ( expires ) {
		expires = expires * 1000;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	if ( '' == domain ) domain = document.location.host;
	if ( 4 <= domain.length && 'www.' == domain.substr( 0, 4 ) ) {
		domain = domain.substr( 4 );
	}
	domain = "." + domain;
	
	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}



// GetCookie( name )
// Gets the value of a cookie
//
// Arguments:
//		name:  Name of the cookie to get.
//
// Return Value:  Value of the cookie.
//
function GetCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}

	if ( start == -1 ) {
		return null;
	}

	var end = document.cookie.indexOf( ";", len );
	
	if ( end == -1 ) {
		end = document.cookie.length;
	}

	return unescape( document.cookie.substring( len, end ) );
}



// GetLocationBarData( name )
// Gets data sent to this page via the GET method.
//
// Arguments:
//		key:  The key of the value to get.
//
// Return Value:  Returns the value associated with the key or false if the key did not exist.
//
function GetLocationBarData( key ) {
	if (document.location.search.length <= 1) {
		return false;
	}

	var recordStart = document.location.search.indexOf( key + "=");	
	if ( recordStart == -1 ) {
		return false;
	}
	recordStart = recordStart + key.length + 1;

	var recordEnd = document.location.search.indexOf( '&', recordStart);
	if ( recordEnd = -1 ) {
		recordEnd = document.location.search.length;
	}

	var recordLen = recordEnd - recordStart;
	var value = document.location.search.substr(recordStart, recordLen);
	return value;
}



var sAdLocations = new Array(
	"adcollages_net.html",
	"adhelenyancy.html",
	"adimagenomic.html",
	"adlifeshwy.html",
	"admidwestpho.html",
	"admidwestpho2.html",
	"adproselect.html",
	"adwacomtablets.html"
);

var sIndexesUsed = "";
var iIndexes = new Array(0,1,2,3,4,5,6,7);

function GetRandomIndex() {
	var randNum = iIndexes[Math.floor(Math.random() * iIndexes.length)];
	while (sIndexesUsed.indexOf(" " + randNum.toString()) > -1)
	{
		randNum = iIndexes[Math.floor(Math.random() * iIndexes.length)];
	}
	sIndexesUsed += " " + randNum.toString();
	return randNum;
}

function GetAdLocation() {
	var iIndex = GetRandomIndex();
	return sAdLocations[iIndex];
}

function GetWriteText(frameWidth) {
	var sText = "<iframe width=\"" + frameWidth + "\" height=\"154\" src=\"/ads/";
	sText += GetAdLocation();
	sText += "\" frameborder=\"no\" scrolling=\"no\" align=\"left\" style=\"border:1px solid black; background-color: #ffffff;\"></iframe>";
	return sText;
}




function ShowPrintablePage( windowWidth, windowHeight ) {
	var currentDir = document.location.protocol + '//' + document.location.host;
	currentDir = currentDir + document.location.pathname.substr(0, document.location.pathname.lastIndexOf('/') + 1);
	
	var printablePageHTML = "<html><head>" + headDoc.innerHTML + "<base href=\"" + currentDir + "\"></head><body><img src=\"/images/web-logo.gif\"><br><br><table width=695><tr><td>" + pageBody.innerHTML + "</td></tr></table></body></html>";
	var printablePage = window.open( '_blank','','height=' + windowHeight + ',width=' + windowWidth + ',menubar=yes,toolbar=yes,scrollbars=yes');
	
	printablePage.document.write(printablePageHTML);
}



function GetSelectedRadioIndex( radioControl ) {
	for ( i = 0; i < radioControl.length; i++ ) {
		if ( radioControl[i].checked ) {
			return i;
		}
	}
	
	return -1;
}



function SetArrowBullet(imageID, bulletStyle) {
	imageBullet = document.getElementById( imageID );
	if ( 'over' == bulletStyle ) {
		imageBullet.style.left = '-3';
	}
	else {
		imageBullet.style.left = '0';
	}
}



function OpenExternalLink( location ) {
	OpenExternalLink( location, 800, 1000 );
}



function OpenExternalLink( location, width, height ) {
	if ( width == undefined ) {
		width = 800;
	}
	if ( height == undefined ) {
		height = 1000;
	}
	
	window.open(location, null,'height=' + height + ',width=' + width + ',location=yes,menubar=yes,toolbar=yes,scrollbars=yes,resizable=yes');
}



// DESCRIPTION: Selects the initial values that have been set for selection boxes.  They must have an attribute named "initialvalue" with the value set to the desired initial value.
//
function SelectInitialValues( l_element ) {
	if ( !l_element ) {
		l_element = document;
	}
	
	var selectBoxes = l_element.getElementsByTagName( "SELECT" );
	for ( var elementIndex = 0; elementIndex < selectBoxes.length; elementIndex++ ) {
		var initialValue = selectBoxes[ elementIndex ].getAttribute( "initialvalue" );
		if ( initialValue ) {
			selectBoxes[ elementIndex ].value = initialValue;
		}
	}
}