// JScript File

function refreshMe() {
	document.forms.SearchForm.SearchString.value = "";
	document.forms.SearchForm.submit();
}

function checkEnter() {
	if (window.event.keyCode == 13) {
		document.forms.SearchForm.submit();
		return false;
	} else {
		return true;
	}
}

function checkText() {
	if (document.forms.SearchForm.SearchString.value == "Enter your question...")
		document.forms.SearchForm.SearchString.value = "";
}

function checkSubmit() {
	if (document.forms.SearchForm.SearchString.value == "Enter your question..." || document.forms.SearchForm.SearchString.value == "")
		return false;
	else {
		document.forms.SearchForm.PageNumber.value = "0";
		return true;
	}
}

function viewFaq(refNo) {
	document.forms.SearchForm.FAQRefNo.value = refNo;
	document.forms.SearchForm.submit();
}

function previousPage() {
	if (document.forms.SearchForm.PageNumber.value != "0") {
		document.forms.SearchForm.PageNumber.value = parseInt(document.forms.SearchForm.PageNumber.value) - 1;
		document.forms.SearchForm.submit();
	}
}

function nextPage() {
	document.forms.SearchForm.PageNumber.value = parseInt(document.forms.SearchForm.PageNumber.value) + 1;
	document.forms.SearchForm.submit();
}

function backToResults() {
	document.forms.SearchForm.SearchString.value = document.forms.SearchForm.LastSearch.value;
	document.forms.SearchForm.submit();
}

function setUpSoftwareName() {
	var softwareName = "";
	if (document.forms.SearchForm.SoftwareName.value)
		softwareName = document.forms.SearchForm.SoftwareName.value;
	else if (top && top.confirmOpener == "FAQ") {
		var programName = top.getProgramName();
		
		// Read '4D Embroidery' as '4D Embroidery Extra' and '4D Embroidery Basic' as '4D Embroidery'
		if (programName.substring(0, 19) == "4D Embroidery Basic")
			programName = "4D Embroidery";
		else if (programName.substring(0, 13) == "4D Embroidery Extra")
			programName = "4D Embroidery Extra";
		else if (programName.substring(0, 13) == "4D Embroidery")
			programName = "4D Embroidery Extra";
		
		// Bug in Windows Vista; reading registry incorrectly.
		var programList = document.forms.SearchForm.ModuleList.value.split(",");
		var correctedName = "Embroidery System";
		for (var i = 0; i < programList.length; i++)
			if (programName.substring(0, programList[i].length) == programList[i])
				correctedName = programList[i];
				
		softwareName = correctedName + " " + top.getVersionNumber();
	}
	
	document.forms.SearchForm.ModuleList.value = "";
	document.forms.SearchForm.SoftwareName.value = softwareName;
	if (document.forms.SearchForm.SearchInModule)
		document.forms.SearchForm.SearchInModule.nextSibling.innerText = "FAQ relevant to " + softwareName;
}

function advancedSearch(ext) {
	var myForm = document.forms.SearchForm;
	myForm.method = "get";
	myForm.action = myForm.action.replace("search" + ext + ".aspx", "advanced" + ext + ".aspx");
	myForm.onSubmit = "";
	if (myForm.SearchString.value == "Enter your question...") myForm.SearchString.value = "";
	myForm["FAQRefNo"].disabled = true;
	myForm["ModuleList"].disabled = true;
	myForm["PageNumber"].disabled = true;
	myForm["LastSearch"].disabled = true;
	myForm["__VIEWSTATE"].disabled = true;
	myForm["__EVENTVALIDATION"].disabled = true;
	myForm.submit();
	return false;
}

function setUpAdvancedSearch(ext) {
	document.getElementById("advancedSearchLink").innerHTML = "<a href=\"advanced" + ext + ".aspx\" onclick=\"return advancedSearch('" + ext + "')\">Advanced&nbsp;Search</a>";
}

function setupAdvanced() {
	var myForm = document.forms.SearchForm;
	var params = location.href.split(/&|\?/);
	for (var p in params) {
		var eqAt = params[p].indexOf("=")
		switch (params[p].slice(0, eqAt)) {
			case "SoftwareName":
				var moduleName = unescape(params[p].slice(eqAt + 1).replace(/\+/g, " ")).replace(/\s*\d+\.\d+$/, "");
				myForm["SoftwareName"].value = moduleName;
				for (var s = 0; s < myForm["ModuleName"].options.length; s++) {
					if (myForm["ModuleName"].options[s].value == moduleName) {
						myForm["ModuleName"].selectedIndex = s;
						myForm["ModuleNameB"].selectedIndex = s;
						break;
					}
				}
				break;
			case "SearchIn":
				myForm["SearchInModule"].checked = unescape(params[p].slice(eqAt + 1).replace(/\+/g, " ")) == "SearchInModule";
				break;
			case "SearchString":
				var myList = unescape(params[p].slice(eqAt + 1).replace(/\+/g, " ")).split(/\s+/);
				for (var w in myList) {
					if (myList[w].slice(0, 1) == "+") {
						myForm["AllWords"].value += myList[w].slice(1) + " ";
					} else if (myList[w].slice(0, 1) == "-") {
						myForm["NoneWords"].value += myList[w].slice(1) + " ";
					} else if (!myList[w].match(/^~.+~$/)) {
						myForm["AnyWords"].value += myList[w] + " ";
					}
				}
				break;
		}
	}
	myForm["AnyWords"].value = myForm["AnyWords"].value.replace(/^\s+|\s+$/g, "");
	myForm["AllWords"].value = myForm["AllWords"].value.replace(/^\s+|\s+$/g, "");
	myForm["NoneWords"].value = myForm["NoneWords"].value.replace(/^\s+|\s+$/g, "");
}

