﻿var operator_selector = {

  subtitle1: {},
  pricing1: {},

  init: function(subtitle1_json, pricing_text1_json, preselected_operator) {
    this.subtitle1 = subtitle1_json;
    this.pricing1 = pricing_text1_json;

    if (!this.subtitle1['default']) this.subtitle1['default'] = '';
    if (!this.pricing1['default'])  this.pricing1['default'] = '';

    if (typeof preselected_operator != 'underfined') { // prepop operator
      preselected_operator += ''; // cast to string
      $('#operatorlist').val(preselected_operator);
      //alert (preselected_operator);
      operator_selector.handle();
    }

    if (Get_Cookie('retry') == 'telco') {
      alert("It looks like the number you submitted doesn't match the carrier you selected.");
      Set_Cookie('retry', null);
    }
  },

  handle: function() {
    try {
      var operatorlist = $('#operatorlist');
      if (operatorlist.attr('selectedIndex') == 0) return;

      var operator_code = operatorlist.val();
      this.showSpecificText(operator_code);
      
      var switch_div = true;

      if ((typeof short_code_specific_operator_handler).toLowerCase() == 'function') {
        if (false == short_code_specific_operator_handler(operator_code)) {
          switch_div = false;
        }
      }

      if (switch_div) {
        $('#inputzone_p1').hide();
        $('#inputzone_p2').show();
        fix_checkbox_message_box_position(); // a global function
      }
    }
    catch (e) {
    }
  },

  handleGridSelect: function(operator_code) {
    if (operator_code === null) {
      $('#operator_hidden').attr('disabled', 'disabled');
    }
    else {
      $('#operator_hidden').val(operator_code);
      this.showSpecificText(operator_code);
    }

    $('#inputzone_p1').hide();
    $('#inputzone_p2').show();
    fix_checkbox_message_box_position(); // a global function
  },

  showSpecificText: function(operator_code) {
    if ($('#subtitle1').length > 0) {
      if ((typeof this.subtitle1[operator_code]).toLowerCase() == 'string') { // check if it exists
        $('#subtitle1').html(this.subtitle1[operator_code]); // this may be empty
      }
      else {
        $('#subtitle1').html(this.subtitle1['default']); // use default one
      }
    }

    if ($('#pricing1').length > 0) {
      if ((typeof this.pricing1[operator_code]).toLowerCase() == 'string') { // check if it exists
        $('#pricing1').html(this.pricing1[operator_code]); // this may be empty
      }
      else {
        $('#pricing1').html(this.pricing1['default']); // use default one
      }
    }
  }
};
