﻿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 == 'string' && preselected_operator.length) { // prepop operator
      $('#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 == 'function') {
        if (short_code_specific_operator_handler(operator_code) == false) {
          switch_div = false;
        }
      }

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

  /**
   * @param string operator_code
   */
  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
  },

  /**
   * @param string operator_code
   */
  showSpecificText: function(operator_code) {
    if (! operator_code) return;

    /**
     * Indicator of operator
     */
    $('#docwrapper').addClass(Landing.getClassForOperator(operator_code));

    if (typeof this.subtitle1[operator_code] == '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 (typeof this.pricing1[operator_code] == 'string') { // check if it exists
      $('#pricing1').html(this.pricing1[operator_code]); // this may be empty
    }
    else {
      $('#pricing1').html(this.pricing1['default']); // use default one
    }

    if (typeof sc_telco_txt_decorator == 'function') {
      sc_telco_txt_decorator(operator_code);
    }
  }
};

