/************************************************************************
 *                                                                      *
 *  emMaps.js   JavaScript to control Google Maps on the emContactForm  *
 *                                                                      *
 *  since: 25.02.2010   author: Kulikov Alexey <a.kulikov@gmail.com>    *
 *                                                                      *
 ************************************************************************/

/***
 *  Init Google Map
 ***/
function initialize(){
    //init map
    var map = new google.maps.Map2(document.getElementById("map1"));
    var geocoder = new GClientGeocoder(); //geocoder

    //customize map view
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    //open a desired address
    showAddress(addressToShow,markerHTML);

    /***
     *  Creates a Marker on the Map
     ***/
    function createMarker(latlng, message) {
        var marker = new GMarker(latlng);

        if(message){
            GEvent.addListener(marker,"click", function() {
                map.openInfoWindowHtml(latlng, message);
            });
        }

        return marker;
    }

    /***
     *  Add a Pointer at a desired Address on the Map
     ***/
    function showAddress(address, html) {
        if (geocoder) {
            if(!html) {
                html = address;
            }

            geocoder.getLatLng(
                address,
                function(point) {
                    if (!point) {
                        alert(address + " not found");
                    } else {
                        map.setCenter(point, mapZoomLevel);
                        var marker = new GMarker(point);
                        map.addOverlay(marker);
                        marker.openInfoWindowHtml(html);

                        GEvent.addListener(marker,"click", function() {
                            map.openInfoWindowHtml(point, html);
                        });
                    }
                }
            );

        }

    } //showAddress

}


/***
 *  Check if a mandatory field is filled or not
 ***/
function checkFieldInput(obj, skip){
    if(!skip){
        var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        if(($(obj).id == 'inputMail' && !$(obj).value.match(emailRegEx)) || $(obj).value.blank()){
            $(obj).up().addClassName('fieldCross');
            $(obj).up().removeClassName('fieldTick');
        }else{
            $(obj).up().addClassName('fieldTick');
            $(obj).up().removeClassName('fieldCross');
        }
    }

    if(!$('inputName').value.blank() && !$('inputMail').value.blank() && !$('inputMessage').value.blank()){
        $('submitButton').disabled = false;
    }else{
        $('submitButton').disabled = true;
    }
}


/***
 *  Sends the Contact form to the Server
 ***/
function sendContactForm(){
    new Ajax.Request('emContactForm.php', {
          method: 'post',
          parameters: {
              from: encodeURIComponent($('inputMail').value),
              name: encodeURIComponent($('inputName').value),
              phon: encodeURIComponent($('inputPhone').value),
              mess: encodeURIComponent($('inputMessage').value)
          },

        onSuccess: function(data) {
            $('allFields').hide();
            $('confirm').show();
        }
    });
}
