var phraseArray;

function searchArray() {
	var txtPhraseResults = "";
	
	var pattExp = $('.searchTextBox').val();

	var stringTester=new RegExp(/^([\w@\s"'\(\)&%+=]*)$/);

	if(stringTester.test(pattExp)){	
	
		pattExp = pattExp.replace(/\'/g, "'");
		pattExp = pattExp.replace(/'/g, "\\'");
		pattExp = pattExp.replace(/\\\(/g, "(");
		pattExp = pattExp.replace(/\(/g, "\\(");
		pattExp = pattExp.replace(/\\\)/g, ")");
		pattExp = pattExp.replace(/\)/g, "\\)");
	
		var patt1=new RegExp(pattExp, 'g');
		rCounter = 0;
		if (phraseArray != null) {
		  for (c = 0;c <phraseArray.length; c++) {				
			  if (phraseArray[c].match(patt1) != null) {
				  txtTmpPhrase = phraseArray[c].replace(patt1,'<strong>' +  $('.searchTextBox').val() + '</strong>');
				  txtPhraseResults = txtPhraseResults + '<li><a rel="' + phraseArray[c] + '" href="javascript:void(0);">' + txtTmpPhrase + '</a></li>'
				  rCounter++;
				  if (rCounter == 10) {
					  break;	
				  }
			  }
		  }
		}
		if (txtPhraseResults != "") {
			$('.predictiveList').html(txtPhraseResults);
			document.getElementById('dvResults').style.display = 'block';
		} else {
			document.getElementById('dvResults').style.display = 'none';
		}
	}
}

function getSearchResults(searchKeyword) {
	PredictiveSearchWS.GetPredictiveSearches(searchKeyword, onSucceeded, onFailed);
}

function parseSearchResults(searchResults) {
	phraseArray = searchResults.split('|');
	searchArray();
}	

function onSucceeded(result,userContext,methodName) {
	/* on successful call, fill the div with with the information obtained*/
	//$get('dvResults').innerHTML = result;
	parseSearchResults(result);
}

function onFailed(error, userContext, methodName) {
	/* if not successful then show error msg */
	//alert("An error occurred")
	document.getElementById('dvResults').style.display = 'none'
}

function OnSelectedPhrase(id, phrase) {
			  
	// if one phrase is selected from the list then fill the textbox with the phrase selected
	document.getElementById(id).value = phrase;

	//hide div with results
	document.getElementById('dvResults').style.display = 'none'

	//set focus on the search box
	document.getElementById(id).focus();
	
	//Submit form on click
	$('.btnFilterSearch').trigger('click');            
}

$(document).ready(function() {
try{
	$('.predictiveList li a').live('click', function(event) {
		$('.ActivePredictiveSearch').val($(this).attr('rel'));
	});	
	
	$('.ActivePredictiveSearch').blur(function(event) {
		$('#dvResults').fadeOut('100'); 
	});	

	var predList = -1;
	var wScroll = 200;
	var initSearch = '';
	$('.ActivePredictiveSearch').keyup(function(e) {
		var predListLI = $('.predictiveList li');
		var predListTextBox = $('.ActivePredictiveSearch');
		listLength = predListLI.length;
		switch (e.keyCode) {
			case 40:
				//down key
				predListLI.removeClass('on');
				predList++;
				if (predList == listLength) {
					predList = 0;
				}
				listItem = predListLI.eq(predList)
				listItem.addClass('on');
				predListTextBox.val(listItem.children('a').attr('rel'));
				break;
			case 38:
				//up key
				predListLI.removeClass('on');
				predList--;
				if (predList < 0) {
					predList = listLength - 1;
				}
				listItem = predListLI.eq(predList)
				listItem.addClass('on');
				predListTextBox.val(listItem.children('a').attr('rel'));

				break;
			default:
				if ($(this).val().length > 2) {
					//if keyword is not empty then search in ws for related phrases
					if ($(this).val().substring(0, 3) != initSearch || $(this).val().length < 3) {
						//Enter AJAX here
						predList = -1;
						initSearch = $(this).val().substring(0, 3);
						getSearchResults($(this).val().substring(0, 3));
					}
					if ($(this).val().length >= 3) {
						if ($(this).val().substring(0, 3) != initSearch) {
							initSearch = $(this).val().substring(0, 3);
							//Enter AJAX here
							predList = -1;
							getSearchResults($(this).val().substring(0, 3));
						}
						else{
							predList = -1;
							searchArray();
						}
					} else {
						predList = -1;
						searchArray();
					}
				}
				else {
					//do not show div if not inserted at least 3 characters
					document.getElementById('dvResults').style.display = 'none'
				}
		}
	});
}catch(ex){
	 // console.log(ex);
}			
});

