    var category_id = "0";
    var program_id = "";
    var school_type = "";
    if(typeof $('#pre_selectedCategory').val() != "undefined"){
        var pre_selectedCategory = $('#pre_selectedCategory').val();
    }    
    if(typeof $('#pre_selectedProgram').val() != "undefined"){
        var pre_selectedProgram  = $('#pre_selectedProgram').val();
    }
    if(typeof $('#country_list').val() != "undefined"){
        loadCountries($('#country_list').val());
    }
    if(typeof $('#destination_list').val() != "undefined"){
        loadDestinations($('#destination_list').val());
    }
  function bySortedValue(obj, context) {
    var tuples = [];
    for (var key in obj) tuples.push([key, obj[key].program_name]);
    tuples.sort(function(a, b) { return a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0 });
    return tuples;
  }

    function bySortedCategory(obj, context){
      var tuples = [];
      for (var key in obj) tuples.push([key, obj[key]]);
      tuples.sort(function(a, b) { return a[1] > b[1] ? 1 : a[1] < b[1] ? -1 : 0 });
      return tuples;
    }

    $(document).ready( function() {        
        if($("#category").html() != null){
            loadCategories(pre_selectedCategory,pre_selectedProgram);        
    
            if($("#category").val() != ''){
                loadPrograms();
            }
            $("#category").change(function() {
                if($("#category").val() != ''){
                    loadPrograms();
                }
            });
            $("#school_type").val(school_type);
        }

    });

    function loadCategories(category,program) {
      $('#category').html('');
      var undecided = false;
      if(typeof $('#category').attr('class') != 'undefined'){
          var classList =$('#category').attr('class').split(/\s+/);
          $.each( classList, function(index, item){
              if (item === 'undecided_category') {
                  undecided = true;
               }
           });
          if(undecided){
              $("<option>").attr("value", "").text("Undecided").appendTo("#category");
          } else {
              $("<option>").attr("value", "").text("Select a Field").appendTo("#category");            
          }
      }
      $.ajax( {
                type : "GET",
                url : "/index.php",
                data : "module=dbviewdata&hive_op=categories&output=1",
                success : function(data) {
                        decoded_data = jQuery.parseJSON(data);
                        decoded_data_sort = bySortedCategory(decoded_data, this);
                        $.each(decoded_data_sort, function(key, value) {
                            // console.debug(key + "=>" + value);
                            $("<option>").attr("value", value[0]).text(value[1]).appendTo("#category");
                        });
                        $("#category").val(category);
                        if($("#category").val() != ''){
                            matched_program = loadPrograms(program);
                        }

                }
            });
    }

    function loadPrograms(program) {
        $('#program').html('');
        var required = false;
        //check if this is a required field if not change text to choose one

        if(typeof $('#program').attr('class') != 'undefined'){
            var classList =$('#program').attr('class').split(/\s+/);        
            $.each( classList, function(index, item){
                if (item === 'required') {
                    required = true;
                 }
             });
            if(required){
                $("<option>").attr("value", "").text("Choose One").appendTo("#program");
            } else {
                $("<option>").attr("value", "").text("Undecided").appendTo("#program");            
            }
        }
        $.ajax( {
            type : "GET",
            url : "/index.php",
            data : "module=dbviewdata&hive_op=programs&output=1&category_id=" + $("#category").val(),
            success : function(data) {
                    decoded_data = jQuery.parseJSON(data);
                    val = $("#category").val();
                    dereferenced_data =  bySortedValue(decoded_data[val], this);
                    $.each(dereferenced_data, function(key, value) {
                        $("<option>").attr("value", value[0]).text(value[1]).appendTo("#program");
                    });
                    $("#program").val(program);

            }
        });
    }
    
    function loadCountries(countriesJson){
        decoded_data = jQuery.parseJSON(countriesJson);
        $.each(decoded_data, function(key, value) {
            $("<option>").attr("value", key).text(value).appendTo("#country");
        });
    }
    
    function loadDestinations(destinationsJson){
        decoded_data = jQuery.parseJSON(destinationsJson);
        $.each(decoded_data, function(key, value) {
            $("<option>").attr("value", key).text(value).appendTo("#destination");
        });
    }


