var iViewPage, iTotalPage, iPageRange, iPageRangeHalf, iMax;
var sFormAction, sSortBy, sSortOrder, sNewKeywords;

function ignoreEnter() {
	if (window.event.keyCode == 13)	return false;
}

function checkMinPrice() {
	if (document.searchOptions.maxPrice.selectedIndex < document.searchOptions.minPrice.selectedIndex)
		document.searchOptions.maxPrice.selectedIndex = iMax;
}

function checkMaxPrice() {
	if (document.searchOptions.minPrice.selectedIndex > document.searchOptions.maxPrice.selectedIndex)
		document.searchOptions.minPrice.selectedIndex = 0;
}

function submitform() {
	sFormAction = "search";
	document.searchOptions.option_submit.value = "search";
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function goNext(content) {
	sFormAction = "gonext_" + content;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function goPrevious(content) {
	sFormAction = 'goprevious_' + content;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function goPage(iPageNo, iTotalPg) {
	sFormAction = "gopage";
	iViewPage = iPageNo;
	iTotalPage = iTotalPg;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function viewText(iPageNo, iTotalPg) {
	sFormAction = "viewtext";
	iViewPage = iPageNo;
	iTotalPage = iTotalPg;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function viewImage(iPageNo, iTotalPg) {
	sFormAction = "viewimage";
	iViewPage = iPageNo;
	iTotalPage = iTotalPg;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function sort(by, order) {
	sFormAction = "sort";
	sSortBy = by;
	sSortOrder = order;
	if (collectOptions(document.searchOptions)) document.searchOptions.submit();
}

function takeSuggestion(sSuggestedWords) {
	sFormAction = "takeSuggestion";
	sNewKeywords = sSuggestedWords;
	if (collectOptions(document.searchOptions))	document.searchOptions.submit();
}

function collectOptions(thisForm) {
	var iOptionNum, iNdx;
	var sOption;

	// submit
	if (sFormAction != "")
		if (sFormAction == "search") {
			// keyword options
			iOptionNum = thisForm.keywordOption.length;
			for (iNdx=0; iNdx<iOptionNum; iNdx++)
				if (thisForm.keywordOption[iNdx].checked)
					thisForm.option_keyword.value = iNdx;

			// content options
			iOptionNum = thisForm.contentOption.length;
			for (iNdx=0; iNdx<iOptionNum; iNdx++)
				if (thisForm.contentOption[iNdx].checked)
					thisForm.option_content.value = iNdx;

		 	// price
		 	//DO NOT REMOVE THIS CODE
		 	//hide price options for now, may be in use when FP wants it
		 	//thisForm.option_minPrice.value = thisForm.minPrice.selectedIndex;
			//thisForm.option_maxPrice.value = thisForm.maxPrice.selectedIndex;
		 	//DO NOT REMOVE THIS CODE

			// page no
			thisForm.option_page_art.value = 1;
			thisForm.option_page_prod.value = 1;
			thisForm.option_page_art_first.value = 1;
			thisForm.option_page_prod_first.value = 1;
			thisForm.option_submit.value = "search";

			// track keywords
			TrackInterstitial('search', 'keyword', thisForm.keywords.value);
		}
		else if (sFormAction == "gopage") {
			if (iViewPage <= iPageRange)
				thisForm.option_page_art_first.value = 1;
			else if (iViewPage%iPageRange == 0)
				thisForm.option_page_art_first.value = (Math.floor(iViewPage/iPageRange) - 1) * iPageRange + 1;
			else
				thisForm.option_page_art_first.value = Math.floor(iViewPage/iPageRange) * iPageRange + 1;

			thisForm.option_page_art.value = iViewPage;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "gonext_art") {
			thisForm.option_page_art.value = Number(thisForm.option_page_art.value) + 1;
			if (thisForm.option_page_art.value > Number(thisForm.option_page_art_first.value) + iPageRange - 1) thisForm.option_page_art_first.value = Number(thisForm.option_page_art_first.value) + iPageRange;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "gonext_prod") {
			thisForm.option_page_prod.value = Number(thisForm.option_page_prod.value) + 1;
			if (thisForm.option_page_prod.value > Number(thisForm.option_page_prod_first.value) + iPageRange - 1) thisForm.option_page_prod_first.value = Number(thisForm.option_page_prod_first.value) + iPageRange;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "goprevious_art") {
			if (thisForm.option_page_art.value == Number(thisForm.option_page_art_first.value)) thisForm.option_page_art_first.value = Number(thisForm.option_page_art_first.value) - iPageRange;
			thisForm.option_page_art.value = Number(thisForm.option_page_art.value) - 1;
			if (thisForm.option_page_art.value < 1) thisForm.option_page_art.value = 1;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "goprevious_prod") {
			if (thisForm.option_page_prod.value == Number(thisForm.option_page_prod_first.value)) thisForm.option_page_prod_first.value = Number(thisForm.option_page_prod_first.value) - iPageRange;
			thisForm.option_page_prod.value = Number(thisForm.option_page_prod.value) - 1;
			if (thisForm.option_page_prod.value < 1) thisForm.option_page_prod.value = 1;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "viewtext" || sFormAction == "viewimage") {
			if (sFormAction == "viewtext")
				thisForm.option_view.value = "text";
			else
				thisForm.option_view.value = "image";

			if (iViewPage <= iPageRange)
				thisForm.option_page_prod_first.value = 1;
			else if (iViewPage%iPageRange == 0)
				thisForm.option_page_prod_first.value = (Math.floor(iViewPage/iPageRange) - 1) * iPageRange + 1;
			else
				thisForm.option_page_prod_first.value = Math.floor(iViewPage/iPageRange) * iPageRange + 1;

			thisForm.option_page_prod.value = iViewPage;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "sort") {
			thisForm.option_sortBy.value = sSortBy;
			thisForm.option_sortOrder.value = sSortOrder;
			thisForm.option_submit.value = "search";
		}
		else if (sFormAction == "takeSuggestion") {
			thisForm.option_submit.value = "takeSuggestion";
			thisForm.option_suggested_keywords.value = sNewKeywords;

			// track keywords
			TrackInterstitial('search', 'keyword', sNewKeywords);
		}

	return true;
}

function searchHelp() {
	window.open("/us/products/searchHelp.htm", 'searchTips', 'left=10,top=10,screenX=10,screenY=10,height=460,width=787,,resizable=yes,scrollbars=yes,toolbar=no,menubar=no;');
}