
// 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 setCookie2(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 eraseCookie(name) {
	var date = new Date();
	date.setTime(date.getTime()+(-1*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
	document.cookie = name+"="+expires+"; path=/";
}
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 getCookie2(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 isIpad(url,timeout,fullsite,mobilesite)
{
	var agent = navigator.userAgent;
    if (agent.match(/iPad/gi) == null)
    {
    	doMobileRedirect(url,timeout,fullsite,mobilesite);
    	/*
    	debug('getCookie: ' + getCookie2(fullsite) + '\ngetQueryParam: ' + getQueryParam(fullsite))
    	debug('getCookie: ' + getCookie2(mobilesite) + '\ngetQueryParam: ' + getQueryParam(mobilesite))
    	//if(!getCookie2(fullsite) && !getCookie2(mobilesite) && !getQueryParam(fullsite) && !getQueryParam(mobilesite))
    	if(!getQueryParam(fullsite) && !getQueryParam(mobilesite))
    	{
    		debug("ipad");
    		window.location="ipadConfirm.jsp";
    		return true;
    	}
    	else
    	{
    		debug("ipad but cookie exists or param present")
    		doMobileRedirect(url,timeout,fullsite,mobilesite);
    		return false;
    	} */
    }
    
    return false;
}


// 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 = /android|BlackBerry|up.browser|up.link|windows ce|iphone|ipad|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) || agent.match(/webos/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, fullsite, mobilesite)	{
	var redirect = true;

	debug('getCookie: ' + getCookie2(fullsite) + '\ngetQueryParam: ' + getQueryParam(fullsite) + '\nisMobile: ' + isMobile())
	debug('getCookie: ' + getCookie2(mobilesite) + '\ngetQueryParam: ' + getQueryParam(mobilesite))
	if ((getCookie2(fullsite) || getQueryParam(fullsite)) && !getQueryParam(mobilesite)) {
		debug("We found the '" + fullsite + "' cookie, not redirecting");
		eraseCookie(mobilesite);
		setCookie2(fullsite, "no", timeout, "/", "", "");
		return;
	}
	else if(getCookie2(mobilesite) || getQueryParam(mobilesite)) {
		debug("We found the '" + fullsite + "' cookie redirecting");
		eraseCookie(fullsite);
		setCookie2(mobilesite, "no", timeout, "/", "", "");
		if (isMobile())
		{
			window.location = url;
		}
	}
	else if (isMobile()) {
		window.location = url;
	}
	return;
}


