﻿var normalSuggClassName = 'suggestionsListItemNormal';
var selectedSuggClassName = 'suggestionsListItemHigh';
var focusedElement = '';
//var hasFocus='no'

/*function requestSuggestions()
	{
		//alert('request suggestions');
		var myContext = 0;
		fullURL = parent.document.URL;
		if(fullURL.toLowerCase().indexOf("/christian/")>0)
			myContext = 1;
		
		getSuggestions(myContext);
		
	}*/
	/*function sendSearchSiteRequest()
	{
		var myContext = 0;
		fullURL = parent.document.URL;
		if(fullURL.toLowerCase().indexOf("/christian/")>0)
			myContext = 1;
		
		SearchSite('',0,myContext);        
		
		
	}*/


function KeyCheck(evt)
{
	var e = evt || window.event;
	//alert('evtSrc'+evt.target.id)
	//alert('WinEvtSrc'+window.event.srcElement.id)
	//alert('key check'+e);
	try
	{
		search = false;
		//alert(e.srcElement);        
		if(navigator.appName=='Netscape')
		{
			if(evt.target.id=='searchTerms')
				search = true;
		}
		else
		{
			if(window.event.srcElement.id=='searchTerms')
				search=true;
		}
		  
		if(search)
		{            
			var KeyID=e.keyCode? e.keyCode : e.charCode                    
			//handle UP DOWN arrow keys
			switch(KeyID)
			{
				case 38: //up arrow
					//alert('UP');
					selectPreviousSuggestion();
					return;
				case 40: //down arrow 
					//alert('DN');
					selectNextSuggestion();
					return;
				case 13: //enter
					//debugger;
					//alert('enter');
					//var flag =
					//if((document.getElementById('searchTerms')).focused)
					//alert('search terms have focus'); 
					setSelectedSuggestionAtIndex(selectedSuggestionIndex());
					
					sendSearchSiteRequest();
					//alert(flag)
					//                if(flag){
					//                    alert('flag is true');//sendSearchSiteRequest();
					//                    return;
					//                }
					break;        
			}   
			//alert(KeyID);
			 if((KeyID>=65 && KeyID<=90) ||  (KeyID>97 && KeyID<=107) || KeyID==46 || KeyID==8 || KeyID==0)
			 {
				//alert('KeyCheck: request Suggestions');
				requestSuggestions();
			 }
			//return false;
		}
	 }
	 catch(excp)
	 {
		//alert(excp);
	 }
	 finally{}
}


function selectedSuggestionIndex()
{
	var i = 0;
	var total = countSuggestions();
	
	for(i=0;i<total;i++)
	{
		var aSuggestion = document.getElementById('suggestionItem'+i);
		
		if(aSuggestion.className==selectedSuggClassName)
		{
			//alert(aSuggestion.className);
			return i;
		}
	}
	//alert(aSuggestion.className);
	return -1;    
}

function selectNextSuggestion()
{
	selectedIndex =  selectedSuggestionIndex();
	//alert('selected index: '+selectedIndex);
	if(selectedIndex >= -1)
		selectSuggestionIndex(selectedIndex+1)
}

function selectPreviousSuggestion()
{
	var selectedIndex = selectedSuggestionIndex();
	//alert('selected index: '+selectedIndex);
	if(selectedIndex > 0)
		selectSuggestionIndex(selectedIndex-1)
}

function selectSuggestionIndex(index)
{
	//alert('selectSuggestionIndex: '+index);
	var i = 0;
	var total = countSuggestions();
	for(i=0;i<total;i++)
	{
		if(i==index)
			(document.getElementById('suggestionItem'+index)).className = selectedSuggClassName;  
		else
			(document.getElementById('suggestionItem'+i)).className = normalSuggClassName;  
	}
	//alert('selected '+index);
}

function setSelectedSuggestionAtIndex(index)
{
	//alert((document.getElementById('suggestionItem'+index)).innerHTML);
	if(selectedSuggestionIndex()>=0)
	{
		selectSuggestion((document.getElementById('suggestionItem'+index)).innerHTML);
		popOutSuggestions();
		return true;
	}
	return false;
}

function countSuggestions()
{
	total = 0; 
	for(i=0;;i++)
	{
		if((document.getElementById('suggestionItem'+i))!=null)
			total = total+1;
		else
			break;        
	}
	//alert('total'+total)
	return total;
}


