//v1.0
// Flash Player Version Detection
// Detect Client Browser type
// Author: Michael Brown, Web Dev Dept.
// Copyright 2010 Utherverse Digital, Inc.  All rights reserved.

// Configurable default settings
// *********************************************
// The Postback URL to post to upon detection
// variables sent are: installDate & installDomain, empty values if client is not installed
var PostbackUrl = '/clientInstalled.aspx';
// Do a postback if the client is detected as installed
var DoPostbackOnInstalled = true;
// Do a postback if the client is detected as not installed
var DoPostbackOnNotInstalled = true;
// *********************************************



// Do not modify settings below
// *********************************************
var isIE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

var ClientInstalled = false;
var PostbackComplete = false;
var receivedClientNotification = false;
var installDate = '';
var installDomain = '';
var xmlhttp =  new ajaxRequest();
var DEBUG = false;
var callback = function() {};	// register empty callback
// *********************************************

// add a hook to the window.onload function to start the detection
addEvent(window, 'load', ClientDetectInit);


function RegisterCallback(func){
	callback = func;
}

// Create an invisible div layer and load the flash detection object
function ClientDetectInit(){
	var width=1;
	var height=1;
	var html = "";
	var newdiv = document.createElement('div');
	newdiv.setAttribute('id', 'clientDetectDiv');
	newdiv.style.width = width;
	newdiv.style.height = height;
	newdiv.style.visibility = 'visible';
	newdiv.style.position = 'absolute';
	newdiv.style.left = 0;
	newdiv.style.top = 0;
	newdiv.style.backgroundColor = '#FF0000';
	//html='<table border=0 cellspacing=0 cellpadding=0 style="width:'+width+';height:'+height+';"><tr><td style="background-color:#FF0000" align="left" valign="top">';
	html='';
	if (isIE && isWin && !isOpera){
		html+= '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" width="'+width+'" height="'+height+'" id="ClientDetect" align="middle">\n';
		html+= '<param name="allowScriptAccess" value="always" />\n';
		html+= '<param name="allowFullScreen" value="false" />\n';
		html+= '<param name="wmode" value="transparent" />\n';
		html+= '<param name="movie" value="/clientdetect/ClientDetect.swf" /><param name="quality" value="high" /><param name="bgcolor" value="#000000" />\n';
		html+= '</object>\n';
	}else{
		html+= '<embed src="/clientdetect/ClientDetect.swf" quality="high" wmode="transparent" bgcolor="#000000" width="'+width+'" height="'+height+'" name="ClientDetect" align="middle" allowScriptAccess="always" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.adobe.com/go/getflashplayer" />\n';
	}
	//html+= '</td></tr></table>\n';
	newdiv.innerHTML = html;
	document.body.appendChild(newdiv);
}

// add an event to the body onload
function addEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
 	}else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	}else{ 
		return false; 
	}
}


// Called by the flash app if client is installed
function SetClientInstalled(idate,idomain){
	ClientInstalled = true;
	installDate=idate;
	installDomain = idomain;
	receivedClientNotification=true;
	// invoke a callback function
	callback();
	if(DoPostbackOnInstalled) DoClientPostback();
	if(DEBUG) alert('Client is installed. Installed on ' + installDate);
}

// Called by the flash app if client is not installed
function SetClientNotInstalled(){
	ClientInstalled = false;
	receivedClientNotification = true;
	installDate = '';
	installDomain = '';
	callback();
	if(DoPostbackOnNotInstalled) DoClientPostback();
	if(DEBUG) alert('Client is NOT installed.');
}

// Perform the postback to the client to set a session variable
function DoClientPostback() {
	// Pass 'installDate' and 'installDomain' query variables to the postback script
	// these values will be null/empty if the client is NOT installed.
	xmlhttp.open('GET', PostbackUrl + '?installDate=' + installDate + '&installDomain=' + installDomain, true);
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send(null);
    //xmlhttp.send('installDate=' + installDate + '&installDomain=' + installDomain);
}

// Ajax state changed
xmlhttp.onreadystatechange = function() {
	if (xmlhttp.readyState == 4) {
		if (xmlhttp.status == 200) {
			//Postback completed
			if (DEBUG) alert('Postback complete!');
		} else {
			//Postback failed
			if (DEBUG) alert('Error: Could not set client installed session variable.');
		}
		PostbackComplete = true;
	}
}

function ajaxRequest(){
	var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"]; //activeX versions to check for in IE
	if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
		for (var i=0; i<activexmodes.length; i++){
			try{
				return new ActiveXObject(activexmodes[i]);
			}
			catch(e){
				//suppress error
			}
		}
	}else if (window.XMLHttpRequest){ // if Mozilla, Safari etc
		return new XMLHttpRequest();
	}else{
		return false;
	}
}


