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(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++], 80, "...");
				var unused	= zSr[i++];
				var clickURL= zSr[i++];
				var title	= truncate(zSr[i++], 40, "...");
				var sitehost= zSr[i++];
				var bid		= zSr[i++];
				itemHtml += '<div><a href="' + clickURL + '">' + title + '</a></div>' + descr + '<div style="width:120px;text-overflow:ellipsis;overflow:hidden;white-space:nowrap;"><a href="' + clickURL + '">' + sitehost + '</a></div>';
				if (i < zSr.length)
					itemHtml += '<hr />';
			}
			document.getElementById("rightAds_container").innerHTML  += itemHtml;
		}
	}
	else
	{
		if (attempt <= 20)
			window.setTimeout("show_ads(" + ++attempt + ", '" + header + "')", 500);
	}
}

function addToProdComp(prodId, checked, maxCount, maxReachedMsg)
{
	var cnt = setCookieValue("prodcomp", prodId, (checked?prodId:null), maxCount, 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 = "";
		if (prodcountB)
			prodcountB.innerHTML = "";
	}
	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 < values.length - 1)
			{
				newvalues = newvalues.concat(values.slice(found + 1, values.length));
			}
			if (found > 0)
			{
				newvalues = newvalues.concat(values.slice(0, found));
			}
			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) + ";";
		else
			document.cookie = name + "=";
		valcount = values.length;
	}
	else
	{
		if (newVal)
		{
			document.cookie = name + "=" + escape(newVal) + ";";
			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("headerTile").style.width = width;
			document.getElementById("dark2whiteBar").style.width = width;
			document.getElementById("footerTile").style.width = width;
		}
	}
	catch(e)	
	{ }
}

function Redirect(strURL) 
{
	window.location=strURL;
	return false;
}

function Refresh(strURL) 
{
	window.location=strURL;
	return false;
}