function checkbg(input,img) {
	if (input.value == "") {
		input.style.backgroundImage = "url('/forum/images/"+img+"')";
	} else {
		input.style.backgroundImage = "";
	}
}

function hlbg(el,color) {
	el.style.backgroundColor = color;
}

function forminit(form,postUrl,ackUrl,ackEl) {
	var theform = document.getElementById(form);
	setexpands(theform);
	setlimits(theform);
	theform.onsubmit = function () {
		
		if(button = document.getElementById('submitbtn')) {
			button.style.display = 'none';
		}
		
		var inputs = this.getElementsByTagName("input");
		for(var i=0; i < inputs.length; i++) {
			if (inputs[i].className.match("required") && inputs[i].getAttribute("vis") != "false") {
				if (inputs[i].value.length == 0) {
					alert("Please complete all fields/questions before sumbitting.");
					inputs[i].focus();
					if (inputs[i].type == "file") {
						inputs[i].parentNode.style.backgroundColor = "#e0e0ff";
					}
					restore();
					return false;
				}
			}
		}

		var inputs = this.getElementsByTagName("textarea");
		for (var i=0; i < inputs.length; i++) {
			if (inputs[i].className.match("required") && inputs[i].getAttribute("vis") != "false") {
				if (inputs[i].value.length == 0) {
					alert("Please complete all fields/questions before sumbitting.");
					inputs[i].focus();
					restore();
					return false;
				}
			}
		}

		var inputs = this.getElementsByTagName("select");
		for (var i=0; i < inputs.length; i++) {
			if (inputs[i].className.match("required") && inputs[i].getAttribute("vis") != "false") {
				if (inputs[i].options[inputs[i].selectedIndex].className.match("default") || !inputs[i].selectedIndex) {
					alert("Please complete all fields/questions before sumbitting.");
					inputs[i].focus();
					restore();
					return false;
				}
			}
		}

		var divs = this.getElementsByTagName("div");
		for (var i=0; i < divs.length; i++) {
			if (divs[i].className.match("required") && divs[i].getAttribute("vis") != "false") {
				var inputslength = 0;
				var divinputs = divs[i].getElementsByTagName("textarea");
				for (var j=0; j < divinputs.length; j++) {
					if (divinputs[j].checked) {
						inputslength += divinputs[j].value.length;
					}
				}
				var divinputs = divs[i].getElementsByTagName("input");
				for (var j=0; j < divinputs.length; j++) {
					if (divinputs[j].checked) {
						inputslength += divinputs[j].value.length;
					}
				}
				if (inputslength == 0) {
					alert("Please complete all fields/questions before sumbitting.");
					divinputs[0].focus();
					restore();
					return false;
				}
			}
		}
		
		//iframes for each 'ajax' file upload must, on success, include an object with id 'status' and content === 'uploaded'
		var iframes = this.getElementsByTagName("iframe");  
		for(var i=0; i < iframes.length; i++) {
			if (iframes[i].className.match("required") && iframes[i].getAttribute("vis") != "false") {
				if (iframes[i].contentDocument) {
					if (!iframes[i].contentDocument.getElementById('status') || iframes[i].contentDocument.getElementById('status').innerHTML != 'uploaded') { //real web browsers
						alert ("File upload failed or is still in progress.  Please check your file upload and submit again.");
						restore();
						return false;
					}
				} else {
					if (!iframes[i].Document.getElementById('status') || iframes[i].Document.getElementById('status').innerHTML != 'uploaded') { //IE
						alert ("File upload failed or is still in progress.  Please check your file upload and submit again.");
						restore();
						return false;
					}
				}
			}
		}
		
		//clear hidden fields to only submit those intended
		var divs = this.getElementsByTagName("div");
		for (var i=0; i < divs.length; i++) {
			if (divs[i].style.display == "none") {
				hiddeninputs = divs[i].getElementsByTagName("input");
				for (var j=0; j < hiddeninputs.length; j++) {
					hiddeninputs[j].value = "";
				}
				hiddeninputs = divs[i].getElementsByTagName("textarea");
				for (var j=0; j < hiddeninputs.length; j++) {
					hiddeninputs[j].value = "";
				}
			}
		}
		if(ackUrl != null) {
			ajaxPost(postUrl,this,ackUrl,ackEl);
			return false;
		}
	}
}

