	var map = new GMap2(document.getElementById("map"));
	var points = Array();
	var totalDistance= 1;
	var counter = 0;
	var request2 = null;
   	 try {
       		request2 = new XMLHttpRequest();
   	 } 
	catch (trymicrosoft) 
	{
        	try {
            		request2 = new ActiveXObject("Msxml2.XMLHTTP");
       		 }
			 catch (othermicrosoft) 
			{
          	  	try {
              	  	request2 = new ActiveXObject("Microsoft.XMLHTTP");
           		 } catch (failed) {
              	 	 request2 = null;
           	 	}
        	}
   	 }

GPolygon.prototype.Contains = function(point) {
  var j=0;
  var oddNodes = false;
  var x = point.lng();
  var y = point.lat();
  for (var i=0; i < this.getVertexCount(); i++) {
    j++;
    if (j == this.getVertexCount()) {j = 0;}
    if (((this.getVertex(i).lat() < y) && (this.getVertex(j).lat() >= y))
    || ((this.getVertex(j).lat() < y) && (this.getVertex(i).lat() >= y))) {
      if ( this.getVertex(i).lng() + (y - this.getVertex(i).lat())
      /  (this.getVertex(j).lat()-this.getVertex(i).lat())
      *  (this.getVertex(j).lng() - this.getVertex(i).lng())<x ) {
        oddNodes = !oddNodes
      }
    }
  }
  return oddNodes;
}

// === A method which returns the approximate area of a non-intersecting polygon in square metres ===
// === It doesn't fully account for spechical geometry, so will be inaccurate for large polygons ===
// === The polygon must not intersect itself ===
GPolygon.prototype.Area = function() {
  var a = 0;
  var j = 0;
  var b = this.Bounds();
  var x0 = b.getSouthWest().lng();
  var y0 = b.getSouthWest().lat();
  for (var i=0; i < this.getVertexCount(); i++) {
    j++;
    if (j == this.getVertexCount()) {j = 0;}
    var x1 = this.getVertex(i).distanceFrom(new GLatLng(this.getVertex(i).lat(),x0));
    var x2 = this.getVertex(j).distanceFrom(new GLatLng(this.getVertex(j).lat(),x0));
    var y1 = this.getVertex(i).distanceFrom(new GLatLng(y0,this.getVertex(i).lng()));
    var y2 = this.getVertex(j).distanceFrom(new GLatLng(y0,this.getVertex(j).lng()));
    a += x1*y2 - x2*y1;
  }
  return Math.abs(a * 0.5);
}

// === A method which returns the length of a path in metres ===
GPolygon.prototype.Distance = function() {
  var dist = 0;
  for (var i=1; i < this.getVertexCount(); i++) {
    dist += this.getVertex(i).distanceFrom(this.getVertex(i-1));
  }
  return dist;
}

// === A method which returns the bounds as a GLatLngBounds ===
GPolygon.prototype.Bounds = function() {
  var bounds = new GLatLngBounds();
  for (var i=0; i < this.getVertexCount(); i++) {
    bounds.extend(this.getVertex(i));
  }
  return bounds;
}

// === A method which returns a GLatLng of a point a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
GPolygon.prototype.GetPointAtDistance = function(metres) {
  // some awkward special cases
  if (metres == 0) return this.getVertex(0);
  if (metres < 0) return null;
  var dist=0;
  var olddist=0;
  for (var i=1; (i < this.getVertexCount() && dist < metres); i++) {
    olddist = dist;
    dist += this.getVertex(i).distanceFrom(this.getVertex(i-1));
  }
  if (dist < metres) {return null;}
  var p1= this.getVertex(i-2);
  var p2= this.getVertex(i-1);
  var m = (metres-olddist)/(dist-olddist);
  return new GLatLng( p1.lat() + (p2.lat()-p1.lat())*m, p1.lng() + (p2.lng()-p1.lng())*m);
}

// === A method which returns the Vertex number at a given distance along the path ===
// === Returns null if the path is shorter than the specified distance ===
GPolygon.prototype.GetIndexAtDistance = function(metres) {
  // some awkward special cases
  if (metres == 0) return this.getVertex(0);
  if (metres < 0) return null;
  var dist=0;
  var olddist=0;
  for (var i=1; (i < this.getVertexCount() && dist < metres); i++) {
    olddist = dist;
    dist += this.getVertex(i).distanceFrom(this.getVertex(i-1));
  }
  if (dist < metres) {return null;}
  return i;
}

// === A function which returns the bearing between two vertices in decgrees from 0 to 360===
// === If v1 is null, it returns the bearing between the first and last vertex ===
// === If v1 is present but v2 is null, returns the bearing from v1 to the next vertex ===
// === If either vertex is out of range, returns void ===
GPolygon.prototype.Bearing = function(v1,v2) {
  if (v1 == null) {
    v1 = 0;
    v2 = this.getVertexCount()-1;
  } else if (v2 ==  null) {
    v2 = v1+1;
  }
  if ((v1 < 0) || (v1 >= this.getVertexCount()) || (v2 < 0) || (v2 >= this.getVertexCount())) {
    return;
  }
  var from = this.getVertex(v1);
  var to = this.getVertex(v2);
  if (from.equals(to)) {
    return 0;
  }
  var lat1 = from.latRadians();
  var lon1 = from.lngRadians();
  var lat2 = to.latRadians();
  var lon2 = to.lngRadians();
  var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ) );
  if ( angle < 0.0 ) angle  += Math.PI * 2.0;
  angle = angle * 180.0 / Math.PI;
  return parseFloat(angle.toFixed(1));
}




