var searcher;
var map;
i = 0;
getCurrent = 0;
var infowindow2 = new Array();


function init() {

    geocoder = new google.maps.Geocoder();
    var latlng = new google.maps.LatLng(39.095963, -95.625);
    var myOptions = {
        zoom: 3,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
    //    google.maps.event.addListener(marker, 'mouseup', fnToolTip);
}

function getaddress(address) {
    // var address1 = document.getElementById("search_locations");

    //address = address2.item.value;
    if (parseInt(address) == NaN) { address = address + ", USA" }

    if (address != "") {
        geocoder = new google.maps.Geocoder();
        geocoder.geocode({ 'address': address }, function(results, status) {

            if (status == google.maps.GeocoderStatus.OK) {

                var location = new google.maps.LatLng();
                var latlon = String(results[0].geometry.location);



                document.getElementById('ctl00_ctl00_main_content_BottomContent_hdLatLan').value = latlon;
                if (latlon != "") {
                    document.aspnetForm.action = "";

                    pageid = document.getElementById('ctl00_ctl00_main_content_BottomContent_hdcontentid').value

                    document.aspnetForm.action = "royalg_locator.aspx?id=" + pageid;
                    document.aspnetForm.submit();
                }
                else {
                    alert("Please try another location");
                }
            }
            else {
                alert("Please try another location");
            }
        });
    }
    else {
        alert("Please try another location");
    }

    //return false;

}

function my() {
    alert("test")
}



function callEnter(e) {


    var key;

    if (e.which)
        key = e.which; //firefox

    else
        key = e.keyCode; //IE
    //alert (String(key));

    if (key == 13) {
        checkdata();
        //Get the button the user wants to have clicked

    }



    //    if (event.keyCode == 13) {
    //        checkdata();
    //    }
}

function seeOnMap(lat, lon, imgId, address1, city, state, zip, phone, cname) {
    cname = cname.replace("'", "");
    _gaq.push(['_trackEvent', 'Dealer Locator', 'Search Results', 'See on Map - '+ cname]);

    if (infowindow2[getCurrent]) {
        infowindow2[getCurrent].close();
    }

    //     infowindow.close();

    //    var myLatLon = lat.split(',');
    geocoder = new google.maps.Geocoder(lat, lon);
    var location = new google.maps.LatLng(lat, lon);

    map.setCenter(location);
    // map.setZoom(9);

    var image = new google.maps.MarkerImage('common/images/markers/' + String(imgId) + '.png',
    // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(24, 39),
      new google.maps.Point(0, 0),
      new google.maps.Point(0, 32));


    var marker = new google.maps.Marker({
        map: map,
        draggable: false,
        icon: image,
        position: location
    });

    marker.setTitle(imgId);

    if (phone != '' || phone != null) {
        vifPhone = '<br\><b>Phone:</b> ' + String(phone);
    }
    else {
        vifPhone = "";
    }


    //    var infowindow = new google.maps.InfoWindow(
    //      { content: contentString

    //      });
    //google.maps.event.addListener(marker, 'click', function() {
    infowindow2[imgId].open(map, marker);
    getCurrent = marker.getTitle();
    //});

    google.maps.event.addListener(marker, 'mouseup', function() {

        if (infowindow2[getCurrent]) {
            infowindow2[getCurrent].close();
        }


        infowindow2[marker.getTitle()].open(map, marker);
        getCurrent = marker.getTitle();
        // infowindow2.open(map, marker);
    });

    google.maps.event.addListener(map, 'click', function() {
        if (infowindow2[getCurrent]) {
            infowindow2[getCurrent].close();
        }
    });
}

function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g, "");
}



function getDirection(address1, city, zip, state, cname) {
    cname = cname.replace("'", "");
    _gaq.push(['_trackEvent', 'Dealer Locator', 'Search Results', 'View Directions - '+cname]);

    window.open('http://www.google.com/maps?q=to:' + trim(address1) + ', ' + trim(city) + ', ' + (zip) + ', ' + (state) + '&hl=en')
}






function addMarker(lat, lon, imgId, address1, city, state, zip, phone, cname) {
    i = i + 1;
    //    var myLatLon = lat.split(',');
    geocoder = new google.maps.Geocoder(lat, lon);
    var location = new google.maps.LatLng(lat, lon);

    map.setCenter(location);
    map.setZoom(8);


    var image = new google.maps.MarkerImage('common/images/markers/' + String(imgId) + '.png',
    // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(24, 39),
      new google.maps.Point(0, 0),
      new google.maps.Point(0, 32));


    var marker = new google.maps.Marker({
        map: map,
        draggable: false,
        icon: image,
        position: location
    });

    marker.setTitle(imgId);

    var contentString = '<h4>' + String(cname) + '</h4><p><b>Address: </b>' + String(address1) + '<br\>' + String(city) + ', ' + String(zip) + ', ' + String(state) + '<br\><b>Phone:</b> ' + String(phone) + '</p>';
    infowindow2[imgId] = new google.maps.InfoWindow({ content: contentString });



    //   var infowindow = new google.maps.InfoWindow(
    //      { content: contentString
    //          
    //      });
    google.maps.event.addListener(marker, 'mouseup', function() {

        if (infowindow2[getCurrent]) {
            infowindow2[getCurrent].close();
        }
        
        infowindow2[marker.getTitle()].open(map, marker);
        getCurrent = marker.getTitle();
        // infowindow2.open(map, marker);
    });

    google.maps.event.addListener(map, 'click', function() {
        if (infowindow2[getCurrent]) {
            infowindow2[getCurrent].close();
        }
    });

}

function visitWebsite(site, cname) {
    
    if (site != '' || site == null) {

        cname = cname.replace("'", "");
        _gaq.push(['_trackEvent', 'Dealer Locator', 'Search Results', 'Visit Website - ' + cname]);
        window.open('http://' + site); 
     }
    else {
    }

}

function checkdata() {

    var address = document.getElementById("ctl00_ctl00_main_content_BottomContent_search_locations").value;

    checkdatalist = "";

    if (address == "") {
        checkdatalist = checkdatalist + "Please enter location\n";
    }

    if (document.getElementById("ctl00_ctl00_main_content_BottomContent_rbType_0").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_rbType_1").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_rbType_2").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_rbType_3").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_rbType_4").checked == false) {
        checkdatalist = checkdatalist + "Please choose type\n";
    }



    if (document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_0").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_1").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_2").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_3").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_4").checked == false && document.getElementById("ctl00_ctl00_main_content_BottomContent_cbCategories_5").checked == false) {
        checkdatalist = checkdatalist + "Please choose Category\n";
    }


    if (checkdatalist != "") {
        alert(checkdatalist);
    }
    else {
        //  alert("mytest");
        getaddress(address);
    }
}


