// Support function for getting URL variable
// ref - http://papermashup.com/read-url-get-variables-withjavascript/

function getUrlVars() {  
	var vars = {};  
	var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {  
		vars[key] = value;  
	});  
	return vars;  
} 


// Support functions for cookie setting and reading
// ref - http://www.quirksmode.org/js/cookies.html

function createCookie(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 readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


// if mobileRedirect is received, save its' value to a cookie
// if mobileRedirect is not received AND no cookie from previous, make a cookie
// Note - mobileRedirect url var is case sensitive

if (getUrlVars()["mobileRedirect"] != undefined) {
	createCookie('mobileRedirect',getUrlVars()["mobileRedirect"],1);}
else {
	if (readCookie('mobileRedirect') == null) {createCookie('mobileRedirect',true,1);}
}



var useragents = ['palm', 'palmos', 'palmsource', 'iphone', 'blackberry', 'nokia', 'phone', 'midp', 'mobi', 'pda', 'wap', 'java', 'nokia', 'hand', 'symbian', 'chtml', 'wml', 'ericsson', 'lg', 'audiovox', 'motorola', 'samsung', 'sanyo', 'sharp', 'telit', 'tsm', 'mobile', 'mini', 'windows ce', 'smartphone', '240x320', '320x320', 'mobileexplorer', 'j2me', 'sgh', 'portable', 'sprint', 'vodafone', 'docomo', 'kddi', 'softbank', 'pdxgw', 'j-phone', 'astel', 'minimo', 'plucker', 'netfront', 'xiino', 'mot-v', 'mot-e', 'portalmmm', 'sagem', 'sie-s', 'sie-m', 'android', 'ipod'];
var agt=navigator.userAgent.toLowerCase();
var is_mobile = false;

for (i=0;i<useragents.length;i++) {
  if(agt.indexOf(useragents[i])!=-1) {
    is_mobile = true;
    user_agent = agt;
    break; 
  }
}

if (is_mobile && readCookie('mobileRedirect') == 'true') { location.replace('http://mobile.queenmary.com')} 