function getSelectedSearchType()
{
//    var e = document.getElementById('searchType');
//	
//	var sType = e.options[e.selectedIndex].value;
    //	return sType;
    //alert($("#<%= searchType.ClientID %>").val());
    return document.getElementById('ctl00_searchType').value;
//    return $("#<%= searchType.ClientID %>").val();
}
function getSuggestions(context)
{
	if(document.getElementById('searchTerms').value.length>2)
	{
		type=0;
		
		var selectedSearchType = getSelectedSearchType();
		//var selectedSearchType= document.getElementById(searchType).value;         
		if(selectedSearchType=='2')
			type = 2;
		else
		    type = 1;
		//alert(type);
		(document.getElementById('suggestions')).innerHtml = '';
		(document.getElementById('suggestions')).style.display="block";
		//searchTerms = document.getElementById('searchTerms').value;
		ajaxPostOffice(type, context, 'suggestions');
		//AjaxGetData('handlers/SuggestionServer.ashx?q='+searchTerms,sendToRecipient);
	}
	
}
function selectSuggestion(myVal)
{   
	document.getElementById('searchTerms').value = myVal;
	popOutSuggestions();
}
function popOutSuggestions()
{  
	try
	{
		//(document.getElementById('suggestions')).innerHtml='';//.style.display="none";        
		(document.getElementById('suggestions')).style.display = 'none';
	}
	catch(ex){}
	finally
	{
		//alert(((document.getElementById('suggestions')).innerHtml));
	}
}
function SearchSite(type, context)
{
	//alert('search site');        
	
	var selectedSearchType = getSelectedSearchType();
	//alert('selectedSearchType: '+selectedSearchType);
	ajaxPostSearchQuery(document.getElementById('searchTerms').value, type, context);
	//document.location="search-results.aspx";    
}
function SearchSite(myKeyword, type, context, pNumber) {

    var selectedSearchType = getSelectedSearchType();
    //alert(selectedSearchType);
    if (selectedSearchType == '2')
        type = 2;
    else if (selectedSearchType == '5')
        type = 5;
    else if (selectedSearchType == '6')
        type = 6;
    else
        type = 1;
//alert(type);
    var resultsPage = '';
    if (context == 1)
        resultsPage = '/christian/search-results.aspx';
    else
        resultsPage = '/search-results.aspx';
		//alert("http://" + currentWebsite + resultsPage + "?q=" + $("[id*='txtSearch']").val() + "&t=" + type + "&c=" + context);
		//document.location = "http://" + currentWebsite + resultsPage + "?q=" + $("[id*='txtSearch']").val() + "&t=" + type + "&c=" + context;
		var cForm = document.getElementById('aspnetForm');
		var sText = document.getElementById('ctl00_txtSearch').value;



		cForm.action = "";
		cForm.action = "http://" + currentWebsite + resultsPage + "?q=" + sText + "&t=" + type + "&c=" + context + "&p=" + pNumber;
		cForm.submit();
		
}  
function SubmitOnEnter(myfield, e)  
{
    alert('sumbit called');
	/*var keycode;  
	if (window.event)  
		keycode = window.event.keyCode;  
	else if (e)  
		keycode = e.which;  
	else  
		return true;  
	if (keycode == 13)  
	{  
		SearchSite();  
		return false;  
	}  
	else  */
		return true;  
}

// Function retrieved from: http://cass- hacks.com/articles/code/js_url_encode_decode/  
function URLEncode (clearString) {  
  var output = '';  
  var x = 0;
  clearString = clearString.toString();
  //debugger;
  //clearString = clearString.replace('\'', '');
  var regex = /(^[a-zA-Z0-9_.]*)/;  
  while (x < clearString.length) {  
	var match = regex.exec(clearString.substr(x));  
	if (match != null && match.length > 1 && match[1] != '') {  
		output += match[1];  
	  x += match[1].length;  
	} else {  
	  if (clearString[x] == ' ')  
		output += '+';  
	  else {  
		var charCode = clearString.charCodeAt(x);
		var hexVal = charCode.toString(16);
		if (hexVal != 2019) {
			output += '%' + (hexVal.length < 2 ? '0' : '') + hexVal.toUpperCase();
		}
	  }  
	  x++;  
	}
}

  return output;  
}  



function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
			 && match.length > 1
			 && match[1] != '') {
	binVal = parseInt(match[1].substr(1),16);
	thisString = String.fromCharCode(binVal);
	output = output.replace(match[1], thisString);
  }
  return output;
}
