/*
Script by Edmund Edgar, March, 2005, http://edochan.com
This script tries to find out if the user is able to use SSL.
If it manages to reach a conclusion one way or the other, it sets a cookie called 'BrowserSSLLooksOK' with the value '1' or '0'.
If it can't reach a conclusion, it doesn't set a cookie either way.
*/

// Put your own images here
var HTTPImage = 'http://g-images.amazon.com/images/G/01/associates/navbar2000/logo-no-border(1).gif';
var HTTPSImage = 'https://images-na.ssl-images-amazon.com/images/G/01/associates/navbar2000/logo-no-border(1)';
// var HTTPSImage = 'https://images-na.ssl-images-amazon.communists/images/G/01/associates/navbar2000/logo-no-border(1)'; // A bogus image that will never load to test the failure scenario.
var secondsHTTPSMayReasonablyTakeLongerThanHTTP = 5; // We'll decide we've got a result after this much time.
// THAT SHOULD BE ALL YOU NEED TO CHANGE

var cookieName = 'BrowserSSLLooksOK';
var cookieSuccessValue = '1';
var cookieFailureValue = '0';

var done = false;

function processLoaded(imageType) {
// Process the loaded event of an image.

   // If it's an HTTPS image that's loaded, we'll declare a success there and then.
   // If not, we'll start the countdown before we decide HTTPS has failed.
   // NB Although we'll act as if SSL failed once the timeout arrives, we'll still keep listening for the HTTPS image's loaded event in case it arrives later than we'd expected. 
   if (imageType == 'https') {
      processResult(true); 
   } else {
      if (done == false) {
         setTimeout('processResult(false)', (secondsHTTPSMayReasonablyTakeLongerThanHTTP)*1000);
      }
   }

}

function processResult(result) {

   if ((done == false) || (result == true)) {
      done = true;
      setSSLCookie(result);
   }

}

function setSSLCookie(success) {

   // var today = new Date()
   // var expires = new Date()
   // expires.setTime(today.getTime() + 1000*60*60*24*7)
   var val;
   if (success) {
      val = cookieSuccessValue;
   } else {
      val = cookieFailureValue;
   }
   // alert('setting cookie'+val);
   setCookie(cookieName, val, false , '/', false, false);
   // alert('set cookie' + getCookie(cookieName));
   // alert('result:' + success); 
}

function writeImageTags() {
// This writes the image tags on the script. 
// You don't have to do this with JavaScript, but it lets you avoid calling up the test images for people who don't have JavaScript enabled and couldn't use the fastest-server-finding functionality anyway.  
// This assumes you've defined the HTTPSImage and HTTPImage at the top of the script.
// Instead, you may prefer to call writeImageTag(<url>,<imageType>) directly from the document.

   writeImageTag(HTTPSImage,'https');
   writeImageTag(HTTPImage,'http');

}

function writeImageTag(imageSRC,imageType) {

   var today = new Date();
   var startTime = today.getTime();

   var url = imageSRC + '?time=' + startTime; //Add the time to the url to make sure the image gets called from the server, not just the browser's cache or that of the proxy server.
   var tag = '<img src="' + url + '" onLoad="processLoaded(' + "'" + imageType + "'" + ');" class="hidden">';
   document.writeln(tag);

}

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

function getCookie(Name) 
{   
   var search = Name + "="; 
   if (document.cookie.length > 0) { // if there are any cookies      
      offset = document.cookie.indexOf(search)       
      if (offset != -1) { // if cookie exists          
         offset += search.length          
         // set index of beginning of value         
         end = document.cookie.indexOf(";", offset)          
         // set index of end of cookie value         
         if (end == -1) { 
            end = document.cookie.length; 
         }
         return unescape(document.cookie.substring(offset, end));
      }
   }
}



