/************************************************************************
 *                                                                      *
 *  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(!$('inputFirstName').value.blank() && !$('inputLastName').value.blank() && !$('inputMail').value.blank() )
	{
	   $('submitButton').disabled = false;
    }else{
        $('submitButton').disabled = true;
    }
	

}

function checkBox (obj) {
/*	if ($('inputMoreInfoA').checked) {$('inputMoreInfoA').value = "Yes"} else {$('inputMoreInfoA').value = "No"}
	if ($('inputMoreInfoB').checked) {$('inputMoreInfoB').value = "Yes"} else {$('inputMoreInfoB').value = "No"}
	if ($('inputMoreInfoC').checked) {$('inputMoreInfoC').value = "Yes"} else {$('inputMoreInfoC').value = "No"}
	if ($('inputMoreInfoD').checked) {$('inputMoreInfoD').value = "Yes"} else {$('inputMoreInfoD').value = "No"}
	if ($('inputMoreInfoE').checked) {$('inputMoreInfoE').value = "Yes"} else {$('inputMoreInfoE').value = "No"}
	if ($('inputMoreInfoF').checked) {$('inputMoreInfoF').value = "Yes"} else {$('inputMoreInfoF').value = "No"}
	if ($('inputMoreInfoG').checked) {$('inputMoreInfoG').value = "Yes"} else {$('inputMoreInfoG').value = "No"}*/
}


/***
 *  Sends the Contact form to the Server
 ***/
function sendContactForm(){
	
	/*if ($('inputMoreInfoA').checked) {$('inputMoreInfoA').value = "Yes"} else {$('inputMoreInfoA').value = "No"}
	if ($('inputMoreInfoB').checked) {$('inputMoreInfoB').value = "Yes"} else {$('inputMoreInfoB').value = "No"}
	if ($('inputMoreInfoC').checked) {$('inputMoreInfoC').value = "Yes"} else {$('inputMoreInfoC').value = "No"}
	if ($('inputMoreInfoD').checked) {$('inputMoreInfoD').value = "Yes"} else {$('inputMoreInfoD').value = "No"}
	if ($('inputMoreInfoE').checked) {$('inputMoreInfoE').value = "Yes"} else {$('inputMoreInfoE').value = "No"}
	if ($('inputMoreInfoF').checked) {$('inputMoreInfoF').value = "Yes"} else {$('inputMoreInfoF').value = "No"}
	if ($('inputMoreInfoG').checked) {$('inputMoreInfoG').value = "Yes"} else {$('inputMoreInfoG').value = "No"}*/
	
    new Ajax.Request('emContactForm.php', {
          method: 'post',
          parameters: {
            /*  from:   	encodeURIComponent($('inputMail').value),
			  prefix: 	encodeURIComponent($('inputPrefix').value),		 
			  fname:  	encodeURIComponent($('inputFirstName').value),	 
			  lname:  	encodeURIComponent($('inputLastName').value),	 
			  address:  encodeURIComponent($('inputAddress').value),	 
			  city: 	encodeURIComponent($('inputCity').value),		 
			  state: 	encodeURIComponent($('inputState').value),		 
			  zip: 		encodeURIComponent($('inputZip').value),		 
			  country: 	encodeURIComponent($('inputCountry').value),			 
              hphone:  	encodeURIComponent($('inputHomePhone').value),
              cphone:  	encodeURIComponent($('inputCellPhone').value),
              visit:  	encodeURIComponent($('inputVisit').value),
              restate: 	encodeURIComponent($('inputRealEstate').value),
              infoa:  	encodeURIComponent($('inputMoreInfoA').value),
              infob:  	encodeURIComponent($('inputMoreInfoB').value),
              infoc:  	encodeURIComponent($('inputMoreInfoC').value),
              infod:  	encodeURIComponent($('inputMoreInfoD').value),
              infoe:  	encodeURIComponent($('inputMoreInfoE').value),
              infof:  	encodeURIComponent($('inputMoreInfoF').value),
              infog:  	encodeURIComponent($('inputMoreInfoG').value),
              mess:  	encodeURIComponent($('inputMessage').value),
			  plan:		encodeURIComponent($('inputPlanVisit').value),
			  datev:	encodeURIComponent($('inputDateVisit').value)*/
          },

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