
var goToSearch = function()	{
	var searhWords = document.getElementById('search').value;
	location = 'search.html?' + searhWords;
}

var startSearch = function(searchWords){
	
	var xmlhttp;
    if (window.XMLHttpRequest) {
        xmlhttp = new XMLHttpRequest();
    }
    else {
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			handleLoginResp(xmlhttp.responseText, searchWords);
        }
    }
    xmlhttp.open("GET", "assets/data/search.json", true);
    xmlhttp.send();
}

var handleLoginResp = function(response, searchWords)	{
	
	theResults = eval(response);
	
	var theOutput = '<div class="search-header"><p>Search results for: <strong>'+searchWords+'</strong></p></div>';
	
	for ( i = 0; i < theResults.length; i++ )	{
		if ( theResults[i].PageText.indexOf(searchWords) != -1 )	{
			
			var theSnippet = theResults[i].PageText.substring(0, 200);
			
			theOutput += '<div class="results-container"><div class="search-title"><a href="'+theResults[i].PageUrl+'">'+theResults[i].PageName+'</a></div><div class="search-desc"><p>'+theResults[i].PageDesc+'</p></div><div class="substring">'+theSnippet+' ...</div></div>';
		}
	}
	
	if ( theOutput == '<div class="search-header"><p>Search results for: <strong>'+searchWords+'</strong></p></div>' )	{
		theOutput += '<div class="results-container"><div class="search-desc"><p>Sorry, no results matched your search terms. Please try again.</p></div></div>';
	}
	
	document.getElementById('search-content').innerHTML = theOutput;

}
