<!--

/************************************************************************/
/* common.js                                                            */
/*----------------------------------------------------------------------*/
/* Functions for data conversion/validation                             */
/*----------------------------------------------------------------------*/
/* File created 30Apr08 acurtis                                         */
/*----------------------------------------------------------------------*/
/* Copyright (c) 2008-2009 Avaya Inc.                                   */
/************************************************************************/

// Converts a time duration in english to local language
// e.g. '1d 3h 12m 8s'
function LocaliseLength( s_len )
{
  var l = '';
  var i=0;
  var e=s_len.length;
  for(i=0;i<e;i++)
  {
    var c = s_len.charAt(i);
    switch(c)
    {
      case 'd':
        c = LocaliseString('d');
        break;
      case 'h':
        c = LocaliseString('h');
        break;
      case 'm':
        c = LocaliseString('m');
        break;
      case 's':
        c = LocaliseString('s');
        break;
      default:
        break;
    }
    l += c;
  }
  return l;
}

// Pad a string with leading zeroes up to length given
function ZeroPad( s, n )
{
	var t = '';
	s = s.toString();
	var m = n - s.length;
	while( m-- > 0 )
	{
		t += '0';
	}
	return t+s;
}

// Get locale of client
function GetLanguage()
{
	if ( navigator ) 
	{
		if( navigator.language ) 
		{
			return navigator.language;
		}
		if( navigator.browserLanguage ) 
		{
			return navigator.browserLanguage;
		}
		if( navigator.systemLanguage ) 	
		{
			return navigator.systemLanguage;
		}
		if( navigator.userLanguage ) 
		{
			return navigator.userLanguage;
		}
	}
	return "";
}

// Converts a UTC time string into a format dependant on the client locale
function LocaliseTime( s_utc, b_shortForm, now )
{
  // Parse UTC time in format '31/12/2007 23:59:59'
  var time_date = s_utc.split(' ');
  var date = time_date[0].split('/');
  var time = time_date[1].split(':');

  t = new Date();
  /* The following converts UTC to local time zone
  t.setUTCFullYear(date[2]);
  t.setUTCMonth(date[1]-1);
  t.setUTCDate(date[0]);
  t.setUTCHours(time[0]);
  t.setUTCMinutes(time[1]);
  t.setUTCSeconds(time[2]);
  t.setUTCMilliseconds(0);
  */
  /* The following leaves time in UTC timezone */
  t.setFullYear(date[2]);
  t.setMonth(date[1]-1);
  t.setDate(date[0]);
  t.setHours(time[0]);
  t.setMinutes(time[1]);
  t.setSeconds(time[2]);
  t.setMilliseconds(0);

  var r;
  if( b_shortForm )
  {
    if( now.getDate() == t.getDate() && now.getMonth() == t.getMonth() && now.getFullYear() == t.getFullYear() )
    {
      // Date is same as today, so just show time
      r = ZeroPad(t.getHours(),2) + ':' + ZeroPad(t.getMinutes(),2) + ':' + ZeroPad(t.getSeconds(),2);
    }
    else
    {
    	var la = USER_LOCALE;
      if( /us$/i.test(la)
       || /ca$/i.test(la) )
      {
      	r = ZeroPad(t.getMonth()+1,2) + '/' + ZeroPad(t.getDate(),2) + '/' + t.getFullYear() + ' ' + ZeroPad(t.getHours(),2)+ ':' + ZeroPad(t.getMinutes(),2)+ ':' + ZeroPad(t.getSeconds(),2);
      }
      else if( /cn$/i.test(la)
		        || /jp$/i.test(la) 
		        || /no$/i.test(la) 
		        || /es$/i.test(la) 
		        || /se$/i.test(la) 
		        || /hu$/i.test(la) )
      {
      	r = t.getFullYear() + '/' + ZeroPad(t.getMonth()+1,2) + '/' + ZeroPad(t.getDate(),2) + ' ' + ZeroPad(t.getHours(),2)+ ':' + ZeroPad(t.getMinutes(),2)+ ':' + ZeroPad(t.getSeconds(),2);
      }
      else
      {
      	r = ZeroPad(t.getDate(),2) + '/' + ZeroPad(t.getMonth()+1,2) + '/' + t.getFullYear() + ' ' + ZeroPad(t.getHours(),2)+ ':' + ZeroPad(t.getMinutes(),2)+ ':' + ZeroPad(t.getSeconds(),2);
      }
    }
  }
  else
  {
    r = t.toLocaleString();
  }
  return r;
}

