var page=0;
var flag=false;
var markers = [];
var mCount =0 ;
 try {
       		request2 = new XMLHttpRequest();
   	 } 
	catch (trymicrosoft) 
	{
        	try {
            		request2 = new ActiveXObject("Msxml2.XMLHTTP");
       		 }
			 catch (othermicrosoft) 
			{
          	  	try {
              	  	request2 = new ActiveXObject("Microsoft.XMLHTTP");
           		 } catch (failed) {
              	 	 request2 = null;
           	 	}
        	}
   	 }


function companyListPagination(obj,pageId)
	{
	
// 			for(i=0; i< markers.length;i++)
// 			{
// 				map.removeOverlay(markers[i]);
// 			}
// 			mCount =0;


	var url = "ajaxDirectory.php";
	var params=''; 
	var cCity='';
	var cCountry='';
	var cName='';
	params+='pageId='+pageId;
	if(obj.dir_keyword.value !="")
	params+='&dir_keyword='+obj.dir_keyword.value;

	if(obj.dir_company.value !="")
	params+='&dir_company='+obj.dir_company.value;

	if(obj.dir_country.value !="Select Country")
	params+='&dir_country='+obj.dir_country.value;
	
	
	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 =getDirectoryResp;
   	request2.send(params);

	return false;
	}

function getDirectoryResp()
	{
    		if (request2.readyState == 4) 
		{
		var response = request2.responseText;
// 		alert(response);
		document.getElementById('directoryList').innerHTML=response;
		if(flag)
		{
		map = new GMap2(document.getElementById("dirmap"));
		if (GBrowserIsCompatible()) {
		map.setCenter(new GLatLng(1,0),2);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
			}
			for(i=0;i<5;i++)
			{
				cCity=document.getElementById('cCity'+i).value;	
				cCountry=document.getElementById('cCountry'+i).value;
				cName=document.getElementById('cName'+i).value;
				cCompany=document.getElementById('cCompany'+i).value;
				cState=document.getElementById('cState'+i).value;
				var company_desc_new='';
				var company_desc=document.getElementById('cDesc'+i).value;
				company_desc=company_desc.toString().substring(0,60);
// 				if(company_desc.toString().length()>60)
// 					{	
// 						desc_length=(company_desc.toString().length())/60;
// 						if(desc_length >3)
// 							desc_length=3;
// 						for(si=0;si < desc_length;si++)
// 						{
// 							company_desc_new+=company_desc.toString().substring((si+59),50);;
// 						}
// 					}alert(company_desc_new+company_desc.toString().length());
// 				company_desc=company_desc_new;
				cRate=document.getElementById('cCompanyId'+i).value;
				
				cUrl=document.getElementById('cUrl'+i).value;
				forUrl=cUrl;
				cUrl='<a href='+cUrl+'>'+cUrl+'</a>';
				cRatelink='To check out rates'+' : <a href='+cRate+'> Click here </a>';
				var imageHtml=getImage(i,cRate);
				if(cState!="")
					if(cCity!="")
						var place=cCity + ','+cState+ ',' + cCountry;
					else
						var place=cState+ ',' + cCountry;	
				else
					if(cCity!="")
						var place=cCity + ',' + cCountry;
					else 
						var place=cCountry;
				var contact =getContact(i,cCity,cState,cCountry,cCompany,forUrl);
// 				var contact ='<b>Owner Name:</b>'+ cName +'<br><br><b>'+'Company name :</b>'+cCompany+'<br>'+cUrl;
				if(cCity!="")
				showAddress(place,map,'lat','lng',contact,company_desc,imageHtml,cRatelink);	
// 		alert(html);
			}

		}

		}

	


 	}

/**
*
*	code for google map for showing companies locations
*/
var map;
var n=0;
function loadDirMap() 
	{
		map = new GMap2(document.getElementById("dirmap"));
		if (GBrowserIsCompatible()) {
		map.setCenter(new GLatLng(1,0),2);
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		flag=true;
		for(i=0;i<5;i++)
			{
			
			cCity=document.getElementById('cCity'+i).value;	
			cCountry=document.getElementById('cCountry'+i).value;
			cName=document.getElementById('cName'+i).value;
			cCompany=document.getElementById('cCompany'+i).value;
			cState=document.getElementById('cState'+i).value;

			company_desc=document.getElementById('cDesc'+i).value;
			company_desc=company_desc.toString().substring(0,60);
			cRate=document.getElementById('cCompanyId'+i).value;
			 
			cUrl=document.getElementById('cUrl'+i).value;
			forUrl=cUrl;
			cUrl='<a href='+cUrl+'>'+cUrl+'</a>';
			cRatelink='To check out rates'+' : <a href='+cRate+'> Click here </a>';
			var imageHtml=getImage(i,cRate);
			if(cState!="")
				if(cCity!="")
					var place=cCity + ','+cState+ ',' + cCountry;
				else
					var place=cState+ ',' + cCountry;	
			else
				if(cCity!="")
					var place=cCity + ',' + cCountry;
				else 
					var place=cCountry;
			var contact=getContact(i,cCity,cState,cCountry,cCompany,forUrl);
// 			var contact ='<b>Owner Name:</b>'+ cName +'<br><br><b>'+'Company name :</b>'+cCompany+'<br>'+cUrl;
			if(cCity!="")
			showAddress(place,map,'lat','lng',contact,company_desc,imageHtml,cRatelink);			
		}
		}
	}	
