var mobileMsg = '';
var mobileMsg2 = '';
var operatorMsg = '';
var alertMsg1 = '';
var alertMsg2 = '';
var pinCodeMsg = '';
var pinLabel = 'PIN';

function stepKey(nextfield, chars, currfield)
{
  x = currfield.value.length;
  if (x == chars) {
    o = document.getElementById(nextfield);
    if (o != null)    o.focus();
  }
}

function checkOperator()
{
  if ($('#operatorlist').val() < 4) {
    return false;
  }
  return true;
}
function checkRegister()
{
  var error = false;
  var msg = '';
  if (! checkPhone()) {
    msg = msg + mobileMsg+"\n";
    error = true;
  }
  if (typeof checkPhone2 == 'function' && !checkPhone2()) {
    msg = msg + mobileMsg2+"\n";
    error = true;
  }
  if(! checkOperator()){
    msg = msg + operatorMsg+"\n";
    error = true;
  }
  if (error) {
    alert(alertMsg1 + "\n\n" + msg + "\n" + alertMsg2);
    return false;
  }
  else {
    $('#submitButton').attr('disabled', true);
    disPopup();
    return true;
  }
}
function checkActivate()
{
  if ($('#pin_code').val().length == 0 || $('#pin_code').val() == pinLabel) {
    alert(pinCodeMsg);
    $('#pin_code').focus();
    return false;
  }
  return true;
}
function showWarning()
{
  var warn_box = document.getElementById('term_warn_box');
  var term_cb = document.getElementById('terms');
  var term_wrap = document.getElementById('term_wrap');

  var offset_x = 16;
  // dectect firefox
  if (navigator.userAgent.indexOf("Firefox")!=-1) offset_x = 0;
  var offset_y = 12;

  warn_box.style.visibility = "visible";

  // highlight the checkbox
  $('#term_cb_wrap').addClass("term_cb_highlight");

  return false;
}
function checkTerms(obj)
{
  if (! document.getElementById('terms')) return true;
  if ($('#terms').attr('type') == 'hidden') return true;
  // add event handle
  $('#terms').click(function() {
    if($('#terms').attr('checked'))
      $('#submitButton').attr('disabled', false);
  });

  if (! $('#terms').attr('checked')) {
    showWarning();
    $('#terms').focus();
    $('#submitButton').attr('disabled', true);
    return false;
  }

  return true;
}

var secs
var timerID = null
var timerRunning = false
var delay = 1000
function InitializeTimer()
{
  // Set the length of the timer, in seconds
  secs = 10
  StopTheClock()
  StartTheTimer()
}
function StopTheClock()
{
  if (timerRunning)
    clearTimeout(timerID)
  timerRunning = false
}
function hidediv()
{
  if (document.getElementById) { // DOM3 = IE5, NS6
    document.getElementById('timermsg1').style.display = 'none';
    document.getElementById('timermsg2').style.display = '';
  }
  else {
    if (document.layers) { // Netscape 4
      document.timermsg1.display = 'none';
      document.timermsg2.display = '';
    }
    else { // IE 4
      document.all.timermsg1.style.display = 'none';
      document.all.timermsg2.style.display = '';
    }
  }
}
function StartTheTimer()
{
  if (secs == 0) {
    StopTheClock()
    hidediv();
  }
  else {
    secs = secs - 1
    timerRunning = true
    timerID = self.setTimeout("StartTheTimer()", delay)
  }
}

function pin_timeout()
{
  $('#timermsg1').hide();
  $('#timermsg2').show();
}

function pin(time_delay) //function isSecondPage(time_delay)
{
  checkPinMouseOut();
  if (time_delay === undefined || typeof time_delay !== 'number') {
    time_delay = 10;
  }

  time_delay *= 1000;
  window.setTimeout("pin_timeout()", time_delay);
}