// === Copy all the above functions to GPolyline ===
GPolyline.prototype.Contains             = GPolygon.prototype.Contains;
GPolyline.prototype.Area                 = GPolygon.prototype.Area;
GPolyline.prototype.Distance             = GPolygon.prototype.Distance;
GPolyline.prototype.Bounds               = GPolygon.prototype.Bounds;
GPolyline.prototype.GetPointAtDistance   = GPolygon.prototype.GetPointAtDistance;
GPolyline.prototype.GetIndexAtDistance   = GPolygon.prototype.GetIndexAtDistance;
GPolyline.prototype.Bearing              = GPolygon.prototype.Bearing;
function loadMap() {
	
		if (GBrowserIsCompatible()) {
		map.setCenter(new GLatLng(1,1),2);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		
		}
	}

function getDistanceMine(){
	var lat1 =document.getElementById("lat").value; 
	var lat2=document.getElementById("lat2").value;
	var lng1=document.getElementById("lng").value;
	var lng2=document.getElementById("lng2").value;
	var value1=(new GLatLng(lat1,lng1).distanceFrom(new GLatLng(lat2, lng2)));
	var bds = new GLatLngBounds(new GLatLng(lat1,lng1), new GLatLng(lat2, lng2));
	var centreLat = (parseFloat(lat1) + parseFloat(lat2))/2 + '' ;
	var centreLong = (parseFloat(lng1) + parseFloat(lng2))/2 + '' ;
	map.setCenter(new GLatLng(centreLat ,centreLong), 5);	
	map.setZoom(map.getBoundsZoomLevel(bds,5));
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	var polyline = new GPolyline([new GLatLng(lat1,lng1),new GLatLng(lat2,lng2)],"#ff0000",5,0.75);
	map.addOverlay(polyline);
	returngetDistanceMine(value1);
	return value1;
}

	
function loadDistance()
{


	if(checkValidation())
		{
		                                            
		   
			map = new GMap2(document.getElementById("map"));	
			loadMap() ;
			var destFrom;
			var destTo;
			destFrom=document.getElementById('destFrom').value;
			destFrom+=','+document.getElementById('country').value;
			destTo=document.getElementById('destTo').value;
			destTo+=','+document.getElementById('country').value;
			showAddress(destFrom,map,'lat','lng');
			showAddress(destTo,map,'lat2','lng2');
			
			if ( document.getElementById("page_block_94") != null )
      {			
		  document.getElementById("page_block_94").style.display="visible";
      document.getElementById("page_block_94").style.height="";
      document.getElementById("page_block_94").style.left="";
      document.getElementById("page_block_94").style.position="";
      //document.getElementById("page_block_94").focus();
      
      document.getElementById("page_block_91").style.display="visible";
      document.getElementById("page_block_91").style.height="";
      document.getElementById("page_block_91").style.left="";
      document.getElementById("page_block_91").style.position="";
      
      
       }
      if ( document.getElementById("page_block_132") != null )
      { 
      document.getElementById("page_block_132").style.display="visible";
      document.getElementById("page_block_132").style.height="";
      document.getElementById("page_block_132").style.left="";
      document.getElementById("page_block_132").style.position="";
      
      document.getElementById("ProfilePhotoBlock").style.display="none";
      
      document.getElementById("page_block_133").style.display="visible";
      document.getElementById("page_block_133").style.height="";
      document.getElementById("page_block_133").style.left="";
      document.getElementById("page_block_133").style.position="";
      }
      
      
      	
		}    
return false;
}

function returngetDistanceMine(val)
{	

	var url = "ajaxCompaniesSearch.php";
	var compId=0;
	var ProfileViewID=0;
	var state=document.getElementById('contriesStates').value;
	var start_loc=document.getElementById('destFrom').value;
	var end_loc=document.getElementById('destTo').value;
	var params='';
	if(document.getElementById('compId').value > 1)
		compId=document.getElementById('compId').value;
	
  if(document.getElementById('ProfileViewID').value > 1)
		ProfileViewID=document.getElementById('ProfileViewID').value;
    	
			 
		document.getElementById("distance").innerHTML =  "Miles: "+(val/1609.344).toFixed(2);
		
		totalDistance=(val/1609.344).toFixed(2);
		
	params+='totalDistance='+totalDistance;
	params+='&countryName='+document.getElementById('country').value;
	params+='&state='+state;
	params+='&start_loc='+start_loc;
	params+='&end_loc='+end_loc;
	params+='&compId='+compId;
	params+='&ProfileViewID'+ProfileViewID;
	
	request2.open("POST", url, true);
    
	request2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request2.setRequestHeader("Content-length", params.length);
	request2.setRequestHeader("Connection", "close");
	request2.onreadystatechange =getResponse;
   	request2.send(params);
	return false;
}

