// File: functions.js
//
// 2002.03.26 Copyright (C) Mark Rovendro
// 2003.03.07 Copyright (C) Mark Rovendro
// 2003.04.10 MJR - Made changes for Netscape 4
// 2003.04.18 MJR - Added "id" tag to bannerAdd() output - Needed for NS7 and W3C standard going forward.
// 2003.09.15 MJR - Modified bannerRotate() to use a standard url for the MA site.

// Global Vars
var ns4 = (navigator.appName == "Netscape") && (parseInt(navigator.appVersion) < 5 ) ;


//// BROWSER/OBJECT FUNCTIONS /////////////////////////////////////////////////////////////////////

// Simply:
//  Nav4/IE4-5 document.elementName
//	NS4:	document.<objecttype>.<name>
//	IE4/5:	document.all[]
//	IE6+:	document.getElementById()
//	NS7+:	document.getElementById() (and I think for NS6)
// NOTE: This requires the use of the "id" and "name" tag.
function findElement( id ) {
	// NS4 has to be directly accessed - if this exists then just return it.
	var ns4id = "document." + id ;
	if( eval(ns4id) ) return eval(ns4id) ;
	// This should take care of all other versions including the W3C DOM Level1 standard.
	if( !document.all && !document.getElementById ) return "" ;
	return document.all ? document.all[id] : document.getElementById(id) ;
} //findElement

function breakOut() {
	if (parent.frames.length > 0) {
	    parent.location.href = self.document.location
	}
} //breakOut

// Got this off the internet at http://www.howtocreate.co.uk/tutorials/index.php?tut=0&part=16
// Made a slight modification to the code.
// dimension = WIDTH, HEIGHT
function getBrowserSize( dimension ) {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  if ( dimension == "WIDTH" ) {
  	return myWidth ;
  } else {
  	return myHeight ;
  }
} //getBrowserSize

// Lookup the id and if it exists set focus to it.
function setFocus( id ) {
	var element = findElement( id ) ;
	if( element )
	{ 
		element.focus() ;
	}
} //setFocus

// Sets the background color of an object. This needs to be worked on where the color is passed in as 
// well.
function setMenuActive( menuId )
{
	// Find the menu id, if valid change the background color
	// var element = document.getElementById? document.getElementById(menuId): document.all? document.all[menuId]: null;
	var element = findElement( menuId ) ;
	if ( element ) {
	    element.style.backgroundColor = "#EBF3FE";  
	}
} //setMenuActive


//// DATE FUNCTIONS /////////////////////////////////////////////////////////////////////

function lastUpdated() {
  // Netscape 4.7 lastModified is in the format "Friday, December 01, 2000 00:35:05"
  // IE 5.0 lastModified is in the format "12/01/2000 00:35:05"
  if ( navigator.appName == "Netscape" )
      document.writeln( 'Last updated ' + document.lastModified.substring(0,document.lastModified.length-9) ) ;
  else
      document.writeln( 'Last updated ' + document.lastModified.substring(0,10) ) ;
} //lastUpdated 

function y2k(number){
	return (number < 1000) ? number + 1900 : number;
} // y2k

function printDate() {
	var now = new Date();
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
	mod = "" //"<font face=\"Verdana,Arial\" size=\"2\">"
	mod +=  days[now.getDay()] + ", " + months[now.getMonth()] + " " 
	mod += date  +  ", " + (y2k(now.getYear())) 
	document.write(mod)
	//document.write("</font>")
} //printDate


//// WINDOW FUNCTIONS /////////////////////////////////////////////////////////////////////

function openSlideShowWindow( url ) {
  previewWindow( url, 600, 570, "no" ) ;
} //openSlideShowWindow

function previewWindow( url, width, height, scrollbars ) {
	// previewWindow.win is a static variable
	// previewWindow.url is a static variable
	//  <A HREF="javascript:previewWindow('file.html',400,600,'yes');"> Link </A>
	// First check if we have a good url
	if ( url && url != "" ) {
		if ( previewWindow.win && previewWindow.win != "" && previewWindow.win.closed == 0 ) {
			previewWindow.win.focus() ;
			if( previewWindow.url != url ) {
				previewWindow.url = url ;
				previewWindow.win.location = url ;
			}
		} else {
			if ( ! (scrollbars && scrollbars != "") ) {
				scrollbars = "yes" ;
			}
			previewWindow.win = window.open(url,"previewWindow","toolbar=no,location=no,status=no,menubar=no,scrollbars=" + scrollbars + ",resizable=yes,height=" + height + ",width=" + width ) ;
		}
    } // url
    else
    {
		alert( "No url was provided to display." ) ;
    }
} // previewWindow


