// Some portions Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

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;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

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";
}

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

function rememberMe (f) {
    var now = new Date();
    fixDate(now);
    now.setTime(now.getTime() + 365 * 24 * 60 * 60 * 1000);
    setCookie('mtcmtauth', f.author.value, now, '', HOST, '');
    setCookie('mtcmtmail', f.email.value, now, '', HOST, '');
    setCookie('mtcmthome', f.url.value, now, '', HOST, '');
}

function forgetMe (f) {
    deleteCookie('mtcmtmail', '', HOST);
    deleteCookie('mtcmthome', '', HOST);
    deleteCookie('mtcmtauth', '', HOST);
    f.email.value = '';
    f.author.value = '';
    f.url.value = '';
}

function init() {
	if(document.getElementById("search")) document.getElementById("search").focus();
}

function OpenBrWindow(theURL,winName,features) {
    window.open(theURL,winName,features);
}

function OpenScrollWindow(theURL,winName,theWidth,theHeight) {
	var x=Math.min(screen.width,theWidth);
	var y=Math.min(screen.height-25,theHeight);
	window.open(theURL,winName,'width='+x+',height='+y+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,top='+((screen.height/2)-(y/2))+',left='+((screen.width/2)-(x/2))+'');
}

function OpenRiddleWindow(theURL,winName,theWidth,theHeight) {
	var x=Math.min(screen.width,theWidth);
	var y=Math.min(screen.height-25,theHeight);
	window.open(theURL,winName,'width='+x+',height='+y+',toolbar=1,location=1,directories=0,status=1,menubar=1,scrollbars=yes,resizable=yes,top='+((screen.height/2)-(y/2))+',left='+((screen.width/2)-(x/2))+'');
}

function OpenBJScrollWindow(theURL,winName,theWidth,theHeight) {
	var w=window.open(theURL,winName,'width='+theWidth+',height='+theHeight+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,top='+((screen.height/2)-(theHeight/2))+',left='+((screen.width/2)-(theWidth/2))+'');
	return w;
}

function OpenJBWindow(theURL,winName,theWidth,theHeight) {
	if(theWidth > screen.width || theHeight > (screen.height-25)) {
		OpenJFSWindow(theURL,winName);
	} else {
		window.open(theURL,winName,'width='+theWidth+',height='+theHeight+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,top='+((screen.height/2)-(theHeight/2))+',left='+((screen.width/2)-(theWidth/2))+'');
	}
}

function OpenBJWindow(theURL,winName,theWidth,theHeight) {
	var w=window.open(theURL,winName,'width='+theWidth+',height='+theHeight+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,top='+((screen.height/2)-(theHeight/2))+',left='+((screen.width/2)-(theWidth/2))+'');
	return w;
}

function OpenFSWindow(theURL,winName) {
	var x=Math.min(screen.width,1200);
	var y=Math.min(screen.height-25,840);
	var w=window.open(theURL,winName,'width='+x+',height='+y+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,top=0,left=0');
	return w;
}

function OpenJFSWindow(theURL,winName) {
	var x=Math.min(screen.width,1200);
	var y=Math.min(screen.height-25,840);
	window.open(theURL,winName,'width='+x+',height='+y+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=yes,resizable=yes,top=0,left=0');
}

function remoteRedirect(w,loc) {
	w.location.replace(loc);
	return;
}

function showSpoiler(spoilerbtn) {
	spoilerbtn.nextSibling.style.display ="block";
	spoilerbtn.parentNode.removeChild(spoilerbtn);
}

function showspoilerinfo() {
	OpenScrollWindow('/spoilerinfo.php','SpoilerInfo',516,550);
	return false;
}

function resetSpoilers(x) {
	var a = x.replace(/<pre><sp>/g,"<spoiler>");
	var b = a.replace(/<\/sp><\/pre>/g,"</spoiler>");
	a = b.replace(/<sp>/g,"<spoiler>");
	b = a.replace(/<\/sp>/g,"</spoiler>");
	a = b.replace(/<pre>/g,"");
	b = a.replace(/<\/pre>/g,"");
	document.comments_form.text.value = b;
}


function fightIlliteracy(x) {

	// If the comment is all uppercase, convert it to lowercase.
	if (x.toUpperCase() == x) {
		x = x.toLowerCase();
	}

	var patterns = new Array(
		"\\bevery1\\b",			// every1	-> everyone
		"\\bany1\\b",			// any1		-> anyone
		"\\bsum1\\b",			// sum1		-> someone
		"\\bsome1\\b",			// some1	-> someone
		"\\bplz\\b",			// plz		-> please
		"\\brly\\b",			// rly		-> really
		"\\breli\\b",			// reli		-> really
		"\\bpwn\\b",			// pwn 		-> own
		"\\br\\b",				// r 		-> are
		"\\bteh\\b",			// teh 		-> the
		"\\bthx\\b",			// thx 		-> thanks
		"\\bu\\b",				// u 		-> you
		"\\bwat\\b",			// wat		-> what
		"!(?:!|1|(?:one)){3,}",		// !!!11oneone	-> ! 		(more than 3 occurences are considered offending)
		"\\?{4,}"			// ???????????	-> ?		(more than 3 occurences are considered offending)
	);
	
	var replacements = new Array(
		"everyone",
		"anyone",
		"someone",
		"someone",
		"please",
		"really",
		"really",
		"own",
		"are",
		"the",
		"thanks",
		"you",
		"what",
		"!",
		"?"
	);
	
	for (var i = 0; i < patterns.length; i++) {
		var pattern = patterns[i];
		x = x.replace(
			new RegExp(pattern, "gi"), 
			function (str) {
				if (str.charAt(0).toUpperCase() == str.charAt(0)) {
					return replacements[i].charAt(0).toUpperCase() + replacements[i].slice(1);
				} else {
					return replacements[i];
				}
			}
		);
	}

	return x;
}

function validateCommentName() {
	var name = $('input#comment-author').val().replace(/^(\s+)/,"");
	$('input#comment-author').val(name);
	var retVal = true;
	if($('input#comment-author').val() == "") {
		if(confirm('Your name is blank. Are you sure want to be known as "Anonymous"? That\'s rather dull. In fact, we are likely to make up a name for you, and you might not like it. So, click \'Cancel\' to go back and enter something more friendly.')) {
			$('input#comment-author').val("Anonymous");
		} else retVal = false;
	}
	return retVal;
}
function validateCommentEmail() {
	var email = $('input#comment-email').val().replace(/(\s+)/,"");
	if((email.length < 6) ||
	  (email.indexOf('@',0) < 1) ||
	  (email.lastIndexOf('@') != email.indexOf('@',0)) ||
	  (email.lastIndexOf('@') > (email.length - 5)) ||
	  (email.lastIndexOf('.') > (email.length - 3)) ||
	  (email.lastIndexOf('.') < (email.length - 4)) ||
	  (email.indexOf('..',0) > -1) ||
	  (email.indexOf('@.',0) > -1) ||
	  (email.indexOf('.@',0) > -1) ||
	  (email.indexOf(',',0) > -1)) {
		$('input#comment-email').val("none@none.com");
	}
}
function validateCommentURL() {
	var url = $('input#comment-url').val().replace(/(\s+)/,"");
	if((url.length < 11) ||
	  (url.indexOf('http://',0) != 0) ||
	  (url.indexOf('jayisgames.com',0) > -1) ||
	  (url.lastIndexOf('http://') != url.indexOf('http://',0)) ||
	  (url.indexOf('..',0) > -1) ||
	  (url.indexOf('@',0) > -1) ||
	  (url.indexOf(',',0) > -1)) {
		$('input#comment-url').val("");
	}
}
function disableSubmit() {
	document.getElementById('comment-post').disable = true;
}
function enableSubmit() {
	document.getElementById('comment-post').disable = false;
}
function validateCommentForm(x) {
	var regExp1 = /(<spoiler>)|(<spoilers>)|(\[spoiler\])|(\[spoilers\])/;
	var regExp2 = /(<\/spoiler>)|(<\/spoilers>)|(\[\/spoiler\])|(\[\/spoilers\])/;
	var x1 = x.search(regExp1);
	var x2 = x.search(regExp2);
	if(x1 != -1 && x2 != -1 && x1 < x2) {
		var w = x.replace(regExp1,"<pre><sp>");
		var z = w.replace(regExp2,"</sp></pre>");
		return validateCommentForm(z);
	} else if(x1 == -1 && x2 == -1) {
		document.comments_form.text.value = x;
		var ok = true;
		if(!commenter_name) {
			ok = validateCommentName();
			if(ok) validateCommentEmail();
		}
		if(ok) validateCommentURL();
		if(ok) {
			alert("Your comment is being submitted. It may take several seconds to process. Please do not submit the comment multiple times or none of them may appear.");
			return true;
		}
		else {
			enableSubmit();
			return false;
		}
	} else {
		alert("You have a problem with the formatting of your spoiler, please correct the problem.\n\nBe sure that you have an equal number of open tags '<spoiler>' as you do close tags '</spoiler>', AND that they are in the correct order.");
		enableSubmit();
		return false;
	}
}
