File size: 3,510 Bytes
a431caa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"use strict";

$(function() {

var onet_ws = null;

function hide_element(id) {
  $('#' + id).hide();
}
function show_element(id) {
  $('#' + id).show();
}
function fill_element(id, text) {
  $('#' + id).text(text);
}
function read_input(id) {
  return $('#' + id).val();
}
function disable_button(id) {
  $('#' + id).prop('disabled', true);
}
function enable_button(id) {
  $('#' + id).prop('disabled', false);
}
function button_is_disabled(id) {
  return $('#' + id).is(':disabled');
}
function attach_event(id, eventname, callback) {
  $('#' + id).on(eventname, callback);
}

function show_error(msg) {
  fill_element('errorMessage', msg);
  show_element('errorPanel');
}
function check_for_error(resp) {
  if (resp.hasOwnProperty('error')) {
    show_error(resp.error);
    show_element('errorPanel');
    return true;
  }
  return false;
}
function reset_error() {
  fill_element('errorMessage', '');
  hide_element('errorPanel');
}

function init_keyword_search_js() {
  fill_element('domainOrigin', document.location.protocol + '//' + document.location.host);
  reset_error();
  hide_element('connectSuccess');
  hide_element('searchSuccess');
  
  if (document.location.protocol != 'http:' && document.location.protocol != 'https:') {
    show_error('You must run this demo from an HTTP or HTTPS server.');
    hide_element('accountForm');
    return;
  }
  
  attach_event('accountForm', 'submit', function(e) {
    e.preventDefault();
    if (button_is_disabled('accountConnect')) { return; }
    reset_error();
    hide_element('connectSuccess');
    
    var username = read_input('accountName');
    if (username == '') {
      show_error('Please enter your O*NET Web Services username.');
      return;
    }
    
    onet_ws = new OnetWebService(username);
    disable_button('accountConnect');
    onet_ws.call('about', null, function(vinfo) {
      enable_button('accountConnect');
      if (check_for_error(vinfo)) { return; }
      
      fill_element('connectVersion', vinfo.api_version);
      show_element('connectSuccess');
    });
  });
  
  attach_event('searchForm', 'submit', function(e) {
    e.preventDefault();
    if (button_is_disabled('searchSubmit')) { return; }
    reset_error();
    hide_element('searchSuccess');
    hide_element('searchNoResults');
    hide_element('searchResults');
    
    var kwquery = read_input('searchQuery');
    if (kwquery == '') {
      show_error('Please enter one or more search terms.');
      return;
    }
    
    disable_button('searchSubmit');
    onet_ws.call('online/search', { keyword: kwquery, end: 5 }, function(kwresults) {
      enable_button('searchSubmit');
      if (check_for_error(kwresults)) { return; }
      
      fill_element('searchQueryEcho', kwquery);
      if (!kwresults.hasOwnProperty('occupation') || !kwresults.occupation.length) {
        show_element('searchNoResults');
      } else {
        for (var i = 0; i < 5; i++) {
          if (i >= kwresults.occupation.length) {
            hide_element('searchItem' + i);
          } else {
            fill_element('searchCode' + i, kwresults.occupation[i].code);
            fill_element('searchTitle' + i, kwresults.occupation[i].title);
            show_element('searchItem' + i);
          }
        }
        show_element('searchResults');
      }
      show_element('searchSuccess');
    });
  });
}

init_keyword_search_js();
});