var deviceIphone = "iphone";
var deviceIpod = "ipod";

var deviceS60 = "series60";
var deviceSymbian = "symbian";
var engineWebKit = "webkit";

var deviceAndroid = "android";

var deviceWinMob = "windows ce";

var deviceBB = "blackberry";

var devicePalm = "palm";

//Initialize our user agent string to lower case.
var uagent = navigator.userAgent.toLowerCase();

//**************************
// Main detect program
//**************************
function DetectMobile(redirectUrl, query, urlArgument)
{
    if (((DetectIphone()) ||
       (DetectIpod()) ||
       (DetectAndroid())) &&
       (noRedirection(urlArgument) == false))
        {   if (query == 'yes') {
                var question = confirm("Would you like to view a mobile device optimized version of our site?")
                if (question) {
                    window.location = redirectUrl;
                // window.location="http://www.guesswho.mobi/pelosihome/"
                }
            } else {
                window.location = redirectUrl;
            }

        }
}

function noRedirection(urlArgument) {
   
    var urlStr = window.location.href;
    if (urlStr.length == 0) return false;
    // Split the url by the ?
    var qpartsStr = urlStr.split("?");
    if (qpartsStr.length <= 1) return false;

    var queryStr = qpartsStr[1];
    if (queryStr.length == 0) return false;

    // Split the variable by =, which splits name and value
    var partsStr = queryStr.split("=");
    if (partsStr.length <= 1) return false
    // Check if the correct variable
    if (partsStr[0] == urlArgument) {
       if (partsStr[1] == "true") return true;
    }
      

    return false;
}

//**************************
// Detects if the current device is an iPhone.
function DetectIphone()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(deviceIphone) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPod Touch.
function DetectIpod()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(deviceIpod) > -1)
      return true;
   else
      return false;
}


//**************************
// Detects if the current browser is the S60 Open Source Browser.
// Screen out older devices and the old WML browser.
function DetectS60OssBrowser()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(engineWebKit) > -1)
   {
     if ((uagent.search(deviceS60) > -1 ||
          uagent.search(deviceSymbian) > -1))
        return true;
     else
        return false;
   }
   else
      return false;
}

//**************************
// Detects if the current device is an Android OS-based device.
function DetectAndroid()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(deviceAndroid) > -1)
      return true;
   else
      return false;
}


//**************************
// Detects if the current device is an Android OS-based device and
//   the browser is based on WebKit.
/*
function DetectAndroidWebKit()
{
   if (DetectAndroid())
   {
     if (DetectWebkit())
        return true;
     else
        return false;
   }
   else
      return false;
}
*/

//**************************
// Detects if the current browser is a Windows Mobile device.
function DetectWindowsMobile()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(deviceWinMob) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current browser is a BlackBerry of some sort.
function DetectBlackBerry()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(deviceBB) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current browser is on a PalmOS device.
function DetectPalmOS()
{
   uagent = navigator.userAgent.toLowerCase();
   if (uagent.search(devicePalm) > -1)
      return true;
   else
      return false;
}

