var searchCategory = 'all';

function setSearchCategory(category) {
	searchCategory = category;
	//alert(searchCategory);
	valueMe('categoryList', category);
}

function searchFormValidate() {
	searchCategory = getValue('categoryList');
	if (searchCategory == 'all' || searchCategory == '') {
		alert("Please select an Information type.");
		return false;
	}
	else if (getValue('publicationList') == 'frasers' && searchCategory == 'articles') {
		valueMe('publicationList','cmo');
	}
	else if (searchCategory == 'web') {
		valueMe('publicationList','frasers');
	}

	var keyword = getValue('keyword');
	if (keyword.toUpperCase() == 'ENTER SEARCH WORDS HERE' || keyword == '') {
		valueMe('keyword','*');
	}
	nameMe('keyword', searchCategory);

	return true;
}

function searchFormKeyPress(e) {
	var keyCode;

	if (window.event) { // IE
		keyCode = e.keyCode;
	}
	else if(e.which) { // Netscape/Firefox/Opera
		keyCode = e.which;
	}
	if (keyCode == 13) {
	    var searchSubmit = document.getElementById('searchSubmit');
		searchSubmit.onclick();
	    return false;
	}
	return true;
}

function searchFormSubmit(form) {
	pageTracker._trackPageview("/Site Search/?q="+getValue('keyword')+"&Cat="+getValue('categoryList'));
	form.submit();
}


function setFrasersDefaultSearchCriteria(url) {
  //var url = location.href;
  //url = url.toLowerCase();
  
  if (url.indexOf("canadianmetalworking") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "canadianmetalworking");
  } else if (url.indexOf("canadianpackaging") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "canadianpackaging");
  } else if (url.indexOf("canadianprinter") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "canadianprinter");
  } else if (url.indexOf("coatings") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "coatings");
  } else if (url.indexOf("designengineering") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "designengineering");
  } else if (url.indexOf("foodincanada") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "foodincanada");
  } else if (url.indexOf("mmd") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "mmd");
  } else if (url.indexOf("plant") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "plant");
  } else if (url.indexOf("plasticsincanada") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "plasticsincanada");
  } else if (url.indexOf("purchasingb2b") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "purchasingb2b");
  } else if (url.indexOf("frasers") != -1) {
    valueMe('categoryList', "productservices");
    valueMe('publicationList', "frasers");
  } else if (url.indexOf("canadianmanufacturing") != -1 || url.indexOf("rogers-cmo-ts") != -1) {
    valueMe('categoryList', "articles");
    valueMe('publicationList', "cmo");
  } else {
    valueMe('categoryList', "productservices");
    valueMe('publicationList', "frasers");
  }
}


function presetSearch(publication) {
  var cookieName = "CMO_SEARCH";
  var searchURL = location.href;
  var crossDomain = getCrossDomain();
  var SEARCH_PARAMETER_COUNT = 7;

  // only create a cookie with the search is performed, i.e. the url in the format:
  // http://www.frasers.com/productservices/frasers/all/bolt/p1/searchResults.jsf
  if (searchURL.indexOf("searchResults.jsf") != -1 && location.href.replace("http://", "").split("/").length == SEARCH_PARAMETER_COUNT) {
    setCookie(cookieName, encode_utf8(searchURL), '', "/", crossDomain);
    searchURL = unescape(searchURL);
  } else if ( searchURL.indexOf("index.") != -1 || searchURL.indexOf("home.") != -1 || location.href.replace("http://", "").split("/").length < 2 ) {
    deleteCookie(cookieName, "/", crossDomain);
    searchURL = "";
  } else {
    searchURL = getCookie(cookieName);
  }

  if (searchURL == null || searchURL.length == 0) {
    setFrasersDefaultSearchCriteria(location.href);
  } else {
    //searchURL = searchURL.replace("http://", "").substring(document.domain.length);
    searchURL = searchURL.replace("http://", "").substring(15);
    var searchParams = searchURL.split("/");
    var searchType = searchParams[1];
    var searchPublication = searchParams[2];
    var searchKeywords = (location.href.indexOf("searchResults.jsf") != -1 && location.href.replace("http://", "").split("/").length != SEARCH_PARAMETER_COUNT) ? "" : searchParams[4];

    if (location.href.indexOf("/public/searchResults.jsf") != -1) {
      /* 
        This is an exception case. When no results are found, results are searched in the web content 
        except for articles where /public/searcResults.jsf loads because no SEO is implemented here and user is switching tabs.
      */
      if (searchType != "articles") {
        searchType = "web";
      } else {
        searchKeywords = searchParams[4];
      }
    }
    valueMe('categoryList', searchType);
    valueMe('publicationList', searchPublication);
    valueMe('keyword', searchKeywords);
  }
}


