var Fields = {};
Fields = {
  all: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state_us', '#contact_state_uk', '#contact_state_canada', '#contact_product_interests', '#contact_product', '#contact_problem_subject', '#contact_problem_desc', '#contact_publication', '#contact_inquiry', '#contact_feedback', '#contact_admin_header', '#contact_tech_header', '#contact_tech_divider', '#contact_tech_first_name', '#contact_tech_last_name', '#contact_tech_email', '#contact_tech_phone', '#contact_tech_title_type', '#contact_tech_title', '#contact_production_server_url', '#contact_test_server_url', '#contact_cms', '#contact_cms_version', '#contact_comments', '#contact_how_did_you_hear', '#contact_address', '#contact_license', '#contact_textbook_title', '#contact_textbook_edition', '#contact_textbook_author', '#contact_productsservices_note', '#contact_techsupport_note', '#contact_productfeedback_note', '#contact_professionalservices_note', '#contact_website_note', '#contact_submit_button'],
  // products_and_services: ['#contact_productsservices_note', '#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_product_interests', '#contact_inquiry', '#contact_submit_button'],
  pricing_inquiry: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_product_interests', '#contact_inquiry', '#contact_submit_button'],
  // general_inquiries: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_institution_name', '#contact_inquiry', '#contact_submit_button'],
  technical_support: ['#contact_techsupport_note', '#contact_first_name', '#contact_last_name', '#contact_email', '#contact_institution_name',  '#contact_product', '#contact_problem_subject', '#contact_problem_desc', '#contact_submit_button'],
  product_feedback: ['#contact_productfeedback_note', '#contact_first_name', '#contact_last_name', '#contact_email', '#contact_institution_name', '#contact_product',  '#contact_feedback', '#contact_submit_button'],
  partner_information: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_inquiry', '#contact_submit_button'],
  investor_and_press: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_publication', '#contact_inquiry', '#contact_submit_button'],
  professional_services: ['#contact_professionalservices_note', '#contact_first_name', '#contact_last_name', '#contact_email', '#contact_institution_name', '#contact_inquiry', '#contact_submit_button'],
  subscribe_to_newsletter: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_product_interests', '#contact_inquiry', '#contact_submit_button'],
  website_feedback: ['#contact_website_note', '#contact_first_name', '#contact_last_name', '#contact_email', '#contact_institution_name', '#contact_feedback', '#contact_submit_button'],
  request_demo_call: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_product_interests', '#contact_inquiry', '#contact_submit_button'],
  get_pronto: ['#contact_first_name', '#contact_last_name', '#contact_email', '#contact_phone', '#contact_institution_name', '#contact_institution_type', '#contact_district', '#contact_title_type', '#contact_title', '#contact_country', '#contact_state', '#contact_admin_header', '#contact_tech_header', '#contact_tech_divider', '#contact_tech_first_name', '#contact_tech_last_name', '#contact_tech_email', '#contact_tech_phone', '#contact_tech_title_type', '#contact_tech_title', '#contact_production_server_url', '#contact_test_server_url', '#contact_cms', '#contact_cms_version', '#contact_comments', '#contact_how_did_you_hear', '#contact_submit_button'],
  
  prepare: function(str) {
    return Fields[str].join(', ');
  },
  lis: function(str) {
    return $(Fields.prepare(str)).parents('li');
  },
  show: function(str) {
    Fields.lis(str).show();
  },
  hide: function(str) {
    Fields.lis(str).hide();
  },
  
  check_dependencies: function() {
    // the two things we gotta check
    var k_twelve = ($('#contact_institution_type').val() == 'k12') && ($('#contact_institution_type').parents('li').css('display') != 'none');
    
    var usa = ($('#contact_country').val() == 'United States') && ($('#contact_country').parents('li').css('display') != 'none');
    var uk = ($('#contact_country').val() == 'United Kingdom') && ($('#contact_country').parents('li').css('display') != 'none');
    var canada = ($('#contact_country').val() == 'Canada') && ($('#contact_country').parents('li').css('display') != 'none');
    
    var diploma = ($('#contact_product').val() == 'Diploma') && ($('#contact_type').val() == 'technical_support') && ($('#contact_product').parents('li').css('display') != 'none');
    
    if (k_twelve) {
      $('#contact_district').parents('li').show();
    } else {
      $('#contact_district').parents('li').hide();
    }
    
    if (usa) {
      $('#contact_state_us').parents('li').show();
    } else {
      $('#contact_state_us').parents('li').hide();
    }
    
    if (uk) {
      $('#contact_state_uk').parents('li').show();
    } else {
      $('#contact_state_uk').parents('li').hide();
    }
    
    if (canada) {
      $('#contact_state_canada').parents('li').show();
    } else {
      $('#contact_state_canada').parents('li').hide();
    }
    
    if (diploma) {
      $('#contact_textbook_title, #contact_textbook_edition, #contact_textbook_author').parents('li').show();
    } else {
      $('#contact_textbook_title, #contact_textbook_edition, #contact_textbook_author').parents('li').hide();
    }
    
  }
};

$(function() {

  // build a select drop down
  var select_tag = " \
<li id=\"select_type\"> \
  <label class=\"desc\" for=\"contact_type\">Area of Interest</label> \
  <div class=\"select wide\"> \
    <select name=\"contact[type]\" id=\"contact_type\" size=\"1\"> \
      <!-- <option value=\"general_inquiries\">General Inquiries</option> --> \
      <!-- <option value=\"products_and_services\">Products and Services</option> --> \
	  <option value=\"\" selected>--choose one to get started--</option> \
	  <option value=\"pricing_inquiry\">Pricing Inquiry</option> \
      <option value=\"product_feedback\">Product Feedback</option> \
      <option value=\"partner_information\">Partnership Inquiries</option> \
      <option value=\"investor_and_press\">Investor Relations & Press Inquiries</option> \
      <option value=\"professional_services\">Implementation & Professional Services</option> \
      <option value=\"subscribe_to_newsletter\">Subscribe to Newsletter</option> \
      <option value=\"request_demo_call\">Request Private Demo/Call </option> \
    </select> \
  </div> \
</li> \
";

  // wire up the institution type and country for dependencies
  $('#contact_institution_type, #contact_country, #contact_product').change(function(e) {
    Fields.check_dependencies();
  })
      
    // insert the select into the top of the ul
    $('#contact-form-list').prepend(select_tag);

    // wire it up
    $('#contact_type').change(function(e) {

		if(this.value == "pricing_inquiry" || this.value == "subscribe_to_newsletter"  || this.value == "request_demo_call") window.location = "http://info.blackboardcollaborate.com/InfoRequest.html?s=Wimba";
		else
		{
			Fields.hide('all');
			Fields.show(this.value);
			Fields.check_dependencies();
     	}

    });
    
    var possible_starter_value = $('#type_hidden').val(); // check for a type being set from the params

    if (possible_starter_value != '') {
      $('#contact_type').val(possible_starter_value); // use it if it's there
    }
      
  // on form submit, remove all fields that are not showing
  $('#form-container form.reg').submit(function() {
    // NOTE: does this unregister the listeners on the country and institution type drop downs?
    $('#contact-form-list li').each(function() {
      if ($(this).css('display') == 'none') { $(this).remove(); }
    });
    
    return true;
  });
  
  // hide all, then show current selected option
  Fields.hide('all');
  Fields.show($('#contact_type').val());
  Fields.check_dependencies();
  
});
