function seoRewriteUrl(rawUrl) 
{
	//just looking for the home page
	if (rawUrl == null || rawUrl == "" || rawUrl == "/") {
		return "";
	}

	//only replace periods in the extenstion since they are used in the prices
	rawUrl = rawUrl.replace('.aspx', '/shp/');
	rawUrl = rawUrl.replace('&', ',');
	return rawUrl;
}

function truncate(s, at, postfix)
{
	if (s.length > at)
	{
		var cutOffIdx = at - 1;
		var spaceIdx = s.lastIndexOf(' ', cutOffIdx);
		if (spaceIdx > -1)
			cutOffIdx = spaceIdx;
		s = s.substr(0, cutOffIdx) + postfix;
	}
	
	return s;
}

function show_ads(target, attempt, header)
{
	if (typeof zSr != 'undefined')
	{
		var i = 6;
		var itemHtml = '';
		if (i < zSr.length)
		{
			if (typeof header != 'undefined')
				itemHtml = unescape(header);

			while (i < zSr.length)
			{
				var descr	= truncate(zSr[i++], 90, "...");
				var unused	= zSr[i++];
				var clickURL= zSr[i++];
				var title	= truncate(zSr[i++], 40, "...");
				var sitehost= zSr[i++];
				var bid		= zSr[i++];
				itemHtml += '<div class="adItem"><a href="' + clickURL + '">' + title + '</a><br>' + descr + '<br><a class="truncated" href="' + clickURL + '">' + sitehost + '</a></div>';
				if (i < zSr.length)
					itemHtml += '<div class=\"HR\"></div>';
			}
			document.getElementById(target).innerHTML  += itemHtml;
		}
	}
	else
	{
		if (attempt <= 20)
			window.setTimeout("show_ads('" + target + "', " + ++attempt + ", '" + header + "')", 500);
	}
}
var s_maxCount = 0;
var s_maxReachedMsg = '';

function setMaxVariables(maxCount, maxReachedMsg) {
	s_maxCount = maxCount;
	s_maxReachedMsg = maxReachedMsg;
}

function addToProdComp(prodId, checked)
{
	var cnt = setCookieValue("prodcomp", prodId, (checked?prodId:null), s_maxCount, s_maxReachedMsg);
	if (cnt < 0)
		return false;
	var prodcountT = document.getElementById("prodCompCountT");
	var prodcountB = document.getElementById("prodCompCountB");
	
	if (cnt > 0)
	{
		if (prodcountT)
			prodcountT.innerHTML = "&nbsp;" + cnt + "&nbsp;";
		if (prodcountB)
			prodcountB.innerHTML = "&nbsp;" + cnt + "&nbsp;";
	}		
	else
	{
		if (prodcountT)
			prodcountT.innerHTML = "&nbsp;";
		if (prodcountB)
			prodcountB.innerHTML = "&nbsp;";
	}
	return true;
}

function setCookieValue(name, currVal, newVal, maxCount, maxReachedMsg) 
{
	var valcount = 0;
	var val = getCookieValue(name);
	if (val && val.length > 0)
	{
		var values = val.split("|");
		var found = -1;
		if (currVal)
			for(var valIdx = 0; valIdx < values.length; valIdx++)
			{
				if (values[valIdx] == currVal)
					found = valIdx;
			}
		if (!newVal && found > -1)
		{
			var newvalues = new Array();
			if (found > 0)
			{
				newvalues = newvalues.concat(values.slice(0, found));
			}
			if (found < values.length - 1)
			{
				newvalues = newvalues.concat(values.slice(found + 1, values.length));
			}
			values = newvalues;
		}
		if (newVal && found == -1)
			if (values.length < maxCount)
				values[values.length] = newVal;
			else
			{
				alert(maxReachedMsg);
				return -1;
			}
		newVal = values.join("|");
		if (newVal.length > 0)
			document.cookie = name + "=" + escape(newVal) + ";path=/";
		else
			document.cookie = name + "=;path=/";
			
		valcount = values.length;
	}
	else
	{
		if (newVal)
		{
			document.cookie = name + "=" + escape(newVal) + ";path=/";
			valcount = 1;
		}
	}
	return valcount;
}

function getCookieValue(name) 
{
	var dc = document.cookie;
	var search = name + "=";
	var pos = dc.indexOf("; " + search);
	if (pos == -1) 
	{
		pos = dc.indexOf(search);
		if (pos == -1) return null;
	} 
	else
	{
		pos += 2;
	}
	var pos2 = dc.indexOf(";", pos);
	if (pos2 == -1)
		pos2 = dc.length;
	var val = dc.substring(pos + search.length, pos2);
	return unescape(val);
}

