var agent = navigator.userAgent.toLowerCase();

// iPhones & iPods
var iphone = devCheck('iphone');
var ipod = devCheck('ipod');
var ipad = devCheck('ipad');

// Symbian S60 Open Source
var engineWebKit = "webkit";
var deviceS60 = devCheck('series60') && isWebKit();
var deviceSymbian = devCheck('symbian') && isWebKit();

// Android devices
var android = devCheck('android');
var androidWK = devCheck('android') && isWebKit();

// Windows Mobile
var windows = devCheck('windows ce');

// Blackberry
var blackberry = devCheck('blackberry');

// Palm OS
var palm = devCheck('palm');

// Detect Compatability
var ajax;
var ajaxSupported = true;

try {
    ajax = new XMLHttpRequest();
} 
catch (e){
    try {
        ajax = new ActiveXObject('Msxml2.XMLHTTP');
    }
    catch (e){
        try {
            ajax = new ActiveXObject('Microsoft.XMLHTTP');
        }
	catch (e){
            ajaxSupported = false;
        }
    }
}

function isWebKit() {
	return ((agent.indexOf(engineWebKit))!=-1);
}

function devCheck( device ) {
	return  ((agent.indexOf(device))!=-1)
}

function detect() {
 	if ( !ajaxSupported ) return false;
	if (iphone || ipod
		|| deviceS60 || deviceSymbian
		|| android || androidWK 
		|| windows || blackberry || palm ) return true;

 	return false;
}

function load() {
	var cookie = readCookie("useMobile");

	if (cookie != null) {
		if( detect() && cookie == "true" ) { 
			if(iphone || ipod || androidWK ) window.location = "/mobile/";
			else window.location = "mobile.htm";
		}
	}
	else { 
		if( detect() )  {
			var val = window.confirm("Would you like to use the mobile version of this site?  Clicking 'Cancel' will take you to the regular site.")
			createCookie( "useMobile", val, 90);
			if( val ) 
			{
				if(iphone || ipod || androidWK ) window.location = "/mobile/";
				else window.location = "mobile.htm";
			}
		}
	}
}