//// IMAGE FUNCTIONS /////////////////////////////////////////////////////////////////////

// 2003.04.10 I believe there might be a conflick with the use of "onLoad" with NS4, renamed
function myOnLoad(image) {
	// The below declarations and array assignment was initially done outside
	// of this function. That allowed this to be set once, and then the func could
	// be called any number of times and not have to waste time setting up the variables.
	var bgImg = new Array() ;
	var menuSize = 0 ;
	bgImg[++menuSize] = "images/bg/daveandrob.jpg" ;
	bgImg[++menuSize] = "images/bg/johnnitro.jpg" ;
	bgImg[++menuSize] = "images/bg/robm1.jpg" ;
	bgImg[++menuSize] = "images/bg/upstream.jpg" ;
	bgImg[++menuSize] = "images/bg/winni.jpg" ;
	bgImg[++menuSize] = "images/bg/robsuburban.jpg" ;
	bgImg[++menuSize] = "images/bg/DCP01479.jpg" ;
	bgImg[++menuSize] = "images/bg/dave.jpg" ;

	if ( document.images && image && image != "" ) {
		document.images[image].src = bgImg[(Math.floor(Math.random()*menuSize) + 1)] ;
	}
} //onLoad

// main image rollover
//var menuImg = new Array() ;
//menuImg[1] = new Image() ;
//menuImg[1].src = "images/btwbd2001-5.jpg" ;
//menuImg[2] = new Image() ;
//menuImg[2].src = "images/btwbd2001-4.jpg" ;
function loadImage(name,index) {
  if ( document.images && name != "" ) {
    document.images[name].src = menuImg[index].src ;
  }
  if ( arguments.length == 3 ) {
    window.status = arguments[2] ;
  } else {
    window.status = '' ;
  }
  return true ;  // THIS IS REQUIRED FOR THE WINDOW.STATUS CHANGE TO OCCUR
} //loadImage


// The below code sets the background image based on the size of the window (to minimize tiling).
// In order to maintain that display we need to track if the page is resized and to adjust accordingly.
// There are two basic methods. The first is to just cause the entire page to reload, therefore reprocessing
// the dimensions of the page, or trap the event and reset the background image.
// The first is the easiest and this can be done by two methods:
//
// 1. BODY TAG
//  <BODY onResize="window.location.reload()">
//
// 2. Creating a handler and and setting the event to that handler.
//   <SCRIPT LANGUAGE="JavaScript1.2">
//    function reDo(){ window.location.reload() }
//    window.onresize = reDo;
//  </SCRIPT>
//
// Presumably we could just reassign the image to the bodytd element and be done with it as well.

// Set the background image to some randomly chosen one
// Set the background image for the body area
// This currently defaults to setting a TABLE TD cell of the name 'bodytd' to a random image
//	<TD ALIGN="left" VALIGN="top" CLASS="bodybg" ID="bodytd">
// However I think this would work for any ID name that has a background property.
//
// This function actually sets the background image to some object. This could be called at the end
// of a html file with a script tag calling this function, or possibly using the BODY onLoad event.
// This function should be modified to pass in the name of the object that we want to set the background
// to.
function setBackgroundImage()
{
	 var x = findElement( "bodytd" ) ;
	 var i = randomBackground(1,12,1080,900) ;
	 if ( x ) {
	 	 // Check if the older x.background is available, otherwise try the style
		 //alert( "x.background: " + x.background ) ;
		 if ( x.background != null ) {
		 	// IE
			x.background = i ;
		 } else {
		 	// FireFox
			// alert( "x.style: " + x.style.backgroundImage ) ;
			x.style.setProperty("background-image","url(" + i + ")","") ;
		 } 
	 }
} //setBackgroundImage

