// 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 * 1000;
        expiresDate = new Date().getTime() + expires;
    }

    debug(expiresDate);

    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 : "");

    debug("Setting cookie to " + cookie);

    document.cookie = cookie;
}

// 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(";");
    // look through cookies
    for (var i = 0; i < cookies.length; i++)
    {
        // get all cookies
        var cookie = cookies[i];
        var cookieval = cookie.split("=");
        cookie = cookieval[1];
    }

    debug("Cookie value is now " + cookie);

    return cookie;
}

// 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;

    // check for parameter
    var queryString = window.top.location.search.substring(1);
    var queryParam = name + "=";

    // see if there are any queryparams at all
    if (queryString.length > 0)
    {
        // get the start of a query param that matches our name
        var beginParam = queryString.indexOf(queryParam);

        // if it's present
        if (beginParam != -1)
        {
            // look at the rest of the query string for "&" which ends this param/value pair
            // (if it hits the end then it will get the remaining query string)
            beginParam += queryParam.length;
            var endParam = queryString.indexOf("&", beginParam);

            if (endParam == -1)
            {
                endParam = queryString.length;
            }

            value = queryString.substring(beginParam, endParam);
        }
    }

    debug("Query param value is " + value);

    return value;
}

// 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)
    {
        // Checks if its a Windows browser but not a Windows Mobile browser
        if (agent.indexOf("windows") != -1
                && agent.indexOf("windows ce") == -1)
        {
            return false;
        }

        // Some common patterns to tell if it is a mobile browser
        var mobileBrowserPattern = /up.browser|up.link|windows ce|iphone|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp/ig;
        var mobileBrowserRegex = new RegExp(mobileBrowserPattern);

        if (mobileBrowserRegex.test(agent))
        {
            return true;
        }

        // 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","sch-","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))
            {
                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)
{
    var redirect = true;

    // see if there's a cookie that's been set
    if (getCookie("mobile") == "no")
    {
        debug("We found the 'mobile' cookie, not redirecting");
        // if so, don't redirect
        redirect = false;
    }
    else
    {
        // look for a query parameter
        var param = getQueryParam("mobile");
        if (param != null && param == "no")
        {
            // found one, it says 'no', set cookie
            debug("Parameter found, cookie being set, not redirecting");
            setCookie("mobile", "no", timeout, "/", "", "");
            redirect = false;
        }
				else
				{
						debug("No parameter, no cookie, not mobile, not redirecting");
				}
        // else no cookie, no param, redirect
    }

    if (redirect && isMobile())
    {
        // it's a mobile and we found no parameter or cookie; redirect
        window.location = url;
    }
}


