Event.observe(window, 'load', function() {
  Event.observe('shipping_method', 'change', selectFreightType);
  Event.observe('tracking_form', 'submit', validateForm)
});

function selectFreightType(){
	switch($F('shipping_method')){
		case 'air':
			showAirOptions();
			break;
		case 'ground':
			showGroundOptions();
			break;
		case 'ocean':
			showOceanOptions();
			break;
	}
}
  
function showAirOptions(){
	$('air_tracking_number').show();
	$('ground_tracking_number').hide();
	$('ocean_tracking_number').hide();
}

function showGroundOptions(){
	$('air_tracking_number').hide();
	$('ground_tracking_number').show();
	$('ocean_tracking_number').hide();
}

function showOceanOptions(){
	$('air_tracking_number').hide();
	$('ground_tracking_number').hide();
	$('ocean_tracking_number').show();
}

function validateForm(event){
  var errors = [];
  switch($F('shipping_method')){
    case 'air':
      if($F('air_way_bill').length == 0){
        errors.push("Air Way Bill can't be blank.");
      }
      break;
      
    case 'ground':
      if($F('pro_number').length == 0){
        errors.push("Pro # can't be blank.");
      }
      break;
      
    case 'ocean':
      if($F('bill_of_lading').length == 0){
        errors.push("Bill of lading can't be blank.");
      }
      break;
  }

  if(errors.size() != 0){
    var message = "";
    errors.each(function(e){
      message += e + "\n";
    });
    alert(message);
    Event.stop(event);
  } 
}
