// Add an eventListener to browsers that can do it somehow.
// Originally by the amazing Scott Andrew.
function addEvent(obj, evType, fn){
  if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
  } else if (obj.attachEvent){
	var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
	return false;
  }
}

addEvent(window,'load',tripcodeSet);
addEvent(window,'load',collapseOrExpand);
addEvent(window,'load',hiliter);

var myOptions = 'hotkeys,scrollbars=yes,width=500,height=500';
var last_hilited = '';

function pop(what) {
	// Using '_blank' for name makes popup separate across invocations
	// ie 2 separate sites won't fight for the same popup, and even within
	// a site, two separate actions won't target the same
	window.name=location.href;
	popUpWin = window.open(what,'_blank',myOptions);
}

function tripcodeSet()
{
	if(me = document.getElementById("tripcode")) {
		if(tripcode = getCookie("tripcode")) {
			me.value = tripcode.replace("+"," "); // Bad interplay between JS/PHP
		}
	}
	if(me = document.getElementById("verify")) {
		me.value = "";
	}
}

function getCookie(name)
{
	var cookie = document.cookie;
	var key = document.cookie.indexOf(name);
	if (key == -1 || !name) {
		return false;
	}
	var start = key+name.length+1;
	var end = document.cookie.indexOf(';',key);
	if (end == -1) {
		end=document.cookie.length;
	}
	return unescape(document.cookie.substring(start,end));
}

function checkPost()
{
	// Name is required
	if (!document.getElementById('name').value) {
		alert('A name is required');
		document.getElementById('name').focus();
		return false;
	}

	if(document.getElementById('title')) {

		// Category is required if thread starter
		me = document.forms["post"];
		count = 0;
		for(i=0;i<me.elements.length;i++) {
			if(me.elements[i].name.indexOf('categories') == 0 &&
				me.elements[i].checked) {
				count++;
			}
		}
		if(count<1) {
			alert('At least one category is required');
			return false;
		}
	}

	// If verification enabled, it is required
	if(me = document.getElementById("verify")) {
		if(!me.value) {
			alert("Please enter a verification as shown");
			me.focus();
			return false;
		}
	}

	// Really should enter a tripcode
	if(!document.getElementById("tripcode").value) {
		return confirm("Are you sure you want to submit without a tripcode? You won't be able to delete your comment without one.");
	}

	return true;
	
}

function collapse(id)
{
	document.getElementById(id).style.display = 'none';
	document.getElementById("collapser_" + id).style.display = 'none';
	document.getElementById("expander_" + id).style.display = 'inline';
	document.getElementById("filler_" + id).style.display = 'block';
}
function expand(id)
{
	document.getElementById(id).style.display = 'block';
	document.getElementById("collapser_" + id).style.display = 'inline';
	document.getElementById("expander_" + id).style.display = 'none';
	document.getElementById("filler_" + id).style.display = 'none';
}
function all(what)
{
	var msg;
	var date = new Date();
	date.setTime(date.getTime()+(24*60*60*1000));
	cookiestring = "; expires=" + date.toGMTString() + "; path=/";
	deletestring = "; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/";
	if(what == "expand") {
		document.cookie = "expand=1" + deletestring;
	} else {
		document.cookie = "expand=0" + cookiestring;
	}


	cblocks = document.getElementsByTagName("blockquote");
	for(var i = 0; i<cblocks.length;i++) {
		thisid = cblocks[i].id;
		if(thisid.indexOf("cblock") == 0 && document.getElementById("collapser_" + thisid)) {
			if(what == "collapse") {
				collapse(thisid);
			} else if (what == "expand") {
				expand(thisid);
			}
		}
	}
}
function collapseOrExpand()
{
	var expand = true;
	if(what = getCookie("expand")) {
		if(what == "0") {
			expand = false;
		}
	}
	if(expand) {
		all('expand');
	} else {
		all('collapse');
	}
}

function fileCheck() {
	filename = document.forms["post"]["file_upload"].value;
	thread   = document.getElementById("thread");
	another  = document.getElementById("anotherbutton");
	iszip	 = filename.length-filename.indexOf("zip") == 3;

	// If it was a zip: get rid of the another button, if it is around
	// and add the "go in 1 thread" checkbox
	if(iszip) {
		if(another) {
			another.parentNode.parentNode.removeChild(another.parentNode);
		}
		if(thread) {
			thread.style.display = 'inline';
		}
	} else if(thread) {
		thread.style.display = 'none';
	}
	
	// If a file was specified and multiple uploads is enabled, add
	// "Another file" button
	if(!iszip && filename && numUploads<maxUploads && !another) {
		button = document.createElement("INPUT");
		button.setAttribute("type","button");
		button.setAttribute("id","anotherbutton")
		button.setAttribute("onclick","doAnother()");
		button.setAttribute("value","Another File");
		p = document.createElement("P");                                                             p.appendChild(button);
		me = document.getElementById("posting").parentNode;
		me.insertBefore(p,document.getElementById("anotherslot"));
	} else if (!filename && another) {
		another.parentNode.parentNode.removeChild(another.parentNode);
	}
		
}
function hilite(which) {
	if(last_hilited) {
		if(me = document.getElementById(last_hilited)) {
			me.style.background='';
		}
	}
	if(me = document.getElementById("i" + which)) {
		me.style.background = '#ffd';
		last_hilited = "i" + which;
		location.href = "#i" + which;
	} else {
		location.href = which;
	}
}
function hiliter() {
	var parts = location.href.split("#");
	if(me = document.getElementById(parts[1])) {
		me.style.background = '#ffd';
		last_hilited = parts[1];
	}
}

function doAnother() {
    if(numUploads<maxUploads && (table=document.getElementById("posting"))) {
        me=table.parentNode;

        table = document.createElement("TABLE");
        tbody = document.createElement("TBODY");
        tr = document.createElement("TR");
        th = document.createElement("TH");
        td = document.createElement("TD");
        text = document.createTextNode("File:");

        th.appendChild(text);
        tr.appendChild(th);

        input = document.createElement("INPUT");
        input.setAttribute("type","file");
        input.setAttribute("name","file[]");
        td.appendChild(input);
        tr.appendChild(td);
        tbody.appendChild(tr);

        tr = document.createElement("TR");
        th = document.createElement("TH");
        td = document.createElement("TD");
        text = document.createTextNode("Comment:");
        th.appendChild(text);
        tr.appendChild(th);

        input = document.createElement("INPUT");
        input.setAttribute("type","text");
        input.setAttribute("name","comment[]")
        input.setAttribute("size","40")
        td.appendChild(input);
        tr.appendChild(td);
        tbody.appendChild(tr);

        table.appendChild(tbody);
        me.insertBefore(table,document.getElementById("anotherbutton").parentNode);

        numUploads++;
        if(numUploads>=maxUploads) {
            me.removeChild(document.getElementById("anotherbutton").parentNode);
        }
    }
}
