var VoxillaForm = {
  speed : 500,
  url_base : './',
  home_redirect_url : 'http://74.53.25.163/~voxilla/residential-voip',
  // Do not change the values below.
  buttons : {},
  step : 0,
  steps : 0,
  direction : 1,
  progress_timer : null,
  target_percent : 0,
  current_percent : 0,
  session_id : null
};

$(function() {
  $.get(
        VoxillaForm.url_base + 'get_id.php',
        {},
        function(data) { VoxillaForm.session_id = data; },
        'text'
      );
  
  VoxillaForm.pages = $('.voxilla_lead_step');
  VoxillaForm.steps = VoxillaForm.pages.length - 1;
  VoxillaForm.buttons.next_prev = $('#voxilla_lead_buttons img');
  VoxillaForm.buttons.finish = $('#voxilla_lead_finish_button');
  
  $('#map_prev_step').click(function() { voxilla_prev(); return false; }).attr('href', '#');
  $('#map_next_step').click(function() { voxilla_next(); return false; }).attr('href', '#');
  VoxillaForm.buttons.finish.click(function() { $('#voxilla_lead_form_element').submit(); });
  $('#voxilla_lead_form_element').submit(function() {
    if(voxilla_validate_form()) {
      voxilla_next();
    } else {
      return false;
    }
    return true;
  });
  $('#voxilla_lead_options input[type=radio]').click(voxilla_next);
  
  $('.javascript_off').hide();
  change_fields();
});


function voxilla_validate_form() {
  var vars = ['first_name', 'last_name', 'email', 'phone', 'company'];
  var blank = false, err_msg;
  
  for (var i=0; v = vars[i]; i++ ) { if (!$('#voxilla_lead_' + v).val()) { blank = true; } }
  if (blank) {
    voxilla_show_error('All fields are required.');
    return false;
  }
  
  var email_format = /^[a-z0-9\.\_\%\+\-]+@[a-z0-9\.\-]+\.[a-z]{2,4}$/i;
  if (!$('#voxilla_lead_email').val().match(email_format)) {
    voxilla_show_error('Invalid email address');
    return false;
  }
  var phone = $('#voxilla_lead_phone').val().replace(/[^0-9]+/, '');
  if((phone.substr(0,1) != 1 && phone.length != 10) || (phone.substr(0,1) == '1' && phone.length != 11)) {
    voxilla_show_error('Please enter your phone number with area code.');
    return false;
  }
  return true;
}

function voxilla_show_error(err_msg) {
  var box = $('#voxilla_lead_instructions');
  box.html(err_msg);
  box.css('color', '#cc0000');
}

function voxilla_next() {
  var val = $('#voxilla_lead_step_' + VoxillaForm.step + ' input:checked').val();
  if (!val) { return false; }
  if (VoxillaForm.step === 0 && val == 'home') {
    location.href = VoxillaForm.home_redirect_url;
    return false;
  }
  
  VoxillaForm.direction = 1;
  var prev_step = VoxillaForm.step;
  VoxillaForm.step += 1;
  if (VoxillaForm.session_id) {
    $.get(VoxillaForm.url_base + 'set_stage.php?stage=' + VoxillaForm.step + '&id=' + VoxillaForm.session_id);
  }
  if (VoxillaForm.step > VoxillaForm.steps ) { VoxillaForm.step = VoxillaForm.steps }
  if (prev_step != VoxillaForm.step ) { change_fields(prev_step); }
  return true;
}

function voxilla_prev() {
  VoxillaForm.direction = 0;
  var prev_step = VoxillaForm.step;
  VoxillaForm.step -= 1;
  if (VoxillaForm.step < 0) { VoxillaForm.step = 0; }
  if (prev_step != VoxillaForm.step ) { change_fields(prev_step); }
}

function change_fields(prev_step) {
  $('#voxilla_lead_buttons').html('');
  if (VoxillaForm.step == VoxillaForm.steps - 1) {
    VoxillaForm.buttons.finish.show();
    $('#voxilla_lead_buttons').append(VoxillaForm.buttons.finish);
  } else {
    $('#voxilla_lead_buttons').append(VoxillaForm.buttons.next_prev);
  }
  
  $(VoxillaForm.pages[VoxillaForm.step]).fadeOut('fast').show();
  var this_page = $(VoxillaForm.pages[VoxillaForm.step]);
  var instruction_text = this_page.children('.voxilla_lead_instructions').html();
  
  if (prev_step !== undefined) {
    $(VoxillaForm.pages[prev_step]).fadeOut(VoxillaForm.speed);
    $('#voxilla_lead_instructions_content').fadeTo(VoxillaForm.speed, 0.01, function() {
      var self = $(this);
      self.css('color', '#000000');
      self.html(instruction_text);
      self.fadeTo(VoxillaForm.speed, 1);
      this_page.fadeIn(VoxillaForm.speed);
    });
  } else {
    var vlic = $('#voxilla_lead_instructions_content');
    vlic.css('color', '#000000');
    vlic.html(instruction_text);
    vlic.fadeTo(VoxillaForm.speed, 1);
    this_page.fadeIn(VoxillaForm.speed);
  }
  VoxillaForm.target_percent = (VoxillaForm.step / VoxillaForm.steps) * 100;
  if (VoxillaForm.progress_timer)  { clearInterval(VoxillaForm.progress_timer); }
  VoxillaForm.progress_timer = setInterval(change_percent, 50);
}

function change_percent() {
  if (VoxillaForm.current_percent == VoxillaForm.target_percent) { clearInterval(VoxillaForm.progress_timer) }
  $('#voxilla_lead_progress_percent').html(VoxillaForm.current_percent + '%');
  $('#voxilla_lead_progress_bar').css('width', VoxillaForm.current_percent + '%');
  VoxillaForm.current_percent += VoxillaForm.direction ? 1 : -1;
  if (VoxillaForm.current_percent < 0) { VoxillaForm.current_percent = 0; }
  if (VoxillaForm.current_percent > 100) { VoxillaForm.current_percent = 100; }
}
