// JavaScript Document
var singleResult;

function updateSearch() {

var searchString = document.getElementById("search_field").value;

if(searchString != "") {
	var params = "searchStr=" + searchString;
	document.getElementById("search_field").style.backgroundImage = "url(skins/Default/media/loading.gif)";
	$.ajax({
		type: "POST",
		url: "skins/about/search_db.php",
		data: "searchStr=" + encodeURIComponent(searchString),
		async: false,
		success: function(result){
			var searchResponse = result;
			
			if(searchResponse != "") {
				var singleResult = searchResponse.split("<-!->");
				var finishedResult = "";
				var splitSingleResult;
				for(x = 0; x < singleResult.length - 1; x++) {
					splitSingleResult = singleResult[x].split("---");
					finishedResult = finishedResult + "<div class=\"single_search_row\"><a href=\"ecom-prodshow/" + splitSingleResult[1] + ".html\">" + splitSingleResult[0] + "</div>";
				}
				
				document.getElementById("searchResults").innerHTML = finishedResult;
				
				document.getElementById("search_field").style.backgroundImage = "none";
				
				$('#searchResults').slideDown(250);
			} else {
				document.getElementById("searchResults").innerHTML = "<span class=\"single_search_row\">No results found.</span>";
				document.getElementById("search_field").style.backgroundImage = "none";
				$('#searchResults').slideDown(250);
			}
		},
		error: function (xhr, ajaxOptions, thrownError){
			document.getElementById("search_field").style.backgroundImage = "none";
		}
	});
} else {
	$('#searchResults').slideUp(250, function() {
    	document.getElementById("searchResults").innerHTML = "";
 	});	
}
}

function updatePeopleSearch() {
	
	
	var ajaxRequest;  // The variable that makes Ajax possible!
	
 try
 {
  var ajaxRequest = new XMLHttpRequest();
 }
 catch(err1)
 {
  var ieXmlHttpVersions = new Array();
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.7.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.6.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.5.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.4.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp.3.0";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "MSXML2.XMLHttp";
  ieXmlHttpVersions[ieXmlHttpVersions.length] = "Microsoft.XMLHttp";

  var i;
  for (i=0; i < ieXmlHttpVersions.length; i++)
  {
   try
   {
    var ajaxRequest = new ActiveXObject(ieXmlHttpVersions[i]);
    //document.getElementById("Content").innerHTML="<h1>Using " + ieXmlHttpVersions[i] + "</h1>";
    break;
   }
   catch (err2)
   {
    //document.getElementById("searchResults").innerHTML = "Instant search is currently offline";
   }
  }
 }

ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		var searchResponse = ajaxRequest.responseText;
		
		if(searchResponse != "") {
			
			singleResult = searchResponse.split("<-!->");
			var finishedResult = "";
			var splitSingleResult;
			for(x = 0; x < singleResult.length - 1; x++) {
				
				splitSingleResult = singleResult[x].split("---");
				
				finishedResult = finishedResult + "<div class=\"single_search_row\"><a href=\"javascript:selectPerson('" + x + "');\">" + splitSingleResult[1] + " (" + splitSingleResult[0] + ")" + "</a></div>";
			}
			
			
			document.getElementById("peopleSearchResults").innerHTML = finishedResult;
			
			document.getElementById("people_search_field").style.backgroundImage = "none";
			
			$('#peopleSearchResults').slideDown(250);
			
		} else {
			document.getElementById("people_search_field").style.backgroundImage = "none";
			document.getElementById("peopleSearchResults").innerHTML = "No results found.";
			$('#peopleSearchResults').slideDown(250);
		}
	} else if(ajaxRequest.readyState == 1 ||ajaxRequest.readyState == 2 || ajaxRequest.readyState == 3) {
		document.getElementById("people_search_field").style.backgroundImage = "url(skins/Default/media/loading.gif)";
	}
}

var searchString = document.getElementById("people_search_field").value;

if(searchString != "") {
	var url = "/skins/Default/search_people_db.php";
	var params = "searchStr=" + searchString;

	ajaxRequest.open("POST",url,true);
	ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajaxRequest.send(params);
}

}

function selectPerson(id) {
	splitPeopleResult = singleResult[id].split("---");
	document.getElementById("people_search_name").innerHTML = splitPeopleResult[0];
	document.getElementById("people_search_country").innerHTML = splitPeopleResult[1];
	document.getElementById("people_search_img").src = "skins/senior_management_team/media/" + splitPeopleResult[2];
	document.getElementById("people_search_img").style.display = "inline";
	document.getElementById("people_search_desc").innerHTML = splitPeopleResult[3];
	document.getElementById("people_search_info").innerHTML = "";
	
	Cufon.refresh();
	
	$('#peopleSearchResults').slideUp(250);
}

function searchFocus() {
	var elem = document.getElementById("search_field");
	if(elem.value == "SEARCH") {
		elem.value = "";
	}
}

function searchBlur() {
	var elem = document.getElementById("search_field");
	if(elem.value == "") {
		elem.value = "SEARCH";
		$('#searchResults').slideUp(250, function() {
    		document.getElementById("searchResults").innerHTML = "";
 		});
	}
}

function closeLoginBox() {
	$.facebox.close();	
}
