var indexPage = false;
	
function alterAction()
	{
		form = document.forms.ebay;
		catSelect = $('cat');
		cat = catSelect.options[catSelect.selectedIndex].value;
		query = $('query').value;
		alrtStr = '';
		hebrew = false;
		newQuery = '';
		for(i=0; i<query.length;i++)
		{
			if(query.charCodeAt(i)>=1488 && query.charCodeAt(i)<=1514)
				hebrew = true;
			else
				newQuery += query.charAt(i);
			//alrtStr += query.charCodeAt(i) + '\n';
		}
		//alert(alrtStr);
		//if(hebrew)
		//	$('hebrewDiv').style.display = "";
		//else
		//	$('hebrewDiv').style.display = "none";
		query = newQuery;
		$('query').value = query;
		if(cat!='' && query!='')
			form.action = 'Search?cat='+cat+'&q='+query;
		else
		{
			if(cat!='')
				form.action = 'Search?cat='+cat;
			if(query!='')
				form.action = 'Search?q='+query;
		}
	}
	function submitForm()
	{
		showLoading();
		alterAction();
		$('submit').click();
	}

window.addEvent('domready', function() {
	if(!indexPage) {
		addAccordion();
		createShipReqs();
		//updateShip(text);
		function updateShipError()
		{
			
		}
		$('ebayForm').addEvent('submit', function(e) { mySubmit(e);	});
		moveLogo();
	}
});
function moveLogo()
{
	size = $('logoDiv').getSize().x - $('babylonDiv').getSize().x - $('logo').getSize().x;
	if(size>0)
		size = size/2;
	else
		size=0;
	$('logoDiv').setStyle('width', $('logo').getSize().x);
	$('logoDiv').setStyle('left', size+80);
}
	
