/* brochure */
var Brochure = {
  init:function() {
    
    $j('.brochure-request-form').hide(); 
    Brochure.bindCountry();
    Brochure.bindNext();
  },
  
  bindNext:function() {
    // value of type is either canada-agent or international
    var brochure_form = $j('.brochure-request-form').eq(0);
    
    if (!brochure_form) {
      return; 
    }
    
    $j('#next').click(function() {
      var guest = $j('#guest').is(':checked');
      var travl = $j('#travel').is(':checked');
      
      
 
      
      var v = $j('#dropdownCountry option:selected').val();
      
      var showNext=(v != '');
      var international = false;
      
      if (!guest && !travl) return false;
      if (v == '') return false;
      
      if (v == '60' && travl == true) {
        // USA
        mainSendData('/external/www.orderrockymountainrail.com/');
        window.open('http://www.orderrockymountainrail.com/fisop/?lcpassthru=Y&lcclient=ROF','mywindow','status=1');
        showNext=false;
      } else if (v == '10' && travl == true) {
        // Canada
        $j('#type').val('canada-agent');
        
        $j('#fieldlist_custom_AddressType').show();
        $j('#fieldlist_fax').show();
        $j('#fieldlist_custom_Number_Of_Brochures_Industry').show();
        
        $j('#fieldlist_custom_Number_Of_Brochures').remove();
        
        var act = brochure_form.attr('action');
        
        $j('.brochure-request-form').get(0).setAttribute('action', act + '&type=canada-agent');
        
  
        
      } else if (v != '') {
        // hide fax
        
        
        if (v == '10' || v == '60') {
          $j('#type').val('guest');
        } else {
          $j('#type').val('international');
          international = true;
        }
        
        
        $j('#fieldlist_custom_Number_Of_Brochures_Industry').remove();
        $j('#fieldlist_custom_AddressType').hide();
        $j('#fieldlist_fax').remove();
        
        // show the appropriate state values for the country selected
        
        
        var act = brochure_form.attr('action');
        $j('.brochure-request-form').get(0).setAttribute('action', act + '&type=' + $j('#type').val());
        
        
      }
    
      if (guest == true) {
        $j('#fieldlist_custom_Agency').remove();
      }
      
      

      
      if (showNext) {
        
    
        
        $j('.brochure-request-form').eq(0).css('display','block');//, {'display':'block'});
        $j('.brochure-request-form').eq(0).show();
        
        //$j('#custom_Country_').selectedIndex = $j('#dropdownCountry').selectedIndex;
        $j("#custom_Country_").attr("selectedIndex", $j('#dropdownCountry').attr("selectedIndex"));
        
        $j('#fieldlist_custom_Country_').hide();
        $j('#custom-country-selector').hide();
      }
      
      var send_type = guest == true ? 'guest' : 'agent';
      if (international == true) send_type = 'international';
      
      mainSendData("/request_a_brochure?step=address&type=" + send_type) 
      
       
    });
    
  },
  
  
  bindCountry:function() {
    if (!$j('#dropdownCountry')) {
      console.log('hello');
      return;
    }
    
    $j('#dropdownCountry').change(function() {
      
      var countryId = $j('#dropdownCountry option:selected').val();
      
      if (countryId == '10' || countryId == '60') {
        $j('#agent-group').show();
      } else {
        $j('#agent-group').hide(); 
      }
      
      // Set the options
      if (countryProvinceOptions[countryId]) {
        
        
        
        /* RENEABLE THIS AND REWRITE */
        
        stateField = $j('#stateField option');
        stateField.remove();
        
        $j('#stateField').append($j('<option value="">--</option>'));
        
        for (i = 0; i < countryProvinceOptions[countryId].length; i++) {
          //stateField[i+1] = countryProvinceOptions[countryId][i];

          $j('#stateField').append($j('<option value="' + countryProvinceOptions[countryId][i].value + '">' + countryProvinceOptions[countryId][i].text + '</option>'));
          
        }
        
        
        
        
        $j('#fieldlist_state').show();
      }
      else {
        stateField = $j('#stateField option');
        stateField.remove();
        
        $j('#stateField').append($j('<option value="0" selected="selected">N/A</option>'));
        
        
        $j('#fieldlist_state').hide();
        
      }
      
      $j('select#stateField').selectmenu({transferClasses: true, width: 200});
      
    });
  }
};

$j(document).ready(function() {
    Brochure.init(); 
});