// There are several ways to make this work. The first is to just use dynamic html to write
// out the tag you want the background image to be attached to:
//
//		<SCRIPT LANGUAGE="Javascript">
//  		document.writeln('<BODY BACKGROUND="' + randomBackground(1,7) + '">');
//		</SCRIPT>
//
// Another approach is to put an ID on the tab you want to change, then at the bottom of the
// file add some script to find the id and assign a background image. The assignment will be in
// different forms based on the type of tag you are using. For a table data tag you would:
//
//		<TD ALIGN="left" VALIGN="top" CLASS="bodybg" ID="bodytd">
//		<SCRIPT LANGUAGE="JavaScript">
//		<!-- Set the background image for the body area
//		 var x = findElement( "bodytd", "document.bodytd" ) ;
//		 if ( x ) {
//	 	 x.background = randomBackground(1,10) ;
//		} 
//		//-->
//		</SCRIPT>
//
// We need to look into using the <BODY onLoad=""> event to initially set the background image.
function randomBackground( randomBackgroundMinimum, randomBackgroundMaximum, widthLimit, heightLimit ) {
  var choice = Math.floor(Math.random()*(randomBackgroundMaximum-randomBackgroundMinimum)+randomBackgroundMinimum) ;
  var width = getBrowserSize("WIDTH") ;
  var height = getBrowserSize("HEIGHT") ;
  var size ;
  // Depending on the size of the browser window, selectively choose a background image that best fits
  // widthxheight   extension
  // 1400x1050      1400
  // 1024x768       1024
  // The limits are used to makeup for the difference if the element isn't the entire size of the browser.
  // This way you can fine tune at what size you really want to make the transition to the larger image.
  // The goal is to not have any repeating image in the background given the initial size of the browser.
  // The next step is to capture the resize event and reavaluate the background image size and assing :)
  size = width < widthLimit && height < heightLimit ? "1024" : "1400" ;
  //alert( "width:" + width + " height:" + height + " size:" + size ) ;
  return 'images/bg/bg' + choice + '.' + size + '.jpg' ;
} //randomBackground



//// Market America Banner Adds /////////////////////////////////////////////////////////////////////

/* UNCOMMENT THIS SECTION TO USE THE BANNER ROTATION FUNCTIONS
var banners = new Array() ;
var bannerCount = 0 ;
var bannerActive = -1 ;

banners[bannerCount++] = new banner( "images/banners/AutoFF.gif", 1 ) ;
banners[bannerCount++] = new banner( "images/banners/GlobalCare1.gif", 2 ) ;
banners[bannerCount++] = new banner( "images/banners/PhonePlanBanner.gif", 3 ) ;
banners[bannerCount++] = new banner( "images/banners/waterfilters.gif", 4 ) ;
banners[bannerCount++] = new banner( "images/banners/Flowerbanner.gif", 5 ) ;
banners[bannerCount++] = new banner( "images/banners/IsoFamily.gif", 8 ) ;
banners[bannerCount++] = new banner( "images/banners/StealthBanner.gif", 8 ) ;
banners[bannerCount++] = new banner( "images/banners/thermo.gif", 8 ) ;
banners[bannerCount++] = new banner( "images/banners/Snap.gif", 9 ) ;
banners[bannerCount++] = new banner( "images/banners/MotivesBanner2.gif", 11 ) ;
banners[bannerCount++] = new banner( "images/banners/ps_banner11.gif", 12 ) ;
banners[bannerCount++] = new banner( "images/banners/PrimeFamilyBanner.gif", 23 ) ;
banners[bannerCount++] = new banner( "images/banners/pethealth_store_banner.gif", 24 ) ;
*/

// Constructor for banner object used in menu list
function banner(src, store) {
	this.src = src ;
 	this.store = store ;
} // banner

function bannerAdd( seconds, width, height ) {
	var mySeconds, MyWidth, MyHeight ;
	mySeconds = ( seconds && seconds != "" ) ? seconds : 12 ;
	myWidth = ( width && width != "" ) ? width : 468 ;
	myHeight = ( height && height != "" ) ? height : 60 ;
	// from MA portal
	// <a href="" name="image_target" border="0" target="main" onClick="frameName()"><img src="http://www.unfranchise.com/images/banners/Flowerbanner.gif" name="banner_image" border="0" width="468" height="60" onMouseOver="window.status=''; return true"></a>
	document.writeln( '<a name="bannerLink" id="bannerLink" border="0" target="_blank" href="">' ) ;
	document.writeln( '<img name="bannerImage" id="bannerImage" border="0" src="" width="' + myWidth + '" height="' + myHeight + '" alt="" onMouseOver="window.status=\'\'; return true"></a>' ) ;
	bannerRotate( mySeconds ) ;
} // bannerAdd

