/*
	Add into HEAD of your document:
	<script src="/scripts/mobile-redirect.js" type="text/javascript" language="javascript"></script>
	<script type="text/javascript">	
	checkCookie();
	window.onload = function ()
	{
		if (screen.width <= 500) { //display "view mobile site" link only to applicable devices
		  //alert('Small device! Display mobile link.');
		  document.getElementById('mobileLink').style.display="block";
		}
	} 
	</script>
	
	Add to website template (at least homepage) as follows:
	<a onClick="javascript:toMobile();" id="mobileLink" style="display:none">View mobile site</a>
	
	Add to mobile template homepage as follows:
	<a onClick="javascript:toMain();">visit full site</a>
	
	Mobile website functions for redirects:
    checkCookie() //function to check mobile cookie on main page, and redirect as necessary. Also displays "view mobile" link for small devices.
	toMobile ()  //sets mobile cookie, redirects to mobile site. Use with "view mobile site" link within main site.
	toMain ()   //sets mobile cookie, redirects to main site. Use with "view main site" link within mobile site.
*/

function setCookie(c_name,value,exdays) //sets a cookie with name, value, and number of days to expiry.
{
	//alert('setting a cookie: '+c_name+', '+value);
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
}

function getCookie(c_name) //returns the value of a specific cookie
{
	//alert('getting a cookie');
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++)
	{
		x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
		y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
		x=x.replace(/^\s+|\s+$/g,"");
		if (x==c_name)
			{ return unescape(y); }
	}
}

function checkCookie() //function to check mobile cookie on main page, and redirect as necessary.
{
	//alert ('Checking cookies.');
	var mobile=getCookie("mobile");
	//alert ('Check cookies. "'+document.cookie+'"');
	if (mobile!=null && mobile!="")
	{
		//alert('mobile cookie is set');
		switch (mobile)
		{
			case "true":
			//alert ('mobile true! redirect.');
			document.location = "/m/";
			break;	
			case "false":
			//alert ('mobile false! stay here.');
			//do nothing - stay on this page.
			break;
			case "undefined":
			case "":
			case null:
			//alert ('mobile undefined! stay here.');
			//do nothing - stay on this page.
			break;
			default:
			//alert ('mobile cookie not T or F.');
			if (mobile=='undefined') {
			  //alert ('undefined!');
			} else if (screen.width <= 500) {
			  //alert ('screen is <500px. redirect.');
			  document.location = "/m/";
			}
			break;	
		}
	} else {
		//alert('mobile cookie not set');
	}
}

function toMobile () { //sets mobile cookie, redirects to mobile site
	//alert ('redirecting to mobile site.');
	if (screen.width <= 500) {
		//set mobile to true.
		setCookie ("mobile", true, 30);
	}
	document.location = "/m/";
}

function toMain () { //sets mobile cookie, redirects to main site
	//alert ('redirecting to main site.');
	//set mobile to false.
	if (true) {
	setCookie ("mobile", false, 30);
	}
	document.location = "/";
}
