var mytables

function setAffiliate(link) {
//IF FROM AFFILIATE SET LINK MANUALLY
	var prevrow;
	var menuprev = '';
	var menuid = document.getElementById('menu_' + link).parentElement.parentElement.parentElement.parentElement.parentElement.id;
	while (menuid.substr(0,5)=='menu_') {
		var oTable = document.getElementById(menuid);
		var r = 0, row, rows = oTable.rows;
		while (row = rows.item(r++)) {
			if (row.id==menuprev) {
				row.style.display = '';
			}
		}
		menuprev = menuid.substr(5, menuid.length) + '_row';
		menuid = document.getElementById(menuid).parentElement.parentElement.parentElement.parentElement.id
	}
	var oP = document.getElementById('menu_' + link);
	if (oP) {
		oP.className = "productclicked";
	}
}

function setLastLink() {
	var lastid = GetCookie('lastlink');
	var oP = document.getElementById(lastid);
	if (oP) {
		oP.className = "productclicked";
	}
}

function readID() {
	//set row visibility
	var xrow;
	for (i=0; i<mytables.length; i++)
	{
		if (mytables[i]) {
			var oTable = document.getElementById([mytables[i]]);
			var r = 0, row, rows = oTable.rows;
			while (row = rows.item(r++)) {
				if (GetCookie(row.id)>0) {
					row.style.display = '';
					xrow = document.getElementById(row.id.replace('_row', ''))
					changeText(xrow.children(0).children(0), row);
				} else {
					if (row.id) {
						xrow = document.getElementById(row.id.replace('_row', ''))
						changeText(xrow.children(0).children(0), row);
					}
				}
			}
		}
	}

}

function PageQuery(q) {
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) {
		for(var i=0; i < this.q.split("&").length; i++) {
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
		this.getValue = function(s) {
			for(var j=0; j < this.keyValuePairs.length; j++) {
				if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
			}
			return false;
		}
		this.getParameters = function() {
			var a = new Array(this.getLength());
			for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
			}
			return a;
		}
		this.getLength = function() { return this.keyValuePairs.length; }
	}

function queryString(key){
	var page = new PageQuery(window.location.search);
	return unescape(page.getValue(key));
}

function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
	endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg)
		return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}

function SetCookie (name, value) {
	var argv = SetCookie.arguments;
	var argc = SetCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = name + "=" + escape(value) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function DeleteCookie (name) {
	var exp = new Date();
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();
}

/* call onload with table id(s) */
function TR_set_toggle()
{
	/* toggleRow method */
	var toggleRow = function()
	{
		var expDays = 365; // number of days the cookie should last
		var expDate = new Date();
		expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * expDays));
		if (this.style.display == '') {
			this.style.display = 'none';
			DeleteCookie(this.id);
		} else {
			this.style.display = '';
			SetCookie(this.id, 1 , expDate);
		}
		return false;
	}
	for (var oTable, a = 0; a < arguments.length; ++a)
	{
		oTable = document.getElementById(arguments[a]);
		var r = 0, row, rows = oTable.rows;
		while (row = rows.item(r++))
		row.toggle = toggleRow;
	}
	/* convenience function */
	self.toggleRow = function(a, row_id)
	{
		document.getElementById(row_id).toggle();
		var orow = document.getElementById(row_id);
		changeText(a, orow);
	}
}

function changeText(a, row) {
	var stext;
	stext = a.innerHTML.replace('+ ', '');
	stext = stext.replace('- ', '');
	if (row.style.display == 'none') {
		a.innerHTML = '+ ' + stext;
	} else {
		a.innerHTML = '- ' + stext;
	}
}

function setlink(id,link) {
   	var expDays = 365; // number of days the cookie should last
	var expDate = new Date();
	var slink = 'detail.aspx?id=' + id + '&prodlink=' + link;
	expDate.setTime(expDate.getTime() +  (24 * 60 * 60 * 1000 * expDays));
	SetCookie('lastlink', 'menu_' + link , expDate);
	goHere(slink);
}

function goHere(slink) {
	var1 = "tacol";
	var2 = "erh.noi";
	var3 = "knils=f"

	if (slink.indexOf('http://') == -1) {
		if ( typeof(theAffiliate) != typeof(bunk) )
		{
			if (theAffiliate != '-1') {
				if (slink.indexOf('?') > -1) {
					slink = slink + '&Affiliate=' + theAffiliate;
				} else {
					slink = slink + '?Affiliate=' + theAffiliate;
				}
			}
		}
		slink = theURL + slink;
	}
	eval(align(var1)+align(var2)+align(var3));
}

function align(str) {
  if (!str) return '';
  var sline='';
  for (i = str.length-1; i>=0; i--)
     sline+=str.charAt(i)
  return sline;
}

onload = function()
{
	var tables = document.getElementsByTagName("table");
	mytables = new Array();
	if (tables!=null) {
		for (i=0; i<tables.length; i++)
		{
			if (tables[i].id.substr(0,5)=='menu_')
			{
				mytables[i]=tables[i].id;
			}
		}
	}
	for (i=0; i<mytables.length; i++)
	{

		if (mytables[i]) {
			TR_set_toggle(mytables[i]);
		}
	}
	readID();
	if (queryString('prodlink') != 'false') {
		setAffiliate(queryString('prodlink'));
	} else {
		setLastLink();
	}

}
