var xmlHttp
var index
var maxSuggest
var txtKeyword
var searchButtonID

function showCustomer(str, e, obj, buttonID, endPt,e)
{ 

var url=endPt;
txtKeyword = obj;
searchButtonID = buttonID;

xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
  
  
  var strKey = "";
for (i=0; i<=str.length-1; i++) {
 if (str.substring(i,i+1).charCodeAt() >= 32 && str.substring(i,i+1).charCodeAt() <= 47 || str.substring(i,i+1).charCodeAt() >= 58 && str.substring(i,i+1).charCodeAt() <= 64 || str.substring(i,i+1).charCodeAt() >= 91 && str.substring(i,i+1).charCodeAt() <= 96 || str.substring(i,i+1).charCodeAt() >= 123 && str.substring(i,i+1).charCodeAt() <= 126) {
   //str = str.substring(i,i+1)
   str.replace(str.substring(i,i+1), "");
   
  } else {
   strKey = strKey + str.substring(i,i+1);
   }
}
    str = strKey;
var strLength = str.length;
  	 

  //keyCode==40 is Down
  
 var key = window.event ? window.event.keyCode : e.which;
if (key==40) {

	if (index < maxSuggest) {

	if (strLength > 1) {
		index += 1;
		
		document.getElementById(index).className = 'suggest_link_over';
		 
		obj.value = document.getElementById(index).innerHTML.replace(/<.+?>/gim,"");
		if (index > 0) {
			document.getElementById(index-1).className = 'suggest_link';
		}
		
	}
		
	}	else {
		index = 0;
		if (maxSuggest > 0 && strLength > 1) {
		document.getElementById(index).className = 'suggest_link_over';
		txtKeyword.value = document.getElementById(index).innerHTML.replace(/<.+?>/gim,"");
		document.getElementById(maxSuggest).className = 'suggest_link';
		}
	}

}

//keyCode==38 is Up
else if (key==38) {
 
	if (index > 0) {
	if (strLength > 1) {
		index -= 1;
		document.getElementById(index).className = 'suggest_link_over';
		txtKeyword.value = document.getElementById(index).innerHTML.replace(/<.+?>/gim,"");
		if (index < maxSuggest) {
			document.getElementById(index+1).className = 'suggest_link';
		}
		}
	}	else {
		index = maxSuggest;
		if (maxSuggest>0 && strLength > 1) {
		document.getElementById(index).className = 'suggest_link_over';
		txtKeyword.value = document.getElementById(index).innerHTML.replace(/<.+?>/gim,"");
		document.getElementById(0).className = 'suggest_link';
		}
	}
	
}
else {

if (strLength>1) {  

	
	url=url+"?q="+encodeURI(str);
	url=url+"&sid="+Math.random();

	xmlHttp.onreadystatechange=handleSearchSuggest;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	

	} else {
	document.getElementById('txtHint').innerHTML = '';
	document.getElementById('txtHint').className = 'hiddenCss';
	}
}
}


