summaryrefslogtreecommitdiff
path: root/httemplate/elements/select-region.html
blob: 46c37c9352a7d0d283afd61bd954d2cdc61873de (plain)
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
<% include('/elements/xmlhttp.html',
              'url'  => $p.'misc/regions.cgi',
              'subs' => [ $opt{'prefix'}. 'get_regions' ],
          )
%>

<SCRIPT TYPE="text/javascript">

  function opt(what,value,text) {
    var optionName = new Option(text, value, false, false);
    var length = what.length;
    what.options[length] = optionName;
  }

  function <% $opt{'state_prefix'} %>state_changed(what, callback) {

    what.form.<% $opt{'prefix'} %>region.disabled = 'disabled';
    what.form.<% $opt{'prefix'} %>region.style.display = 'none';
    var regionwait = document.getElementById('<% $opt{'prefix'} %>regionwait');
    regionwait.style.display = 'inline';
    var regionerror = document.getElementById('<% $opt{'prefix'} %>regionerror');
    regionerror.style.display = 'none';

    what.form.<% $opt{'prefix'} %>phonenum.disabled = 'disabled';

    state = what.options[what.selectedIndex].value;

    function <% $opt{'prefix'} %>update_regions(regions) {

      var reply = JSON.parse(regions);
      // blank the current region
      for ( var i = what.form.<% $opt{'prefix'} %>region.length; i >= 0; i-- )
          what.form.<% $opt{'prefix'} %>region.options[i] = null;
      // blank the current phonenum too
      for ( var i = what.form.<% $opt{'prefix'} %>phonenum.length; i >= 0; i-- )
          what.form.<% $opt{'prefix'} %>phonenum.options[i] = null;
      if ( what.form.<% $opt{'prefix'} %>phonenum.type != 'select-multiple' ) {
        opt(what.form.<% $opt{'prefix'} %>phonenum, '', 'Select phone number');
      }

%     if ($opt{empty}) {
        opt(what.form.<% $opt{'prefix'} %>region, '', '<% $opt{empty} %>');
%     }

      // add the new regions
      var regionArray = reply.regions;
      for ( var s = 0; s < regionArray.length; s++ ) {
          var regionLabel = regionArray[s];
          if ( regionLabel == "" )
              regionLabel = '(n/a)';
          opt(what.form.<% $opt{'prefix'} %>region, regionArray[s], regionLabel);
      }

      regionwait.style.display = 'none';
      if ( regionArray.length >= 1 ) {
        what.form.<% $opt{'prefix'} %>region.disabled = '';
        what.form.<% $opt{'prefix'} %>region.style.display = '';
      } else {
        var regionerror = document.getElementById('<% $opt{'prefix'} %>regionerror');
        regionerror.style.display = 'inline';
        if ( reply.error ) {
          regionerror.textContent = reply.error;
        } else {
          regionerror.textContent = 'Select a different state';
        }
      }

      //run the callback
      if ( callback != null ) 
        callback();
    }

    // go get the new regions
    <% $opt{'prefix'} %>get_regions( state, <% $opt{'svcpart'} %>, <% $opt{'prefix'} %>update_regions );

  }

</SCRIPT>

<DIV ID="<% $opt{'prefix'} %>regionwait" STYLE="display:none"><IMG SRC="<%$fsurl%>images/wait-orange.gif"> <B>Finding regions</B></DIV>

<DIV ID="<% $opt{'prefix'} %>regionerror" STYLE="display:none; font-weight: bold"><IMG SRC="<%$fsurl%>images/cross.png"></DIV>

<SELECT NAME="<% $opt{'prefix'} %>region" onChange="<% $opt{'prefix'} %>region_changed(this); <% $opt{'onchange'} %>" <% $opt{'disabled'} %>>
  <OPTION VALUE="">Select region</OPTION>
</SELECT>

<%init>

my %opt = @_;

$opt{disabled} = 'disabled' unless exists $opt{disabled};

</%init>