var SVG_UNKNOWN         = 0 ;
var SVG_CAPABLE         = 1 ;
var SVG_INCAPABLE       = 2 ;
var SVG_BROWSER_TOO_OLD = 3 ;
var verdict             = SVG_BROWSER_TOO_OLD ;
var svgInstallPage = "http://download.adobe.com/pub/adobe/magic/svgviewer/win/6.x/6.0x38363/en/SVGView.exe" ;

function isUndefined(a) 
{
    return typeof a == 'undefined';
}

function svgMimeType(mt)
{
  var found = false ;
  for (var j=0; j<navigator.mimeTypes.length; j++)
    {
      if ( (navigator.mimeTypes[j].type == mt) )
        {
          found = true ;
          break ;
        }
    }
  return found ;
}

function getSvgViewer() 
{
  if(confirm('The Adobe SVG Viewer is not installed. Download now?'))
    location=svgInstallPage;
}

function CheckForNavigator()
{
  verdict = SVG_BROWSER_TOO_OLD ;     

  if ((agnt.indexOf('mozilla')!=-1) && (major>=4))
    { //Browser not too old

      verdict = SVG_INCAPABLE ;     

      if (      svgMimeType( "text/svg"      ) )
        {
          verdict = SVG_CAPABLE ;
//          document.writeln('text/svg mimetype is present');
        }
      else if ( svgMimeType( "image/svg"     ) )
        {
          verdict = SVG_CAPABLE ; 
//          document.writeln('image/svg mimetype is present');
        }
      else if ( svgMimeType( "image/svg-xml" ) )
        {
          verdict = SVG_CAPABLE ; 
//          document.writeln('image/svg-xml mimetype is present');
        }
      else if ( svgMimeType( "image/svg+xml" ) )
        {
          verdict = SVG_CAPABLE ; 
//          document.writeln('image/svg+xml mimetype is present');
        }
    }
}