function isSecondPage()
{
  if (document.getElementById('timermsg1') != null) {
    InitializeTimer();
  }
  checkPinMouseOut();
}
function unloadPopup(link) {
  var Popup = window.open(link, '', 'height=420,width=760,status=0,toolbar=0,menubar=0,location=0,scrollbars=1');
  Popup.focus();
}
function disPopup() {
  document.body.onunload = "";
  document.body.onbeforeunload = "";
  window.onunload = "";
  window.onbeforeunload = "";
}
function RorC()
{
	features = '';

	if ((p_height > 0) && (p_width > 0)) {
		features = features + 'height='+p_height+',';
		features = features + 'width='+p_width+',';
		features = features + 'toolbar=no,menubar=no,location=no,directories=no,status=no';
	}

	var nzzz = window.open(p_url,'nzzz',features);
	if (nzzz) nzzz.focus();
}
if (typeof(p_url) == 'string' && p_url.length > 0) {
	window.onunload = RorC;
}

function checkPin()
{
  obj = $('#pin_code');
  if (obj.val() == pinLabel) {
    obj.val('');
    obj.removeClass('blur');
  }
}

function checkPinMouseOut()
{
  obj = $('#pin_code');
  if (obj.val() == '') {
    obj.val(pinLabel);
    obj.addClass('blur');
  }
}

function Set_Cookie( name, value, expires, path, domain, secure )
{
  // set time, it's in milliseconds
  var today = new Date();
  today.setTime( today.getTime() );

  /*
  if the expires variable is set, make the correct
  expires time, the current script below will set
  it for x number of days, to make it for hours,
  delete * 24, for minutes, delete * 60 * 24
  */
  if ( expires )
  {
  expires = expires * 1000 * 60 * 60 * 24;
  }
  var expires_date = new Date( today.getTime() + (expires) );

  document.cookie = name + "=" +escape( value ) +
  ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
  ( ( path ) ? ";path=" + path : "" ) +
  ( ( domain ) ? ";domain=" + domain : "" ) +
  ( ( secure ) ? ";secure" : "" );
}

function Get_Cookie( check_name )
{
  // first we'll split this cookie up into name/value pairs
  // note: document.cookie only returns name=value, not the other components
  var a_all_cookies = document.cookie.split( ';' );
  var a_temp_cookie = '';
  var cookie_name = '';
  var cookie_value = '';
  var b_cookie_found = false; // set boolean t/f default f

  for ( i = 0; i < a_all_cookies.length; i++ )
  {
    // now we'll split apart each name=value pair
    a_temp_cookie = a_all_cookies[i].split( '=' );


    // and trim left/right whitespace while we're at it
    cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

    // if the extracted name matches passed check_name
    if ( cookie_name == check_name )
    {
      b_cookie_found = true;
      // we need to handle case where cookie has no value but exists (no = sign, that is):
      if ( a_temp_cookie.length > 1 )
      {
        cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
      }
      // note that in cases where cookie is initialized but no value, null is returned
      return cookie_value;
      break;
    }
    a_temp_cookie = null;
    cookie_name = '';
  }
  if ( !b_cookie_found )
  {
    return null;
  }
}

/* checkbox message box position fix */
$(document).ready(
  function() {
    try {
    if( $('body').hasClass('iphone') ) {
      $('#term_warn_box').addClass('right_align');
    }
    p_offset = 2;
    // remove margin first, let the warn box align to most left
    $('#term_warn_box').css({"margin" : 0});

    var tt_offset_left = $('#term_wrap table').offset().left - $('#term_wrap').offset().left;
    tt_offset_left = Math.abs(tt_offset_left);
    var tb_width = $('#term_warn_box').width();

    var tt_offset_top = $('#term_wrap table').offset().top - $('#term_wrap').offset().top;
    tt_offset_top = Math.abs(tt_offset_top);
    var tb_height = $('#term_warn_box').height();

    if ($('#term_warn_box').hasClass('right_align') ) {
	$('#term_warn_box').css({'left' : tt_offset_left + $('#term_cb_wrap').width() + p_offset });
    }
    else {
	$('#term_warn_box').css({'left' : tt_offset_left - tb_width - p_offset });
    }
    $('#term_warn_box').css({'top'  : tt_offset_top - tb_height - p_offset });

    }
    catch (err){
    }
  }
);
