Skip to content

Instantly share code, notes, and snippets.

@vanpelt
Created November 3, 2009 23:09
Show Gist options
  • Save vanpelt/225560 to your computer and use it in GitHub Desktop.
Save vanpelt/225560 to your computer and use it in GitHub Desktop.
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)
}
})
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment