/************************************************************************
 *                                                                      *
 *  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 (window.$ === window.jQuery){ //jQuery specific
        _JQcheckFieldInput(obj, skip);
    }else{
        if(!skip){
            var fail = false;
            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');
                fail = true;
            }else{
                $(obj).up().addClassName('fieldTick');
                $(obj).up().removeClassName('fieldCross');
            }
        }
    
        if(!$('inputName').value.blank() && !$('inputMail').value.blank() && !$('inputMessage').value.blank() && !fail){
            $('submitButton').disabled = false;
        }else{
            $('submitButton').disabled = true;
        }
    }
}


/***
 *  Sends the Contact form to the Server
 ***/
function sendContactForm(){
    if (window.$ === window.jQuery){ //jQuery specific
        _JQsendContactForm();
    }else{    
        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();
            }
        });
    }
}



/***
 *  Check if a mandatory field is filled or not
 ***/
function _JQcheckFieldInput(obj, skip){
    
    if(!skip){
        var fail = false;
        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 == ''){
            $(obj).parent().addClass('fieldCross');
            $(obj).parent().removeClass('fieldTick');
            fail = true;
        }else{
            $(obj).parent().addClass('fieldTick');
            $(obj).parent().removeClass('fieldCross');
        }
    }

    if($('#inputName').val() && $('#inputMail').val() && $('#inputMessage').val() && !fail){
        $('#submitButton').removeAttr('disabled');
    }else{
        $('#submitButton').attr('disabled','true');
    }
}


/***
 *  Sends the Contact form to the Server
 ***/
function _JQsendContactForm(){
    $.post(
        "emContactForm.php", { 
            from: encodeURIComponent($('#inputMail').val()),
            name: encodeURIComponent($('#inputName').val()),
            phon: encodeURIComponent($('#inputPhone').val()),
            mess: encodeURIComponent($('#inputMessage').val())
        },
        
        function(data){
            $('#allFields').hide();
            $('#confirm').fadeIn();
        });
}
