﻿var cookieID // = "TMWebSearch";
function setCookie(c_name,value,expiredays)
	{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	//document.write("In setCookie: document.cookie=" + document.cookie + "<br />")
	}
function getCookie(c_name)
	{
	//document.write("In getCookie: document.cookie=" + document.cookie + "<br />")
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1; 
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    } 
	  }
	return "";
	}
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
    	window.onload = func;}
	else {
    	window.onload = function() {
      		if (oldonload) {
        		oldonload();}
      		func();
    		}
		}
	}
var CoList = new Array()
var CoWindow = new Array()
function writeCompanyInfo(CoName, NeedZip, DefaultURL) {
	//var CoNameID = CoName.replace(" ","").replace("!","")
	var CoNameID = CoName.replace(/[^a-zA-Z0-9]/,"","g")
	CoList[CoList.length] = CoNameID
	var sText = "<div class='CompanyDiv'>"
	sText += '<input id="' + CoNameID + '" type="checkbox"  checked="checked"/>  '
	sText += '<b><label id="' + CoNameID +'lbl" for ="' + CoNameID + '">' + CoName + '</label></b>' 
	//sText += '<span id="' + CoNameID +'lbl"><b>' + CoName + '</b></span>' 
	if (NeedZip) {
		sText += ' requires a zip code <input id="' + CoNameID + 'ZipCode" type="text" />'
		}
	sText += '<div class="URLDiv">Default URL'
	sText += '<input id="' + CoNameID + 'URL" type="text" value="' + DefaultURL + '" size="75"/>'
	sText += '</div>'	//URLDiv
	sText += '</div>'	//CompanyDiv
	//document.write(sText)
	//alert (sText)
	document.getElementById("SearcherList").innerHTML += sText
	}
function launchCoWindow(CoName, CoURL){
	if (CoWindow[CoName]) {
		try{
			CoWindow[CoName].location.href=CoURL
			}
		catch (e){
			CoWindow[CoName]=window.open(CoURL,CoName)
			}
		}
	else {
		//alert(CoURL + ", " + CoName)
		CoWindow[CoName]=window.open(CoURL, CoName)
		}
	}

function processSearch(){
	var product = document.getElementById('Product').value
	product = product.replace(' ', '+')
	if (product=='') {
		alert ('Please enter a search phrase')
		//document.getElementById('Product').setFocus
		return (false)
		}
	for (var i=0; i < CoList.length; i++) {
		var CoName=CoList[i]
		//alert(CoName + ' ' + document.getElementById(CoName).checked + '\n'
		//	+ document.getElementById(CoName + 'URL').value)	
		if (document.getElementById(CoName).checked) {
			var sURL = document.getElementById(CoName + 'URL').value
			var ZipCode = document.getElementById(CoName + 'ZipCode')
			if (!ZipCode) {
				ZipCode=''
				}
			else {
				ZipCode = ZipCode.value
				if (ZipCode=='') {
					alert ('Please enter a zipcode')
					return (false)
					}
				}
			//alert(sURL)
			sURL = sURL.replace("#product#", escape(product))
			sURL = sURL.replace("#zip#", ZipCode)
			//alert (sURL)
			//try{window.open(sURL, CoName)} finally {}
			launchCoWindow(CoName, sURL)
			//alert('After window.open ' + sURL)
			}
		//alert ("Next i")
		}
	return(false)
	}