function submitProdComp(name, msg)
{
	var val = getCookieValue(name);
	if (!val || val.length == 0 || val.split("|").length < 2)
	{
		alert(unescape(msg));
		return false;
	}
	return true;
}

function adjustScreenElementsWidth(width)
{
	try
	{
		var availWidth;
		if (document.body.clientWidth)
			availWidth = document.body.clientWidth;
		else 
			availWidth = window.innerWidth;
		if (width > availWidth)
		{
			document.getElementById("header").style.width = width;
			document.getElementById("dark2whiteBar").style.width = width;
			document.getElementById("shopFooter").style.width = width;
		}
	}
	catch(e)	
	{ }
}

function restoreProdCompCookie(max, redirUrl)
{
	setMaxVariables(max, '');
	var matches = window.location.search.match(/itemId=[0-9]+/gi);
	for(i = 0; i < matches.length; i++)
	{
		addToProdComp(matches[i].substring(7, matches[i].length), true);
	}
	if (redirUrl.length > 0)
		window.location = redirUrl;
	else
		history.back();
}

function Redirect(strURL) 
{
	window.location=strURL;
	return false;
}

function Refresh(strURL) 
{
	window.location=strURL;
	return false;
}

function switchDesc(id) {
	var button = document.getElementById("switch_"+id);

	if(button==null)
		return true;

	var more = document.getElementById("more_"+id);
	var part2 = document.getElementById("fullDesc_"+id);
		
	if(button.className=='plusDesc') { //expand
		button.className='minusDesc';
		if(part2!=null) part2.style.display='inline';
		if(more!=null) more.style.display='none';
	} else if(button.className=='minusDesc') { // contract
		button.className='plusDesc';
		if(part2!=null) part2.style.display='none';
		if(more!=null)more.style.display='inline';
	}
	if( navigator != null && navigator.userAgent != null && navigator.userAgent.indexOf("Firefox") != -1 )
		document.body.innerHTML = document.body.innerHTML; // force firefox to re-compute the layout
	return false;
}

function DoSearch(searchText, msg, baseURL) {
	if(!searchText || searchText.length<1 || Trim(searchText).length<1) {
		alert(msg);
		return false;
	}
	
	if(baseURL && baseURL!='') {
		window.location=baseURL+seoRewriteUrl('/results.aspx?text='+encodeURIComponent(searchText));
		return false;
	}
	return true;
}
function Trim(source) {
  source = source.replace( /^\s+/g, "" );
  return source.replace( /\s+$/g, "" );
}

function clearChks()
{	
	if( navigator != null && navigator.userAgent != null && navigator.userAgent.indexOf("Firefox") != -1 )
	{
		var inputs = document.body.getElementsByTagName("input");				
		var cookieValue = getCookie("prodcomp");
		var j=-1;
		var prodId;
		var checkId;
		var values;
		
		if(cookieValue !=null)
		{
			values = cookieValue.split("|");
			for(var i=0;i<inputs.length;i++)
			{
				j = inputs[i].id.indexOf("prodcomp_");
				if(j >=0)
				{
					checkId = inputs[i].id;	
					prodId = checkId.substring(9,checkId.length);
					if(cookieValue.length == 0 || !isProdExists(values,prodId))
					{
						inputs[i].checked=false;
					}
				}					
			}
			
			var prodcountT = document.getElementById("prodCompCountT");
			var prodcountB = document.getElementById("prodCompCountB");
			
			if(values !=null && values.length > 0 && values[0].length !=0)
			{
				if (prodcountT)
					prodcountT.innerHTML = "&nbsp;" + values.length + "&nbsp;";
				if (prodcountB)
					prodcountB.innerHTML = "&nbsp;" + values.length + "&nbsp;";
				
			}else
			{
				if (prodcountT)
					prodcountT.innerHTML = "&nbsp;";
				if (prodcountB)
					prodcountB.innerHTML = "&nbsp;";			
			}
							
		}
	}
		return false;
}

function isProdExists(values, prodId)
{
	if (values !=null)
	{
		for(var i=0;i<values.length;i++)
		{
			if(prodId == values[i])
				return true;
		}
	}		
	return false;
}
function getCookie(name)
{
    var cookie = document.cookie;
    var prefix = name + "=";
    var begin = cookie.indexOf("; " + prefix);    
    //if its the only cookie
    if (begin == -1)
    {
        begin = cookie.indexOf(prefix);        
        if (begin != 0) return null;
    }
    else
    {
		//go two characters one for ";" and one for space " "
        begin += 2;
    }
    //find the length of the cookie
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = cookie.length;
    }
    //return the value
    return unescape(cookie.substring(begin + prefix.length, end));
}
