function point(x, y, name, zip, city, address, phone) {
    this.x = x
    this.y = y
    this.name = name
    this.zip = zip
    this.city = city
    this.address = address
    this.phone = phone
}

function ByDistance ( a, b ) {
    if ( a.distance < b.distance ) { 
        return -1 
    } else if ( a.distance > b.distance ) {
        return 1
    } else if ( a.name < b.name ) {
        return -1
    } else if ( a.name > b.name ) {
        return 1
    } else if ( a.phone < b.phone ) {
        return -1
    } else if ( a.phone > b.phone ) {
        return 1
    } else  {
        return 0
    }
}

function check() {

    x = event.offsetX
    y = event.offsetY
    
    for (i in salespoint) {
        salespoint[i].distance = Math.sqrt((x-salespoint[i].x)*(x-salespoint[i].x) 
                                            + (y-salespoint[i].y)*(y-salespoint[i].y))
    }
        
    sorted = salespoint.sort(ByDistance)
    
    second = 0
    for ( i in sorted ) {
        if (second && ( sorted[i].distance > second ) ) {
            limit = sorted[i].distance
            break
        } else if ( sorted[i].distance > sorted[0].distance ){
            second = sorted[i].distance
        }    
    }
    
    code = '<table width="100%" cellpadding="0" cellspacing="0" border="0"><tr>\
	<td>Nazwa sklepu</td>\
	<td>Miejscowość</td>\
	<td>Adres</td>\
	<td>Telefon</td>\
	</tr>';
    
    for ( i in sorted ) {
        if ( sorted[i].distance <= limit ) {
            code = code + '<tr><td><strong>&nbsp;' + sorted[i].name + '</strong></td><td>' + sorted[i].city 
                        + '</td><td>' + sorted[i].address + '</td><td>' + sorted[i].phone 
                        + '</td></tr>'
        } else {
            break
        }
    }
    
    code = code + '</table>'
    
    document.all['table'].innerHTML = code
}

function init() {
    map.onMouseDown = check()
}
