function setCookie(c_name,value,exdays){
	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;+"path=/";
}
 
function getCookie(c_name){
	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(){
	var username=getCookie("fullSite");
	if (username!=null && username!=""){
		return 1;
	}else{
			return 0;
		}
}

function runRedirect(){

//jQuery(document).ready(function($){

        var answer = checkCookie();  
        var full = window.location.host;
		var parts = full.split('.'); 
		var sub = parts[0]; 	// subdomain
		var domain = parts[1];	// domain
		var ext = parts[2]; 	//  extension 
		var deviceAgent = navigator.userAgent.toLowerCase();
		var agentID = deviceAgent.match(/(iphone|android|blackberry|BlackBerry)/);
        var url = new String(window.location);
        var onMobile = url.indexOf("m.adt");

if(full == 'www.adt.co.uk'){var reDirectUrl = "http://m.adt.co.uk/"; var oldUrl = "http://www.adt.co.uk";}
if(full == 'www.adt.co.za'){var reDirectUrl = "http://m.adt.co.za/"; var oldUrl = "http://www.adt.co.za";}
if(full == 'www.adt.eu'){var reDirectUrl = "http://m.adt.eu/"; var oldUrl = "http://www.adt.eu";}
if(full == 'm.adt.co.uk'){var reDirectUrl = "http://m.adt.co.uk/"; var oldUrl = "http://www.adt.co.uk";}
if(full == 'm.adt.co.za'){var reDirectUrl = "http://m.adt.co.za/"; var oldUrl = "http://www.adt.co.za";}
if(full == 'm.adt.eu'){var reDirectUrl = "http://m.adt.eu/"; var oldUrl = "http://www.adt.eu";}

if(onMobile > 0){
	  //- <![CDATA[
		  var Tag1 = document.createElement('meta');
		  Tag1.name = "viewport";Tag1.content = "width = device-width, user-scalable=no minimum-scale=1.0 maximum-scale=1.0";
		  var Tag2 = document.createElement('meta'); Tag2.name = "apple-touch-fullscreen"; Tag2.content = "YES";
		  document.getElementsByTagName('head')[0].appendChild(Tag1); document.getElementsByTagName('head')[0].appendChild(Tag2);
	  //- ]]>
}
					

	if(agentID){
		if(document.referrer != reDirectUrl){
			if(full != reDirectUrl){
				setCookie('fullSite','yes',0.02);
				if(answer != 1){
                    if(onMobile < 0){
                       window.location = reDirectUrl;
                    }
				} 
			}  
		}
	}else{

	if((window.location.host != "www.adt.co.uk") && (window.location.host != "www.adt.co.za")){

         window.location = oldUrl;

        }}

}

//});
