// Depending on how they got to the page, search form will appear with different criteria/priorities
function isset(item) {
  return typeof(item) != 'undefined';
}

function parseQueryString(q) {
  var get = [];
  if ( q == null ) q = location.search;
  q = (q.indexOf('?')=== 0)? q.substr(1): q;
  var items = q.split('&');
  if ( items.length ) {
    for (i = 0; i < items.length; i++ ) {
      var pair = items[i].split('=');
      get[""+pair[0]] = ""+pair[1];
    }
  }
  return get;
}

// Return new array with duplicate values removed
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };


$(document).ready(function() {
  var get = parseQueryString();
  $("#jobSearchForm").append('<h2>Search by:</h2>');
  $("#jobSearchForm").append('<select name="filter1" id="filter1" size="1"><option value="Title,ne,ZZZ">by Name</option></select>');
  $("#jobSearchForm").append('<select name="filter2" id="filter2" size="1"><option value="Department,ne,ZZZ">by Type</option></select>');
  $("#jobSearchForm").append('<select name="filter3" id="filter3" size="1"><option value="StateDropDown,ne,ZZZ">by State</option></select>');
  $("#jobSearchForm").append('<input type="hidden" name="filter4" id="filter4" value="Active,eq,on" /><input type="hidden" name="filterrel" value="filter1 AND filter2 AND filter3 AND filter4" />');

  // Get cities dynamically
  pixelsilk2.getListItems({path:"/employment/find-a-job/", fields:["StateDropDown", "Department", "Title"], filters:[{field:"Active", comparison:"eq", value:"on"}], filterrel:"", sorts:[], pagesize:1000, page:1}, function(data) {
    if ( data.length ) {
      var states = [];
      var departments = [];
      var jobtitles = [];
      for ( i = 0; i < data.length; i++ ) {
        states.push(data[i].StateDropDown);
        departments.push(data[i].Department);
        jobtitles.push(data[i].Title);
      }
      states = states.unique();
      states.sort();
      departments = departments.unique();
      departments.sort();
      jobtitles = jobtitles.unique();
      jobtitles.sort();
      var html = '';
      for ( i = 0; i < jobtitles.length; i++ ) {
        html += '<option value="Title,eq,' + jobtitles[i] + '"';
        if ( isset(get['filter1']) && get['filter1'].replace(/\+/g, ' ').indexOf(jobtitles[i]) > -1 ) html += ' selected="selected"';
        html += '>' + jobtitles[i] + '</option>';
      }
      $("#filter1").append(html);
      html = '';
      for ( i = 0; i < departments.length; i++ ) {
        html += '<option value="Department,eq,' + departments[i] + '"';
        if ( isset(get['filter2']) && get['filter2'].replace(/\+/g, ' ').indexOf(departments[i]) > -1 ) html += ' selected="selected"';
        html += '>' + departments[i] + '</option>';
      }
      $("#filter2").append(html);
      html = '';
      for ( i = 0; i < states.length; i++ ) {
        html += '<option value="StateDropDown,eq,' + states[i] + '"';
        if ( isset(get['filter3']) && get['filter3'].replace(/\+/g, ' ').indexOf(states[i]) > -1 ) html += ' selected="selected"';
        html += '>' + states[i] + '</option>';
      }
      $("#filter3").append(html);
    }
  });
  $("#jobSearchForm").append('<input type="image" id="jobSearchSubmit" src="/submit-button.gif" width="108" height="40" value="Submit" />');
});

