/// <summary>
///     Fucntions for returning information about the browser 
///     and it's version </summary>
/// <remarks>
/// <vesion>1.12
///     2007-05-22 11:19</version>
/// <versionHistory>
///    1.12
///       * added isSafari() and isFirefox()
/// </versionHistory>
/// <source> Goodman 2004 > JavaScript Bible 5th Edition 
///   Danny Goodman, Mar 2004. > Bonus Chapter Page 74 </source>
/// <created>   <date>12 Aug 2003</date> 
///             <author> John Bentley  </author> </created><br />  
/// </remarks>

///   Summary:  Tests whether your Broswer qualifies for your code.
///   Remarks:  You need to adjust this according to your browsers needs.
///             It writes information and test results to your web pages
///   Usage:    
///   Source:   Goodman 2004 > JavaScript Bible 5th Edition 
///             Danny Goodman, Mar 2004. > Bonus Chapter Page 71
///   Modified by: John Bentley


var IEMin = 6;
var IEMinString = ("" + IEMin.toFixed(1));
var FirefoxMin = 1.5;
var FirefoxMinString = ("" + FirefoxMin.toFixed(1));

function checkBrowserEnvironmentExplicitly() {
    var intro = "";
    var result = "";
    var recommendation = "";
    var includeDownloadInstructions = false;
    
    intro +=  "<h2>Testing Your Browser</h2>";
    intro +=  "<ol>";
    intro +=  "<li> A modern Browser (World Wide Web Consortium Compatible)? ... " + isW3C() + "</li>";
    intro +=  "<li> A browser the developer specifically targeted (peformed testing with respect to):";
    intro +=  " Microsoft IE " + IEMinString + " minimum; Or Firefox " + FirefoxMinString + " minimum? ... " + wasTargeted() + "</li>";
    intro +=  "</ol>";

    var downloadInstructions = "<h2>Download Instructions</h2>"
    downloadInstructions += "<p>The following browsers are modern, World Wide Web Consortium compatible. They are also one of the browser brands/versions specifically targeted by the developer of this web site. Download either of these:</p>";
    downloadInstructions += " <ul><li><a href='http://www.microsoft.com/downloads/details.aspx?FamilyID=1e1550cb-5e5d-48f5-b02b-20b602228de6&DisplayLang=en'>Microsoft Internet Explorer Version 6.0 or later</a> the most popular browser.</li>";
    //downloadInstructions += " <ul><li><a href='http://www.microsoft.com/windows/ie/downloads/default.mspx'>Microsoft Internet Explorer Version 6.0 or later</a> the most popular browser.</li>";
    downloadInstructions += "   <li>OR <a href='http://www.mozilla.org/products/firefox/'>Firefox 1.0 or later</a> A free, open source browser.</li></ul>"
    downloadInstructions += "<p>Alternatively you could review <a href='http://www.webhits.de/webhits/browser.htm'>the latest range of popular browsers.</a></p>";
    // Pass & Pass
    if (isW3C() && wasTargeted()) {
      result = "<p>Your browser is a modern (W3C compatible) browser.";
      result += " It was also one of the brands/minimum versions these web pages where tested on.";
      result += " You should therefore see these web pages as the developer intended.</p>";
      
      recommendation += "<p>There is nothing you need to do.</p>";
    // Pass & Fail
    } else if (isW3C() && !wasTargeted()) {
      result = "<p>Your browser is a modern (W3C compatible) browser."
      result += " Therefore you will probably see these pages as the developer intended.";
      result += " However your browser brand/minimum version is not one the developer specifically targeted.";
      result += " We can't be as confident as we could be, therefore, that the web site appears as as the developer intended.</p>";
      
      recommendation = "<p>If the web pages look odd, gives rise to errors, or doesn't give you the functionality you expect then download one";
      recommendation += " of the browsers.</p>";
      includeDownloadInstructions = true;
    
    // Fail & Pass
    } else if (!isW3C() && wasTargeted()) {
      result = "<p>Error: Your browser is not modern (W3C compatible) but is a brand/minimum version specifically targeted by the developer.";
      result += "This is an unexpected result. The developer should be targeting  browser brands/minimum version that are also modern (W3C compatible).</p>"
      
      recommendation = "<p>Inform the developer of their error in this web site or ignore this result.</p>"
    
    // Fail & Fail
    } else if (!isW3C() && !wasTargeted()) {
      result = "<p>Your browser is not modern (W3C compatible) nor a brand/minimum version specifically targeted by the developer.";
      result += " The web pages probably don't appear to you as the developer intended.</p>";
      
      recommendation = "<p>Download a modern (W3C compatible) browser. The latest Microsoft Internet Explorer or Mozilla browser will fulfill this";
      recommendation += " requirement and also be one of the browser brands/versions specifically targeted by the developer of this web site.</p>"
      includeDownloadInstructions = true;
    } else {
      result = "<p>Error: Unexpected Else reached</p>";
      
      recommendation = "<p>Inform the developer of their error in this web site or ignore this result.</p>"
    }

    return  intro + result + "<h2>Recommendation</h2>" + recommendation + (includeDownloadInstructions ? downloadInstructions : "");
}


