var cPage = 1;
var nTotalPages;
var cSorting = '';

function gridPage(golf_city, nPageNum, totalRecords,isShowAll)
{
	cPage = nPageNum;
	
	reloadGolfCourses(golf_city, nPageNum, totalRecords,isShowAll);
}

function waitToLoad()
{
    window.location = "#map_top";
	document.getElementById("preloadingGraphic").style.display = "inline";
	document.getElementById("mapGolfCourseResults").style.display = "none";
	document.getElementById("showCount").style.display = "none";
	document.getElementById("showALL").style.display = "none";
	document.getElementById("total").style.display = "none";
	
	loadedStatus = 1;
}

function isLoaded()
{
	document.getElementById("preloadingGraphic").style.display = "none";
	document.getElementById("mapGolfCourseResults").style.display = "block";
	document.getElementById("showCount").style.display = "block";
	document.getElementById("showALL").style.display = "block";
	document.getElementById("total").style.display = "block";
	document.getElementById("mapGolfCourseLoadBox").style.backgroundColor = "";
	loadedStatus = 0;
}

function reloadGolfCourses(golf_city, nPage, totalRecords, isShowAll)
{
    waitToLoad();
	/*
	 * This function will call reloadGolfCourses() which is defined in
	 * sajax_functions.inc.php, then return of this function is used by
	 * showNewCourses funtion.
	 * golf_city, nPage, totalRecords, isShowAll are the arguments passed to
	 * the function reloadGolfCourses. This is Sajax property
	*/
    x_reloadGolfCourses(golf_city, nPage, totalRecords, isShowAll, showNewCourses);
}

function showNewCourses(results)
{
	isLoaded();
	spResults = results.split("//");
		
	var totalRecordsDisplay = 10;
	golf_city = spResults[3];
	
	document.getElementById("mapGolfCourseResults").innerHTML = spResults[0];
	
	// Removed previous marker
	for(mapItem in aData){
		map.removeOverlay(aData[mapItem]['marker']);
	}
	
	// Initialized the array containing marker details	
	//aData = new Array();
	//eval(spResults[4]);
	totalRecords = spResults[5];
	// Rebuild the marker array again
	//buildMarkers();
	if(spResults[2] != 0)
	{
	    nTotalPages = parseInt(spResults[2]); // means total number of records
		var nPages = Math.ceil(nTotalPages/totalRecordsDisplay)
		var sLinks = '';
		if(nPages > 1 )
		{
			if( (cPage+1) < nPages && cPage > 0)
			{
				sLinks = "<a href='javascript:void(0);' onclick='gridPage(\""+golf_city+"\","+(cPage-1)+","+totalRecords+",0);'><img src=\"/images/shell/golfing/cal_left_grey.gif\" border='0'  align='absmiddle' /></a>&nbsp;";
				sLinks += "Page "+(cPage+1)+" of "+nPages+"";
				sLinks += "&nbsp;<a href='javascript:void(0);' onclick='gridPage(\""+golf_city+"\","+(cPage+1)+","+totalRecords+",0);'><img src=\"/images/shell/golfing/cal_right_grey.gif\" border='0' align='absmiddle' /></a>";
			}
			else if((cPage+1) >= 1)
			{
				if((cPage+1) == nPages && cPage == 0)
				{
					sLinks += "Page "+(cPage+1)+" of "+nPages+"";
				}
				else if( (cPage+1) == nPages)
				{
					sLinks = "<a href='javascript:void(0);' onclick='gridPage(\""+golf_city+"\","+(cPage-1)+","+totalRecords+",0);'><img src=\"/images/shell/golfing/cal_left_grey.gif\" border='0'  align='absmiddle'/></a>&nbsp;";
					sLinks += "Page "+(cPage+1)+" of "+nPages+"";
				}
				else
				{
					sLinks = "Page "+(cPage+1)+" of "+nPages+"";
					sLinks += "&nbsp;<a href='javascript:void(0);' onclick='gridPage(\""+golf_city+"\","+(cPage+1)+","+totalRecords+",0);'><img src=\"/images/shell/golfing/cal_right_grey.gif\" border='0' align='absmiddle' /></a>";
				}
			}			
		}
		else
		{
			sLinks = "";
		}
				
		if(spResults[6]==0){
			nEndPage = (cPage+1)*totalRecordsDisplay;
			document.getElementById("total").innerHTML = sLinks;
		}else{
			nEndPage = nTotalPages;
			document.getElementById("showALL").innerHTML = ""
			document.getElementById("total").innerHTML = "";
		}
		
		if(nEndPage>nTotalPages){
			nEndPage = nTotalPages;
		}
		nStartPage = 1+(cPage*totalRecordsDisplay);		
		
		page_count = nStartPage+" - "+nEndPage+" of "+nTotalPages;
		
		document.getElementById("showCount").innerHTML = page_count;
	}
	else
	{
	   document.getElementById("mapGolfCourseResults").innerHTML = "<span class='regularNonBold'>No results found.</span>";		   
	   document.getElementById("total").innerHTML = "";
	   
	}
	// Initialized the array containing marker details
    aData = new Array();
    dataArr = spResults[4].split("||");
    /* Added code for finding errors in the adata array.
    It will check in the try block. if there are any errors then it goes to catch block.17-12-08*/
    for(l=0;l<dataArr.length-1;l++){
    try
    {
	eval(dataArr[l]);
    }
    catch(err)
    {
	
    }
}
// Rebuild the marker array again
buildMarkers();

}
