/**************************************************************
 *  DYNAMIC SELECTOR
 *  Purpose: To dynamically add [Source-Detail] select box depending on the choice of [Lead-Source].
 *  This file should be deprecated eventually and productized into the UI
 *  Requirements: jQuery 1.3.2 or better needs to be included in the landing page.
 */


jQuery(document).ready(function(){
  var stateOptions = {};

  // Get a reference to the state [Source-Detail] drop down (so we don't have to keep
  // finding it over and over again).
  var jState = jQuery( "#Source-Detail" );

  //IE6 is acting weird with the width
  jState.css("width","");

  //put the rel on the [Source-Detail]
  jQuery("#Source-Detail option").each(function() {

    var optionVal = jQuery(this).attr("value");
    switch(optionVal)
    {

	  case "Community College Publication":
	  case "Business/IT/HR Publication":
	  case "Education Publication":
	  case "Environmental Publication":
	  case "Fire/Emergency Publication":
	  case "History Publication":
	  case "Homeland Security/Security Publication":
	  case "Intelligence/National Security Publication":
	  case "Law Enforcement/Police Publication":
	  case "Military Base Newspaper":
	  case "Military Times":
	  case "Nursing/Health Publication":
	  case "Sports Publication":
	  case "Transportation/Logistics Publication": 
	    jQuery(this).attr("rel","Ad in Newspaper or Magazine");
        break;
		
	case "Choose Publication":
           jQuery(this).attr("rel","Ad in Newspaper or Magazine").attr("value","");
     break;


     case "Article or News Report":
        jQuery(this).attr("rel","Article or News Report");
        break;
		
	case "Choose Article or News Report":
           jQuery(this).attr("rel","Article or News Report").attr("value","");
     break;
		
	  case "Expo/Trade Show":

	    jQuery(this).attr("rel","Expo/Trade Show");
        break;
		
	case "Choose Expo/Show":
           jQuery(this).attr("rel","Expo/Trade Show").attr("value","");
     break;
		
	  case "Internet/Website":
	       jQuery(this).attr("rel","Internet Search Engine");
        break;
		
	case "Choose Search Engine":
           jQuery(this).attr("rel","Internet Search Engine").attr("value","");
     break;
		
	  case "Co Worker/Supervisor":
	  case "Connections Academy":
	  case "Employer/HR/Training/Education Office":
	  case "Family Friend":
	  case "Military Base Education Office":
	  case "Student/Alumni/Faculty/Staff":
	    jQuery(this).attr("rel","Personal Referral");
        break;
		
	case "Choose Referral":
           jQuery(this).attr("rel","Personal Referral").attr("value","");
     break;
		
	  case "Promotional Material (brochure/CD/etc.)":
	    jQuery(this).attr("rel","Promotional Material (brochure/CD/etc.)");
        break;
		
	case "Choose":
           jQuery(this).attr("rel","Promotional Material (brochure/CD/etc.)").attr("value","");
     break;
		
	  case "Fort Bliss Billboard":
	  case "Fort Carson Billboard":
	  case "Fort Hood Billboard":
	  case "Langley AFB Billboard":
	    jQuery(this).attr("rel","Public Space/Sporting Event");
        break;
		
	case "Choose Billboard":
           jQuery(this).attr("rel","Public Space/Sporting Event").attr("value","");
     break;
		
      case "Albuquerque (NM)":
      case "Jacksonville (FL)":
      case "Orlando (FL)":
      case "Salt Lake City (UT)":
      case "Washington (DC)":
      case "West Palm Beach (FL)":
	    jQuery(this).attr("rel","Radio");
        break;
		
	case "Choose Radio Station":
           jQuery(this).attr("rel","Radio").attr("value","");
     break;
		
      case "ABC/NBC/CBS/FOX/Mynetwork,CW":
      case "Bravo/Style/Oxygen/ect":
      case "Centric/BET/Bounce/etc":
      case "Discovery/Tru TV/Bio/A&E/etc":
      case "Lifetime/WE/Hallmark/etc":
      case "USA/TNT/Nick@Nite/etc":
	    jQuery(this).attr("rel","Television");
        break;
		
	case "Choose TV Station":
           jQuery(this).attr("rel","Television").attr("value","");
     break;
		
	  case "Criminal Justice":
	  case "Emergency and Disaster Mgmt/Homeland Security":
	  case "General Admission":
	  case "History":
	  case "Hospitality Management":
	  case "Information Technology":
	  case "International Relations":
	  case "Legal Studies":
	  case "Master of Education":
	  case "Religion and Philosophy":
	  case "Security Management":
	  case "Sports and Health Sciences":
	  case "Transportation and Logistics Mgmt":
	    jQuery(this).attr("rel","Virtual Admissions Open House");
        break;
		
	case "Choose Open House":
           jQuery(this).attr("rel","Virtual Admissions Open House").attr("value","");
     break;
		
	  case "Walmart Corporate Website":
	    jQuery(this).attr("rel","Walmart/Sams Club");
        break;
		
   case "Choose Website":
           jQuery(this).attr("rel","Walmart/Sams Club").attr("value","");
     break;
		
      case "":
        break;

      default:
        jQuery(this).attr("rel","Select Source"); 
    }
  });

  // Loop over each country [Lead-Source] and create an entry in the state [Source-Detail] options cache.
  jQuery( "#Lead-Source" ).find( "option" ).each(
    function( index, option ){

      // Check to see that this option value has a length.
      // We can only create a key based on a valid value.
      if (option.value.length){

        // Create a cache for the state-based [Source-Detail] options. This will
        // be a jQuery collection of all the state options [Source-Detail] that should
        // be displayed if this country [Lead-Source] is selected.
        stateOptions[ option.value ] = jState.find( "option[ rel = '" + option.value + "' ]");
      }
    }
  );

  // This is a function that will update the options in the state-based [Source-Detail]
  // select box, based on the values in the country [Lead-Source] box.
  var updateStateList = function( countryCode ){
    // No matter what we do, we have to clear the state [Source-Detail] list first.
    jState.empty();

    // Now that we have an empty state [Source-Detail] list, let's see if we can repopulate
    // it with values from our state [Source-Detail] / country [Lead-Source] cache.
    if (countryCode.length && stateOptions[ countryCode ]){

      // Add the jQuery collection that we have cached at this country [Lead-Source] code.
      jState.append( stateOptions[ countryCode ] );

      // Select the first item in list.

      setTimeout(function () {
        jQuery("#Source-Detail option:first").attr("selected",true);
        //var thisJState = jQuery( "#State" );
        //thisJState.get(0).selectedIndex = 0;
        }, 50);
    }
  }

  jQuery("#Lead-Source").change(function()  {
    var selected = this.options[this.selectedIndex].value;
    var stateRow = jQuery("#Source-Detail").parents("li:first");

    switch(selected)
    {
		
		case "Ad in Newspaper or Magazine":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Article or News Report":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;

        case "Expo/Trade Show":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Internet Search Engine":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Personal Referral":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Promotional Material (brochure/CD/etc.)":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Public Space/Sporting Event":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Radio":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Television":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Virtual Admissions Open House":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
		
		case "Walmart/Sams Club":
        jState.addClass("mktFReq");
        stateRow.addClass("mktFormReq").slideDown().find("label").html("Specific Source:");

        // Update the state list.
        updateStateList( this.value );

        break;
				
      default:
        stateRow.removeClass("mktFormReq").slideUp();
        jState.removeClass("mktFReq");

        // Update the state [Source-Detail] list.
        updateStateList( this.value );
    }
  }).change();

  // pre-fill state/province [Source-Detail] since we just manipulated that select list.
  setTimeout(function () {
    if ((typeof mktoPreFillFields != 'undefined') && (mktoPreFillFields) && mktoPreFillFields['Source-Detail']) {
      var jStateOption = jQuery('#Source-Detail option[value="' + mktoPreFillFields['Source-Detail'] + '"]');
      if (jStateOption.val()) {
        jStateOption.attr('selected',true);
      }
    }
  }, 50);
});
