//added a helper class to group the common codes
var searchHelper = function() {

	function processSearchForm(formObj, trail) {
		if (formObj) {

			var docSortField = formObj.find('input[name=docSort]');
			var docSortOrderField = formObj.find('input[name=docSortOrder]');
			var docSortPropField = formObj.find('input[name=docSortProp]');
			formObj.find('input[name=pageNum]').val(0);
			if (trail != null) {
				formObj.find('input[name=trail]').val(trail);
			}
			if (docSortField.val() == "relevance" || docSortField.val() == "") {
				docSortField.val("numprop");
			}
			if (docSortOrderField.val() == "") {
				docSortOrderField.val("descending");
			}
			if (docSortPropField.val() == "") {
				docSortPropField.val("childSKUs.creationDate");
			}
			formObj.submit();
		}
	}

	return {
		processSearchForm : function(formObj, trail) {
			processSearchForm(formObj, trail);
		}
	}
}();

var searchJS = {

	// this is the function used to submit the color form
	colorFacetSearch : function(trail) {

		var colorSearchForm = $('#colorSearchForm');
		searchHelper.processSearchForm(colorSearchForm, trail);

	},

	// this is the function used to submit the standard color form
	standardColorFacetSearch : function(trail) {

		var standardColorSearchForm = $('#standardColorSearchForm');
		searchHelper.processSearchForm(standardColorSearchForm, trail);

	},

	// this is the function used to submit the text search form
	textFacetSearch : function() {

		var textSearchForm = $('#textSearchForm');

		var question = $("input[name='question']").val();
		if (question == $("input[name='question']").attr("deftxt")) {
			question = "";
		}
		textSearchForm.find('input[name=question]').val(question);

		searchHelper.processSearchForm(textSearchForm, null);
	},

	// this is the function used to submit the category form
	categoryFacetSearch : function(trail) {
		var categorySearchForm = $('#categorySearchForm');
		searchHelper.processSearchForm(categorySearchForm, trail);
	},

	// this is the function used to submit the standard category form
	standardCategoryFacetSearch : function(trail) {
		var standardCategorySearchForm = $('#standardCategorySearchForm');
		searchHelper.processSearchForm(standardCategorySearchForm, trail);

	},

	// this is the function used to submit the product browse form
	productBrowseFacetSearch : function(trail) {
		var productBrowseSearchForm = $('#productBrowseSearchForm');
		searchHelper.processSearchForm(productBrowseSearchForm, trail);
	},

	// this is the function used to submit the standard browse form
	standardBrowseFacetSearch : function(trail) {
		var standardBrowseSearchForm = $('#standardBrowseSearchForm');
		searchHelper.processSearchForm(standardBrowseSearchForm, trail);
	}

};