function createDefaultSearch(){
	document.getElementById("SearcherList").innerHTML=""
	setCookie(cookieID, "", -1)
	CoList = new Array();
	switch (cookieID){
	case "TMWebSearch":
		writeCompanyInfo("Google",false,
			"http://www.google.com/search?q=#product#")
		writeCompanyInfo ("Yahoo!", false,
			"http://search.yahoo.com/search?p=#product#")
		writeCompanyInfo ("Live Search", false,
			"http://search.live.com/results.aspx?q=#product#")
		break;
	case "TMProductSearch":
		writeCompanyInfo("Staples",false,
			"http://www.staples.com/office/supplies/StaplesSearch?searchkey=#product#&storeId=10001&catalogId=10051&langId=-1&fromUrl=home")
		writeCompanyInfo ("Office Depot", false,
			"http://www.officedepot.com/textSearch.do?uniqueSearchFlag=true&Ntt=#product#")
		writeCompanyInfo ("Best Buy", false,
			"http://www.bestbuy.com/site/olspage.jsp?id=pcat17071&type=page&st=#product#&usc=All+Categories&sc=Global&nrp=15")
		writeCompanyInfo ("Costco", false,
			"http://www.costco.com/Common/Search.aspx?N=0&Ntt=#product#")
		writeCompanyInfo ("Frys", false, 
			"http://www.frys.com/search?search_type=regular&query_string=#product#")
		writeCompanyInfo ("Amazon", false, 
			"http://www.amazon.com/s/ref=nb_ss_gw/104-4585045-5172748?url=search-alias%3Daps&field-keywords=#product#&x=0&y=0")
		break;
	case "TMRetailSearch":
		writeCompanyInfo("Nordstrom",false,
			"http://shop.nordstrom.com/SR?KeyWord=#product#")
		writeCompanyInfo ("Macys", false,
			"http://www.macys.com/search/index.ognc?Keyword=#product#")
		writeCompanyInfo ("JC Penney", false,
			"http://www.jcpenney.com/jcp/SearchDepartment.aspx?SearchString=#product#")
		writeCompanyInfo ("Sears", false,
			"http://www.sears.com/shc/s/search_10153_12605?keyword=#product#")
		writeCompanyInfo ("Target", false, 
			"http://www.target.com/gp/search/191-1387414-1668760?field-keywords=#product#")
		writeCompanyInfo ("Walmart", false, 
			"http://www.walmart.com/search/search-ng.do?search_query=#product#")
		writeCompanyInfo ("Amazon", false, 
			"http://www.amazon.com/s/ref=nb_ss_gw/104-4585045-5172748?url=search-alias%3Daps&field-keywords=#product#&x=0&y=0")
		break;
		}
	//alert (CoList.length)
	}
function newSearcher(){
	writeCompanyInfo("New" + CoList.length ,false,"")
	}
function cookiesEnabled(){
	setCookie("TMTest","test",1)
	var returnedCookie=getCookie("TMTest")
	//document.write("returnedCookie=" + returnedCookie + "<br />")
	setCookie("TMTest","",-1)
	return(returnedCookie!="")
	}
function saveSearches(){
	var Rslt = ""
	for (var i=0; i < CoList.length; i++) {
		Rslt += CoList[i] + ";" + escape(document.getElementById(CoList[i] + "lbl").innerHTML) + ";" + escape(document.getElementById(CoList[i] + "URL").value) + ";"
		//alert (Rslt)
		}
	//alert(Rslt + "\n" + escape(Rslt))
	setCookie(cookieID,Rslt,365)
	}
function getSavedSearches(){
	var returnedCookie = getCookie(cookieID)
	if (returnedCookie==""){
		return(false)
		}
	else {
		CoList = new Array();
		var CookieArr = returnedCookie.split(";")
		for (var i = 0; i < CookieArr.length-1; i+=3) {
			writeCompanyInfo(unescape(CookieArr[i+1]), false, unescape(CookieArr[i+2]))
			}
		return (true)
		}
	}
	function createCookieID(){
		var pageHref=document.location.href
		pageHref = pageHref.substring(pageHref.lastIndexOf("/")+1)
		pageHref=pageHref.substring(0,pageHref.indexOf("."))
		//alert(pageHref);
		switch (pageHref){
		case "web":
			cookieID = "TMWebSearch"
			break;
		case "product":
			cookieID = "TMProductSearch"
			break;
		case "retail":
			cookieID = "TMRetailSearch"
			break;
		default:
			}
		//alert(cookieID)
		}
function createSearches() {
	createCookieID();
	if (!getSavedSearches()){
		createDefaultSearch()
		}
	enableCustomization()
	//alert(document.location.href)
	submitDefaultSearch()
	}
function enableCustomization(){
	var canSaveCookies =  cookiesEnabled()
	document.getElementById("SaveCustomizations").disabled=!canSaveCookies
	document.getElementById("EnableCookies").style.visibility=(canSaveCookies ? "hidden" : "visible") 
	}
function submitDefaultSearch(){
	var searchStr=document.location.search
	//alert(searchStr.length)
	
	var myHREF=document.location.href
	//alert(document.location.href + "," + document.location.search + "," + document.location.pathname);
	
	var QPos =  myHREF.indexOf("?")
	if (QPos >= 0){
		document.getElementById("Product").value=unescape(myHREF.substr(QPos+1))
		//alert(document.getElementById("Product").value)
		processSearch()
		}
	}
addLoadEvent(createSearches)