//  Summary:  Checks the users Internet Browser Environment to see
//            if the required function and browser version is adequate.
//  Remarks:  Does not display anything of all is OK>
//            
//  Depends:  outputAppend(..
//  Usage:    1. Call this function from <body onload...> event. Eg via a main function.
//               function main() {
//                  checkBrowserEnvironment();
//                }                
//                //-->
//                </script>
//              </head>
//              <body onload="main()">
//            2. Ensure your html page has an element with content id.
//                Eg <div id="content> ...
//            3. You should also have some info in <noscript> within your page
//            to take care of when javascript is turned off.
function checkBrowserEnvironmentSilentlyIfOK() {
  
  if (!navigator.cookieEnabled) {
    outputAppend("Cookies Enabled?... no. (Please turn cookies on).");
  }   
  if (!isW3C) {
    outputAppend("Browser is Modern (W3C DOM support)? ... no. (Updgrade your browser. Eg Get the free <a href='http://www.mozilla.org/products/firefox/'>Firefox<\/a>)");
  }
  
  return true;
}
  

// Returns: True if your browser was one on which the web site was targeted (explicitly developed for and tested against).
// Remarks: Passes the test if brand/version is a later browser.
//          Eg if Web pages tested on IE 6.0 and you have IE 7.0, then you pass.
function wasTargeted() {
  return isMSIEMinVer(IEMin) || isFirefoxMinVer(FirefoxMin);
}

// Basic Browser Type
function isW3C() {
  return ((document.getElementById) ? true : false);
}

// Versions

// Summary: Is Microsoft Internet Explorer minimumVersion or greater
// Usage:
// By: John Bentley
// From:  Goodman 2004 > JavaScript Bible 5th Edition > See P 194. 
function isMSIEMinVer(minimumVersion) {
 return (navigator.appName.indexOf("Microsoft") == 0 && getIEVersion() >= minimumVersion);
}

function isMozMinVer(minimumVersion) {
  return (isMoz() && getMozVersion() >= minimumVersion);
}

function isFirefoxMinVer(minimumVersion) {
  return (isMoz && getFirefoxVersion() >= minimumVersion);
}

function getIEVersion() {
  var ua = navigator.userAgent;
  var IEOffset = ua.indexOf("MSIE ");
  return parseFloat(ua.substring(IEOffset + 5, ua.indexOf(";", IEOffset)));
}

function getMozVersion() {
  var ua = navigator.userAgent;
  var revisionOffset = ua.indexOf("rv:");
  return parseFloat(ua.substring(revisionOffset + 3, ua.indexOf(")", revisionOffset)));
}

function getFirefoxVersion() {
  var ua = navigator.userAgent;
  var fireFoxOffset = ua.indexOf("Firefox");
  return parseFloat(ua.substr(fireFoxOffset + 8));
}

// Brands
function isIE() {
  return (navigator.appName == "Microsoft Internet Explorer");
}

function isSafari() {
  return (navigator.userAgent.indexOf("Safari") != -1);
}

function isFirefox() {
  return (navigator.userAgent.indexOf("Firefox") != -1);
}

function isMoz() {
  var ua = navigator.userAgent;
  // Has in string: Mozilla and Gecko; but not Netscape
  return (ua.indexOf("Mozilla") == 0 && ua.indexOf("Gecko") != -1 && ua.indexOf("Netscape") == -1)
}

// Operating Systems
function isWindows() {
  return (navigator.appVersion.indexOf("Win") != -1);
}

function isMac() {
  return (navigator.appVersion.indexOf("Mac") != -1);
}

function isMacPPC() {
  return (isMac() && (navigator.appVersion.indexOf("PPC") != -1 || navigator.appVersion.indexOf("PowerPC") != -1));
}

function isUnix() {
  return (navigator.appVersion.indexOf("X11") != -1);
}

// Utility
function outputAppend(message) {
  
  var newElement;
  // If there is no 'output' div yet ...
  if (document.getElementById("output")== null) {
    newElement = document.createElement("div"); 
    newElement.setAttribute("id","output");
    newElement.style.border = "1px solid black";
    newElement.style.padding = "0 1em";

    var stringBuilder = "";
    stringBuilder += "<p>This page will be displayed with all relevant information.";
    stringBuilder += " However, the following aspects of your internet browser";
    stringBuilder += " environment may mean the page is displayed with reduced";
    stringBuilder += " function:<\/p>";
    stringBuilder += "<ul id='outputList'><li>" + message + "<\/li><\/ul>";  
    newElement.innerHTML = stringBuilder;    
    insertHtmlAtTop(newElement);
  } else {
    var ele = document.getElementById("outputList");
    newElement = document.createElement("li"); 
    newElement.innerHTML = message;
    //alert(newElement.innerHTML);
    ele.appendChild(newElement);
  }
  
  return true;

}

// Insert the supplied HTML as the first child node of the content element.
function insertHtmlAtTop(newElement) {
  //var element =  document.getElementsByTagName("body")[0]        
  var element =  document.getElementById("content");   
  element.insertBefore(newElement, element.firstChild);   
  return true; 
}

// From:  Goodman 2004 > JavaScript Bible 5th Edition > See BC324
/* 
function getObject(obj) {
  var theObj;
  if (document.layers) {
    if (typeof obj == "string") {
      // just one layer deep
      return document.layers[obj];
    } else {
      // can be a nested layer
      return obj;
    }
  }

  if (document.all) {
    if (typeof obj == "string") {
      return document.all(obj).style;
    } else {
      return obj.style;
    }
  }
  
  if (document.getElementById) {
    if (typeof obj == "string") {
      return document.getElementById(obj).style;
    } else {
      return obj.style;
    }
  }
  
  return null;
}
*/
