// Copyright 2000 Adobe Systems Incorporated. All rights reserved. Permission
// to use, modify, distribute, and publicly display this file is hereby
// granted. This file is provided "AS-IS" with absolutely no warranties of any
// kind. Portions (C) Netscape Communications 1999.

// If you modify this file, please share your changes with Adobe and other SVG
// developers at http://www.adobe.com/svg/.

// Version 11/30/01
// 10/21/02 Modified by WDM (Bill McTeer, McTeer Systems, Inc., wmcteer@mctsys.com)
//	to clean up Mac installs.
// 10/30/02 Modified by WDM to add signalSVGViewerOutdated and special handling for 
//	Netscape 7 

function getBrowser()
{
var agt=navigator.userAgent.toLowerCase();
var v_maj=parseInt(navigator.appVersion);
var v_min=parseFloat(navigator.appVersion);
is_nav=((agt.indexOf('mozilla')!=-1)&&(agt.indexOf('spoofer')==-1)&&
	(agt.indexOf('compatible')==-1)&&
	(agt.indexOf('webtv')==-1)&&(agt.indexOf('msie')==-1));
is_nav3=(is_nav&&(v_maj==3));
is_nav4up=(is_nav&&(v_maj>=4));
is_nav407up=(is_nav&&(v_min>=4.07));
is_nav408up=(is_nav&&(v_min>=4.08));
is_ie=(agt.indexOf("msie")!=-1);
is_ie3=(is_ie&&(v_maj<4));
is_ie4=(is_ie&&(v_maj==4)&&(agt.indexOf("msie 5")==-1));
is_ie4up=(is_ie&&(v_maj>=4));
is_ie5=(is_ie&&(v_maj==4)&&(agt.indexOf("msie 5")!=-1)); 
is_ie5up=(is_ie&&!is_ie3&&!is_ie4);
is_win=((agt.indexOf("win")!=-1)||(agt.indexOf("16bit")!=-1));
is_win95=((agt.indexOf("win95")!=-1)||(agt.indexOf("windows 95")!=-1));
is_win98=((agt.indexOf("win98")!=-1)||(agt.indexOf("windows 98")!=-1));
is_winnt=((agt.indexOf("winnt")!=-1)||(agt.indexOf("windows nt")!=-1));
is_win32=(is_win95||is_winnt||is_win98||
	((v_maj>=4)&&(navigator.platform=="Win32"))||
	(agt.indexOf("win32")!=-1)||(agt.indexOf("32bit")!=-1));
is_mac=(agt.indexOf("mac")!=-1);
is_macPPC=(is_mac&&((agt.indexOf("ppc")!=-1)||(agt.indexOf("powerpc")!=-1)));
// WDM the Mac OS X test is bad, but moot because the Adobe auto-install page is broken. 
// A better test would be:
// is_maxosx=((agt.indexOf("powerpc")!=-1)||(agt.indexOf("mac os x")!=-1));
// This test is still wrong because all MSIE on PPC will be classified as OS X 
// (MSIE provides no way to distinguish OS X).  Classifying all MSIE as OS X would be better
// for auto-install than the other way around if Adobe fixed their install page to install 
// the Carbon version for IE/OSX, since the Carbon version also works on OS 9 but not vice-versa.
is_macOSX=(is_mac&&((agt.indexOf("powerpc")!=-1)&&(agt.indexOf("msie 5.1")!=-1)));
is_x86linuxmoz=((agt.indexOf("linux")!=-1)&&(agt.indexOf("mozilla")!=-1)&&(agt.indexOf("i686")!=-1));
is_solarismoz=((agt.indexOf("sunos")!=-1)&&(agt.indexOf("mozilla")!=-1));
// WDM added
is_nav7=(agt.indexOf("netscape/7.0")!=-1);
}

