/*
File: internship/scripts/intern-submit-int-script.js

MAKE SURE YOU KEEP TRACK OF EVERYTHING YOU DO!

Author: Dwight VanTuyl
DD-MMM-YYYY: 14-Mar-2008
Purpose: Form manipulation specifically for the internship tab of the submit form.
+++++++++++++
Modifications
+++++++++++++
Editor:		
DD-MMM-YYYY:
Watcha did:	
~~~~~~~~~~~~~
*/

Ext.namespace('Intern.submit');

Intern.submit.int = {

  init: function() {
    
    //On or Off site radio hides or shows the country and region select fields
    $("input[name='int_hasLocation']").click(function(){
      if($(this).val() == 'T'){
        $("div#countryRegionSelect").show();
      }else{
        $("div#countryRegionSelect").hide();
      }
    }); 

    // enables/disables salary field depending on comp radio button
    $("input[name='int_comp']").click(function(){
      if($(this).val() == 'Paid'){
        $("input[name='int_salary']").removeAttr("disabled");
      }else{
        $("input[name='int_salary']").attr("disabled", true);
      }
    });
	
    
    //These three JsonStores retrieves data by calling the 'intern-json-subj_classification.cfm'
    //Output is used for selection boxes that asynchronisly populate based on user input.
    this.fieldSelectStore = new Ext.data.JsonStore({
      url: '/internship/queries/intern-json-subj_classification.cfm?classtype=LF',
      root: 'data',
      totalProperty: 'recordcount',
      fields: ['subclassid', 'classlabel']
    });
    
    //enable or disable intern type 'other' based on selection in drop down box
    $("input[name='int_typeOther']").attr("disabled", "disabled");
    $("select[name='int_typeSelect']").change(function(){
      var value = $(this,"option:selected").val();
      if(value == 'other'){
        $("input[name='int_typeOther']").removeAttr("disabled");
      }else{
        $("input[name='int_typeOther']").attr("disabled", "disabled");
      }
    });
  },

  //Listen to "Add" button and insert another selection field when clicked  
  addFieldSelect: function(){
    var container = $("#lingFields");
    var i = container.children().length;
    container.append("<div id='lingField" + i + "' class='selectWrapper'></div>");
    
    //Create a new linguistic field comboBox
    var fieldSelect = new Ext.form.ComboBox({
      store: Intern.submit.int.fieldSelectStore,
      forceSelection: true,
      displayField:'classlabel',
      valueField:'subclassid',
      typeAhead: true,
      minChars: 1,
      name: "int_lingFields",
      cls: 'selectCmb',
      triggerAction: 'all',
      selectOnFocus:true,
      emptyText:'Select from dropdown',
      loadingText:'Searching',
      queryDelay: 0,
      editable: false
    });
    fieldSelect.render("lingField"+i);
  },
  
  //Listen to "Remove" buttons and remove the last selection field
  removeFrom: function(containerID){
    var container = $("#" + containerID);
    
    var combo = Ext.get($("#" + containerID + " div.selectWrapper:last-child").attr('id'));
    if(combo){
      combo.remove();
    }
  }
}

Ext.onReady(Intern.submit.int.init, Intern.submit.int);