var TE_search_submitOK = true;

var TE_defaultSearchText = "What can we help you find?";

// Except for i18n and type-ahead functionalilty, do not use Dojo functions.  
//	This allows this javascript to be used on sites that do not use/load Dojo.

if (typeof TE_searchTerms == "undefined") {
	TE_searchTerms = "";
}

function addLoadListener(fn) {
	if (typeof window.addEventListener != 'undefined') {
		window.addEventListener('load', fn, false);
	} else if (typeof document.addEventListener != 'undefined') {
		document.addEventListener('load', fn, false);
	} else if (typeof window.attachEvent != 'undefined') {
		window.attachEvent('onload', fn);
	} else {
		var oldfn = window.onload;
		if (typeof window.onload != 'function') {
			window.onload = fn;
		} else {
			window.onload = function() {
				oldfn();
				fn();
			};
		}
	}
}

if (typeof dojo != "undefined") {
	// Type-ahead only provided for English initially
	dojo.addOnLoad(function() {
		// default to English if locale is not defined
		if (typeof TE_locale == "undefined") {
			TE_locale = "en";
		}
		TE_displaySearchText2();
		if (TE_locale == "en" || TE_locale == "zh-hans") {
			TE_initTypeAheadSearch();
		}
	});
} else {
	addLoadListener(TE_displaySearchText2);
}

function TE_initTypeAheadSearch() {
	dojo.require("te.widget.TETypeAheadComboBox");
	dojo.require("te.TEQueryReadStore");
	
	var typeAheadHost;
	var pageHost = window.location.protocol + "//" + window.location.host;
	
	if (window.location.protocol == "https:") {
		typeAheadHost = TE_getSecureWWWServerUrl();
	} else {
		typeAheadHost = TE_getApplicationServerUrlWWW();
	}
		
	var suggestionStore = new te.TEQueryReadStore({
		url: typeAheadHost + "/catalog/svc/suggestions/" + TE_locale,
		doCachingQuery: false,
		requestMethod: (pageHost == typeAheadHost) ? "get" : "script.get",
		urlPreventCache: false
	});
	
	var searchComboBox = new te.widget.TETypeAheadComboBox({
		id: "te-searchBox",
		name: "q",
		value: TE_searchTerms.length == 0 ? TE_initialSearchText : TE_searchTerms,
		store: suggestionStore
	}, "te-searchBox");

	if (TE_searchTerms.length != 0) {
		dojo.byId("te-searchBox").className = "text-box active";
	}

	dojo.connect(dojo.byId("te-searchBox"), "onfocus", focusTESearchBox);
	dojo.connect(dojo.byId("te-searchBox"), "onblur", blurTESearchBox);
	
	dojo.connect(dijit.byId("te-searchBox"), "_showResultList", TE_adjustSearchDropdownPos);
	dojo.connect(dijit.byId("te-searchBox"), "_selectOption", TE_submitSearch);
	dojo.connect(dijit.byId("te-searchBox"), "_onKeyPress", TE_search_handleEnterKey);
}

function TE_displaySearchText2() {	
	if(typeof TE_initialSearchText == "undefined"){
		TE_initialSearchText = TE_defaultSearchText;
	}
	var sBox = document.getElementById("te-searchBox");
	if (sBox != null) {
		if(TE_searchTerms.length == 0) {
			sBox.value = TE_initialSearchText;
		} else {
			sBox.value = TE_searchTerms;
			sBox.className = "text-box active";
		}
	}
	
	TE_enableSearch();
}

function blurTESearchBox() {
	var sBox = document.getElementById("te-searchBox");
	if(sBox != null) {
		if (sBox.value.length == 0) {
			sBox.value = TE_initialSearchText;
			sBox.className = "text-box";
		}
	}
}

function focusTESearchBox() {
	var sBox = document.getElementById("te-searchBox");
	if(sBox != null) {
		if (sBox.value == TE_initialSearchText) {
			sBox.value = "";
			sBox.className = "text-box active";
		}
	}
}

function TE_search_handleEnterKey(/*Event*/ evt) {
	var key = evt.charOrCode;
			
	if (key == dojo.keys.ENTER) {
		TE_submitSearch();
	}	
}

function TE_adjustSearchDropdownPos() {
	var coords = dojo.coords(dojo.byId("te-searchForm"));
	if (dojo.isFF) {
		dojo.marginBox(dojo.byId("widget_te-searchBox_dropdown"), {t: coords.y + 28, l: coords.x + 2});
	} else {
		dojo.marginBox(dojo.byId("widget_te-searchBox_dropdown"), {t: coords.y + 26, l: coords.x});
	}
}

function TE_submitSearch() {
    // leave if button is disabled - shouldn't be here...
	if (!TE_search_submitOK) {
		return false;
	}
    
    // Prevent another quick submit and submit this question
    TE_disableSearch();  
    
	TE_setLangInSearchURL();
	
	// Strip leading and trailing whitespace of all kinds
	var qboxValue = document.getElementById("te-searchBox").value.replace(/^\s+/g,'').replace(/\s+$/g,'');
	document.getElementById("te-searchBox").value = qboxValue;
    
	if (qboxValue.length > 0 && qboxValue != TE_initialSearchText) {
		// Post the search to the page
    	document.getElementById("te-searchForm").submit();
		
    	// Wait 10 seconds before submit available
    	setTimeout("TE_enableSearch();", 10000);
	} else {
		TE_enableSearch();
	}
	
    return false;
}

function TE_setLangInSearchURL() {
	var searchURL, newSearchURL;
	
	if (typeof TE_locale != "undefined" && TE_locale != "en") {
		searchURL = document.getElementById("te-searchForm").action;
		newSearchURL = new String(searchURL);
		newSearchURL = newSearchURL.replace(/(\/)(en)/, "$1" + TE_locale);
		document.getElementById("te-searchForm").action = newSearchURL;
	}
}

/***
 *  Disable the search button.
 ***/
function TE_disableSearch() {
	TE_search_submitOK = false;
	
	document.getElementById("te-searchButton").disabled = true;
}

/***
 *  Enable the search button.
 ***/
function TE_enableSearch() {
	TE_search_submitOK = true;
	
	document.getElementById("te-searchButton").disabled = false;
}

function TE_submitSearchWithin() { 
	
	// Strip leading and trailing whitespace of all kinds
	var qboxValue = document.getElementById("te-searchWithinBox").value.replace(/^\s+/g,'').replace(/\s+$/g,'');
	document.getElementById("te-searchWithinBox").value = qboxValue;
    
	if (qboxValue.length > 0 ) {
		// Post the search to the page
    	document.getElementById("te-searchWithinForm").submit();    	
	}
	
    return false;
}

