
doMobileRedirect('http://m.insurekidsnow.gov', 10000, 'mobile');

// simple consoldated alert method for debugging
function debug(string)
{
  //  alert(string + '\n\n' + navigator.userAgent);
}

// write a cookie

// name:    the name of the cookie to set

// value:   the value to set

// expires: how long the cookie lasts, in minutes

// path:    the site path for which the cookie is valid

// domain:  the domain for which the cookie will be set

function setCookie(name, value, expires, path, domain)

{

    var expiresDate = null;



    //debug(expires);

    // if it's not null, set it.  if it's null it will be a session cookie

    if (expires && expires != "0")

    {

        // turn it into milliseconds

        //expires = expires * 60 * 60 * 1000;

        expires = expires * 60 * 1000;

        expiresDate = new Date().getTime() + expires;

    }





    var cookie;

    // if name is not set it won't work anyway, so we don't need to check

    cookie = name + "=" + value;

    cookie += ((expires && expires != 0) ? ";expires=" + new Date(expiresDate).toGMTString() : "");

    cookie += ((path) ? ";path=" + path : "");

    cookie += ((domain) ? ";domain=" + domain : "");

	 

    document.cookie = cookie;

}



function trim(str) { return str.replace(/^\s+|\s+$/, ''); };

// get the value of a cookie

// name:  the name of the cookie for which you want the value

function getCookie(name)	{

	var cookie = null;

	var cookies = document.cookie.split(";");

	for (var i = 0; i < cookies.length; i++)	{

		var cookie = cookies[i];

		var cookieval = cookie.split("=");

		if (trim(cookieval[0].toLowerCase()) == trim(name.toLowerCase())) return true;

	}

	

	return false;

}



// get the value of a query parameter

// name:  the name of the query parameter for which you want the value

function getQueryParam(name)	{

	var value;

	var queryString = window.top.location.search.substring(1);

	var queryParam = name + "=";

	

	if (queryString.length > 0)	{

		var beginParam = queryString.indexOf(queryParam);

		

		if (beginParam != -1)	{

			beginParam += queryParam.length;

			var endParam = queryString.indexOf("&", beginParam);

			

			if (endParam == -1)	{

				endParam = queryString.length;

			}

			value = queryString.substring(beginParam, endParam);

		}

	}

	return typeof value != 'undefined';

}



// determine whether the device is a mobile device or not

function isMobile()

{

   var agent = navigator.userAgent;

   var accept = navigator.accept;

	
    // Checks the user-agent

    if (agent != null)

    {


        // Some common patterns to tell if it is a mobile browser  // removed windows ce mobile stuff from mobile pattern "iemobile|Windows CE|MSIE" 
		var mobileBrowserPattern = /up.browser|up.link|iphone|mini|mmp|symbian|midp|wap|mobile|phone|pocket|IPad|pda|psp|SCH-I800|kindle|Xoom|Playbook|IPad|touchpad|A501|A100|A500|iemobile|Flyer|GRI40/ig;

        var mobileBrowserRegex = new RegExp(mobileBrowserPattern);

        // this finds the unique text in the browser string for the tablet...this could get long! It also ignores windows mobile windows|[SCH^1800]
		// other tablets
        var tabletBrowserPattern = / Windows CE|9300|9330/ig;

        var tabletBrowswerRegex = new RegExp(tabletBrowserPattern);
				
        if (mobileBrowserRegex.test(agent) && !agent.match(tabletBrowserPattern))    
        {			
            return true;
			
        } else {
		
			return false;
			
		}

		

        // bunch of mobile useragent strings

        var userAgents = ["acs-","alav","alca","amoi",

            "audi","aste","avan","benq",

            "bird","blac","blaz","brew",

            "cell","cldc","cmd-","dang",

            "doco","ef81","eric","hipt","inno",

            "ipaq","java","jigs","kddi",

            "keji","leno","lg-c","lg-d",

            "lg-g","lge-","maui","maxo",

            "midp","mits","mmef","mobi",

            "mot-","moto","mwbp","nec-",

            "newt","noki","opwv","palm",

            "pana","pant","pdxg","phil",

            "play","pluc","port","pre:","prox",

            "qtek","qwap","sage","sams",

            "sany","sec-","send",

            "seri","sgh-","shar","sie-",

            "siem","smal","smar","sony",

            "sph-","symb","t-mo","teli",

            "tim-","tosh","tsm-","upg1",

            "upsi","vk-v","voda","w3c ",

            "wap-","wapa","wapi","wapp",

            "wapr","webc","winw","winw","wurf",

            "xda","xda-"];



        // look for the possible user agents of mobile browsers

        for (var i = 0; i < userAgents.length; i++)

        {

            if ((userAgents[i] == agent.substring(0, 3) || agent.match(/webos/gi)) && (!agent.match(tabletBrowserPattern)))  //tabletBrowserPattern   /iPad/gi

            {

                return true;

            } 

        }



        // Checks the accept header for wap.wml or wap.xhtml support

        if (accept != null)

        {

            if (accept.indexOf("text/vnd.wap.wml") != -1

                    || accept.indexOf("application/vnd.wap.xhtml+xml") != -1)

            {

                return true;

            }

        }

    }



    // otherwise, not mobile

    return false;

}

// redirect the browser to a URL if it is determined it is a mobile device

// url:     the URL to which the browser will be redirected

// timeout: the number of hours the redirect cookie will exist; if it is 0 it will be considered a session cookie

function doMobileRedirect(url, timeout, cookieVariableName)	{

	var redirect = true;



	debug('getCookie: ' + getCookie(cookieVariableName) + '\ngetQueryParam: ' + getQueryParam(cookieVariableName) + '\nisMobile: ' + isMobile())

	if (getCookie(cookieVariableName)) {

		debug("We found the '" + cookieVariableName + "' cookie, not redirecting");

		return;

    	}

   else if (getQueryParam(cookieVariableName)) {

		setCookie(cookieVariableName, "no", timeout, "/", "");

		return;

		}

	else if (isMobile()) {
		
     window.location = url;

		}

}