function restore() {
	if(button = document.getElementById('submitbtn')) {
		button.style.display = 'inline';
	}
	return false;
}


function limit(){
	phone = "()-+. 0123456789";
	number = "0123456789";
	time = ":0123456789";
	if (this.getAttribute("limit") == "phone") {
		allowed = phone;
	} else if (this.getAttribute("limit") == "number") {
		allowed = number;
	} else if (this.getAttribute("limit") == "time") {
		allowed = time;
	}
	var value = "";
	for (i=0; i < this.value.length; i++) {
		char = this.value.charAt(i);
		if (allowed.indexOf(char,0) != -1) {
			value += char;
		}
	}
	this.value = value;
	if (this.value == "" && this.getAttribute('date')) {
		this.style.backgroundImage = "url('/forum/images/"+this.getAttribute('date')+".png')";
	} else {
		this.style.backgroundImage = "";
	}
}


function setlimits(theform) {
	inputlist = theform.getElementsByTagName("input");
	for (var i=0; i < inputlist.length; i++) {
		if (inputlist[i].getAttribute("limit")) {
			inputlist[i].onkeyup = limit;
		}
	}
}


function highlight() {
	if (this.getAttribute("date")) {
		dateformat = this.getAttribute("date");
		this.style.backgroundImage = "url('/forum/images/"+dateformat+".png')";
	}
	this.style.backgroundColor = "#e0e0ff";
	this.style.backgroundPosition = "right";
	this.style.backgroundRepeat = "no-repeat";
}


function nolight() {
	this.style.backgroundColor = "";
	this.style.backgroundImage = "";
}

function setexpands(theform) {
	inputlist = theform.getElementsByTagName("input");
	for (var i=0; i < inputlist.length; i++) {
		inputlist[i].onfocus = highlight;
		inputlist[i].onblur = nolight;
		if (inputlist[i].getAttribute("expand") == "true") {
			inputlist[i].onchange = expandcall;
			inputlist[i].onkeyup = expandcall;
		}
	}
	inputlist = theform.getElementsByTagName("textarea");
	for (var i=0; i < inputlist.length; i++) {
		inputlist[i].onfocus = highlight;
		inputlist[i].onblur = nolight;
		if (inputlist[i].getAttribute("expand") == "true") {
			inputlist[i].onchange = expandcall;
			inputlist[i].onkeyup = expandcall;
		}
	}
	inputlist = theform.getElementsByTagName("select");
	for (var i=0; i < inputlist.length; i++) {
		inputlist[i].onfocus = highlight;
		inputlist[i].onblur = nolight;
		if (inputlist[i].getAttribute("expand") == "true") {
			inputlist[i].onchange = expandcall;
			inputlist[i].onkeyup = expandcall;
		}
	}
	divlist = theform.getElementsByTagName("div");
	for (var i=0; i < divlist.length; i++) {
		if ((divlist[i].getAttribute("expand") == "true") || (divlist[i].getAttribute("expand") == "flip")) {
			divinputs = divlist[i].getElementsByTagName("input");
			for (var j=0; j < divinputs.length; j++) {
				divinputs[j].onclick = expandset;
			}
			divinputs = divlist[i].getElementsByTagName("textarea");
			for (var j=0; j < divinputs.length; j++) {
				divinputs[j].onclick = expandset;
			}
		}
	}
}


function expandcall() {
	expand(this);
}

