// Validation of all forms filled

function check_fields(theForm,ignoreThese) {
	var formId = theForm.getAttribute('id'); // Get the form's ID
	var fields = document.getElementById(formId).getElementsByTagName("input"); // Put all the form's inputs into an array
	for (var i=0; i<fields.length; i++){ // Loop through all forms
		var field = fields[i]; // Put this input into a variable
		var ignored = false; // Create "ignored" boolean
		if (ignoreThese) { // If "ignoreThese" exists
			for (var n=0;n<ignoreThese.length;n++) { // Loop through all elements in "ignoreThese"
				if (field==document.getElementById(ignoreThese[n])) {
					ignored = true; // If the field matches an ignored field, set ignored to true and break
					break;
				}
			}
		}
		/*if (field.checked && field.checked!='checked' && !ignored) {
			alert("Please fill in all fields before submitting.");
			return false;
		}*/
		if (field.value == '' && !ignored){
			alert("Please fill in all fields before submitting.");
			return false;
		}
	}
	if (document.getElementById('agreeterms')&& document.getElementById('agreeterms').checked == false) {
		alert("Please agree to the terms and conditions before submitting.");
		return false;
	}
	return true;
}

// Hide comments
function hide_comment(theLink) {
	var theComment = find_comment_contents(theLink);
	for (var i=0; i<theComment.childNodes.length; i++) {
		if (theComment.childNodes[i].style) {
			theComment.childNodes[i].style.display = "none";
		}
	}
	theComment.innerHTML+='<p>This comment is hidden. <a href="javascript:void(0)" onclick="show_comment(this);return false">Show this comment</a>.</p>';
}

// Show comments
function show_comment(theLink) {
	var theComment = find_comment_contents(theLink);
	for (var i=0; i<theComment.childNodes.length; i++) {
		if (theComment.childNodes[i].style) {
			if (theComment.childNodes[i].style.display == "none") {
				theComment.childNodes[i].removeAttribute('style');
			}
		}
	}
	var deleteThisParagraph = theLink.parentNode;
	deleteThisParagraph.parentNode.removeChild(deleteThisParagraph);
}

function find_comment_contents(theLink) {
	var theComment = "theLink";
	var n=0;
	while (n==0) {
		if (eval(theComment).className == "comment_content") {
			n++;
		} else {
			if (eval(theComment+".parentNode")) {
				theComment+=".parentNode";
			} else {
				return false
			}
		}
	}
	return eval(theComment);
}

function redirect(destination) {
	window.location = destination;
}

// "Welcome" Header cell functions

function header_cell(theContent,cookieName) {
	// If a header cell with the same content has already been closed on this page, don't bother building the header cell
	if (cookieName) {
		if (getCookie(cookieName) && getCookie(cookieName) == theContent) return false;
	}
	var normBG = new Image; // Makes sure normal BG is loaded in IE6
	normBG.src = "/images/menu_bg.gif";
	var menu = document.getElementById("menu") // Find menu
	menu.className = 'header_cell_top'; // Change menu class for integration of header cell
	var headerCellHTML = '<div id="header_cell">';
	if (cookieName) {
		headerCellHTML+= '<a class="close" href="javascript:void(0)" onclick="header_cell_close(\''+cookieName+'\')"></a>';
	}
	headerCellHTML+= '<p>'+theContent+'</p><div id="header_cell_bottom"></div></div>';
	document.write(headerCellHTML); // Insert header cell with content
}

function header_cell_close(cookieName) {
	document.getElementById("menu").className = ''; // Erase the menu's class
	document.getElementById("header_cell").style.display = 'none'; // Hid the header cell
	document.getElementById("header_cell_bottom").style.display = 'none';
	// Cookie to keep closed
	var theDate = new Date("January 1, 2050");
	theDate = theDate.toGMTString();
	document.cookie = cookieName + "=" + escape(document.getElementById('header_cell').getElementsByTagName('p').item(0).innerHTML) + ";expires=" + theDate;
}

function getCookie(name) {
	var temp = name + "=";
	var tempLen = temp.length;
	var cookieLen = document.cookie.length;
	var i = 0;
	while (i < cookieLen) {
		var j = i + tempLen;
		if (document.cookie.substring(i,j) == temp) {
			var endstr = document.cookie.indexOf (";",j);
			if (endstr == -1) {
				endstr = document.cookie.length;
			}
			return unescape(document.cookie.substring(j,endstr));
		}
		i = document.cookie.indexOf(" ",i) + 1;
		if (i==0) break;
	}
	return null;
}

// Image Upload Validator
function validate_image(theForm) {
		var formId = theForm.getAttribute('id'); // Get the form's ID
		var fields = document.getElementById(formId).getElementsByTagName("input");

		for (var i=0; i<fields.length; i++){
			if(fields[i].getAttribute('type') == 'file') {
				var imgRef = fields[i].value;
				if (!/(\.(gif|jpg|jpeg|png))$/i.test(imgRef)){
				alert("Please attach an image.");
				return false;
				}
			}
		}
		
		return true;
}

// Full List generator for comics page

function full_list(theLink,artistArr) {
	if (!artistArr || artistArr.constructor.toString().indexOf("Array") == -1) return false;
	var posLeft = findPos(theLink)[2]+5;
	var posTop = findPos(theLink)[3]-5;
	if (document.getElementById('comic_full_list')) full_list_close();
	var listContents = '<div id="comic_full_list" style="top:'+posTop+'px;left:'+posLeft+'px;"><a class="close" href="javascript:void(0)" onclick="full_list_close()"></a><ul>';
	for (var i=0;i<artistArr.length;i++) {
		listContents+= '<li><a href="/user.php?id='+artistArr[i][0]+'">'+artistArr[i][1]+'</a></li>';
	}
	listContents+= '</ul></div>';
	document.getElementsByTagName("body").item(0).innerHTML+=listContents;
}

function full_list_close() {
	var theListCell = document.getElementById('comic_full_list');
	theListCell.parentNode.removeChild(theListCell);
}

function findPos(obj) {
	var objorig = obj;
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
		curright = curleft + objorig.offsetWidth
		curbottom = curtop + objorig.offsetHeight
	}
	return [curleft,curtop,curright,curbottom];
}
