
doMobileRedirect(10, 'mobile');

// redirect the browser to a URL if it is determined it is a mobile device
// timeout: the number of hours the redirect cookie will exist; if it is 0 it will be considered a session cookie
function doMobileRedirect(timeout, cookieVariableName)	{
	var redirect = false;

	//debug('getCookie: ' + getCookie(cookieVariableName) + '\ngetQueryParam: ' + getQueryParam(cookieVariableName) + '\nisMobile: ' + isMobile())
	if (getQueryParam(cookieVariableName)) {
		setCookie(cookieVariableName, "no", timeout, "/", "", "");
		return;
		}
}

// simple consoldated alert method for debugging
function debug(string)
{
    //alert(string);
}

// 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';
}