function getContact(val,cCity,cState,cCountry,cCompany,forUrl)
	{
		var 	html='';
		var cPhone1=cUrl=document.getElementById('cPhone1'+val).value;

		html='<table border="0"><tr>';//Start table
		var  cImageCount=document.getElementById('cImageCount'+val).value;
			
			if(cImageCount<1)
				{
					image_path='media/images/profile/default.jpg';	
				}
			else
				{
					var cMemberId=document.getElementById('cMemberId'+val).value;
					var image_path='media/images/profile/'+cMemberId+'/thumb_';
					var img_var_name='cImageName_'+val+'_'+0;
					image_path+=document.getElementById(img_var_name).value;
					
				}
			html+='<td style="padding: 1px;">&nbsp;<a href="'+forUrl+'"><img src="'+image_path+'" height="50" width="50" border="1" /> </a>&nbsp;</td>';
			html+='<td>'+cCompany+',';
			if(cState!="")
				{
					if(cCity!="")
						var place=cCity + ','+cState+ ',' + cCountry;
					else
						var place=cState+ ',' + cCountry;	
				}
			else
				{
					if(cCity!="")
						var place=cCity + ',' + cCountry;
					else 
						var place=cCountry;
				}
			html+=place;
			if(cPhone1!="")
				{
					html+=',<br>Phone No:'+cPhone1;
				}	
			var cUrl='<br /><a href='+forUrl+'>'+forUrl+'</a>';
			html+=cUrl+'&nbsp;</td></table>';
			return html;
	}
function showAddress(address,map,lat,lng,contact,company_desc,imageHtml,cRate)
{	
	
	var lat1;
	var lat2;
	var long1;
	geocoder = new GClientGeocoder();
	map.addControl(new GScaleControl());
	geocoder.getLatLng(address,function(point) {
				if (!point) 
				{
					if(address!="")
					  {
					  //alert(address + " not found");
					  }
				} 
				else
				{
					
				//createTLabel1(new GLatLng(point.lat().toFixed(5),point.lng().toFixed(5)),htmlData);
				mk = createTabbedMarker(new GLatLng(point.lat().toFixed(5),point.lng().toFixed(5)),[contact , company_desc ,imageHtml, cRate],["Contact","Description","Photos","Rates"],1);
				map.addOverlay(mk);
				map.setCenter(new GLatLng(point.lat().toFixed(5),point.lng().toFixed(5)),6);
				markers[mCount] = mk;
				mCount++;	
				}
					
				}
		
		);
	
}
	function getImage(val,cUrl)
		{
			var imageHtml='';
			var cImageCount=document.getElementById('cImageCount'+val).value;
			
			if(cImageCount<1)
				{
					imageHtml='<div> <b> No image found. </b></div>';	
					return imageHtml;
				}
			imageHtml='<div>';
			var cMemberId=document.getElementById('cMemberId'+val).value;
			if(cImageCount > 4)
				cImageCount=4;
			
			for(k=0;k<cImageCount;k++)
				{
					
					var  image_path='media/images/profile/'+cMemberId+'/thumb_';
					var img_var_name='cImageName_'+val+'_'+k;
						image_path+=document.getElementById(img_var_name).value;
						imageHtml+='<a href="'+cUrl+'"><img src="'+image_path+'" height="50" width="50" border="1" /> </a>&nbsp;&nbsp;';
				}
			imageHtml+='</div>';
		return imageHtml;
		}
// function createTLabel1(point,html) {
//         var label = new TLabel();
//          label.id = 'Label '+n;
//          label.anchorLatLng = point;
//          label.anchorPoint = 'bottomCenter';
//          label.content = '<img src="anicon.gif" width=20 height=34 />';
//          label.markerOffset = new GSize(-1,-5);
//          map.addTLabel(label);
//          GEvent.addDomListener(document.getElementById(label.id), "click", function() {
//            map.openInfoWindowHtml(point, html, {pixelOffset: new GSize(0,-34)} );
//          });         
//          n++;
//       }

function createTabbedMarker(point,htmls,labels,num) {     
        var icon = new GIcon();
        icon.image = "anicon.gif";
        icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
        icon.iconSize = new GSize(20, 34);
        icon.shadowSize = new GSize(37, 34);
        icon.iconAnchor = new GPoint(6, 20);
        icon.infoWindowAnchor = new GPoint(5, 1);
           
        var marker = new GMarker(point,icon);
        GEvent.addListener(marker, "click", function() {
          // adjust the width so that the info window is large enough for this many tabs
          if (htmls.length > 2) {
            htmls[0] = '<div style="width:'+htmls.length*88+'px">' + htmls[0] + '</div>';
          }
          var tabs = [];
          for (var i=0; i<htmls.length; i++) {
            tabs.push(new GInfoWindowTab(labels[i],htmls[i]));
          }
          marker.openInfoWindowTabsHtml(tabs);
        });
        return marker;
      }

function getMap()
{
	var url = "example1.php";
	var params=''; 
	
	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 =getMapRes;
   	request2.send(params);
}
function getMapRes()
	{
		if (request2.readyState == 4) 
		{
		var response = request2.responseText;
		alert(response);
		document.getElementById('dirmap1').innerHTML=response;
 		
		}

	}
/*function getDirectoryResult(obj,pageId)
	{
// 		alert(obj.name);
		var url = "ajaxDirectory.php";
		var params=''; 
		params+='dir_submit=Search';
		if(obj.dir_keyword.value !="")
		params+='&dir_keyword='+obj.dir_keyword.value;

		if(obj.dir_company.value !="")
		params+='&dir_company='+obj.dir_company.value;

		if(obj.dir_country.value !="Select Country")
		params+='&dir_country='+obj.dir_country.value;

	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 =getDirectorySearchRes;
   	request2.send(params);
	return false;
	}*/
// function getDirectorySearchRes()
// 	{
// 		if (request2.readyState == 4) 
// 		{
// 		var response = request2.responseText;
// // 		alert(response);
// 		document.getElementById('directoryList').innerHTML=response;
// 
// 		}
// 
// 	}