function expand(el) {
	if ((el.value.length > 0 && !el.checked) || el.checked ) {
		expandnames = el.getAttribute("expandnames");
		var children = expandnames.split(","); 
		for (var i=0; i < children.length; i++) {
			var kid = document.getElementById(children[i]);
			kid.style.display = 'inline';
			grandkids = kid.getElementsByTagName("input");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "true");
			}
			grandkids = kid.getElementsByTagName("textarea");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "true");
			}
			grandkids = kid.getElementsByTagName("iframe");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "true");
			}
		}
	}
	if (el.value.length == 0 || ((el.type == "radio" || el.type == "checkbox") && !el.checked)) {
		expandnames = el.getAttribute("expandnames");
		var children = expandnames.split(","); 
		for (var i=0; i < children.length; i++) {
			var kid = document.getElementById(children[i]);
			kid.style.display = 'none';
			grandkids = kid.getElementsByTagName("input");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "false");
			}
			grandkids = kid.getElementsByTagName("textarea");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "false");
			}
			grandkids = kid.getElementsByTagName("iframe");
			for (var j=0; j < grandkids.length; j++) {
				grandkids[j].setAttribute("vis", "false");
			}
		}
	}
	if (!document.height) iefooterfix();
}

function expandset() {
	if (this.parentNode.getAttribute("expand") == "flip") {
		choices = this.parentNode.getElementsByTagName("input");
		if (this.checked) {
			swapval = false;
		} else {
			swapval = true;
		}
		for (i=0; i < choices.length; i++) {
			if (choices[i] != this) {
				choices[i].checked = swapval;
			}
		}
	}
	setinputs = this.parentNode.getElementsByTagName("input");
	for (i=0; i < setinputs.length; i++) {
		if (setinputs[i].getAttribute("expandnames")) {
			expand(setinputs[i]);
		}
	}
	setinputs = this.parentNode.getElementsByTagName("textarea");
	for (i=0; i < setinputs.length; i++) {
		if (setinputs[i].getAttribute("expandnames")) {
			expand(setinputs[i]);
		}
	}
}


