var mobile_devices = new Array()
var total_array = 6;

mobile_devices[0]="iphone";
mobile_devices[1]="ipod";
mobile_devices[2]="android";
mobile_devices[3]="windows ce";
mobile_devices[4]="windows phone";
mobile_devices[5]="blackberry";

//Initialize our user agent string to lower case.
var uagent = navigator.userAgent.toLowerCase();
var mobile_value = "df";
var mobile_cookie = "dfmob";
var MOBILE_DOMAIN = "m.directferries.de"

function get_cookie(Name) {
  var search = Name + "="
  var returnvalue = "";
  if (document.cookie.length > 0) {
    offset = document.cookie.indexOf(search)
    // if cookie exists
    if (offset != -1) { 
      offset += search.length
      // set index of beginning of value
      end = document.cookie.indexOf(";", offset);
      // set index of end of cookie value
      if (end == -1) end = document.cookie.length;
      returnvalue=unescape(document.cookie.substring(offset, end))
      }
   }
  return returnvalue;
}

function save_cookie(Name) {
	document.cookie=mobile_cookie+'='+mobile_value;
}

//removes a leading occurance of "http://" and "https://"
function removeHttp(myUrl) {
	var httpRegEx = /^https?:\/\//i;
	return removeRegEx(httpRegEx, myUrl);
}

//removes the site name (directferries.xx/)
function removeSite(myUrl) {
	var siteRegEx = /^[^\/\r\n]*\//i;
	return removeRegEx(siteRegEx, myUrl);
}

//gets the queryString from the url
function getQueryString(myUrl) {
	var queryStringRegEx = /[^\?\r\n]*\?/i;
	return removeRegEx(queryStringRegEx, myUrl)
}

//takes a regex and a string. Removes any matches of the regEx from the string
function removeRegEx(regEx, stringToRemoveFrom) {
	try {
		return stringToRemoveFrom.replace(regEx, '');
	}
	catch (err) {
		//incase regex goes wrong
		return myUrl;
	}
}

function getSubDomain(pageUrl) {
	//if no arguments have been passed in, get the url the user entered
	if (pageUrl == null || pageUrl == "") {
		//get the url the user searched for
		pageUrl = document.URL;
	}
	var subDomain = removeHttp(pageUrl);
	subDomain = removeSite(subDomain);
	return subDomain;
}

//changes the location of the current page and points it to the current site with the given location
function redirect(redirectLocation) {
	if (redirectLocation.charAt(0) != '/') {
		redirectLocation = '/' + redirectLocation;
	}
	window.location = 'http://' + MOBILE_DOMAIN + redirectLocation;
}

//check if the user entered with a querystring from the mobile site
if (getQueryString(document.URL).indexOf('showDesktop=1') > -1) {
	//if so, set the cookie to always display desktop site
	save_cookie(mobile_cookie)
}

// Detects if the current device is a Mobile Device.
// and also user has not got a session cookie 
var cookie_value = get_cookie(mobile_cookie)
if (cookie_value!=mobile_value)
{
	for (var i=0;i<total_array;i++)
	{
	   if (uagent.search(mobile_devices[i]) > -1)
	   {
		//redirect them
		redirect(getSubDomain());
	   }	
	}
}

