var xmlhttp;
var url;

function GetXmlHttpObject()
{
if (window.XMLHttpRequest)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}
function loadXMLDoc(url)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support Ajax! Some features may not work");
  return;
  }
xmlhttp.onreadystatechange=stateChanged;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function loadXMLDocRecommend(url)
{
xmlhttp=GetXmlHttpObject();
if (xmlhttp==null)
  {
  alert ("Your browser does not support Ajax! Some features may not work");
  return;
  }
xmlhttp.onreadystatechange=stateChangedRecommend;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}
function stateChanged()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {
        if(xmlhttp.responseText == '1')
        {
            formSuccess();
        }
        else
        {
            formFailed(xmlhttp.responseText);
        }
    }
  else
    {
        alert("Problem contacting Practo Server. Please try again later.");
    }
  }
}
function stateChangedRecommend()
{
if (xmlhttp.readyState==4)
  {
  if (xmlhttp.status==200)
    {

		document.getElementById('recommendResponse').style.display = "block";
        if(isNaN(parseInt(xmlhttp.responseText)))
        {
            document.getElementById('recommendResponse').innerHTML = xmlhttp.responseText;
        }
        else
        {
            clinicName = document.getElementById('clinicNameSpan').innerHTML;



			if(parseInt(xmlhttp.responseText)==1){

				document.getElementById('recommendation').innerHTML = "<span id=\"clinicNameSpan\">"+ clinicName+ "</span>" + " has been recommeded by you.";
			}
			else{

				document.getElementById('recommendation').innerHTML = "<span id=\"clinicNameSpan\">"+ clinicName+ "</span>" + " has been recommended by "+ xmlhttp.responseText+" people";
			}

			document.getElementById('recommendResponse').innerHTML = "";
            document.getElementById('recommendResponse').innerHTML = 'Thank You for your Recommendation';
        }
    }
  else
    {
    alert("Problem contacting TurboDoc Server. Please try again later.");
    }
  }
}

