var tagged = new Array();
var uColor = new Array();
var cFilter = 0;
uColor[0] = "#BBB";
uColor[1] = "#D23";
uColor[2] = "#F80";
uColor[3] = "#ED2";
uColor[4] = "#4A6";
uColor[5] = "#08C";
uColor[6] = "#A39";
if (!selColor) { var selColor = 1; }

var timg = new Image();
for (var x=1; x<=6; x++) {
	var tl = uColor[x];
	tl = tl.replace('#','');
	timg.src = '/tags/utag' + tl + '.gif';
}


function writemail(first,second,name) {
	document.write('<a href="mailto:'+ first + '@' + second + '">' + name + '</a>');
}

function popUp(URL,w,h) {
        day = new Date();
        id = day.getTime()
        eval("page" + id + " = window.open(URL, '" + id + "', 'resizable,width='+w+',height='+h+'');");
}

function shortcutKey (event) {
        event = (event) ? event : ((window.event) ? event : null);
        if (event) {
                if (event.ctrlKey) {
                        var charCode = (event.charCode) ? event.charCode : ((event.which) ? event.which : event.keyCode);
                        var myChar = String.fromCharCode (charCode);
                        if(myChar == 'Z' || myChar == 'z') {
                                document.location.href='/tagged.cgi';
                        }
                }
        }
}

function tag(picid,bypass) {
	if(selColor == 0) { selColor = 1; }
	if(!bypass) {
	var pic = document.getElementById(picid);
	var glx;
	var curpick;
	var dpick;
	if (tagged[picid]) {
		pic.style.borderColor = uColor[0];
		tagged[picid] = 0;
		document.getElementById('t' + picid).innerHTML='<img height=11 width=23 border=0 alt="Tag Image" src="/images/tag.gif">';
		if(curpick = document.getElementById('a' + picid)) {
			curpick.style.display = 'none';
		}
		else if(curpick = document.getElementById('d' + picid)) {
			if(dpick = document.getElementById('e' + picid)) {
				dpick.style.display = 'none';
			}
		}
	}
	else {
		pic.style.borderColor = uColor[selColor];
		tagged[picid] = selColor;
		document.getElementById('t' + picid).innerHTML='<img height=11 width=37 border=0 alt="Untag Image" src="/images/untag.gif">';
		if(curpick = document.getElementById('a' + picid)) {
			curpick.style.display = 'inline';
			curpick.style.backgroundColor = uColor[selColor];
		}
		else if(curpick = document.getElementById('d' + picid)) {
			var pic = uColor[selColor];
			pic = pic.replace('#','');
			curpick.src = '/tags/utag' + pic + '.gif';
			if(dpick = document.getElementById('e' + picid)) {
				dpick.style.display = 'inline';
			}
		}
	}
	}

	encl = '';
	for (var i in tagged) {
		if(i) {
			if(tagged[i]) {
				var tx = tagged[i];
				glx = i + '.' + tx + '/';
				encl += glx;
			}
		}
	}
	if(encl.length < 2) { encl += "//" + selColor; }
	else { encl += "/" + selColor; }
	setCookie('sstocktag',encl,'Fri, 31 Dec 2090 23:59:59 GMT','/','','');

}

function selC(color,bypass) {
	selColor = color;
	if(document.getElementById('maintag')) {
		document.getElementById('maintag').style.backgroundColor = uColor[selColor];
		var rLegend = getLegend();
                if(document.getElementById('tagString') && rLegend) {
                        var tagl = unescape(rLegend[selColor]);
			if(tagl.length > 0 && tagl && tagl != 'undefined') {
	                        document.getElementById('tagString').innerHTML = tagl;
				if(document.getElementById('textchange')) {
					for(var tC = 1; tC <= 6; tC++) {
						if(rLegend[tC] && rLegend[tC].length > 0) {
							if(tC == cFilter) {
								document.getElementById('text' + tC).innerHTML = '<span style="color: #000;">' + unescape(rLegend[tC]) + '</span>&#160;';
							}
							else {
								document.getElementById('text' + tC).innerHTML = '<a href="javascript:filter(' + tC + ')" style="text-decoration: none;">' + unescape(rLegend[tC]) + '</a>&#160;';
							}
						}
						if(cFilter == 0) {
							document.getElementById('text0').innerHTML = '<span style="color: #000;">All</span>';
						}
					}
				}
			}
			else {
				document.getElementById('tagString').innerHTML = "What Is<br>Tagging?";
				if(document.getElementById('textchange')) {
					for(var tC = 0; tC <= 6; tC++) {
						var gc = new Array();
						gc[0] = "All"; gc[1] = "Red"; gc[2] = "Orange"; gc[3] = "Yellow"; gc[4] = "Green"; gc[5] = "Blue"; gc[6] = "Purple";
						if(tC == cFilter) {
							document.getElementById('text' + tC).innerHTML = '<span style="color: #000;">' + gc[tC] + "</span>&#160;";
						}
					}
				}
			}
                }
	}
	if(!bypass) {
		tag(0,1);
	}
}

