
var waitTime;
var decTime = 50;
var started = false;
var popped=false;
var lastDomChecked = '';
var fld;
var rawWords;

    function setTime(e,field)
    {
    	fld = field;
    	var oEvent = window.event ? window.event : e;
    	var keyId = oEvent.keyCode;
    	//alert(keyId);
    	if (keyId in {9:'',16:'',17:'',18:'',19:'',35:'',36:'',37:'',38:'',39:'',40:'',45:''}) {/* alt:tab?,  shift,ctrl,alt,pause? spacebar,end,home,arrows, insert,del*/ }
    	else {  waitTime=500;  popped=false;  if (started==false) { started=true; setTimeout(areWeReady, (decTime)); }  }
    }

    function cleanDomain()
    {
    	fqdn = fld; //document.getElementById('fqdn');
    	//fqdn.value = fqdn.value.replace(/[^a-zA-Z0-9-\ ]/g, "").toLowerCase();
    	//while (fqdn.value.substr(0,1)=="-") { fqdn.value = fqdn.value.substr(1); }
    	//while (fqdn.value.substr(fqdn.value.length-1,1)=="-") { fqdn.value= fqdn.value.substr(0,fqdn.value.length-1); }
    	//while (fqdn.value.substr(fqdn.value.length-1,1)==" ") { fqdn.value= fqdn.value.substr(0,fqdn.value.length-1); }
    	newFqdn = fqdn.value;
    }

    function showMe(state) { if (state==true) document.getElementById('time').innerText = waitTime+ " - " + decTime + " - " + popped + " - " + started + " >> "; }
    function areWeReady() {	if (waitTime <= 0){ getData(); } else { waitTime -= decTime; setTimeout(areWeReady, (decTime)); }  }
    function getData() { if (popped==false) { popped=true; started=false; cleanDomain(); getAjax(); } }

    function checkIfSame(domtocheck) { if (domtocheck == lastDomChecked) { return true; } else { lastDomChecked=domtocheck; return false; } }

    function getAjax()
    {
    	userDom = newFqdn.replace(/[^a-zA-Z0-9- ]/g, "");
    	updateSuggestSource(userDom);
    }

    function updateSuggestSource(term)
    {
    	cleanTerm = escape(term);
    	//alert(cleanTerm);
        	olddiv = document.getElementById('gs');
        	head.removeChild(olddiv);
        	olddiv = document.getElementById('as');
        	head.removeChild(olddiv);
        	olddiv = document.getElementById('yt');
        	head.removeChild(olddiv);
        	//olddiv = document.getElementById('eb');
        	//head.removeChild(olddiv);
        	
        	//Goggle
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.id = 'gs';
		newScript.src = 'http://www.google.com/complete/search?hl=en&qu='+cleanTerm;
		head.appendChild(newScript);
		
		//Amazon
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.id = 'as';
		newScript.src = 'http://completion.amazon.com/search/complete?method=completion&q='+cleanTerm+'&search-alias=aps&client=amazon-search-ui&mkt=1&x=azComplete';
		head.appendChild(newScript); 
		
		//YouTube
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.id = 'yt';
		newScript.src = 'http://suggestqueries.google.com/complete/search?hl=en&client=suggest&hjson=t&ds=yt&jsonp=ytComplete&q='+cleanTerm+'&cp=12';
		head.appendChild(newScript); 
		
		//Yahoo
		var newScript = document.createElement('script');
		newScript.type = 'text/javascript';
		newScript.id = 'yh';
		newScript.src = 'http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&output=json&callback=yhComplete&results=50&query='+cleanTerm;
		head.appendChild(newScript); 
		
		//Ebay
		//var newScript = document.createElement('script');
		//newScript.type = 'text/javascript';
		//newScript.id = 'eb';
		//splitTerm=sliceIt(cleanTerm);
		//alert(splitTerm);
		//newScript.src = 'http://include.ebaystatic.com/autofill/f/0/1236071361/m/r/_20/t/o/a/d.js';
		//head.appendChild(newScript);    
    }
    
    	var head = document.getElementsByTagName("head")[0];
    	window.google={ac:{h:function(data){Gsug1(data[1])}}};
	function azComplete(){ Azsug1(completion[1]); }
	function ytComplete(ytdata){ Ytsug1(ytdata[1]); }
	function yhComplete(yhdata){ Yhsug1(yhdata.ResultSet.Result); }
	vjo={darwin:{domain:{finding:{autofill:{AutoFill:{_do:function(data){Esug1(data)}}}}}}};

    function Gsug1(gwordlist)
    {
	$('gsug').innerHTML = '';
    	if (gwordlist.length > 0)
    	{
    		//alert(gwordlist);
    		gwordlist.each(function(word,windex)
    		{
    		//	alert(word[0] + ' ... ' + word[1]);
    			baseWord = fixBaseWord(word[0]);
    			alt_title = "Search term in new Google window";
    			sugBaseLink = '<a class="suglink" href="http://www.google.com/search?q='+baseWord.replace(/ /g,'+')+'&safe=active" alt="'+alt_title+'" title="'+alt_title+'" target="_blank">'+baseWord+'</a>'+' ... '+word[1];
    			addSuggest('gsug', sugBaseLink);
    		});
    	} else addSuggest('gsug', 'No Suggestions From Google');
    }
    
    function Azsug1(awordlist)
    {
	$('asug').innerHTML = '';
    	if (awordlist.length > 0)
    	{
    		//alert(awordlist);
    		awordlist.each(function(word,windex)
    		{
    			//alert(word[0] + ' ... ' + word[1]);
    			baseWord = fixBaseWord(word);
    			alt_title = "Search term in new Amazon window";
    			sugBaseLink = '<a class="suglink" href="http://www.amazon.com/s/ref=nb_ss_gw_0_4?url=search-alias%3Daps&field-keywords='+baseWord.replace(/ /g,'+')+'&tag=employeentrep-20" alt="'+alt_title+'" title="'+alt_title+'" target="_blank">'+baseWord+'</a> ';
    			addSuggest('asug', sugBaseLink);
    		});
    	} else addSuggest('asug', 'No Suggestions From Amazon');
    }
    
    function Ytsug1(ytwordlist)
    {
	$('ytsug').innerHTML = '';
    	if (ytwordlist && ytwordlist.length > 0)
    	{
    		//alert(ytwordlist);
    		ytwordlist.each(function(word,windex)
    		{
    			//alert(word + ' > ' + word[0] + ' ... ' + word[1]);
    			baseWord = fixBaseWord(word[0]);
    			alt_title = "Search term in new YouTube window";
    			sugBaseLink = '<a class="suglink" href="http://www.youtube.com/results?search_type=&search_query='+baseWord.replace(/ /g,'+')+'&safe_search=on&aq=0" alt="'+alt_title+'" title="'+alt_title+'" target="_blank">'+baseWord+'</a> ... ' + word[1];
    			addSuggest('ytsug', sugBaseLink);
    		});
    	} else addSuggest('ytsug', 'No Suggestions From YouTube');
    }
    
    function Yhsug1(yhwordlist)
    {
    	//yhwordlist = ywordlist.ResultSet.Result;
	$('yhsug').innerHTML = '';
	$('esug').innerHTML = ' ... coming soon ...';

    	if (yhwordlist && yhwordlist.length > 0)
    	{
    		//alert(yhwordlist);
    		yhwordlist.each(function(word,windex)
    		{
    			//alert(word[0] + ' ... ' + word[1]);
    			baseWord = fixBaseWord(word);
    			alt_title = "Search term in new Yahoo window";
    			sugBaseLink = '<a class="suglink" href="http://search.yahoo.com/search?p='+baseWord.replace(/ /g,'+')+'&ei=UTF-8&vm=r" alt="'+alt_title+'" title="'+alt_title+'" target="_blank">'+baseWord+'</a> ';
    			addSuggest('yhsug', sugBaseLink);
    		});
    	} else addSuggest('yhsug', 'No Suggestions From Yahoo');
    }
    
    function Esug1(ewordlist)
    {
	$('ehsug').innerHTML = ' coming soon ...';
    	if (ewordlist.length > 0)
    	{
    		//alert(ewordlist);
    		ewordlist.each(function(word,windex)
    		{
    			//alert(word[0] + ' ... ' + word[1]);
    			baseWord = fixBaseWord(word[0]);
    			alt_title = "Search term in new Ebay window";
    			sugBaseLink = '<a class="suglink" href="http://www.amazon.com/s/ref=nb_ss_gw_0_4?url=search-alias%3Daps&field-keywords='+baseWord.replace(/ /g,'+')+'" alt="'+alt_title+'" title="'+alt_title+'" target="_blank">'+baseWord+'</a> ';
    			addSuggest('esug', sugBaseLink);
    		});
    	} else addSuggest('esug', 'No Suggestions From Ebay');
    }
    
    function addSuggest(box, line)
    {
    	$(box).insert(line+'<br />');
    }
    
    function sliceIt(term)
    {
    	return term.split('').join('/');
    }
    
    
    function fixBaseWord(baseWord) // fix apostrophes returned from suggest
    {
    	baseWord = baseWord.replace(/'/g,'');
    	baseWord = baseWord.replace(/\\x27/g,'\'');
    	return baseWord;
    }
