﻿
// gmap functions, need jquery

var xreq;

function gmapfind(gmapid)
{
    var address=document.getElementById(gmapid+"_address").children[0];
    var town=document.getElementById(gmapid+"_town").children[0];
    var cp=document.getElementById(gmapid+"_cp").children[0];
    var country=document.getElementById(gmapid+"_country").children[0];
    var c=(country.value=="unset" || country.value=="")?"":"FR";
    var q=address.value+",+"+town.value+",+"+cp.value+",+"+c;
    q=q.replace(" ","+");
    var url="gmap.aspx?q="+q;
    url=encodeURI(url);
    //$.ajaxSetup({ scriptCharset: "utf-8" , contentType: "application/javascript; charset=utf-8"});
    $.getJSON(           
                url, {}, 
                function(data)
                {
                    if((data.Status.code==200)&&(data.Placemark[0]!=undefined))
                    {
                        var dplace=data.Placemark[0];
                        var dcountry=dplace.AddressDetails.Country;
                        country.value=dcountry.CountryNameCode;
                        var dloc=dcountry.AdministrativeArea.SubAdministrativeArea.Locality;
                        town.value=dloc.LocalityName;
                        var dcp=dloc.PostalCode;
                        cp.value=dcp.PostalCodeNumber;
                        address.value=dloc.Thoroughfare.ThoroughfareName;
                    }
                }
    );
    /*
    {
        xreq = new XMLHttpRequest();
        xreq.open("GET", url, true);
        xreq.onreadystatechange = gmaphandle;
        xreq.send(null);
    }
    */
}

function gmaphandle()
{
   if (xreq.readyState == 4) 
   {
       var data = eval('(' + xreq.responseText + ')');
       var i=0;
   }
}