function setCookie(name, value, expires, path, domain, secure) {
var curCookie=name+"="+escape(value)+
	((expires)?"; expires="+expires.toGMTString():"")+
	((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+
	((secure)?"; secure":"");
document.cookie=curCookie;
}

// returns null if cookie not found
function getCookie(name) {
var dc=document.cookie;
var prefix=name+"=";
var begin=dc.indexOf("; "+prefix);
if(begin==-1) {
	begin=dc.indexOf(prefix);
	if(begin!=0)
		return null;
	}
else
	begin+=2;
// WDM correct reference inconsistency
var end=dc.indexOf(";",begin);
if(end==-1)
end=dc.length;
return unescape(dc.substring(begin+prefix.length,end));
}

// WDM Not used in this code
function deleteCookie(name, path, domain) {
if(getCookie(name))
	document.cookie=name+"="+((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+"; expires=Thu, 01-Jan-70 00:00:01 GMT";
}

function fixDate(date) {
var base=new Date(0);
var skew=base.getTime();
if(skew>0)
	date.setTime(date.getTime()-skew);
}

var svgInstallBase="http://www.adobe.com/svg/viewer/install/";
var svgInstallPage=svgInstallBase+"auto/";
var svgInfoPage="http://www.adobe.com/svg/";
var svgDownloadPage=svgInstallBase;
// WDM moved within setSVGCookie
// var checkIntervalDays=30;
var firstSVG=true; // Ask only once per page even without cookies

function getSVGInstallPage() {
// WDM revised to point to "best" place for Mac and NS 7.  Remove if Adobe corrects their 
// auto download page or comes up with a version for NS 7.
	if (is_mac || is_nav7)
		return svgDownloadPage;
	return svgInstallPage+"?"+location;
}

// WDM moved within setSVGCookie
// function getCheckInterval() {
// return checkIntervalDays*24*60*60*1000;
// }

// WDM The purpose of the SVGCheck cookie is to avoid hassling the user too often 
// about installing the SVG Viewer if the user declines the Download Now.  
// The value of the cookie is '0'. We need some value, but it doesn't matter what.
// We set the cookie for the entire site by specifying the path '/'.
// We could include something from adobe.com and set the cookie for that site.
// This would enable only asking once no matter how many sites a user encounters
// with SVG.
function setSVGCookie() {
// WDM modified to make checkInterval inline
// if(getCheckInterval()>0) {
	var expires=new Date();
	fixDate(expires); // NN2/Mac bug
	expires.setTime(expires.getTime()+
		30*60*1000);  // 30 minutes
	setCookie('SVGCheck','0',expires,'/')
//	}
}

function isSVGPluginInstalled() {
return (navigator.mimeTypes["image/svg"]&&navigator.mimeTypes["image/svg"].enabledPlugin!=null)||
       (navigator.mimeTypes["image/svg-xml"]&&navigator.mimeTypes["image/svg-xml"].enabledPlugin!=null)||
       (navigator.mimeTypes["image/svg+xml"]&&navigator.mimeTypes["image/svg+xml"].enabledPlugin!=null);
}

// WDM We could replace VBScript function if all IE browsers involved supported 
// try .. catch syntax.  Unfortunately IE 4 may not, depending on JavaScript version.
// function isSVGControlInstalled() {
//   // Syntax based on http://javascript.about.com/library/weekly/aa101501a.htm
//   // Only called in IE, uses eval to avoid confusing NN loader with try ... catch syntax
//   var result = false;
//   var code =    'try {';
//   code = code +   'var xObj = new ActiveXObject("Adobe.SVGCtl");';
//   code = code +   'if (xObj) result = true; xObj = null;';
//   code = code + '} catch (e) {}';
//   eval (code);
//   return result;
// }

function checkSVGViewer() {
window.askForSVGViewer=false;
window.svgViewerUnavailable=false;
if(window.svgInstalled)
	return;
getBrowser();
if(is_win32 && is_ie4up) {
	window.svgViewerAvailable=true;
	window.svgInstalled=isSVGControlInstalled();
	if(!window.svgInstalled)
		window.askForSVGViewer=true;
	}
// WDM Activate autoinstall page for Mac IE (seems to work fine, except for OSX bugs)
// else if((is_win32 && is_nav4up) || (is_macPPC && is_nav407up) || is_x86linuxmoz || is_solarismoz) {
else if((is_win32 && is_nav4up) || is_macPPC || is_x86linuxmoz || is_solarismoz) {
	window.svgViewerAvailable=true;
	window.svgInstalled=isSVGPluginInstalled();
// WDM 	// if(!window.svgInstalled&&((is_nav408up&&navigator.javaEnabled())||is_x86linuxmoz||is_solarismoz))
	if(!window.svgInstalled&&!(is_win32 && (!is_nav408up||!navigator.javaEnabled())))
		window.askForSVGViewer=true;
	}
// WDM
// else if(is_macPPC && is_ie5up) 
//	window.svgViewerAvailable=true;
}

function getSVGViewer() {
// WDM Let them know the bad news for Netscape 7.0
if (is_win32 && is_nav7) 
	alert('Unfortunately, no SVG Viewer plugin is available for Windows Netscape 7.0.'); 
else 
	if(confirm('The Adobe SVG Viewer is not installed. Download now?'))
		location=getSVGInstallPage();
}

// WDM Can be called from SVG to signal outdated viewer
var firstSVGOutdated = true;

function signalSVGViewerOutdated() {
if (firstSVGOutdated) {
	var svgCookie=getCookie('SVGOutdated');
	if(!svgCookie) {
		var expires=new Date();
		fixDate(expires); // NN2/Mac bug
		expires.setTime(expires.getTime()+
			7*24*60*60*1000);  // 7 days
		setCookie('SVGOutdated','0',expires,'/')
		if(confirm('Your Adobe SVG Viewer is outdated. Download newer version now?'))
			location=getSVGInstallPage();
		}

	firstSVGOutdated=false;
	}
}

// WDM Called from HTML page once before first SVG embed on page
function checkAndGetSVGViewer() {
checkSVGViewer();
var svgCookie=getCookie('SVGCheck');
if(firstSVG&&!svgCookie) {
	if(window.askForSVGViewer) {
		setSVGCookie();
		getSVGViewer();
		}
	firstSVG=false;
	}
}

// WDM Called from HTML page instead of the SVG embed 
// (depends on values set by checkAndGetSVGViewer()
// (Why not simply use firstSVG here to decide whether to call checkAndGet??)
function emitSVG(embedAttrs) {
if(window.svgInstalled)
	document.writeln('<embed '+embedAttrs+'>');
else if(window.askForSVGViewer)	{
	if(navigator.appName=='Netscape') {
		document.writeln('<p>To view this page you need an SVG viewer.');
		document.writeln('<a href="'+getSVGInstallPage()+'">Click here</a> for more information.</p>');
		}
	else
		document.writeln('<embed '+embedAttrs+' pluginspage="'+getSVGInstallPage()+'">');
	}
else if(window.svgViewerAvailable)
	document.writeln('<embed '+embedAttrs+' pluginspage="'+svgDownloadPage+'">');
else {
// WDM This code cannot know that no SVG Viewer has been created since it was written
//	document.writeln('<p>To view this page you need an SVG viewer. There is currently no Adobe SVG ');
//	document.writeln('Viewer available for your browser. ');
	document.writeln('<p>To view this page you need an SVG viewer.');
	document.writeln('<a href="'+svgInfoPage+'">Click here</a> for more information.</p>');
	}
}
