
var disclaimWin = null;
var entryWin = null;
var memberWin = null;
var sshowWin = null;
var tourWin = null;

var allowedTags = new Array("i", "em", "cite", "b", "strong", "u", "ol", "ul", "li");
var allowedTagsDescr = new Array("<i>Italic</i>", "<i>Emphasis</i> (same as &lt;i&gt;)", "<cite>Citation</cite>", "<b>Bold</b>", "<strong>Bold</strong> (same as &lt;b&gt;)", "<u>Underline</u>", "Ordered (numbered) list", "Unordered (bulleted) list", "List item");

function AdminSetChlg(sel)
{

	var idx = sel.selectedIndex;
	var value = sel[idx].value;

	if (value.indexOf("prevote") >= 0) setCookie("setchlg", "prevote", "", "/", ".fujimugs.com");
	else if (value.indexOf("postvote") >= 0) setCookie("setchlg", "postvote", "", "/", ".fujimugs.com");
	else if (value.indexOf("vote") >= 0) setCookie("setchlg", "vote", "", "/", ".fujimugs.com");
	else deleteCookie("setchlg", "/", ".fujimugs.com");
	self.location.reload();
}

function CheckHTMLTags(str, title)
{

	var tagopen = null;
	var tagclose = null;

	str = str.toLowerCase();

	// Check for unbalanced tags.
	for (var i = 0 ; i < allowedTags.length ; i++)
	{
		tagopen = "<" + allowedTags[i] + ">";
		tagclose = "</" + allowedTags[i] + ">";

		var c1 = str.split(tagopen).length;
		var c2 = str.split(tagclose).length;
		if (c1 != c2)
		{
			alert("WARNING: Unbalanced tag: " + tagopen);
			return false;
		}
	}

	// Remove allowed tags and see what's left.
	for (var i = 0 ; i < allowedTags.length ; i++)
	{
		tagopen = "<" + allowedTags[i] + ">";
		tagclose = "</" + allowedTags[i] + ">";
		while (str.indexOf(tagopen) >= 0)
		{
			str = str.replace(tagopen, "");
		}
		while (str.indexOf(tagclose) >= 0)
		{
			str = str.replace(tagclose, "");
		}
	}

	if (str.indexOf("<") >= 0)
	{

		var alertmsg = title + " contains forbidden HTML tags.\n";
		alertmsg += "Only the following HTML tags may be used:\n";
		for (var i = 0 ; i < allowedTags.length ; i++)
		{
			alertmsg += "<" + allowedTags[i] + "> and </" + allowedTags[i] + ">\n";
		}

		alert(alertmsg);
		return false;
	}

	return true;
}

function CheckLoginForm(frm)
{

	if (frm.username.value.length == 0)
	{
		alert("Enter your username");
		frm.username.focus();
		return false;
	}

	else if (frm.passwd.value.length == 0)
	{
		alert("Enter your password.\nIf you do not remember your password, click the 'forgot password' link");
		frm.passwd.focus();
		return false;
	}

	else return true;
}

function CheckTags(text, tag)
{

	var t1 = "<" + tag + ">";
	var t2 = "</" + tag + ">";

	var c1 = text.split(t1).length;
	var c2 = text.split(t2).length;
	if (c1 != c2)
	{
		alert("WARNING: Unbalanced HTML tag: <" + tag + ">");
		return false;
	}
	else return true;
}

function ConfirmURL(msg, url)
{

	if (confirm(msg)) self.location.href = url;
}

function DeleteCookies()
{

	deleteCookie("userName", "/", ".fujimugs.com");
	deleteCookie("userPasswd", "/", ".fujimugs.com");
	deleteCookie("web_session", "/", ".fujimugs.com");

	deleteCookie("userName", "/", "");
	deleteCookie("userName", "/", "fujimugs.com");
	deleteCookie("userName", "/", "www..fujimugs.com");

	deleteCookie("userPasswd", "/", "");
	deleteCookie("userPasswd", "/", "fujimugs.com");
	deleteCookie("userPasswd", "/", "www.fujimugs.com");

	deleteCookie("web_session", "/", "");
	deleteCookie("web_session", "/", "fujimugs.com");
	deleteCookie("web_session", "/", "www.fujimugs.com");
}

function Disclaimer()
{

	if (! disclaimWin || disclaimWin.closed) disclaimWin = open("/disclaimer.htm", "disclaimWindow", "width=600,height=700,resizable=1,scrollbars=1");
	disclaimWin.focus();
}

function EntrySetStart(start)
{

	if (! document.entryForm) alert("entryForm not found");

	var frm = document.entryForm;
	frm.start.value = "" + escape(start);
	frm.submit();
}

function FavoritesClear(chid, cat)
{
	if (confirm("Really remove all " + cat + " favorites?"))
	{
		var url = "/challenge/favorites_clear.php?chid=" + escape(chid) + "&cat=" + escape(cat);
		self.location.href = url;
	}
}

function ForgotPasswd()
{

	var email = prompt("Enter your registered email address and your password will be sent to you", "");
	if (email) self.location.href = "forgot_passwd.php?email=" + escape(email);
}

function LogOff(prmpt)
{

	if (prmpt && ! confirm("Log off?")) return;

	DeleteCookies();
	self.location.replace("/index.php");
}

function MemberEmail(memberid)
{
	var tmpwin = open("/member_email.php?member=" + escape(memberid), "tmpwindow", "width=500,height=100");
	tmpwin.focus();
}