function initialise_init(){
    url = $("feedback").getAttribute("action");
    $("feedback").setAttribute("action","");
    hideShowCaptha();
}
function submitForm(){
    name = document.getElementById('name').value;
    email = document.getElementById('email').value;
    message = document.getElementById('message').value;
    recaptcha_response_field = document.getElementById('recaptcha_response_field').value;
    recaptcha_challenge_field = document.getElementById('recaptcha_challenge_field').value;
    clinicName = document.getElementById('clinic').value;
    clinicEmail = document.getElementById('clinicEmail').value;
    uniqueIdentifier = document.getElementById('uniqueIdentifier').value;

    var data = '{"Name":"' + name + '","Email":"' + email + '","Description":"' + message + '","recaptcha_response_field":"' + recaptcha_response_field + '","recaptcha_challenge_field":"' + recaptcha_challenge_field + '","ClinicName":"' + clinicName +'", "ClinicEmail":"'+ clinicEmail +'", "UniqueIdentifier":"'+ uniqueIdentifier +'"}';
    newurl = '/clinic/feedback/data/'+data;
    loadXMLDoc(newurl);
    return false;
}
function formSuccess(){
    document.getElementById('error').innerHTML = '<p>Your Feedback has been sent to the practice. Thank You<p>';
    document.getElementById('name').value = '';
    document.getElementById('email').value = '';
    document.getElementById('message').value = '';
    document.getElementById('recaptcha_response_field').value = '';
    Recaptcha.reload();
}
function formFailed(string){
    document.getElementById('error').innerHTML = string;
    Recaptcha.reload();
}
function hideShowCaptha(){
        $("message").onfocus = function(){

		$("recaptcha_widget_div").style.display = "block";
	}
}
function $(el) {
  return document.getElementById(el);
}
function initialize() {

	if (GBrowserIsCompatible()) {
            map = new GMap2(document.getElementById("mapCanvas"));
            map.setCenter(new GLatLng(clincLatLong[0],clincLatLong[1]), 15);
            map.addControl(new GSmallMapControl);
            map.addControl(new GNavLabelControl);

            geocoder = new GClientGeocoder();
            directions = new GDirections(map, $("directionsDiv"));

            var dentistIcon = new GIcon(G_DEFAULT_ICON);
            dentistIcon.image = clinicLogo;
            dentistIcon.iconSize = new GSize(25, 25);
            dentistIcon.shadowSize = new GSize(0, 0);
            markerOptions = { icon:dentistIcon };
            clincMarker = new GMarker(new GLatLng(clincLatLong[0],clincLatLong[1]),markerOptions)

            map.addOverlay(clincMarker);
            infoWindowText = clinicName + clinicAddress;
            clincMarker.openInfoWindowHtml(infoWindowText);
            GEvent.addListener(map, "click", mapClick);

            patientIcon = new GIcon(G_DEFAULT_ICON);
            patientIcon.image = "/images/clinic/patientIcon.png";
            patientIcon.iconSize = new GSize(25, 25);
            patientIcon.shadowSize = new GSize(0, 0);
            patientMarkerOptions = { icon:patientIcon,draggable: true };

	}
	attachEvents();
}
function attachEvents(){

	$("patientaddress").value = "Search..";

	$("patientaddress").onfocus = function(){
		if(this.value == 'Search..')
		{
			this.value = '';
		}
	}

	$("patientaddress").onblur= function(){
		if (this.value == '') {
			this.value = 'Search..';
		}
	}

	$("srch_clear").onclick = function(){
		if($("patientaddress").value!="" && $("patientaddress").value!="Search.."){
			addressSearch($("patientaddress").value);
		}
	}
	$("patientaddress").onkeypress = function(e){
			 e = e || window.event;
			if(e.keyCode==13){
				if($("patientaddress").value!="" && $("patientaddress").value!="Search.."){
					addressSearch($("patientaddress").value);
				}
			}

	}
}
function mapClick(overlay, latlng) {
	if (latlng != null){

		if(patientMarker!=null){
			map.removeOverlay(patientMarker);
		}
		patientMarker = new GMarker(latlng,patientMarkerOptions);
		attchEventToPatientmarker();

		map.addOverlay(patientMarker);
		geocoder.getLocations(latlng, showAddressFromClick);
		setMapZoom();
		showDirections();
	}
}
function attchEventToPatientmarker(){
	GEvent.addListener(patientMarker, "dragstart", function() {
  			map.closeInfoWindow();
  	});

	GEvent.addListener(patientMarker, "dragend", function(){
		patientLatLong = patientMarker.getLatLng();

		if (patientLatLong != null) {
    		geocoder.getLocations(patientLatLong, showAddressFromClick);
    		setMapZoom();
			showDirections();
		}
	});
}
function showDirections(){

	clinicPoint = clincMarker.getLatLng();
	patientPoint = patientMarker.getLatLng();
	gudduString = "to: " + clinicPoint['y'] + "," + clinicPoint['x'];
	fromString = "from: " + patientPoint['y'] + "," + patientPoint['x'] + " ";
	directions.load(fromString+gudduString);
	clincMarker.openInfoWindowHtml(clinicAddress);
}
function setMapZoom(){
	var latlngbounds = new GLatLngBounds();
	clinicPoint = clincMarker.getLatLng();
	patientPoint = patientMarker.getLatLng();

	latlngbounds.extend(clinicPoint);
	latlngbounds.extend(patientPoint);

   	map.setCenter( latlngbounds.getCenter( ), map.getBoundsZoomLevel( latlngbounds ) );
}
function showAddressFromClick(response) {
	if (!response || response.Status.code != 200)
	{
    	alert("This address could not be located");
  	}
    else
  	{
    	place = response.Placemark[0];
		patientMarker.openInfoWindowHtml(place.address);
	}
}
function addressSearch(address){
	if (geocoder) {
		geocoder.setBaseCountryCode("in");
		geocoder.getLatLng(address, function(point){
			if (!point) {
				alert(address + " not found");
			}
			else {

				if(patientMarker!=null){
					map.removeOverlay(patientMarker);
				}
				patientMarker = new GMarker(point,patientMarkerOptions);
				attchEventToPatientmarker();

				map.addOverlay(patientMarker);
				patientMarker.openInfoWindowHtml(address);
				setMapZoom();
				showDirections();






			}
		});
	}

}

window.onload = function(){
    initialise_init();
    if(mapExists){
        initialize();
    }
}
