var mobileDevice = "false";
var mobileURL = "http://m.bcbstx.com/";
var mobileSite;

// Mobile Device detection logic
if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/Blackberry/i)) || (navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/Windows CE/i)) || (navigator.userAgent.match(/Palm/i)) || (navigator.userAgent.match(/Windows Phone OS 7/i))  ) {
mobileDevice = "true";
}

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 gup( name )  // G.U.P (get URL parameter)
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

// Read URL parameters or cookie value to see whether mobile site is turned off
var mobile = gup( 'mobile' );
var mobilecookie = readCookie('mobile_site');
if (mobile == "off")
{mobileSite = "off"}
else if (mobilecookie == "off")
{mobileSite = "off"}
else 
{mobileSite = "on"}

// If mobile site cookie is not turned off, redirect to mobile site
if (mobileSite == "on") { 
	if(mobileDevice == "true") {
	document.cookie = "mobile_site=on"; 
	window.location.href= mobileURL;
	}
}

if (mobileSite == "off"){ // turn on session cookie that mobile site is off
document.cookie = "mobile_site=off"; // Sets cookie that mobile site is off
}

// Turns mobile site back on if user views full site
function mobileOn() {
	var date = new Date();
	date.setTime(date.getTime() - 1 );
	var expires = "; expires=" + date.toGMTString();
	document.cookie = "mobile_site=off" + expires; // Expires mobile site cookie to turn it off
	document.cookie = "mobile_site=on"; // Sets cookie that mobile site is off		
	window.location.href= mobileURL;
}

// Display option to view mobile site once again if user views full site
function checkMobileAlert() {
	document.write('<!--[if IE]> ');
document.write('<style>#mobilealert{display:none}</style>');
document.write('<![endif]--> ');
if(mobileDevice == "true")
{ 
if(mobileSite == "off")
	{
document.getElementById('mobilealert').innerHTML = "<a href='" + mobileURL + "' onClick='mobileOn();'>A mobile version of this site is available.<br>View Blue Cross Blue Shield of Texas Mobile</a>.";
	}
}
}