function mySubmit(e)
{
	var filter = $('noShip').checked;
	if(filter)
	{
		e.stop();
		sendFilterRequest();
	}
	else
	{
		alterAction();
	}
	
}
var pages = 0;
	function disableEnterKey(e)
	{
		//alert("don't do anything");
		/*var key;
		
		if(window.event)
			key = window.event.keyCode;     //IE
		else
			key = e.which;     //firefox
		if(key == 13)
			submitForm();*/
    }

	function loading()
	{
		//document.body.onkeypress = enterKey;
		
		
	}
	function filter()
	{
		/*var filter = $('noShip').checked;
		if(filter)
		{
			//$('submit').set('type','');
			$('ebayForm').removeEvent('submit');
			$('ebayForm').addEvent('submit',function(e){e.stop();});
			$('submitButton').set('type','button');
			$('submitButton').addEvent('click',function(){sendFilterRequest();});
		}
		if(!filter)
		{
			$('ebayForm').removeEvent('submit');
			$('ebayForm').addEvent('submit', function(e) {mySubmit(e);});
			$('submitButton').set('type','submit');
			$('submitButton').removeEvent('click');
		}
		*/
	}
	
	function showLoading()
	{
		document.getElementById('loading').style.display = '';
	}
	
	
	function alterAction2()
	{
		showLoading();
		var filter = $('noShip').checked;
		if(!filter)
		{
			form = document.forms.ebay;
			catSelect = $('cat');
			cat = catSelect.options[catSelect.selectedIndex].value;
			query = $('query').value;
			alrtStr = '';
			hebrew = false;
			newQuery = '';
			for(i=0; i<query.length;i++)
			{
				if(query.charCodeAt(i)>=1488 && query.charCodeAt(i)<=1514)
					hebrew = true;
				else
					newQuery += query.charAt(i);
				
			}
			//alert(alrtStr);
			/*if(hebrew)
				$('hebrewDiv').style.display = "";
			else
				$('hebrewDiv').style.display = "none";*/
			query = newQuery;
			$('query').value = query;
			if(cat!='' && query!='')
				form.action = 'Search?cat='+cat+'&q='+query;
			else
			{
				if(cat!='')
					form.action = 'Search?cat='+cat;
				if(query!='')
					form.action = 'Search?q='+query;
			}
		}
		//else
		
	}
	function sendFilterRequest()
	{
		showLoading();
		pages=0;
		$('results').setStyle('visibility','hidden');
		catSelect = $('cat');
		cat = catSelect.options[catSelect.selectedIndex].value;
		query = $('query').value;
		var link = '';
		if(cat!='' && query!='')
			link = 'filter.jsp?cat='+cat+'&q='+escape(query)+'&page=1';
		else
		{
			if(cat!='')
				link = 'filter.jsp?cat='+cat+'&page=1';
			if(query!='')
				link = 'filter.jsp?q='+escape(query)+'&page=1';
		}
		min = $('min').value;
		max = $('max').value;
		//maxship = $('maxship').value;
		maxship = '';
		if(min != '')
			link += '&Min=' + min;
		if(max != '')
			link += '&Max=' + max;
		if(maxship != '')
			link += '&maxship=' + maxship;
		
		var req = new Request.HTML({
			url: link,
			onSuccess: function(text){pages=0;updateResults(text);},
			onFailure: function(){updateShipError();}
		});
		
		req.send();
	}
	
	function submitForm2()
	{
		/*showLoading();
		alterAction();
		var filter = $('noShip').checked;
		if(!filter)
		{
			$('ebayForm').send();
			//$('submit').click();
		}*/
	}
	
	
	function updateResults(html)
	{
		//alert(pages);
		pages++;
		var newPage = new Element('div', {id: 'page'+pages});
		newPage.adopt(html);
		if(pages==1)
		{
			$('results').empty();
		}
		$('results').setStyle('visibility','visible');
		newPage.inject($('results'));
		//alert(pages);
		if(pages>1)
		{
			$('page'+(pages-1)).setStyle('display','none');
			$('pagesLinks').dispose();
			
		}
		
		pagesLinksStr = '';
		//alert(pages);
		for(i=1;i<=pages;i++)
		{
			pagesLinksStr += '&nbsp;<a class="pageBtn" href="javascript:;" onclick="showFilteredPage('+i+');">'+i+'</a>';
		}
		pagesLinksStr += '&nbsp;<a class="pageBtn" id="nextBtn" href="javascript:;" onclick="getNextPage();">next</a><img id="nextLoading" src=http://doesitship.com/loading.gif style="display:none;" />';
		var pagesLinks = new Element('div', {id: 'pagesLinks'});
		pagesLinks.set('html',pagesLinksStr);
		pagesLinks.inject($('results'));
		$('results').dir='ltr';
		$('loading').style.display = 'none';
		toManyPages = $('toManyPages').get('text','');
		if(toManyPages == 'toManyPages')
		{
			$('loading').style.display = 'none';
			$('nextBtn').setStyle('display','none');
		}
		if($('pageLinks')!=null)
			$('pageLinks').setStyle('display','none');
		if($('googleAd1')!=null)
		{
			$('googleAd1').setPosition($('googleAd2').getPosition());
		}
		
	}
	function showFilteredPage(page)
	{
		for(i=1;i<=pages;i++)
			$('page'+i).setStyle('display','none');
		$('page'+page).setStyle('display','');
	}
	function getNextPage()
	{
		$('nextBtn').setStyle('display','none');
		$('nextLoading').setStyle('display','');
		
		catSelect = $('cat');
		cat = catSelect.options[catSelect.selectedIndex].value;
		query = $('query').value;
		var link = '';
		if(cat!='' && query!='')
			link = 'filter.jsp?cat='+cat+'&q='+escape(query)+'&page='+(pages+1);
		else
		{
			if(cat!='')
				link = 'filter.jsp?cat='+cat+'&page='+(pages+1);
			if(query!='')
				link = 'filter.jsp?q='+escape(query)+'&page='+(pages+1);
		}
		min = $('min').value;
		max = $('max').value;
		//maxship = $('maxship').value;
		maxship = '';
		if(min != '')
			link += '&Min=' + min;
		if(max != '')
			link += '&Max=' + max;
		if(maxship != '')
			link += '&maxship=' + maxship;
		
		var endtimes = $('endtime'+pages);
		link += '&end=' + endtimes.get('text','');
		var req = new Request.HTML({
			url: link,
			onSuccess: function(text){updateResults(text);},
			onFailure: function(){updateShipError();}
		});
		req.send();
	}
	function updateShip(text)
	{
		var id = text.trim().split('%')[0];
		var cost = text.trim().split('%')[1];
		if(cost.contains('-2'))
		{
			var req = new Request({
	
			url: 'shipping.jsp?id='+id,
	
			onSuccess: function(text){
				updateShip(text);
			},
			onFailure: function(){
				updateShipError();
			}

		});
		req.send();
		}
		else
		{
			if(cost.contains('Ask') || cost.contains('Free'))
				$(id).set('text',cost);
			else {
				txt = $(id).get('text');
				$(id).set('text',txt+cost);
			}
		}
	}
