﻿

//if (!Local_Map) {
//    alert("map point not set");
//}
function ValidatePostcode(PC) {
    // parse the postcode string to see if it is in a valid format
    // i.e. AADDL SCC,
    // where:	AA is a 1 or 2 two letters Area code
    //			DD is a 1 or 2 digit code to specify the district
    //			L is 0 or 1 letters used in central London (e.g. EC1A district types)
    //			S is one digit sector code
    //			CC is two letters finishing the full postcode
    //

    var reg = /^([a-z]{1,2})(\d{1,2})[a-z]? \d[a-z]{2}$/ig;

    return reg.test(PC);
}

function GetPCsector(PC) {
    // extract the sector only, as Google maps API won't geocode full postcodes
    // Remember to ensure postcode is valid before calling this
    var Sector;
    var match;
    var reg = /^([a-z]{1,2})(\d{1,2})[a-z]? \d/ig;
    match = PC.match(reg);
    Sector = match[0];
    return Sector;
}

//Local_Map.setmap =
function GetLatLong(PostcodeHolderId, NationalPlaceName,challengeid,name) {

    var Address = $("#" + PostcodeHolderId).val()
    alert(Address);
    if (Address != '') {


        var geocoder = new GClientGeocoder();
        // setup using postcode sector
        geocoder.setBaseCountryCode(NationalPlaceName);
        geocoder.getLatLng(Address + ',' + NationalPlaceName,
						function(latlng) {
						    if (latlng) {

						        latlng = latlng.toString().replace("(", "").toString();
						        latlng = latlng.toString().replace(")", "").toString();

						        //alert(latlng.toString().replace("", "").toString());
						        //alert(latlng);

						        //alert(latlng.toString().split(",")[0]);
						        //now we have the latlong - query the database
						        var arrlatlng = latlng.split(",");
                                
						        GetNearestTown(arrlatlng[0], arrlatlng[1],challengeid,name);




						    } // end if latlng
						} // end callback function
					);
    }
    

}

//Gets the nearest town from bestof database based on the latlong coordinates given
function GetNearestTown(latitude,longitude,challengeid,name) {

    //create holder for helper service
    var servicepath = "/_services/helper.ashx?function=getnearestplace&lat=" + latitude + "&long=" + longitude
    
    //now check to see if the challengeid and name are present i.e not null
    if(challengeid != '' && name != '')
    {
        
        //include challengeid and name in service path
        servicepath = servicepath + "&challengeid=" + challengeid + "&name=" + name;
    
    
    }
    

    $.get(servicepath, function(result) {
        //alert(result);
        $("#ClosestPlace").show();
        $("#ClosestPlace").html(result.toString());

        $.scrollTo('#sp', 800);
        
        

    });



}


//this functions does the same as GetNearestTown except the comparison is being drawn from a Place that is already on our 
//database, and therefore already has coordinates.
function GetNearestTownFromPlace(challengeid, name,postbackstr) {

    
    
    //get the placeref
    var PlaceRef = $("input[title='hdplaceref']").val();
    //get the selected placename
    var PlaceName = $("input[rel='places']").val();

    //first check to see if a place has been selected
    if (PlaceRef == '') {

        //no place selected, get the first town we have
        if ($(".selection > li:first a")) {
            //then trigger the onclick.
            $(".selection > li:first a").trigger("onclick");

            //we have executed the click event for the first town we have seen because the user has not selected one yet.
            //now we have the chosen placeref, make sure we assign it to the variable.
            PlaceRef = $("input[title='hdplaceref']").val();
            PlaceName = $("input[rel='places']").val();
        }
    }

    
    
    //create holder for helper service
    var servicepath = "/_services/helper.ashx?function=getnearestplacefromselectedtown&placeref=" + PlaceRef + "&placename=" + PlaceName;

    //now check to see if the challengeid and name are present i.e not null
    if (challengeid != '' && name != '') {

        //include challengeid and name in service path
        servicepath = servicepath + "&challengeid=" + challengeid + "&name=" + name;
        

    }


    $.get(servicepath, function(result) {

        //check to see if a placeref has been returned
        //alert(result);
        if (!(result.toLowerCase() == PlaceRef.toLowerCase())) {
            $("#ClosestPlace").show();
            $("#ClosestPlace").html(result.toString());

            //now scroll down because the rest of the information is down further

            $.scrollTo('#sp', 800);
            return false;

        }
        else {

            //is the same, so we really want to do a postback
            //eval(postbackstr);
            eval(unescape(postbackstr));
        }

    });

     
    

}



function Hit(a, b, c, d) {

    alert('hit');


}