function bannerRotate( seconds ) {
	// The getElementById is for Netscape 6+, IE 5+, document.all is IE4
	// For Netscape 4 we need to access directly.
	if( ns4 ) {
		var link = document.anchors.bannerLink ;
		var image = document.bannerImage ;
	} else
	{
		// var link = document.all ? document.all["bannerLink"] : document.getElementById("bannerLink");
		// var image = document.all ? document.all["bannerImage"] : document.getElementById("bannerImage");
		var link = findElement( "bannerLink" ) ;
		var image = findElement( "bannerImage" ) ;
	}
	if( !link ) {
		alert( "bannerRotate: Could not find link object." ) ;
		return ;
	}
	if( !image ) {
		alert( "bannerRotate: Could not find image object." ) ;
		return ;
	}
	// Special Case - Randomly select the first banner.
	if( bannerActive == -1 ) {
		bannerActive =  Math.floor(Math.random()*(bannerCount-1)) ;
	} else {
		bannerActive++ ;
	}
	if( bannerActive == bannerCount ) { bannerActive = 0; } ;
	//Testing: alert( "bannerRotate: processing: " + bannerActive + "  store: " + banners[bannerActive].store ) ;
	// 2003.09.15 MJR - The Market America site stopped processing this link url.
	// Using a standard link for now.
	// link.href = "http://living-gr8.unfranchise.com/banner1.cfm?ID=7247&StoreID=" + banners[bannerActive].store ;
	link.href = "http://living-gr8.unfranchise.com/" ;
	image.src = banners[bannerActive].src ;
	//window.setTimeout("bannerRotate();",eval(seconds*1000)) ;
	window.setTimeout("doRotate(" + seconds + ");",eval(seconds*1000)) ;
} // bannerRotate

function doRotate( seconds ) {
	// Looks like there might be a bug where you can't setTimeout on yourself without getting stuck in an infinite loop.
	// This appears to work correctly. Need to check into setInterval and what level broswer supports that function.
	bannerRotate( seconds ) ;
} //doRotate


//// ACROBAT FUNCTIONS /////////////////////////////////////////////////////////////////////

// The following functions are used to check if the Acrobat plugin is installed on the computer
// accessing this web page. It is very important that the object statement be written to the page
// and processed before we check for the plugin. For now we are going to just write it out to the
// page regardless. We may want to wrap this in a function and only call this with the onLoad event
// or something on the pages we know we want to check for the plugin.

// Inorder for the check to work on IE we need to have this object created 
document.write( "<OBJECT id=Pdf1 height=0 width=0 classid=clsid:CA8A9780-280D-11CF-A24D-444553540000></OBJECT>" ) ;

// Return true if installed, false if not.
function acrobatPluginCheck()
{
	var agt=navigator.userAgent.toLowerCase();
	if(agt.indexOf("msie") != -1)
	{
		return acrobatPluginCheckIE();
	}
	else if(agt.indexOf("mozilla") != -1 || ((agt.indexOf("netscape") != -1) || (agt.indexOf("; nav") != -1)))
	{
		return acrobatPluginCheckNN();
	}
} // acrobatPluginCheck

function acrobatPluginCheckNN()
{
	if (navigator.plugins && navigator.plugins["Adobe Acrobat"].description != "") 
	{
		var description = navigator.plugins["Adobe Acrobat"].description; 
		var regEx = /(\d+)/;
		var version = 0;
		if(regEx.test(description))
		{
			version = RegExp.$1;
		}
		return true ;
	}
	else
	{
		return false ;
	}
} // acrobatPluginCheckNN

function acrobatPluginCheckIE()
{
	// Call the ActiveX Object and get the Version Information
	var versionData = Pdf1.GetVersions();
	if ( versionData )
	{
		return true ;
	} else
	{
		return false ;
	}
} //acrobatPluginCheckIE


//// ???? /////////////////////////////////////////////////////////////////////

// End: functions.js