function MugSetStart(start)
{

	if (! document.memberForm) alert("memberForm not found");

	var frm = document.memberForm;
	frm.start.value = "" + escape(start);
	frm.submit();
}

function MugThumbsLarge()
{

	if (! document.memberForm) alert("memberForm not found");

	var frm = document.memberForm;
	frm.thumb.value = "large";
	frm.submit();
}

function MugThumbsSmall()
{

	if (! document.memberForm) alert("memberForm not found");

	var frm = document.memberForm;
	frm.thumb.value = "small";
	frm.submit();
}

function OfflineDownload(zipfile)
{
	alert("Downloading the ZIP file will begin when you click OK below.\n" +
	       "Save the ZIP file to your computer, extract the contents to a folder, then double-click 'index.htm'");
	open(zipfile, "zipWindow");
}

function PageFooter()
{

	var winx = 0;
	if (window.innerWidth) winx = self.innerWidth;
	else if (window.clientWidth) winx = window.clientWidth;
	else if (document.body.offsetWidth) winx = document.body.offsetWidth;
	else winx = 810;

	setCookie("pagex", winx, "", "/", ".fujimugs.com");
	setCookie("pagex", winx, "", "/");
}

function PageLoad()
{

	if (document.loginForm) document.loginForm.username.focus();
}

function SetUserTimezone()
{
	var visitortime = new Date();
	var tz = visitortime.getTimezoneOffset() / 60;
	setCookie("usertz", tz, expDate, "/", ".fujimugs.com");
}

function ShowEntry(url, pagetitle, imgx, imgy)
{

	var winx = imgx + 20;
	var winy = imgy + 100;

	if (entryWin && ! entryWin.closed) entryWin.close();
	entryWin = open("/blank.htm", "entryWindow", "width=" + winx + ",height=" + winy + ",resizable=1");

	var d = entryWin.document;
	d.open();
	d.writeln("<html><head><title>" + pagetitle + "</title>\n" +
	           "<link rel='stylesheet' type='text/css' href='/fujimugs.css'></head>\n" +
	           "<body bgcolor='#C0C0C0'><center>");

	d.writeln("<span class='caption'>" + pagetitle + "</span><br>");
	d.writeln("<img src='" + url + "' width='" + imgx + "' height='" + imgy + "' border='0'><br><br>");

	d.writeln("<form><input type='button' class='small button' value='Close' onClick='if (opener) opener.focus(); self.close()'></form>");
	d.writeln("</body>\n</html>");
	d.close();

	entryWin.focus();
}

function ShowMember(memid, pagetitle, imgx, imgy)
{

	var winx = imgx + 20;
	var winy = imgy + 100;

	if (memberWin && ! memberWin.closed) memberWin.close();
	memberWin = open("/blank.htm", "memberWindow", "width=" + winx + ",height=" + winy + ",resizable=1");

	var d = memberWin.document;
	d.open();
	d.writeln("<html><head><title>" + pagetitle + "</title>\n" +
	           "<link rel='stylesheet' type='text/css' href='/fujimugs.css'></head>\n" +
	           "<body bgcolor='#DFDFDF'><center>");

	d.writeln("<img src='/mugshots/" + memid + ".jpg' width='" + imgx + "' height='" + imgy + "' border='0'><br><br>");

	d.writeln("<form><input type='button' class='small button' value='Close' onClick='if (opener) opener.focus(); self.close()'></form>");
	d.writeln("</body>\n</html>");
	d.close();

	memberWin.focus();
}

function Slideshow(fname)
{

	if (! sshowWin || sshowWin.closed) sshowWin = open("/slideshow.php?fname=" + escape(fname), "sshowWindow", "width=1100,height=850,resizable=1,scrollbars=1");
	sshowWin.focus();
}

function Tour()
{

	if (! tourWin || tourWin.closed) tourWin = open('/tour/index.php', 'tourWindow', 'width=1000,height=600,scrollbars=1,resizable=1');
	tourWin.focus();
}

function VoteBlockInfo()
{
	var msg = "You may not vote for the person who submitted this entry.\n";
	alert(msg);
}

function WithdrawEntry(entryid)
{

	var msg = "If you remove this entry, you can upload another one\n";
	msg += "or re-upload this one any time before voting starts.\n\n";
	msg += "Remove this entry?";

	var withdraw = confirm(msg);
	if (withdraw)
	{
		var url = "/challenge/entry_withdraw.php?entry=" + escape(entryid);
		self.location.href = url;
	}
}


// Cookie functions.
function setCookie(name, value, expires, path, domain, secure)
{

	var curCookie = name + "=" + escape(value) +
	                ((expires) ? "; expires=" + expires.toGMTString() : "") +
	                ((path) ? "; path=" + path : "") +
	                ((domain) ? "; domain=" + domain : "") +
	                ((secure) ? "; secure" : "");
	document.cookie = curCookie;

}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
function getCookie(name)
{

	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	}

	else begin += 2;

	var end = document.cookie.indexOf(";", begin);
	if (end == -1) end = dc.length;

	return unescape(dc.substring(begin + prefix.length, end));

}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain)
{

	if (getCookie(name))
	{
		document.cookie = name + "=" +
		                  ((path) ? "; path=" + path : "") +
		                  ((domain) ? "; domain=" + domain : "") +
		                  "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}

}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date)
{

	var base = new Date(0);
	var skew = base.getTime();
	if (skew > 0) date.setTime(date.getTime() - skew);

}

// Cookie expiration date: +6 months.
var expDate = new Date();
expDate.setTime(expDate.getTime() + (180 * 24 * 60 * 60 * 1000));

