// Show/hide function for search selector FOR COMPONENTS PAGES
var detailVisible = false; 
var qna_submitOK = true;
var TE_initialSearchText = "Search by Keyword or Part #";

function onBlurSearchBox() {
	var sBox = document.getElementById("question_box");
	if (sBox.value.length == 0) {
		sBox.value = TE_initialSearchText;
	}
}

function onFocusSearchBox() {
	var sBox = document.getElementById("question_box");
	if (sBox.value == TE_initialSearchText) {
		sBox.value = "";
	}
}

function TE_displaySearchText() {
	var locale = TE_getLocale();
	
	dojo.requireLocalization("te", "ewt", locale);
	var ewt = dojo.i18n.getLocalization("te", "ewt", locale);

	var searchSel1Node = dojo.byId("TE_Inquira_searchselector");
	var searchSel2Node = dojo.byId("search_selector");
	var searchByNode = dojo.byId("TE_Inquira_searchBy");
	var partNumOption = dojo.byId("inquiraTypePart");
	var textOption = dojo.byId("inquiraTypeText");
	var closeSearchSelImage = dojo.byId("TE_close_searchselector");
	var searchSel1ClassName = "TE_Inquira_searchselector";
	var searchSel2ClassName = "search_selector";	

	var langId = dojo.cookie("language_id");
	
	if (langId && langId.length > 0 && langId != "1") {
		searchSel1ClassName = searchSel1ClassName + "_" + langId;
		searchSel2ClassName = searchSel2ClassName + "_nonEng";
	}

	dojo.addClass(searchSel1Node, searchSel1ClassName);
	dojo.addClass(searchSel2Node, searchSel2ClassName);
	
	searchByNode.appendChild(document.createTextNode(ewt.searchByMessage));
	partNumOption.appendChild(document.createTextNode(ewt.partNumberMessage));
	textOption.appendChild(document.createTextNode(ewt.textMessage));
	closeSearchSelImage.alt = ewt.closeSearchSelMessage;
	closeSearchSelImage.title = ewt.closeSearchSelMessage;
	enableSubmit();
	//Added during DOJO upgrade to enable search button incase of back button pressed
}

function showTE_Inquira_searchselector(layerToShow)
{
	if(detailVisible == false)
	{
		document.getElementById(layerToShow).style.display="block";
		detailVisible = true;
		if( document.getElementById("search_type").value == "Text" ){
			document.getElementById("search_selector").selectedIndex = 1
		}
        else {
			document.getElementById("search_selector").selectedIndex = 0
		}

	}
	
}

function closeTE_Inquira_searchselector(layerToShow)
{
	if(detailVisible == true)
	{
		document.getElementById(layerToShow).style.display="none";
		detailVisible = false;
	}
	
}

function TE_SetSearchType(){
	var searchTypeIndex;
	searchTypeIndex = document.getElementById("search_selector").selectedIndex
	//console.warn("search type: " + searchTypeIndex);
	if (searchTypeIndex == 1){
		document.getElementById("search_type").value = "Text";
	} 
	else {
		document.getElementById("search_type").value = "Part";
	}
}

function search_inquira_submitQuestion()
{   
    // leave if button is disabled - shouldn't be here...
	if (!qna_submitOK){
        return false;
	}
    // Prevent another quick submit and submit this question
    disableSubmit();    
    // Set the type of search Part vs Text
	TE_SetSearchType()
	// Strip leading and trailing whitespace of all kinds
	var qboxValue = document.getElementById("question_box").value.replace(/^\s+/g,'').replace(/\s+$/g,'');
	if (qboxValue == TE_initialSearchText) {
		qboxValue = "";
	}
    document.getElementById("question_box").value = qboxValue;
	// Post the question to the page (action reference)
    document.getElementById("TEsearch").submit();
    
    // Wait 10 seconds before submit available
    setTimeout("enableSubmit();", 10000);

    return false;
}


/**
  * Disable the submit feature.
  */
function disableSubmit()
{
    qna_submitOK = false;
    document.getElementById("searchBtn").disabled = true;
}

/**
  * Enable the submit feature.
  */
function enableSubmit()
{
    qna_submitOK = true;
    document.getElementById("searchBtn").disabled = false;
}

function chkForm(e)
{
	var k;
	if (window.event)
	{
		k = window.event.keyCode;
	}
	else
	{
		k = e.which;
	}
	
	if (k == 13)
	{
		search_inquira_submitQuestion();
	}
}