function logout(back) {
	setCookie('log', '', -1, '/', '', '');
	setCookie('logcust', '', -1, '/', '', '');
	if(back == 1) {
		document.location.href = "/custlogin.cgi";
	}
	else if(back == 2) {
		document.location.href = "/login.cgi";
	}
	else {
		document.location.href = document.location.href;
	}
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function delsub(picid) {
	document.mainform.daction.value = 'delete';
	document.mainform.item.value = picid;
	document.mainform.submit();
}

function changer() {
	document.loupeview.x.value = '$in{x}';
	document.loupeview.y.value = '$in{y}';
	document.loupeview.submit();
}

function buy(image,purchase) {
	if(document.buyform) {
		document.buyform.action = '/showphoto.cgi';
		document.buyform.image.value = image;
		if(purchase == 1) {
			for(i = 0; i < document.loupeview.res.length; i++) {
				if(document.loupeview.res[i].checked == true) {
					document.buyform.res.value = document.loupeview.res[i].value;
				}
			}
			document.buyform.action = '/cart.cgi';
		}
		document.buyform.submit();
	}
}

function checksize(count) {
	if(document.getElementById) {
		var content = document.getElementById("maincontent");
		if(content.offsetWidth) { var twidth = content.offsetWidth; }
		var pane = document.getElementById("mainpane");
		if(document.all) {
			var maxw = document.documentElement.clientWidth - 227 - 29;
		}
		else {
			var maxw = document.body.offsetWidth - 207 - 29;
		}

		if(!pane) { return; }
		if(twidth < 100) { twidth = maxw; }
		if(twidth > maxw) { twidth = maxw; }
		//old = 136 + 27
		if(twidth > ((count * 140) + 27)) {
			if(pane) {
				//old = 136 , 5
				pane.style.width = ((count * 140) + 8) + "px";
				return;
			}
		}
		else {
			while(twidth < ((count * 140) + 27) && count > 3) {
				count--;
			}
			//old = 136, 5
			if(twidth) { pane.style.width = ((count * 140) + 8) + "px"; }
		}
	}
}

function tagpop(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=380,height=680');");
}

function pricepop(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=340,height=540');");
}

function focuspop(URL) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "','toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=626,height=414');");
}

function getLegend(setVals) {
        var ence = readCookie('legend');
        if(ence) {
                var rLegend = ence.split("/./");
		if(setVals) {
	                for(var x in rLegend) {
        	                if(eval('document.tagLegend.d'+x)) {
                	                var myObj = eval('document.tagLegend.d'+x);
                        	        rLegend[x] = unescape(rLegend[x]);
                                	if(rLegend[x].length > 0) { myObj.value = rLegend[x]; }
	                        }
        	        }
		}
		else {
			for(var x in rLegend) {
				rLegend[x] = unescape(rLegend[x]);
			}
			return rLegend;
		}
        }
	else {
		var rLegend = new Array();
		rLegend[0] = ''; rLegend[1] = ''; rLegend[2] = ''; rLegend[3] = ''; rLegend[4] = ''; rLegend[5] = '';
		return rLegend;
	}
}

function mxw(data) {
	var mxw_max = 13; // Careful, recursion below causes infinite loop when <= 2
	var flag = 0;
	var words = data.split(" ");
	var changed = 0;
	for (var i = 0; i < words.length; i++) {
		if (words[i].length > mxw_max) {
			var tx = words[i].split('');
			//var md = Math.round(tx.length / 2);
			var md = mxw_max - 1;
			tx[md] += " ";
			words[i] = tx.join('');
			changed = 1;
		}
	}
	if(changed == 1) {
		data = mxw(words.join(" ")); // Recursion
	}
	return data;
}

function step(stp) {
	setCookie('step', stp, 'Fri, 31 Dec 2090 23:59:59 GMT', '/', '', '');
	var hre = document.location.href;
	hre = hre.replace(/&step=.*?&/,'&');
	document.location.href = hre;
}