function checkAdvancedSubmit() {
	var myForm = document.forms.SearchForm;
	var mySearchString = "";
	myForm["AnyWords"].value = myForm["AnyWords"].value.replace(/^\s+|\s+$/g, "");
	myForm["AllWords"].value = myForm["AllWords"].value.replace(/^\s+|\s+$/g, "");
	myForm["NoneWords"].value = myForm["NoneWords"].value.replace(/^\s+|\s+$/g, "");
	myForm["FAQRefNo"].value = "";
	mySearchString += (myForm["AnyWords"].value != "") ? myForm["AnyWords"].value.replace(/^|\s+/g, " ") : "";
	mySearchString += (myForm["AllWords"].value != "") ? myForm["AllWords"].value.replace(/^|\s+/g, " +") : "";
	mySearchString += (myForm["NoneWords"].value != "") ? myForm["NoneWords"].value.replace(/^|\s+/g, " -") : "";
	mySearchString = mySearchString.replace(/^\s+/, "");
	if (mySearchString != "") {
		myForm["SearchString"].value = mySearchString;
		myForm["SoftwareName"].value = (myForm["SearchInModule"].checked) ? myForm["ModuleName"].options[myForm["ModuleName"].selectedIndex].value : "";
		myForm["SoftwareName"].value = myForm["ModuleName"].options[myForm["ModuleName"].selectedIndex].value;
		myForm["AnyWords"].disabled = true;
		myForm["AllWords"].disabled = true;
		myForm["NoneWords"].disabled = true;
		myForm["ModuleName"].disabled = true;
		if (myForm["__VIEWSTATE"]) {
			myForm["__VIEWSTATE"].disabled = true;
			myForm["__EVENTVALIDATION"].disabled = true;
		}
		return true;
	} else
		return false;
}

function submitBrowse() {
	var myForm = document.forms.SearchForm;
	myForm["SearchString"].value = "~all~";
	myForm["SoftwareName"].value = myForm["ModuleNameB"].options[myForm["ModuleNameB"].selectedIndex].value;
	myForm["SearchInModule"].checked = true;
	myForm["AnyWords"].disabled = true;
	myForm["AllWords"].disabled = true;
	myForm["NoneWords"].disabled = true;
	myForm["ModuleName"].disabled = true;
	myForm["FAQRefNo"].value = "";
	if (myForm["__VIEWSTATE"]) {
		myForm["__VIEWSTATE"].disabled = true;
		myForm["__EVENTVALIDATION"].disabled = true;
	}
	myForm.submit();
}

function submitViewFAQ() {
	var myForm = document.forms.SearchForm;
	var faqForm = document.forms.ViewFaqForm;
	if (faqForm["ViewFAQ"].value.replace(/^\s+|\s+$/g, "") != "") {
		myForm["FAQRefNo"].value = faqForm["ViewFAQ"].value.replace(/^\s+|\s+$/g, "");
		myForm["SearchString"].value = "";
		myForm["SearchInAll"].checked = true;
		myForm["AnyWords"].disabled = true;
		myForm["AllWords"].disabled = true;
		myForm["NoneWords"].disabled = true;
		myForm["ModuleName"].disabled = true;
		if (myForm["__VIEWSTATE"]) {
			myForm["__VIEWSTATE"].disabled = true;
			myForm["__EVENTVALIDATION"].disabled = true;
		}
		myForm.submit();
	}
	return false;
}

function submitNewest() {
	var myForm = document.forms.SearchForm;
	myForm["SearchString"].value = "~all~ ~new~";
	myForm["SoftwareName"].value = myForm["ModuleName"].options[myForm["ModuleName"].selectedIndex].value;
	myForm["SearchInAll"].checked = true;
	myForm["AnyWords"].disabled = true;
	myForm["AllWords"].disabled = true;
	myForm["NoneWords"].disabled = true;
	myForm["ModuleName"].disabled = true;
	myForm["FAQRefNo"].value = "";
	if (myForm["__VIEWSTATE"]) {
		myForm["__VIEWSTATE"].disabled = true;
		myForm["__EVENTVALIDATION"].disabled = true;
	}
	myForm.submit();
}