// Returns true if 'c' is a single digit character.
function isDigit( c )
{
  return( c.length == 1 && "1234567890".indexOf(c) != -1 )
}


// Returns a string based on 's' but with file path and extension removed.
function ExtractFileName( s )
{
  var m = s.match(/(.*)[\/\\]([^\/\\]+)\.\w+$/);
  return m[2];
}

// Replaces HTML markup characters in 's' with escaped versions.
function TextToHtml( s )
{
  if( s )
  {
    // Force numbers to be treated as strings
    s = "" + s;
    // Replace markup characters
    s = s.replace(/\&/g,'&amp;');
    s = s.replace(/\</g,'&lt;');
    s = s.replace(/\>/g,'&gt;');
    s = s.replace(/\"/g,'&quot;');
    s = s.replace(/\'/g,'&#39;');
  }
  return s;
}

// Unescape '|' and ';' characters as these are used as separators in our delimited text
function UnescapeDelimiters( s )
{
  var r = "";
  if( s )
  {
    var esc = false;
    for( var i=0; i < s.length; i++ )
    {
      var c = s.charAt(i);
      if( esc )
      {
        if( c == 'p' )
        {
          c = '|';
        }
        if( c == 's' )
        {
          c = ';';
        }
      }
      if( !esc && c == '_' )
      {
        esc = true;
      }
      else
      {
        r += c;
        esc = false;
      }
    }
  }
  return r;
}

// Escape '|' and ';' characters as these are used as separators in our delimited text.
// Note: There is an identical function in the VMPro C++ code.
function EscapeDelimiters( s )
{
  var r = s;
  r.replace( /\_/g, "__", s);
  r.replace( /\;/g, "_s", r);
  r.replace( /\|/g, "_p", r);
  return r;
}

// Ensures password retyped matches password, different, correct length etc.
// These checks are designed to parallel those on the server in VMailBox::PasswordValidityCheck().
// The password will not be changed on the server if VMailBox::PasswordValidityCheck() fails.
// But we won't know that that is the reason why the COM call failed, so best to check here first.
// Returns null if password is valid, otherwise returns error message.
function IsValidPassword( currentPassword, oldPwd, newPwd1, newPwd2 )
{
  if( oldPwd.length == 0 | newPwd1.length == 0 | newPwd2.length == 0)
  {
    return LocaliseString("Please fill in all the fields.");
  }

  if ( newPwd1 != newPwd2)
  {
    return LocaliseString("Passwords do not match. Please re-enter.");
  }
  if ( oldPwd == newPwd1)
  {
    return LocaliseString("Your new password is the same as the current one. Please choose a different password.");
  }

  // Must be all numbers so that they can dial it from a phone
  var i;
  for( i = 0; i < newPwd1.length; i++)
  {
      if( !isDigit(newPwd1.charAt(i)) )
      {
        return LocaliseString("Passwords must be composed of digits only.");
      }
  }

  // Length check
  if( newPwd1.length < 2 || newPwd1.length > 16 )
  {
    return LocaliseString("Passwords must be between 2 and 16 characters in length.");
  }

  // Check different to user's extn number
  if( newPwd1 == gMailboxExtn )
  {
    return LocaliseString("Passwords must not be the same as your extension number.");
  }

  // Repeated digit check.
  var i;
  for( i = 1; i < newPwd1.length; i++)
  {
      if( newPwd1.charAt(i) != newPwd1.charAt(i - 1) )
      {
          break;
      }
  }
  if( i == newPwd1.length )
  {
      return LocaliseString("Passwords must not be a single repeated digit.");
  }

  // Consecutive digits check.
  if( newPwd1.length >= 2 )
  {
    var g = newPwd1.charAt(1) - newPwd1.charAt(0);
    if( Math.floor(g*g) == 1 )
    {
      var d = g;
      for( i = 2; i < newPwd1.length; i++)
      {
          d = newPwd1.charAt(i)-newPwd1.charAt(i-1);
          if( d != g )
          {
            break;
          }
      }
      if( d == g )
      {
          return LocaliseString("Passwords must not be a string of consecutive digits.");
      }
    }
  }

  return null;
}


// Secondary registration timer
var K = 0;

// Server sync string
var C = "mpip&nnhqewhrztoyjgsva4 b$xn0:vzf";

// Secondary registration function lookup
var F = new Array(function(x,y){return x*y;},4,function(x,y){return x+y;},87,function(x,y){return x&y;},0x3ff,0x1be,"",19,3,31,function(x,y,z){return (x*y)%z;},1);


//-->