//Called when the AJAX response is returned.
function handleSearchSuggest() {
			
	if (xmlHttp.readyState == 4) {

		var ss = document.getElementById("txtHint");
		ss.innerHTML = '';
		var str = new Array();
		
		//*********************/ Problem********************************//
		var strKey = "";
		
		//var strValue = document.getElementById('ctl00_txtKeyword').value;
		var strValue = txtKeyword.value;
        for (i=0; i<=strValue.length-1; i++) {
           if (strValue.substring(i,i+1).charCodeAt() >= 32 && strValue.substring(i,i+1).charCodeAt() <= 47 || strValue.substring(i,i+1).charCodeAt() >= 58 && strValue.substring(i,i+1).charCodeAt() <= 64 || strValue.substring(i,i+1).charCodeAt() >= 91 && strValue.substring(i,i+1).charCodeAt() <= 96 || strValue.substring(i,i+1).charCodeAt() >= 123 && strValue.substring(i,i+1).charCodeAt() <= 126) {
              strValue.replace(strValue.substring(i,i+1), "");
             } else {
              strKey = strKey + strValue.substring(i,i+1);
              }
           }
               strValue = strKey;
               var strLength = strValue.length;
		//*********************/ Problem********************************//
		
		//var userTXT = document.getElementById('ctl00_txtKeyword').value.replace(/\(/gim,"\\(");
		//userTXT = userTXT.replace(/\)/gim,"\\)");
		userTXT = strValue;
		if (txtKeyword.value=='') {
			document.getElementById('txtHint').innerHTML = '';
		}
		else {

			str = xmlHttp.responseText.split("!");
			
			for(i=0; i < str.length - 1; i++) {
				//Build our element string.  This is cleaner using the DOM, but
				//IE doesn't support dynamically added attributes.
				var suggest = '<div style="word-wrap: break-word;" id="' + i + '" onmouseover="javascript:suggestOver(this, this.innerHTML);" ';
				suggest += 'onmouseout="javascript:suggestOut(this);" ';
				suggest += 'onclick="setSearch(this.innerHTML); return false;" ';
				suggest += 'class="suggest_link">' + str[i].replace(eval("/"+ txtKeyword.value.replace(/([\W])/g,'').replace(/^\s\s*/, '').replace(/\s\s*$/, '') + "/gi"),'<span class="SuggestRedBold">$&</span>') + '</div>';
				ss.innerHTML += suggest;
				
			}
			index = -1;
			maxSuggest = str.length - 2;
			document.getElementById('txtHint').className = 'txtHint';
		}
		
		if (str.length <= 1) {
		document.getElementById('txtHint').className = 'hiddenCss';
		}
	}
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


//Mouse over function
function suggestOver(div_value, value) {
	div_value.className = 'suggest_link_over';
//	document.getElementById('ctl00_txtKeyword').value = value.replace(/<.+?>/gim,'');
}
//Mouse out function
function suggestOut(div_value) {
	div_value.className = 'suggest_link';
}
//Click function
function setSearch(value) {

	txtKeyword.value = value.replace(/<.+?>/gim,'');
	document.getElementById('txtHint').innerHTML = '';
	document.getElementById('txtHint').className = 'hiddenCss';

    __doPostBack(searchButtonID, '');
    //alert(searchButtonID);
   
//	var strPriceFrom = "&PriceFrom=" + document.getElementById('ctl00_hidPriceFrom').value;
//	var strPriceTo = "&PriceTo=" + document.getElementById('ctl00_hidPriceTo').value;
//	var strSizeFrom = "&SizeFrom=" + document.getElementById('ctl00_hidSizeFrom').value;
//	var strSizeTo = "&SizeTo=" + document.getElementById('ctl00_hidSizeTo').value;
//	var strRentFrom = "&RentFrom=" + document.getElementById('ctl00_hidRentFrom').value;
//	var strRentTo = "&RentTo=" + document.getElementById('ctl00_hidRentTo').value;
	
	
//	var strBuy = "PropertyList.aspx?isClear=1" + strPriceFrom + strPriceTo + "&SellType=S&keyword=" + document.getElementById('ctl00_txtKeyword').value;
//	var strRent = "PropertyList.aspx?isClear=1" + strRentFrom + strRentTo + "&SellType=R&keyword=" + document.getElementById('ctl00_txtKeyword').value;
//	var strBuy = "PropertyList.aspx?isClear=1&SellType=S&keyword=" + document.getElementById('ctl00_txtKeyword').value;
//	var strRent = "PropertyList.aspx?isClear=1&SellType=R&keyword=" + document.getElementById('ctl00_txtKeyword').value;
//	alert(document.getElementById('ctl00_hidPriceFrom').value);
//	if (document.getElementById('ctl00_rbType_0').checked == true) {
//	window.location.href(strBuy);
//	} else {
//	window.location.href(strRent);
//	}
    //__doPostBack('ctl00_imgbtnQSrh','');
    
//   return WebForm_FireDefaultButton(event, "ctl00_imgbtnQSrh");
	
}

//function HideSuggest(value) {
////    setSearch(document.getElementById('ctl00_txtKeyword').value);
//    alert(document.getElementById('0').onblur);
//	document.getElementById('txtHint').innerHTML = '';
//	document.getElementById('txtHint').className = 'hiddenCss';
//}


function hideSuggest(e)
{
	var mouseX = e.clientX + document.body.scrollLeft;
	var mouseY = e.clientY + document.body.scrollTop;
	var hintX = document.getElementById("txtHint").offsetLeft;
	var hintY = document.getElementById("txtHint").offsetTop;
	var hintWidth = document.getElementById("txtHint").offsetWidth;
	var hintHeight = document.getElementById("txtHint").offsetHeight;
//	alert(mouseX + " " + mouseY);
	if ((mouseX > hintX) && (mouseX < hintX + hintWidth) && (mouseY > hintY) && (mouseY < hintY + hintHeight)) {
	} else {
		/*document.getElementById('txtHint').innerHTML = '';
		document.getElementById('txtHint').className = 'hiddenCss';*/		
		setTimeout("hideActual()", 500);
	}
}

function hideActual()
{

		document.getElementById('txtHint').innerHTML = '';
		document.getElementById('txtHint').className = 'hiddenCss';
}
