var _urlSearch = "/rs/search.jsp";
var _urlModifyHistory = "/rs/modify_history";
var _urlCompare = "/rs/compare.jsp";
var _urlUpdate = "/rs/update_prefs";
var _urlProduct = "/rs/show_product.jsp";
var _fauxSearchForm = "psearchform";
var _cookie_pagesize = "c_page_size";
var _cookie_pagesort = "c_page_sort";
var _cookie_history = "c_history";
var browser=navigator.appName;

/* submits the actual call */
function submitAction(url) {
	var searchForm = document.getElementById(_fauxSearchForm);
	/* Check the cookie for page size and sort preferences */
	var pref_pagesort=readCookie(_cookie_pagesort);
	if (pref_pagesort != '' && pref_pagesort != 'undefined') {
		url += "&sf=" + encodeURIComponent(pref_pagesort);
		addFieldToForm(searchForm, "hidden", "sf", pref_pagesort);
		}
	var pref_pagesize=readCookie(_cookie_pagesize);
	if (pref_pagesize != '' && pref_pagesize != 'undefined') {
		url += "&prodPage=" + encodeURIComponent(pref_pagesize);
		addFieldToForm(searchForm, "hidden", "prodPage", pref_pagesize);
		}		
	
	/* If the customer doesn't have a source code, add the default */
	mycookiesource=mysource;
	if (mycookiesource=='') {		
		url += "&special_upref:" + "=" + "oseg=" + _defaultsource;
		addFieldToForm(searchForm, "hidden", "special_upref:", "oseg=" + _defaultsource);
		}
		
	/* Add the history to the url */
	url = appendHistory(url, false);
	
	/* If the URL is under 1900 characters, assign it directly, otherwise do a form POST */
	if (url.length < 1900) {
		window.location.assign(url);
	} else {
		searchForm.submit();
		}	
}

/* creates a search action with an arbitrary number of parameters */
function performSearch() {
	/* Set up both the url and the form so both are ready */
	var url = _urlSearch;
	var searchForm = document.getElementById(_fauxSearchForm);
	for (var n = 0; n < (arguments.length - 1); n = n+2) {
		if (n < 1) {
			url += "?";
		} else {
			url += "&";
			}
		if (arguments[n]=='freeText' && arguments[n+1].toUpperCase().indexOf(' OR ') > -1) {
			arguments[n+1]=arguments[n+1].replace(/\ OR\ /gi,"|");
			}
		url += arguments[n] + "=" + encodeURIComponent(arguments[n+1]);
		addFieldToForm(searchForm, "hidden", arguments[n], arguments[n+1]);
		}	
		
	submitAction(url);	
}


/* This is used when doing a free keyword search */
function performFreeSearch() {
	var text = document.getElementById('freeText').value;
	if (text == "") {
		alert("Please enter your search query");
	    return false;
	    }
	
	var refine = false;
	var refineObj = document.getElementById("searchInResults");	
	if (refineObj != null) {
		refine = refineObj.checked;
		}
	
	if (refine) {
		performSearch("freeText", text, "rf", "1");
	} else {
		performSearch("freeText", text);
	    }
}


/* Calls the product detail page */
function performProductPage() {    
	/* Set up both the url and the form so both are ready */
	var url =  _urlProduct;
	var searchForm = document.getElementById(_fauxSearchForm);
	searchForm.action = url;
	for (var n = 0; n < (arguments.length - 1); n = n+2) {
		if (n < 1) {
			url += "?";
		} else {
			url += "&";
			}
		url += arguments[n] + "=" + encodeURIComponent(arguments[n+1]);
		addFieldToForm(searchForm, "hidden", arguments[n], arguments[n+1]);
		}	
	
	/* Add the history to the url so we can see if it's too long*/
	var url_with_history = appendHistory(url, false);
	
	/* If the URL with the history is under 2000 characters, include it, otherwise toss it */
	if (url_with_history.length < 2000) {
		window.location.assign(url_with_history);
	} else {
		window.location.assign(url);
		}
}


/* Changes the page size */
function changeResultsPrefs(param, value, businessSegmentId) {
	if (param=='pageSize') {
		writeCookie(_cookie_pagesize,value,30);
		}
	performSearch("upf:pm", param,
	"upf:pv", value,
	"special_upref:","bseg=" + businessSegmentId,
	"rf", "1");
}


/* Changes the page sort */
function performSort(sortField, businessSegmentId) {
	writeCookie(_cookie_pagesort,sortField,30);
	performSearch("sf", sortField, "rf", "1",
	"upf:pm","bseg",
	"upf:pv",businessSegmentId);
}


/* These functions are for modifying the history */
function performModifyHistory(modifyType, uid) {
	var searchForm = document.getElementById(_fauxSearchForm);
	url = _urlModifyHistory;
	searchForm.action = url;
	addFieldToForm(searchForm, "hidden", "modifyType", modifyType);
	addFieldToForm(searchForm, "hidden", "uid", uid);
	url += "?modifyType" + "=" + modifyType;
	url += "&uid" + "=" + uid;
	submitAction(url);	
}

function removeEntry(entry) {
	performModifyHistory('2', entry);
}

function removeAfter(entry) {
	performModifyHistory('1', entry);
}

function appendHistory(url, first) {
	var newURL = url;
	if (!first) {
		newURL += "&";
		}
	/* 03/28/08 - MSIMONE - Try to get the history from the cookie on product pages */
	var pagehistory=getHistory();
	var cookiehistory=readCookie('c_history');
	if (pagehistory.indexOf('PRODUCT') > -1 && cookiehistory != '') {
		myhistory=cookiehistory;
	} else {
		myhistory=pagehistory;
		}
	/* 03/06/08 - MSIMONE - Found that Firefox is touchy about symbols in the URL, so we're encoding the history string for non-IE browsers */
	if (browser == 'Microsoft Internet Explorer')	{
		return newURL + "history=" + myhistory;
	} else {
		return newURL + "history=" + encodeURIComponent(myhistory);
	}
}

function readCookie(cookiekey)
{
	var cookies=document.cookie.split('; ');
	var cookievalue='';
	for (c=0; c<cookies.length; c++) {
		var thiscookie=cookies[c].split('=');
		if (thiscookie[0]==cookiekey) {
			cookievalue=unescape(thiscookie[1]);
		}
	}
	return cookievalue;
}

function writeCookie(c_name,value,expiredays) {
   var exdate=new Date()
   exdate.setDate(exdate.getDate()+expiredays)
   document.cookie=c_name+"="+escape(value)+((expiredays==null) ? "" : "; expires="+exdate)+"; path=/; domain=ross-simons.com";
}

/* We always end up needing this */
function getHistory() 
{ 
myhistory=readCookie('c_history');
document.getElementById('history').value=myhistory;
return myhistory;
}

/* Not sure what this does but it seems to be necessary */
function ignore(val) {
}


/* Utility function stolen from www.faqts.com/knowledge_base/view.phtml/aid/1785 */
function addFieldToForm(form, fieldType, fieldName, fieldValue) {
if (document.getElementById) {
	var input = document.createElement('input');
	if (document.all) { // what follows should work with NN6 but doesn't in M14
		input.type = fieldType;
		input.name = fieldName;
		input.value = fieldValue;
		}
	else if (document.getElementById) { // so here is the NN6 workaround
		input.setAttribute('type', fieldType);
		input.setAttribute('name', fieldName);
		input.setAttribute('value', fieldValue);
		}
	form.appendChild(input);
	}
}

