Drupal.companyLookup = function() {
	$('#edit-company-lookup:not(.processed)').addClass('processed').keyup(
		function(){
			$.get("/company-state-ajax/" + this.value , function(data) {
				$("#lookup_results_foo").html(data);
				$(".inc5000-application-footer").css('clear','left');
				
				//$(".inc5000-application-footer").css('display','none');
				$('a[@href="#company-not-found"]').click(function(){
            $("#edit-is-new-company-this-is-a-new-company").click();
        });
			});


		}
	)

}


Drupal.initInc5000Registration = function() {
  if ( $('div.messages.error').length == 0 ) {
  $(".new-inc5000-fieldset:not(.processed)").each(function() {
    $(".company_lookup-fieldset").css('display','inline');
    //$(this).addClass("processed").show();
    $('.new-inc5000-fieldset .form-inc_buttons input[@value="No"]').each(function() {
      $(this).click(function(){
        $('.new-inc5000-fieldset .inc_buttons').each(function(){$(this).hide();});
        $('.parent-q5-inelegible').show();
      });
    });
    $(".new-inc5000-fieldset #edit-q1-yes").click(function() {
      $(".new-inc5000-fieldset .parent-q1").hide();
      $(".new-inc5000-fieldset .parent-q2").show();
    });
    $(".new-inc5000-fieldset #edit-q2-yes").click(function() {
      $(".new-inc5000-fieldset .parent-q2").hide();
      $(".new-inc5000-fieldset .parent-q3").show();
    });
    $(".new-inc5000-fieldset #edit-q3-yes").click(function() {
      $(".new-inc5000-fieldset .parent-q3").hide();
      $(".new-inc5000-fieldset").hide();
      $(Drupal.settings.inc5000RegistrationItems.toString()).show();
    });
    /*
    $(".new-inc5000-fieldset #edit-q4-yes").click(function() {
      $(".new-inc5000-fieldset").hide();
      $(".company_lookup-fieldset").css('display','inline');
    });
    */
    $(".form-inc_buttons img").click(function(){
      $(".company_lookup-fieldset").hide();
      $('.new-inc5000-fieldset').show();
      //$(Drupal.settings.inc5000RegistrationItems.toString()).show();
    });
    
    $('.parent-q2 .description').click(function(){
      $(".new-inc5000-fieldset .parent-q2").hide();
      $(".new-inc5000-fieldset .parent-q1").show();
    });
    $('.parent-q3 .description').click(function(){
      $(".new-inc5000-fieldset .parent-q3").hide();
      $(".new-inc5000-fieldset .parent-q2").show();
    });
    $('.parent-q4 .description').click(function(){
      $(".new-inc5000-fieldset .parent-q4").hide();
      $(".new-inc5000-fieldset .parent-q3").show();
    });
    $('.parent-q5-inelegible .description').click(function(){
      $(".new-inc5000-fieldset .parent-q5-inelegible").hide();
      $(".new-inc5000-fieldset .parent-q1").show();
    });

    $(Drupal.settings.inc5000RegistrationItems.toString()).hide();
    $(".parent-q1").show();
  });
  }
}
Drupal.settings = Drupal.settings || {};
Drupal.settings.inc5000RegistrationItems = [
  '#user-register #field-company_information',
  '#user-register #personal_information',
  '#user-register #edit-field-first-name-0-value-wrapper',
  '#user-register #edit-field-last-name-0-value-wrapper',
  '#user-register #edit-job-title-wrapper',
  '#user-register #edit-name-wrapper',
  '#user-register #edit-mail-wrapper',
  '#user-register #edit-conf-mail-wrapper',
  '#user-register #edit-pass-wrapper',
  '#user-register #checkboxes_agreements',
  '#user-register #field-has_authority',
  '#user-register #field-privately_held',
  '#user-register #field-termsprivacy',
  '#user-register #edit-submit',
  '#user-register #user_reg_notes'
];

// Global killswitch
if (Drupal.jsEnabled) {
  $(document).ready(
    function() {
    	Drupal.companyLookup();
      Drupal.initInc5000Registration();
    }
	);
}