function setCountry(countryOpt) {
	var opt = countryOpt.options[countryOpt.selectedIndex];
	//alert(opt.value +' '+opt.text);
	Cookie.write('country',opt.value);
	Cookie.write('countryName',opt.text);
}


function _cb_FindItemsAdvanced(root)
{
	var customid = Cookie.read('reff');
	if(customid == null)
		customid = "";
	var items = root.SearchResult[0].ItemArray.Item || [];
	var html = [];
	html.push('<table>');
	html.push('<tr><th></th><th>Item</th><th>Price</th></tr>');
	for (var i = 0; i < items.length; ++i)
	{
		var item = items[i];
		var title = item.Title;
		var itemID = item.ItemID;
		//var viewitem = '/url.jsp?ebayID='+itemID;
		var viewitem = 'http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=4&campid=5336143582&toolid=10013&customid='+customid+'&mpre=http%3A%2F%2Fcgi.ebay.com%2Fws%2FeBayISAPI.dll%3FViewItem%26item%3D'+itemID;
		var itemImgLink = "http://thumbs.ebaystatic.com/pict/" + itemID + ".jpg";
		var buyitnow;
		if(item.ConvertedBuyItNowPrice != null)
			buyitnow = item.ConvertedBuyItNowPrice.Value;
		var currPrice = item.ConvertedCurrentPrice.Value;
		if (null != title && null != viewitem)
		{
		  html.push('<tr><td><a href="' + viewitem + '"><img src="'+itemImgLink+'" /></a></td><td><a href="' + viewitem + '">' + title + '</a></td><td>'+currPrice+'</tr>');
		}
	}
	html.push('</table>');
	document.getElementById("results").innerHTML = html.join("");

}
function Util_nextPageStore(nextPageCont,results)
{
	//nextPageCont = nextPageCont.replace(/<!--<img/g, '<img');
	nextPageCont = nextPageCont.replace(/!--<img/g, '!<img');
	nextPageCont = nextPageCont.replace(/!<img/g, '<img');
	nextPageCont = nextPageCont.replace(/<<img/g, '<img');
	nextPageCont = nextPageCont.replace(/--></g, '<');
	nextPageCont = nextPageCont + '<img style="text-decoration:none;border:0;padding:0;margin:0;" src="http://rover.ebay.com/roverimp/1/711-53200-19255-0/1?pub=5574637837&toolid=10013&campid=5336143582&mpt=[CACHEBUSTER]">'
	results.push(nextPageCont);
	//results.push('<img style="text-decoration:none;border:0;padding:0;margin:0;" src="http://rover.ebay.com/roverimp/1/711-53200-19255-0/1?pub=5574637837&toolid=10013&campid=5336143582&mpt=[CACHEBUSTER]">');
	
	var pages = 'Pages: ';
	for(var i = 0; i<(results.length-1) && i<=60; i++)
	{
		 pages += "<a class='pageBtn' href='javascript:;' onclick='showPage("+(i+1)+");'>"+(i+1)+"</a>";
		 if(i == 30)
			pages += "<br \>";
	}
	if(i<=60)
		pages += "<a class='pageBtn' id='nextBtn' href='javascript:;' onclick='showPage("+(i+1)+");'>Next</a>";
	$('pageLinks').innerHTML = pages;
	if(newrequest)
	{
		newrequest = false;
		Util_showPage(1, results);
		document.getElementById('loading').style.display = 'none';
	}
}
function Util_showPage(pageNum, results)
{
	$('query').focus();
	if(results.length>pageNum)
		$('results').innerHTML = results[pageNum-1];
	else if ((results.length+1) <= totalPages)
	{
		$('nextBtn').setStyle('visibility','hidden');
		$('results').innerHTML = results[pageNum-1];
		$('pageLinks').innerHTML += loadingImg;
		newsrc = ''+nextpagereq+(results.length+1);
		
		myIFrame.set('src', newsrc);
		$('results2').innerHTML= '';
		$('results2').grab(myIFrame);
	}
	else
	{
		$('nextBtn').innerHTML = ''+pageNum;
		$('results').innerHTML = results[pageNum-1];
	}
	
}
function Util_cb_FindPopularSearches(root,cat,popStr)
{
	var keys = root.PopularSearchResult || [];
	var popular = [];
	var html = "";
	for (var i = 0; i < keys.length; ++i)
	{
		if(keys[i].RelatedSearches != null)
			popular = keys[i].RelatedSearches.split(';');
	}
	
	for(i=0; null != popular && i<popular.length; i++)
	{
		if(i==0)
			html += "<table bgcolor='#e2ecfa' width='100%'>\n<tr>\n<td>"+popStr+"</td>\n<td>\n<table width='100%'>\n";
		if(i%7==0)
			html += "<tr>\n";
		if(cat != null)
			html += "<td><a href='Search?cat="+cat+"&q="+popular[i].replace(/" "/g,"%20")+"'>"+popular[i]+"</a></td>\n";
		else
			html += "<td><a href='Search?q="+popular[i].replace(/" "/g,"%20")+"'>"+popular[i]+"</a></td>\n";
		if(i%7==6)
			html += "</tr>\n";
	}
	if(i>0 && i%7==0)
		html += "</tr>\n";
	if(i>0)
	{
		html += "</table>\n</td>\n</tr>\n</table>";
		document.getElementById("popular").innerHTML = html;
	}
}
function Util_cb_FindPopularSearches2(root,cat,popStr)
{
	var keys = root.PopularSearchResult || [];
	var popular = [];
	var html = "";
	for (var i = 0; i < keys.length; ++i)
	{
		if(keys[i].RelatedSearches != null)
			popular = keys[i].RelatedSearches.split(';');
	}
	
	for(i=0; null != popular && i<popular.length; i++)
	{
		if(i==0)
		{
			html += "<table bgcolor='#e2ecfa' width='100%'>\n<tr>\n<td><div id='popStrShow'>"+popStr+"<br /><a href='javascript:;' onclick='hidePop();'>hide</a></div>";
			html += "<div id='popStrHide' style='display:none;'><a href='javascript:;' onclick='showPop();'>"+popStr.replace(/"<br \/>"/g," ")+"</a></div></td>\n";
			html += "<td>\n<div id='popularHideDiv'><table width='100%'>\n";
		}
		if(i%7==0)
			html += "<tr>\n";
		if(cat != null)
			html += "<td><a href='Search?cat="+cat+"&q="+popular[i].replace(/" "/g,"%20")+"' onclick='getCatNQuery(\""+cat+"\",\""+popular[i].replace(/" "/g,"%20")+"\");return false'>"+popular[i]+"</a></td>\n";
		else
			html += "<td><a href='Search?q="+popular[i].replace(/" "/g,"%20")+"'>"+popular[i]+"</a></td>\n";
		if(i%7==6)
			html += "</tr>\n";
	}
	if(i>0 && i%7==0)
		html += "</tr>\n";
	if(i>0)
	{
		html += "</table></div>\n</td>\n</tr>\n</table>";
		document.getElementById("popular").innerHTML = html;
	}
}
function showPop()
{
	document.getElementById("popStrShow").style.display = '';
	document.getElementById("popularHideDiv").style.display = '';
	document.getElementById("popStrHide").style.display = 'none';
}
function hidePop()
{
	document.getElementById("popStrShow").style.display = 'none';
	document.getElementById("popularHideDiv").style.display = 'none';
	document.getElementById("popStrHide").style.display = '';
}
var urlTo='http://rover.ebay.com/rover/1/711-53200-19255-0/1?type=4&campid=5336143582&toolid=10001&customid=&mpre=http%3A%2F%2Fcgi.ebay.com%2Fws%2FeBayISAPI.dll%3FViewItem%26item%3D';
var urlFrom = '';
function changeTo(obj, id)
{
	urlFrom = obj.href;
	obj.href=urlTo + id;
}
function changeFrom(obj)
{
	obj.href=urlFrom;
}
function ebayClick(id)
{
	//var logRequest = new Request.HTML({url:'/click.jsp'}).get({'ebayID': id});
	//logRequest.send();
	logIFrame = new IFrame({ 
			src: '/click.jsp?ebayID='+id,
			events: {
				load: function(){
					;
				}
			}
		});
	$('stam').grab(logIFrame);
}