function detectSvg()
{
var agnt     = navigator.userAgent.toLowerCase() ;
var apnam    = navigator.appName.toLowerCase() ;
var major    = parseInt(navigator.appVersion) ;
var minor    = parseFloat(navigator.appVersion);

var ieCompat  = agnt.indexOf('msie')       != -1 ;
var microsoft = apnam.indexOf('microsoft') != -1 ;
var internet  = apnam.indexOf('internet')  != -1 ;
var explorer  = apnam.indexOf('explorer')  != -1 ;
var maybeIE   = microsoft && internet && explorer ;
      // Operating System
var isWin95 = ((agnt.indexOf("win95")!=-1)||(agnt.indexOf("windows 95")!=-1));
var isWin98 = ((agnt.indexOf("win98")!=-1)||(agnt.indexOf("windows 98")!=-1));
var isWinNT = ((agnt.indexOf("winnt")!=-1)||(agnt.indexOf("windows nt")!=-1));
var isWin32 = (isWin95||isWin98||isWinNT||
              ((major>=4)&&(navigator.platform.toLowerCase() == 'win32'))||
              (agnt.indexOf("win32")!=-1)||(agnt.indexOf("32bit")!=-1));

var isMac    = (agnt.indexOf("mac")!=-1);
var isMacPPC = (isMac&&((agnt.indexOf("ppc")!=-1)||(agnt.indexOf("powerpc")!=-1)));

var isNav= ((agnt.indexOf('mozilla')!=-1)&&(agnt.indexOf('spoofer')==-1)&&
            (agnt.indexOf('compatible')==-1)&&(agnt.indexOf('opera')==-1)&&
            (agnt.indexOf('webtv')==-1));
var isNav4up   = (isNav&&(major>=4))    ;
var isNav407up = (isNav&&(minor>=4.07)) ;

var isIe3   = (maybeIE&&(major<4));
var isIe4   = (maybeIE&&(major==4)&&(agnt.indexOf("msie 5.")==-1)&&(agnt.indexOf("msie 6.")==-1));
var isIe5up = (maybeIE&&!isIe3&&!isIe4);
  
//  alert(isWin32) (isNav4up && isIe5up)
  
  re1 = /\x29/ ;
  wordlist = agnt.split(re1) ;
  if ( isUndefined(wordlist[1]) ) 
    {
      var isIE      = isWin32 && maybeIE ;
      var isOpera   = false ;
      var isFireFox = false ;
      var isOther   = !isIE ;
    }
  else
    {
      var isIE        = false ;
      var isOpera     = wordlist[1].indexOf('opera') != -1 ;
      var isFireFox   = wordlist[1].indexOf('firefox') != -1 ;
      var isOther     = (!isOpera) && (!isFireFox) ;
    }
    
//  alert('IE='+isIE);
//  alert('isOpera='+isOpera);
//  alert('isFireFox='+isFireFox);
//  alert('isOther='+isOther);

var verdict = SVG_UNKNOWN ;

//  alert('ismac='+isOpera);
//  alert('isOpera='+isOpera);
  if ( !isIE  ||  (isMacPPC && isNav407up)  )
    { // Definitely NOT Internet Explorer, so navigator object should work.
      verdict = CheckForNavigator();
    }

  if ( (verdict != SVG_CAPABLE) && (isWin32 || (isMacPPC && isIe5up) ) )
    { // try Adobe Plugin and Corel Plugin via VBscript
      // we test for ability to create an object

      if ( ieCompat )
        { 
          re1 = /msie\s/
          wordlist = agnt.split(re1)
          var versio=parseInt(wordlist[1])
          if ((versio>=5) && (major >=4))
            {
              verdict = CheckForSvgViewerInVbScript();
            }
            else
            {
              verdict = SVG_BROWSER_TOO_OLD
            }
        }
      else
        {
          //alert('not IE compat');
        }
    }
//alert('last verdict='+verdict);

  if (verdict == SVG_CAPABLE )
    {
      alert('PENDRAW BROWSER SVG CAPABILITY CHECK RESULTS:-\n\nYour Web Browser is SVG capable. It should need no attention.  It should function well.\n\n(If you experience any delays in viewing SVG graphics, you might want to try a different plug-in)');
    }
  if (verdict == SVG_INCAPABLE )
    {
      if (isNav4up || isIe5up)
        {
          alert("PENDRAW BROWSER SVG CAPABILITY CHECK RESULTS:-\n\nYour browser is not SVG capable and can not show SVG\n\nBut you CAN add a plug-in to show SVG.");
          getSvgViewer();
        }
      else
        {
          alert("PENDRAW BROWSER SVG CAPABILITY CHECK RESULTS:-\n\nYour browser is not SVG capable and can not show SVG.\n\nYou may need to switch to a different browser, to see SVG.  Or you may need to download and install a plug-in\n\nWe have found that Internet Explorer 6 is good for SVG. And in our experience Adobe v6 SVG Viewer works well.");
        }
    }
  if (verdict == SVG_BROWSER_TOO_OLD )
    {
      alert("PENDRAW BROWSER SVG CAPABILITY CHECK RESULTS:-\n\nYour Browser is too old for SVG to be reliable.\n\nHere's what to do if you want to use SVG.\n\n1. Get the very latest version of Internet Explorer / Firefox / Opera / etc.\n\n2. Then download and install an SVG Viewer.\n\n\nWe have found that Internet Explorer 6 is good for SVG. And in our experience Adobe v6 SVG Viewer works well.");
    }
    
  if (verdict == SVG_UNKNOWN )
    {
      alert("PENDRAW BROWSER SVG CAPABILITY CHECK RESULTS:-\n\nWe are sorry, but we can not identify whether your browser can show you SVG or not (it does not appear to be one of the well-known browsers).\n\nWe recommend that you try installing the Adobe plug-in. \n\nIf that fails, we suggest you get the very latest version of Internet Explorer for your computer, plus the Adobe plug-in.");
      getSvgViewer();
    }
}