function ajaxAppend(loc,next,docEl,rowName) {
	var nextRowDiv = rowName + next;
	if (document.getElementById(nextRowDiv) && document.getElementById(nextRowDiv) != null) {
		return false;
	}
	try {
		xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch (e) {}
	targetEl = document.getElementById(docEl);
	xmlhttp.onreadystatechange = function () {
		if ((xmlhttp.readyState == 4) && (xmlhttp.status == 200)) {
			var newlines = document.createElement('div');
			newlines.innerHTML = xmlhttp.responseText;
			targetEl.appendChild(newlines);
			if (!document.height) iefooterfix();
		}
	}
	var dest = loc + "?q=" + next;
	xmlhttp.open("GET", dest);
	xmlhttp.send(null);
	iefooterfix();
}


function upload(elem,userid,large) {
	if (!large || large == null) {
		large = "";
	}
	filefield = elem.name;
	var parent = elem.parentNode;
	var parentname = parent.tagName;
	while( parentname != "FORM" && parentname != "BODY" ) {
		var parent = parent.parentNode;
		var parentname = parent.tagName;
	}
	if (parentname == "BODY") {
		return false;
	}
	form = document.getElementById(parent.id);
	form.target = filefield+'iframe';
	form.setAttribute('enctype',"multipart/form-data");
	form.setAttribute('action',"upload.php?filefield="+filefield+"&user="+userid+"&large="+large);
	form.setAttribute('method',"post");
	form.submit();
}


function comparepw(pwconfid,pwid) {
	el = document.getElementById(pwconfid);
	pw = document.getElementById(pwid);
	if ( el.value == pw.value ) {
		el.style.color='#000';
	} else {
		el.style.color='#f00';
	}
}



function madePublic(pageid) {
	publicspan = document.getElementById('public'+pageid);
	privatespan = document.getElementById('private'+pageid);
	privatespan.style.display = 'none';
	publicspan.style.display = 'inline';
}

function madePrivate(pageid) {
	publicspan = document.getElementById('public'+pageid);
	privatespan = document.getElementById('private'+pageid);
	publicspan.style.display = 'none';
	privatespan.style.display = 'inline';
}
	
function deletedRow(pageid) {
	deadrow = document.getElementById('pagerow'+pageid);
	deadrow.parentNode.removeChild(deadrow);
}

function deletedCell(pageid,table,col) {
	ajax1('profileline.php?table='+table+'&col='+col+'&pageid='+pageid,'pagecell'+pageid);
}

function editRow(pageid,newcontent,table,row,span) {
	edittr = document.getElementById('pagerow'+pageid);
	edittd = edittr.insertCell(0); 		//doesn't replace anything, so the row now has one extra cell (added to beginning)
	for (i=0; i < span; i++) {			//remove all the old cells
		edittr.deleteCell(-1);
	}
	edittd.colSpan = ""+span;
	edittd.className = 'editline';
	edittd.innerHTML = newcontent;
	forminit('formrow'+pageid,'/forum/post.php','profileline.php?table='+table+'&row='+row+'&pageid='+pageid,'ajaxactiondiv');
	iefooterfix();
}

function editCell(pageid,newcontent,table,col,nodel) {
	edittd = document.getElementById('pagecell'+pageid);
	edittd.innerHTML = newcontent;
	forminit('formrow'+pageid,'/forum/post.php','profileline.php?table='+table+'&col='+col+'&pageid='+pageid+'&nodel='+nodel,'pagecell'+pageid);
	iefooterfix();
}

function editPair(pageid,newcontent,target) {
	editdiv = document.getElementById('pair'+pageid);
	editdiv.innerHTML = newcontent;
	forminit('formrow'+pageid,'/forum/post.php',target,'pair'+pageid);
	iefooterfix();
}


function newRow(pageid,newcontent,table,span) {
	tbodyid = table + "table";
	tbody = document.getElementById(tbodyid);
	newtr = tbody.insertRow(-1);
	newtr.id = 'pagerow' + pageid;
	newtd = newtr.insertCell(0);
	newtd.colSpan = ""+span;
	newtd.className = 'editline';
	newtd.innerHTML = newcontent;
	forminit('formrow'+pageid,'/forum/post.php','profileline.php?table='+table+'&row=NEW&pageid='+pageid,'ajaxactiondiv');
	document.getElementById(table+'table').parentNode.style.display = 'block';
	iefooterfix();
}

function moveHtoRow(pageid) {
	//delete all td elements currently in row
	tr = document.getElementById('pagerow'+pageid);
	oldrowtds = tr.getElementsByTagName('td');
	for (i=0; i < oldrowtds.length; i++) {
		tr.deleteCell(i);
	}
	//for each h6 element in ajaxactiondiv, create a new td at the end of tr, and copy h6.innerHTML to it
	hloc = document.getElementById('ajaxactiondiv');
	hs = hloc.getElementsByTagName('h6');
	for (i=0; i < hs.length; i++) {
		newtd = tr.insertCell(i);
		newtd.innerHTML = hs[i].innerHTML;
		if (i == hs.length-1) newtd.className = 'endopts';
	}
}


function toggleBox(input,table) {
	col = input.name;
	if (input.checked) {
		value = 'on';
	} else {
		value = '';
	}
	ajax1('/forum/post.php?table='+table+'&col='+col+'&value='+value,'ajaxactiondiv');
}

function getBoxValues(formid,table,col) {
	var form = document.getElementById(formid);
	inputs = form.getElementsByTagName('input');
	var params = 'table='+table+'&col='+col+'&';
	for ( var i = 0; i < inputs.length; i++ ) {
		if (inputs[i].type == 'checkbox' && inputs[i].checked) {
			params +=inputs[i].name + '=on&';
		}
	}
	ajax1('/forum/post.php?'+params,'ajaxactiondiv');
}
