vanpelt (owner)

Revisions

gist: 225560 Download_button fork
public
Public Clone URL: git://gist.github.com/225560.git
Embed All Files: show embed
way_hotter_than_yours.js #
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
126
127
128
129
130
131
132
133
134
window.addEvent('domready', function(){
    $$('.address')[0].getParent('form').addEvent('submit', function(e){
      var bad = $$('.address').some(function(a){
        if(a.retrieve('verified') != a.get('value')) {
          return a.fireEvent('blur')
        } else {
          return false
        }
      })
      if(bad)
        e.stop()
    })
    function meta(payload) {
      this.getParent().getElements('.meta').dispose()
      //Add the original address
      new Element('input', {
        type:'hidden',
        name: this.get('name').replace(/\[.+?\]/, "[original_address]"),
        value: this.get('value'),
        'class': 'meta'
      }).inject(this, 'after')
      payload.Point.coordinates.each(function(c,i){
        if(i < 2) {
          new Element('input', {
            type:'hidden',
            name: this.get('name').replace(/\[.+?\]/, i == 0 ? '[lat]' : '[long]'),
            value: c,
            'class': 'meta'
          }).inject(this, 'after')
        }
      },this)
    }
    function ensure_accurate(p, input) {
      if(p.AddressDetails.Accuracy <= 6) {
        input.eliminate('verified')
        //IE IS BORKED
        try {
          input.focus.delay(100,input)
          input.select.delay(100,input)
        } catch (e) {}
        input.store('requested', input.get('value'))
        input.getAllNext('.errorMsg').dispose()
        input.setStyle('background','#d88b7e').grab(
          new Element('span', {
            'class': 'errorMsg',
            style: 'display:block;color:red',
            html: 'This address is too generic, please be more specific.'
          }), 'after'
        )
        return false
      } else {
        input.setStyle('background','').getAllNext('.errorMsg').dispose()
        input.store('verified', input.get('value'))
        return true
      }
    }
    function check(payload){
      var pmark = payload.Placemark
      var input = this
      if(pmark) {
        if(pmark.length > 1) {
          $$('ul.suggest').dispose()
          input.set('value', 'Please choose the most appropriate address...').addClass('choose')
          var pos = input.getCoordinates()
          new Element('ul', {
            'class':'suggest',
            styles:{
              position:'absolute',
              top: pos.bottom,
              left: pos.left,
              width: pos.width
            }}).adopt(pmark.map(function(pm){
            return new Element('li', {
              html: pm.address,
              events: {
                click: function(e) {
                  meta.bind(input)(pm)
                  input.set('value', this.get('html')).removeClass('choose').focus()
                  ensure_accurate(pm,input)
                  this.getParent().dispose()
                }
              }
            })
          })).inject(input, 'after')
        } else {
          meta.bind(input)(pmark[0])
          input.set('value', pmark[0].address)
          ensure_accurate(pmark[0], input)
        }
      } else if(payload.Status.code == 602) {
        input.eliminate('verified')
        //IE IS BORKED
        try {
          input.focus.delay(100,input)
          input.select.delay(100,input)
        } catch (e) {}
        input.getAllNext('.errorMsg').dispose()
        input.setStyle('background','#d88b7e').grab(
          new Element('span', {
            'class': 'errorMsg',
            style: 'display:block;color:red',
            html: 'This does not seem to be a valid address, please try another variation.'
          }), 'after'
        )
      }
    }
    $$('input.address').each(function(a){
      var callback = 'check_'+a.id.split("_")[0]
      window[callback] = check.bind(a)
      a.addEvent('blur', function(e){
        if(this.retrieve('requested') == this.get('value')) {
          return true
        }
        if(!this.get('value').test(/^\s*$/)) {
          var verified = this.retrieve('verified')
          if(verified && verified == this.get('value')) {
            //console.log("Verified", callback)
            return true
          }
          var tries = (this.retrieve('tries') || 0)
          this.store('tries', tries + 1)
          //Only do this 3 times, hopefully they got the message
          if((this.retrieve('tries') || 0) > 3) {
            this.setStyle('background','').getAllNext('.errorMsg').dispose()
            this.store('verified', this.get('value'))
            return true
          }
          this.store('requested', this.get('value'))
          //console.log("Requesting", callback)
          new Element('script', {src:'http://maps.google.com/maps/geo?q='+encodeURIComponent(this.get('value'))+'&ue=utf8&callback='+callback+'&sensor=false&key=ABQIAAAAo7WZCzZIkSblHMg0GJ-AwRTO9bHCf1xwbKWofRtvGHi3UueLXRRlU_wLzBzvBmEBRO_34kWbmaAngQ'}).inject(document.body)
        }
      })
    })
  })