function searchNavigation(browse, itemPerPage, selectedPageNum, sortByValue, hideTerm, cache, id) {
    document.getElementById("refinementItemPerPage").value = itemPerPage;
    document.getElementById("refinementSortBy").value = sortByValue;
    document.getElementById("refinementSuccessURL").value = "/krw/search/search-product-list.jsp?browse=" + browse + "&selectedPageNum=" + selectedPageNum + "&hideTerm=" + hideTerm + "&cache=" + cache + "&id=" + id;
    document.getElementById("refinementSearch").click();

    return false;
}

function refineByCategory(categoryId) {
    document.getElementById("categoryId").value = categoryId;
    document.getElementById("refinementSearch").click();

    return false;
}

function refineBy(type, name, value, op) {
    document.getElementById("refinementType").value = type;
    document.getElementById("refinementName").value = name;
    document.getElementById("refinementValue").value = value;
    document.getElementById("refinementOp").value = op;
    document.getElementById("refinementSearch").click();

    return false;
}

/**
 * Updates a named input in the form with the given name in the current
 * document to the new value specified.
 **/
function setFormElement(formName,inputName,value) {
  var form = document.forms[formName];
  if(form == null){
    return false;
  }
  input = form.elements[inputName];
  if(input == null){
    return false;
  }

  // the jstl core library escapes apostrophes, so be sure to change them
  // back before setting the form element
  value = value.replace('&apos;',"'");

  input.value=value;
  return true;
}

/**
 * Submits a form with the given name in the current document
 **/
function submitForm(formName) {
  var form = document.forms[formName];
  if(form == null)
    return false;
  var submitButton = form.elements['submit'];
  if(submitButton == null){
    return false;
  }
  //alert('in submitForm(' + formName + ')');
  submitButton.click();
  return true;
}

/**
 * Changes the page number and resubmits the form.
 **/
function changePage(page, submit) {
  setFormElement('searchForm2','pageNum',page);
  if(submit == true)
    submitForm('searchForm2');
  return true;
}

/**
 * Changes the page number and resubmits the form.
 **/
function changePageBrowse(page, submit) {
  setFormElement('searchForm2','pageNum',page);
  if(submit == true)
    submitForm('searchFormBrowse');
  return true;
}

/**
 * Sorts the results by the value of the 'sortSelect'
 * select box in the 'sortForm' form.
 **/
function sort() {
  var sortForm = document.forms['sortForm'];
  if(sortForm == null)
    return false;
  var sortSelect = sortForm.elements['sortSelect'];
  if(sortSelect == null)
    return false;
  var sortValue = sortSelect.value;
  //alert('in sort: ' + sortValue);
  setFormElement('searchForm2','docSort',sortValue);
  changePage(0, false);
  submitForm('searchForm2');
  return true;
}

/**
 * Replaces a spelling term in the question text
 * at the given offset through the given lenght
 * with the given text.
 **/
function changeSpellingTerm(text, offset, length) {
  var sortForm = document.forms['searchForm2'];
  if(sortForm == null)
    return false;
  var question = sortForm.elements['question'];
  if(question == null)
    return false;
  var value = question.value;
  if(value == null)
    return false;
  var newValue = '';
  var offsetInt = parseInt(offset);
  var lengthInt = parseInt(length);
  if(offsetInt > 1)
    newValue = value.substring(0,offsetInt - 1);
  newValue = newValue + text;
  if((offsetInt + lengthInt - 1) < parseInt(value.length))
    newValue = newValue + value.substring(offsetInt + lengthInt - 1, value.length);
  //alert('in spelling: \"' + value + '\" to \"' + newValue + '\"');
  setFormElement('searchForm2','question',newValue);
  changePage(0, false);
  submitForm('searchForm2');
  return true;
}

/**
 * Sets the refinement text value and submits the form
 **/
function refine(type, metaPropertyName, value) {
  //alert('in refine: metaPropertyName=\"' + metaPropertyName + '\" value=\"' + value + '\"');
  setFormElement('searchForm2','newRefinementType',type);
  setFormElement('searchForm2','newRefinementName',metaPropertyName);
  setFormElement('searchForm2','newRefinementValue',value);
  changePage(0, false);
  submitForm('searchForm2');
  return true;
}


/**
 * Sets the refinement text value and submits the form
 **/
function refineBrowse(type, metaPropertyName, value) {
  //alert('in refineBrowse: metaPropertyName=\"' + metaPropertyName + '\" value=\"' + value + '\"');
  setFormElement('searchFormBrowse','newRefinementType',type);
  setFormElement('searchFormBrowse','newRefinementName',metaPropertyName);
  setFormElement('searchFormBrowse','newRefinementValue',value);
  changePageBrowse(0, false);
  submitForm('searchFormBrowse');
  return true;
}


/**
 * Sets the refinement text value and submits the form
 **/
function setPriceRange(minPrice,maxPrice) {
  //alert('in setPriceRange: minPrice=\"' + minPrice + '\" maxPrice=\"' + maxPrice + '\"');
  setFormElement('searchForm2','minPrice',minPrice);
  setFormElement('searchForm2','maxPrice',maxPrice);
  submitForm('searchForm2');
  return true;
}


function setSearch(text,img,cache){
	document.forms.searchForm.question.value = text;
	document.forms.searchForm.action = '/krw/search/search-product-list.jsp?image=' + imgPath + "&cache=" + cache;
	document.forms.searchForm.submit();

    return false;
}

function setSearchForBrand(brand){
    document.getElementById("refinementValue").value = brand;
    document.getElementById("refinementSearch").click();

    return false;
}

function showImg(){
    //find what the image querry string contains
	var str = location.search.substring(1);

	startAt = str.indexOf("image=");
	startAt = startAt + 6;

	endAt = str.indexOf(".jpg");

	if (endAt == '-1'){
		endAt = str.indexOf(".gif");
	}

	endAt = endAt + 4;

	var imgPath = str.slice(startAt,endAt);

	pathLength = imgPath.length;

	if (pathLength > 0){
		//set image on page to what is after "image=" in querry string
		document.images['urlImg'].src = imgPath ;
	}
}
