/*  JavaScript for Microsoft NZ - YDL Upgrade Message
 *  Kris McGlashan
 *  090603 -  v3.1b
 */

function ieCloseMessage() {
	ieCreateCookie('iedetect', 'shown', 1 );
	document.getElementById('iemodal').style.display = "none";
}

function ieCreateCookie(name, value, days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function ieReadCookie(name) {
  var ca = document.cookie.split(';');
  var nameEQ = name + "=";
  for(var i=0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
  return null;
}

function ieEraseCookie(name) {
  createCookie(name, "quit", -1);
}

// Modified detection script based on quirksmode.org code
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "unknown";
		this.version = this.searchVersion(navigator.userAgent) || this.searchVersion(navigator.appVersion) || "unknown";
		this.OS = this.searchString(this.dataOS) || "unknown";
		this.CPUClass = this.searchString(this.dataCPUClass) || "unknown";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		if (navigator.userAgent.indexOf("Trident/4.0") != -1) {
			return 8;
		}
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{ // for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ // for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.userAgent,
			subString: "Windows NT 6.1",
			identity: "Windows 7"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 6",
			identity: "Windows Vista"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 5.1",
			identity: "Windows XP"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 5",
			identity: "Windows 2000"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 4.90.3000",
			identity: "Windows ME"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 4.10.2222",
			identity: "Windows 98SE"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 4.0.1998",
			identity: "Windows 98"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 4.0",
			identity: "Windows 95"
		},
		{
			string: navigator.userAgent,
			subString: "Windows NT 3.1",
			identity: "Windows NT"
		},
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.userAgent,
			subString: "iPhone",
			identity: "iPhone/iPod"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	],
	dataCPUClass : [
		{
			string: navigator.userAgent,
			subString: "Win64",
			identity: "64-bit"
		},
		{
			string: navigator.userAgent,
			subString: "WOW64",
			identity: "32-bit emulation"
		},
		{
			string: navigator.cpuClass,
			subString: "64",
			identity: "64-bit"
		},
		{
			string: navigator.cpuClass,
			subString: "32",
			identity: "32-bit"
		},
		{
			string: navigator.platform,
			subString: "Win64",
			identity: "64-bit"
		},
		{
			string: navigator.platform,
			subString: "Win32",
			identity: "32-bit"
		}
	]
};

function triggerIEModal(modalImpPxSrc) {
	if (navigator.cookieEnabled == true) {	//if cookies available
		if (ieReadCookie('iedetect') != "shown") {	//and message hasn't been shown today
			if (BrowserDetect.OS == "Windows Vista" || BrowserDetect.OS == "Windows XP") {	//and Vista or XP
				if (!(BrowserDetect.browser == "Explorer" && BrowserDetect.version == 8)) {	//and not already IE8	
					document.getElementById('iemodal').style.display = "inline";	//then... show the message
					document.getElementById('modalImpPx').src = modalImpPxSrc;	//... and trigger modal impression tracking pixel
	}	}	}	}
}
