﻿/* Directory scripting
--------------------------  */

function addbusiness() {
    // Disable button and show animation
    disableform(document.getElementById("profileform"));
    document.getElementById("ajaxloader").className = "";

    // Validate the form entries
    if (!validate()) {
        enableform(document.getElementById("profileform"));
        document.getElementById("ajaxloader").className = "hidden";
        return false;
    }

    var mtitle, mfname, msname, memail, mbusiness;

    if (document.getElementById("signuptitle")) {
        mtitle = document.getElementById("signuptitle").value;
        mfname = document.getElementById("signupforename").value;
        msname = document.getElementById("signupsurname").value;
        memail = document.getElementById("signupemail").value;
        mbusiness = document.getElementById("signupbusinesstype").value;
    }
    else {
        mtitle = "";
        mfname = "";
        msname = "";
        memail = "";
        mbusiness = "";
    }

    if (document.getElementById("busdesc").value.length > 750) {
        alert("The maximum business description size is 750 characters. Your description currently contains " + document.getElementById("busdesc").value.length + " characters. Please trim your description and try again.");
        document.getElementById("ajaxloader").className = "hidden";
        enableform(document.getElementById("profileform"));
        return false;
    }

    // Save details
    TheSniffa.Add_Directory_Business(document.getElementById("buscat").value, document.getElementById("bustitle").value, document.getElementById("busdesc").value, document.getElementById("busurl").value, document.getElementById("addcountry").value, document.getElementById("addregion").value, document.getElementById("addstreet").value, document.getElementById("addcity").value, document.getElementById("addpostcode").value, document.getElementById("loclatitude").value, document.getElementById("loclongitude").value, mtitle, mfname, msname, memail, mbusiness, addbusinesscallback);
}

function addbusinesscallback(response)
{
    // Check for errors
    if(response.error != null) {
    	alert("A system error occurred. Please try again. Error message: " + response.error.Message);
        // Enable button and hide animation
        enableform(document.getElementById("profileform"));
        document.getElementById("ajaxloader").className = "hidden";
		return;
	}
    // Get the response value
    if (response.value == "error url") {
        alert("A business with the specified URL has already been registered. If you wish to edit your entry please sign in and go to your my account page.");
        enableform(document.getElementById("profileform"));
        document.getElementById("ajaxloader").className = "hidden";
    }
    else if (response.value == "error email") {
        alert("The e-mail address specified has already been registered on TheSniffa.com. To add your business and link it to your profile please sign in first and then add your business.");
        enableform(document.getElementById("profileform"));
        document.getElementById("ajaxloader").className = "hidden";
    }
    else {
        window.location.href = "/sniffa-directory/" + response.value;
    }
    
}

function canceladdbusiness(cat) {
    window.location.href = "/sniffa-directory/" + cat;
}

/*
    Geo location
*/

var map = new GMap2(document.getElementById("map_canvas"));
var geocoder = new GClientGeocoder();

function showAddress() {
    if (GBrowserIsCompatible()) {
        
        //Get address
        var address = document.getElementById("addstreet").value + " " + document.getElementById("addcity").value + " " + document.getElementById("addpostcode").value;
        
        geocoder = new GClientGeocoder();
        geocoder.getLatLng(
            address,
            function(point) {
                if (!point) {
                    alert(address + " not found");
                } else {
                    map = new GMap2(document.getElementById("map_canvas"));
                    map.setCenter(point, 15);
                    map.setUIToDefault();
                    geocoder.getLocations(point, showDragAddress)                    
                    document.getElementById("map_canvas").className = "showmap"
                }
            }
        );
    }

}

function showDragAddress(response) {
    map.clearOverlays();
    if (!response || response.Status.code != 200) {
        alert("Status Code:" + response.Status.code);
    } else {
        place = response.Placemark[0];
        point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);

        var marker = new GMarker(point, {draggable: true});

        GEvent.addListener(marker, "dragstart", function() {
            map.closeInfoWindow();
        });

        GEvent.addListener(marker, "dragend", function(dpoint) {
            geocoder.getLocations(dpoint, showDragAddress)
            //marker.openInfoWindowHtml("Just bouncing along..." + geocoder.getLocations(dpoint, showAddress));
        });

        map.addOverlay(marker);
        marker.openInfoWindowHtml("<h2>" + place.address + "</h2><p>Exact match? If not you can drag the marker to the correct location.</p>");

        document.getElementById("loclatitude").value = point.y;
        document.getElementById("loclongitude").value = point.x;
    }
}