function getResponse()
	{
    		if (request2.readyState == 4) {
		var response = request2.responseText;
 		document.getElementById("companyList").innerHTML=response;
		}
 	}
function showAddress(address,map,lat,lng)
{	
	var lat1;
	var lat2;
	var long1;
	geocoder = new GClientGeocoder();
	map.addControl(new GScaleControl());
	geocoder.getLatLng(address,function(point) {
				if (!point) 
				{
					//alert(address + " not found");
				} 
				else
				{
					document.getElementById(lat).value=point.lat().toFixed(5);
					document.getElementById(lng).value=point.lng().toFixed(5);
					points[counter] = point;
					counter++;
					lat1=(point.lat().toFixed(5));
					long1=point.lng().toFixed(5);				
					if ( counter == 1 ) map.addOverlay(new GMarker(point,G_START_ICON));
					if ( counter == 2 ) map.addOverlay(new  GMarker(point,G_END_ICON));
					if(points.length==2){
						start() ;
						points.length=0;counter=0;
					}
					
				}
		}
		);
	
}

function createMarker(point, name, address, type) 
{
	var marker = new GMarker(point, '');
	var html = "<b>" + name + "</b> <br/>" + address+"<img src=\"caricon.png\"  width=\"80px\" height=\"70px\">";
	GEvent.addListener(marker, 'click', function() {
			marker.openInfoWindowHtml(html);
		}
	);
      	return marker;
}

 if (GBrowserIsCompatible()) {
 	loadMap() ;
      var dirn = new GDirections();
      var poly;
      var eol;
      var marker;
      var k=0;
      

      GEvent.addListener(dirn,"load", function() {
	poly=dirn.getPolyline();
        eol=poly.Distance();
        map.setCenter(poly.getVertex(0),8);
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());
	map.addControl(new GScaleControl());
        map.addOverlay(new GMarker(poly.getVertex(0),G_START_ICON));
        map.addOverlay(new GMarker(poly.getVertex(poly.getVertexCount()-1),G_END_ICON));
	map.addOverlay(poly) ;
	returngetDistanceMine(eol);
      });


      GEvent.addListener(dirn,"error", function() {
	getDistanceMine() ;
      });

      function start() {
        var startpoint = document.getElementById("destFrom").value;
        var endpoint = document.getElementById("destTo").value;
			var country=','+document.getElementById('country').value;
			startpoint+=country;
			endpoint+=country;
			dirn.loadFromWaypoints([startpoint,endpoint],{getPolyline:true,getSteps:true});
      }

    }
//  for getting states name according to seleted country
function getStatesNames(obj)
{
	
	var url = "ajaxGetStates.php";
	var params=''; 
	params+='countryCode='+obj.value;
	params+='&page=search';
	request2.open("POST", url, true);
    
	request2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request2.setRequestHeader("Content-length", params.length);
	request2.setRequestHeader("Connection", "close");
	request2.onreadystatechange =getStateResponse;
   	request2.send(params);
}

function getStateResponse()
	{
    		if (request2.readyState == 4) 
		{
		var response = request2.responseText;
		document.getElementById('stateNameDiv').innerHTML=response;
 		
		}
 	}

function getCompanyList(){

	
	var url = "ajaxGetCompanyList.php";
	var params=''; 
	params+='countryId='+document.getElementById('country').value;
	params+='distance='+
	request2.open("POST", url, true);
    
	request2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request2.setRequestHeader("Content-length", params.length);
	request2.setRequestHeader("Connection", "close");
	request2.onreadystatechange =getCompanyResponse;
   	request2.send(params);

}
function getCompanyResponse(){

	if (request2.readyState == 4) 
		{
		var response = request2.responseText;
		alert(response);
 		}

}

function checkValidation(){

	if(document.getElementById('country').value=="Select Country")
	{
		alert("Please select country name.");
		return false;
	}
  else	
		return true;
}
function ajaxForPaging(recordeno,obj)
{
	 
	var url = "ajaxCompaniesSearch.php";
	var start_loc=document.getElementById('destFrom').value;
	var end_loc=document.getElementById('destTo').value;
	params='recordeno='+recordeno;
	params+='&countryName='+document.getElementById('country').value;
	params+='&state='+document.getElementById('contriesStates').value;
	params+='&start_loc='+start_loc;
	params+='&end_loc='+end_loc;
	
	params+='&totalDistance='+totalDistance;
	if(obj==2)
		{
		params+='&flag=unset';
		}
	 
  	request2.open("POST", url, true);
    	request2.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	request2.setRequestHeader("Content-length", params.length);
	request2.setRequestHeader("Connection", "close");
	request2.onreadystatechange =updatersvp;
	request2.send(params);
}

function updatersvp()
{
	if (request2.readyState == 4) {
	var response = request2.responseText;
	document.getElementById('companyList').innerHTML = response;
	
	}
}
