﻿var deviceIphone = "iphone";
var deviceIpod = "ipod";

//Initialize our user agent string to lower case.
var uagent = navigator.userAgent.toLowerCase();

//**************************
// Detects if the current device is an iPhone.
function DetectIphone()
{
   if (uagent.search(deviceIphone) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPod Touch.
function DetectIpod()
{
   if (uagent.search(deviceIpod) > -1)
      return true;
   else
      return false;
}

//**************************
// Detects if the current device is an iPhone or iPod Touch.
function DetectIphoneOrIpod()
{
    if (DetectIphone())
       return true;
    else if (DetectIpod())
       return true;
    else
       return false;
}

function Init()
{
    var s = navigator.id;
    return uagent;
}

var systemServiceObj = null; // Called from the onload event handler to initialize the widget.

function Initialize() 
{
	try
	{ 
	    systemServiceObj = device.getServiceObject("Service.SysInfo", "ISysInfo");   
    }
    catch (ex) 	
    { 
        alert("System Service object cannot be found.");
        return;    
    } 
    
    getIMEI();
}
    
function getIMEI()
{    
     // Initialize the criteria for the service object and obtain the    // information    
     var criteria = new Object();
     criteria.Entity = "Device";
     criteria.Key = "IMEI";    
     
     try 
     {
         var result = systemServiceObj.ISysInfo.GetInfo(criteria);   
     }
     catch (ex)
     {
         alert(ex);
         return;
     } 
     
     alert("IMEI: " + result.ReturnValue.StringData);
 }

function IsValidIMEI (s) 
{
    var etal = /^[0-9]{15}$/;
    if (!etal.test(s))
        return false;
  
    sum = 0; mul = 2; l = 14;
    for (i = 0; i < l; i++) 
    {
        digit = s.substring(l-i-1,l-i);
        tp = parseInt(digit,10)*mul;
        if (tp >= 10)
             sum += (tp % 10) +1;
        else
             sum += tp;
        if (mul == 1)
             mul++;
        else
             mul--;
    }
    
    chk = ((10 - (sum % 10)) % 10);
    if (chk != parseInt(s.substring(14,15),10))
        return false;
        
  return true;
}