function presetSearch_ORIGINAL(publication) {
	var searchURL = location.href;

	if (searchURL.indexOf("http", 0) == -1 || searchURL.indexOf("searchResults.jsf") == -1) {
    setFrasersDefaultSearchCriteria();
		return;
	} else {

    searchURL = searchURL.substring(10);

    var searchParams = searchURL.split("/");
    if (searchParams.length > 4) {
      var searchType = searchParams[1];
      var searchPublication = searchParams[2];
      var searchKeywords = searchParams[4];

      valueMe('categoryList', searchType);
      valueMe('publicationList', searchPublication);
      valueMe('keyword', searchKeywords);
    }
  }

	return;


	/*if (location.href.indexOf("http", 0) == -1) {
		return;
	}

	var searchURL = "";
	var cookieName = "CMO_SEARCH";

	if ((location.href.indexOf("searchResults.jsf?publication=") != -1) && (location.href.indexOf("location") == -1)) {
		searchURL = location.href;
		var parts = document.domain.split('.');
		if (parts.length > 0 ) {
			// set cookie for cross domain ex: .canadianmanufacturing.com
			var crossDomain = (parts.length > 2) ? '.' + parts[parts.length-2] + '.' + parts[parts.length-1] : '.' + parts[parts.length-1];
			//setCookie(cookieName, URLDecode(searchURL), '', "/", crossDomain);
			setCookie(cookieName, encode_utf8(searchURL), '', "/", crossDomain);
			//setCookie(cookieName, searchURL, '', "/", crossDomain);
			searchURL = unescape(searchURL);
		}
	} else {
		searchURL = getCookie(cookieName);
		if (searchURL == "" || searchURL == null) {
			//set default
			//searchURL = "?publication=all&all=";
			searchURL = document.domain;
			searchURL += "?publication=" + ((publication.toLowerCase() == "cmo") ? "all" : publication.toLowerCase()) + "&all=";
			searchURL += "Enter search words here";
		}
	}

	searchURL = decode_utf8(searchURL);
	var params = searchURL.split(new RegExp("[?&]{1}", "g"));
	var isSamePublication = "false";
	for (var i = 1; i < params.length; i++) {
		var pos = params[i].indexOf("=");
		if (pos != -1) {
			var paramName = params[i].substring(0, pos);
			var paramValue = params[i].substring(pos+1);
			if (paramName == "publication") {
				valueMe('publicationList', publication);
				if (paramValue == publication) {
					isSamePublication = "true";
				}
			}
			else if (paramName == "location") {
			}
			else {
				if (isSamePublication == "true") {
					if (paramName == "all") {
						if (publication.toLowerCase() == "frasers") {
							setSearchCategory('productservices');
						}
						else {
							setSearchCategory('articles');
						}
					}
					else {
						setSearchCategory(paramName);
						valueMe('keyword', paramValue);
						if (paramName != "all") {
						}
					}
				}
				else {
					if (paramName == "all") {
						if (publication.toLowerCase() == "frasers") {
							setSearchCategory('productservices');
						}
						else {
							setSearchCategory('articles');
						}
					}
					else {
						if (publication.toLowerCase() == "frasers") {
							setSearchCategory('productservices');
						}
						else {
							setSearchCategory(paramName);
							valueMe('keyword', paramValue);
						}
					}
				}
			}
		}
	}*/

}

//Decodes a UTF8 formated string
function utf8Decode(utf8str)
{
	var str = new Array();
	var pos = 0;
	var tmpStr = '';
	var j=0;
	while ((pos = utf8str.search(/[^\x00-\x7F]/)) != -1) {
		tmpStr = utf8str.match(/([^\x00-\x7F]+[\x00-\x7F]{0,10})+/)[0];
		str[j++]= utf8str.substr(0, pos) + _utf8Decode(tmpStr);
		utf8str = utf8str.substr(pos + tmpStr.length);
	}

	str[j++] = utf8str;
	return str.join('');
}