var perScrollData = 10;
var cntResult = 0;
var varCatID = 0;
var varCity = "";
var changeCity = 1;
var changeCategory= 1;

var getData = true;
function doStartSearch()
{	
	$('#searchResult').html('');
	$('#searchResult').html('<div id="refDiv"></div>');
	doSearch('', '');
	return false;
}

function setCity()
{
	changeCity = 1;
	varCity = $('#cmbLeftCity').val();	
	doSearch('', varCity);	
}

function setCat()
{
	changeCategory = 1;
	varCatID = $('#cmbLeftCat').val();
	doSearch(varCatID);
}

function doSearch(catID, city)
{	
	catID = $('#cmbLeftCat').val();
	city = $('#cmbLeftCity').val();

	objForm = document.frmSearch;	
	getData  = false;	
	
	if (catID != undefined || catID == '')
	{		
		varCatID = catID;
	}

	if (city != undefined && city != '')
	{
		varCity = city;
	}	

	

	//if (city != "" && city != undefined)
	
	/* if (varCity  != '')
	{
		$('#searchResult').html('');
		$('#searchResult').html('<div id="refDiv"></div>');
		$('#cityContainer').html('<img src="images/close.png" width="16" height="16" alt="Remove City" title="Remove City" align="absMiddle" onclick="javascript:window.location.reload();" style="cursor: pointer;"/>'+varCity+'<img src="images/arrowRight.jpg" width="16" align="absMiddle" style="padding-left: 4px;" />');
		//$('.searchInputBox input').width('240px');
	}
	*/

	/*
	if (catID > 0 && catID != undefined)
	{
		$('#searchResult').html('');
		$('#searchResult').html('<div id="refDiv"></div>');		
	}		

	if (city != ""  && city != undefined)
	{
		$('#searchResult').html('');
		$('#searchResult').html('<div id="refDiv"></div>');		
	}	
*/

	if (changeCity == 1)
	{
		$('#searchResult').html('');
		$('#searchResult').html('<div id="refDiv"></div>');					
	}	
	
	if (city != "")
	{
		$('#cityContainer').html('<img src="images/close.png" width="16" height="16" alt="Remove City" title="Remove City" align="absMiddle" onclick="javascript:window.location.href = \''+ HTTP_SERVER +'/search.php\'" style="cursor: pointer;"/>'+varCity+'<img src="images/arrowRight.jpg" width="16" align="absMiddle" style="padding-left: 4px;" />');
	}
	else
	{
		$('#cityContainer').html('');
	}

	if (changeCategory == 1)
	{
		$('#searchResult').html('');
		$('#searchResult').html('<div id="refDiv"></div>');		
	}
	
	objForm.hidCatID.value = varCatID;

	var startLimit = $("div[id^='result']").length;	
	var endLimit = parseInt(perScrollData);	
	
	// loder image show	
	$('#moreResults').html('<img src="'+ DIR_WS_IMAGES +'loader.gif">');
	$('#moreResults').show();
	// loder image show

	$.ajax({
		type: "POST",
		url: HTTP_SERVER+DIR_WS_HTTP+"ajaxSearch.php",
		data: "actionType=search&txtSearch=" + objForm.txtSearch.value + "&startLimit="+ startLimit + "&endLimit="+ endLimit+"&perScrollData="+perScrollData+"&catID="+ objForm.hidCatID.value+"&city="+ varCity,
		success: function(result){
			if (result != "")
			{				
				result = jQuery.trim(result);
			
				$('#searchResult #refDiv').html('');				
				if (result == 'END_OF_RESULT')
				{					
					var strMessage = "<div class='noSearch' >Result End - In order to show you the most relevant results, we have omitted some entries </div>";
					$(strMessage).insertBefore("#refDiv");
					//$('#searchResult').html(strMessage+'<div id="refDiv"></div>');
					//$('#moreResults').html(strMessage);
					$('#moreResults').html('more results..');
					$('#moreResults').hide();
					cntResult = 0;
				}
				else if (result == 'NO_RESULT')
				{
					var strMessage = "<div class='noSearch' >We did not find any results for <b>"+ objForm.txtSearch.value +"</b>.<br/><br/><b>Search tips:</b><br/>* Ensure words are spelled correctly.<br/>* Try rephrasing keywords or using synonyms.<br/>* Try less specific keywords.<br/>* Make your queries as concise as possible.</div>";

					$('#searchResult').html(strMessage+'<div id="refDiv"></div>');
					$('#moreResults').hide();
					cntResult = 0;
				}
				else 
				{
					var arrResult = result.split('<hr>');
					cntResult = arrResult.length;
					/*
					if (document.getElementById('pageTitle'))
					{
						document.getElementById('pageTitle').text = objForm.txtSearch.value + ' ' + document.getElementById('pageTitle').text;
					}
					if (document.getElementById('pageMetaDescription'))
					{
						document.getElementById('pageMetaDescription').text = objForm.txtSearch.value + ' ' + document.getElementById('pageMetaDescription').text;
					}
					if (document.getElementById('pageMetaKeywords'))
					{
						document.getElementById('pageMetaKeywords').text = objForm.txtSearch.value + ' ' + document.getElementById('pageMetaKeywords').text;
					}
					*/

					jQuery.each(arrResult, function(i) {
						var result = arrResult[i];
						//$(result).insertBefore("#refDiv").slideDown(1000);
						if (result != '')
						{
							//$(result).insertBefore("#refDiv").show();
							//$(result).insertBefore("#refDiv");				
							$(result).insertBefore("#refDiv").show();							
						}
					});	

					$('#moreResults').html('more results..');

					if (varCity != "")
					{
						$('#searchResult').highlight(varCity);
					}

					if (objForm.txtSearch.value != "")
					{
						var arrSearch = objForm.txtSearch.value.split(" ");
						if (arrSearch.length > 0)
						{
							var i=0;
							for (i=0;i<=arrSearch.length;i++)
							{
								$('#searchResult').highlight(arrSearch[i]);
							}
						}
						//$('#searchResult').highlight(objForm.txtSearch.value);
					}
					getData = true;
				}
			}					
			// loder image hide
		}
	});
	changeCity = 0;
	changeCategory = 0;
}

function getPageContent(divID)
{
	//alert("asd");
	//$("#demo1Btn").mopBox({'target':'#'+divID , 'w':270, 'h':240}); 
}
