var searchString_val = '';
var searchByIndustry_val = '';
var searchByActivity_val = '';
var searchByBrand_val = '';
var global_resCount;

function getHomeSearchResults(ID1, ID2, ID3, brandId, resultCount, isIndustryDropdown, trigger) {
	global_resCount = resultCount;
	
	if(trigger){
		if(trigger == 'dropdown'){
			$(ID1).val("");
		}
	}
	if($(ID1).val() != ""){
		searchString_val = $(ID1).val();
	}
	else{
		searchString_val = "";
	}
	if(searchString_val != ""){
		
		$('#SearchByIndustry option:selected').attr("selected", false);
		$('#SearchByActivity option:selected').attr("selected", false);
		
		$('#SearchByIndustry').val("");
		$('#SearchByActivity').val("");
		
		$('#SearchByIndustry option:first').attr("selected", true);
		$('#SearchByActivity option:first').attr("selected", true);
	}
	if($(ID2).val() != "Search+by+Industry"){
		searchByIndustry_val = $(ID2).val();
	}
	else{
		searchByIndustry_val = "";
	}
	if($(ID3).val() != "Search+by+Activity"){
		searchByActivity_val = $(ID3).val();
	}
	else{
		searchByActivity_val = "";
	}
	if(brandId != ""){
		searchByBrand_val = brandId;
	}
	else{
		searchByBrand_val = "";
	}
	
	

	if (!isIndustryDropdown) {
		runSearch();
	} else {
		$.ajax({
			type: "POST",
			url: "/_commongoal/cg_content/Products_ajax.cfc",
			dataType: "json",
			data: {
			   method: 'getApplicationsByIndustry',
			   industries: searchByIndustry_val,
			   brand: searchByBrand_val,
			   currentlySelectedApplications: searchByActivity_val
			},
			success: processAppList
		})
	}
}


var processAppList = function(results) {
	var appsHTML = "";
	appsHTML += '<option class="optionsClass" value="">Search by Activity</option>';
	appsHTML += '<option class="optionsClass" value="">ALL</option>';
	$.each(results, function(i, currentApplication) {
		appsHTML += '<option class="optionsClass" value="' + currentApplication.APPLICATIONID + '"';
		if (currentApplication.CHECKED) {
			appsHTML += ' SELECTED ';	
		}
		appsHTML += '>' + currentApplication.NAME + '</option>';
		
	});
	
	$('#SearchByActivity').html(appsHTML);
	
	runSearch();
	
}

var runSearch = function() {
	$.ajax({
		type: "POST",
		url: "/_commongoal/cg_content/Products_Ajax.cfc",
		dataType: "json",
		data: {
		   method: 'searchProducts',
		   industries: searchByIndustry_val,
		   applications: searchByActivity_val,
		   brand: searchByBrand_val,
		   searchString: searchString_val
		},
		success: displayHomePageResults
	});
}



var goToProductDetail = function(productId, brandId) {
	if(brandId != ""){
		searchByBrand_val = brandId;
	}
	else{
		searchByBrand_val = "";
	}
	
	var switchToURL = "/index.cfm/products/?productId=";
		switchToURL += productId;
		switchToURL += "&industries=";
		switchToURL += searchByIndustry_val;
		switchToURL += "&applications=";
		switchToURL += searchByActivity_val;
		switchToURL += "&brand=";
		switchToURL += searchByBrand_val;
		switchToURL += "&searchString=";
		switchToURL += searchString_val;
	
	location.href=switchToURL;
}

var displayHomePageResults = function(results) {
	//alert(global_resCount);
	var newHTML = "<strong>Are you looking for:</strong><ul>";
	if(results.length >= 1){
		$.each(results, function(i, currentProject) {
			if(i < global_resCount){
				var switchToURL = "/index.cfm/products/?productId=";
				switchToURL += currentProject.PRODUCTID;
				switchToURL += "&industries=";
				switchToURL += searchByIndustry_val;
				switchToURL += "&applications=";
				switchToURL += searchByActivity_val;
				switchToURL += "&brand=";
				switchToURL += searchByBrand_val;
				switchToURL += "&searchString=";
				switchToURL += searchString_val;
				
				newHTML += '<li><a href="' + switchToURL + '">';
				newHTML += currentProject.PRODUCTNAME;
				newHTML += '</a></li>';
			}
		});

	}
	/*else if(results.length == 1){
		var switchToURL = "/index.cfm/products/?productId=";
		switchToURL += results[0].PRODUCTID;
		switchToURL += "&industries=";
		switchToURL += searchByIndustry_val;
		switchToURL += "&applications=";
		switchToURL += searchByActivity_val;
		switchToURL += "&brand=";
		switchToURL += searchByBrand_val;
		switchToURL += "&searchString=";
		switchToURL += searchString_val;
		
		location.href = switchToURL;
	}*/
	else{
		newHTML += '<li>No Results Found</li>';
	}
	newHTML += '</ul>';
	if ((results.length) > 2) {
	newHTML += '<div id="prodSearchListLink"><a href="';
	
	var switchToURL2 = "/index.cfm/products/";
	switchToURL2 += "?industries=";
	switchToURL2 += searchByIndustry_val;
	switchToURL2 += "&applications=";
	switchToURL2 += searchByActivity_val;
	switchToURL2 += "&brand=";
	switchToURL2 += searchByBrand_val;
	switchToURL2 += "&searchString=";
	switchToURL2 += searchString_val;
	
	newHTML += switchToURL2;
	newHTML += '">view complete results</a> <a href="';
	newHTML += switchToURL2;
	newHTML += '"><img src="/graphics/orangeBullet.gif" border="0" /></a></div>';
	}
	$('#Home_AreYouLookingFor').html(newHTML);
		
}

function searchInputEnterKey(evt, inputVal) {
	/*alert(inputVal);*/
	evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		if(evt.keyCode == 13){
			getHomeSearchResults('#HomeSearchText', '#SearchByIndustry', '#SearchByActivity', '', 3);
		}
	}
}


