
doMobileRedirect('http://m.pier1.com', 10, 'mobile');

// 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';
}

// determine whether the device is a mobile device or not
function isMobile()
{
   var agent = navigator.userAgent;
   var accept = navigator.accept;
	//var agent = 'Mozilla/5.0 (iPad; U; CPU OS 3_2 like Mac OS X; en-us) AppleWebKit/531.21.10 (KHTML, like Gecko) Version/4.0.4 Mobile/7B334b Safari/531.21.10';
	//var agent = 'Mozilla/5.0 (iPhone; U; CPU like Mac OS X; en) AppleWebKit/420+ (KHTML, like Gecko) Version/3.0 Mobile/1A543a Safari/419.3'

    // 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|iphone|android|blackberry|mini|mmp|symbian|midp|wap|pocket|psp/ig;
        // var mobileBrowserPattern = /up.browser|up.link|windows ce|iphone|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|psp/ig;
        //var mobileBrowserPattern = /up.browser|up.link|windows ce|iphone|iemobile|mini|mmp|symbian|midp|wap|phone|pocket|mobile|pda|psp/ig;

        //android|
        // /ig ce
        //psp ce
        
        var mobileBrowserRegex = new RegExp(mobileBrowserPattern);

        if (mobileBrowserRegex.test(agent) && agent.match(/iPad/gi) == null)
        {
            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) || agent.match(/webos/gi)) && (!agent.match(/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;

	
	if (document.domain != "www.pier1.ca") {
	    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;
	    }
	}
}


