
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;
	var end=document.cookie.indexOf(";",begin);
	if(end==-1)
		end=dc.length;
	return unescape(dc.substring(begin+prefix.length,end));
}

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 svgInfoPage="http://www.examotion.com/";
var svgDownloadPage="Resources/SVG.msi";
var checkIntervalDays=1;
var firstSVG=true; // Ask only once per page even without cookies

function getCheckInterval()
{
	return checkIntervalDays*24*60*60*1000;
}

// 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()
{
	if(getCheckInterval()>0)
	{
		var expires=new Date();
		fixDate(expires); // NN2/Mac bug
		expires.setTime(expires.getTime()+getCheckInterval());
		setCookie('SVGCheck','0',expires,'/')
	}
}

function isSVGAdobeInstalled()
{
	try
	{
		var x = new ActiveXObject("Adobe.SVGCtl.3");
		return true;
	}
	catch(e)
	{
		return false;
	}
}
	
function isSVGRenesisInstalled()
{
	try
	{
		var x = new ActiveXObject("RenesisX.RenesisCtrl.1");
		return true;
	}
	catch(e)
	{
		return false;
	}
}
	
function checkSVGViewer()
{
	if(window.svgInstalled)
		return;
	if(is.ie5up)
	{
		window.svgViewerAvailable=true;
		window.svgInstalled=isSVGAdobeInstalled();
		if(!window.svgInstalled)
			window.svgInstalled=isSVGRenesisInstalled();
	}
	else if(is.gecko)
	{
		window.svgSupported=true;
	}
}

function getSVGViewer()
{
	if(confirm('The SVG Viewer is not installed. Download now?'))
		location=svgDownloadPage;
}

function checkAndGetSVGViewer()
{
	checkSVGViewer();
	var svgCookie=getCookie('SVGCheck');
	if(firstSVG&&!svgCookie)
	{
		if(window.askForSVGViewer)
		{
			setSVGCookie();
			getSVGViewer();
		}
		firstSVG=false;
	}
}

function emitSVG(embedAttrs)
{
	if(window.svgInstalled)
	{
		document.writeln('<embed type="image/svg-xml" '+embedAttrs+'>');
	}
	else if(window.svgSupported)
	{
		document.writeln('<embed type="image/svg+xml" '+embedAttrs+'>');
	}
	else if(window.svgViewerAvailable)
	{
		document.writeln('<embed type="image/svg-xml" '+embedAttrs+' pluginspage="'+svgDownloadPage+'">');
	}
	else
	{
		document.writeln('<p>To view this page you need an SVG viewer. There is currently no SVG ');
		document.writeln('Viewer available for your browser. ');
		document.writeln('<a href="'+svgInfoPage+'">Click here</a> for more information.</p>');
	}
}
