var xmlHttp;
var runtime;
var allowClick = true;
var postID;

// AJAX STUFF

function writeIt(n, d, c, s, o) {
	xmlHttp=GetXmlHttpObject();
	
	var url="/writer.php";
	var params="name="+encodeURIComponent(n)+"&descrip="+encodeURIComponent(d)+"&category="+c+"&spamtest="+s+"&otherbox="+o;
	xmlHttp.open("POST",url,true);

	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=stateChanged;

	xmlHttp.send(params);
} 

function stateChanged() {
	// processing
	if (xmlHttp.readyState==1) {
		document.formy.subbutt.disabled = true;
		document.getElementById('loading').style.visibility = "visible";
	}
	
	if (xmlHttp.readyState==4) {
		// failed
		if (xmlHttp.responseText.charAt(0) == "~") {
			// hide submit button
			var timecounter = 50;
			for (i = .9; i >= 0; i-=.1) {
				setTimeout("document.getElementById('submit').style.opacity='"+i+"';", timecounter);
				setTimeout("document.getElementById('submit').style.filter='alpha(opacity="+i*100+")';", timecounter);
				timecounter += 50;
			}
			// show error
			setTimeout("document.getElementById('submit').innerHTML = \"<span id='error'>"+xmlHttp.responseText.substring(1)+"</span>\";", timecounter);
			for (i = .1; i <= 1; i+=.1) {
				setTimeout("document.getElementById('submit').style.opacity='"+i+"';", timecounter);
				setTimeout("document.getElementById('submit').style.filter='alpha(opacity="+i*100+")';", timecounter);
				timecounter += 50;
			}
			// make text nice and pretty again for IE
			setTimeout("document.getElementById('submit').style.filter='';", timecounter-50);
		}
		// passed
		else {
			// fade list in
			timecounter = 50;
			for (i = .9; i >= 0; i-=.1) {
				setTimeout("document.getElementById('thelist').style.opacity='"+i+"';", timecounter);
				setTimeout("document.getElementById('thelist').style.filter='alpha(opacity="+i*100+")';", timecounter);
				timecounter += 50;
			}
			setTimeout("document.getElementById('thelist').innerHTML = xmlHttp.responseText;", timecounter);
			for (i = .1; i <= 1; i+=.1) {
				setTimeout("document.getElementById('thelist').style.opacity='"+i+"';", timecounter);
				setTimeout("document.getElementById('thelist').style.filter='alpha(opacity="+i*100+")';", timecounter);
				timecounter += 50;
			}
			// make text nice and pretty again for IE
			setTimeout("document.getElementById('thelist').style.filter='';", timecounter-50);
			
			// array makes it easier to loop through tags
			var tagIDs = ['submit', 'terms', 'spamline', 'category', 'because'];
			timecounter = 50;
			// fade tags
			for (var x in tagIDs) {
				// set opacity for each tag as we loop through
				for (i = .9; i >= 0; i-=.1) {
					setTimeout("document.getElementById('"+tagIDs[x]+"').style.opacity='"+i+"';", timecounter);
					setTimeout("document.getElementById('"+tagIDs[x]+"').style.filter='alpha(opacity="+i*100+")';", timecounter);
					timecounter += 50;
				}
				setTimeout("document.getElementById('"+tagIDs[x]+"').style.display='none';", timecounter);
				// reset counter + 100
				timecounter = (parseInt(x)+2)*50;
			}
		}
	}
}

function agreeIt(x, y) {
	postID = y;
	
	xmlHttp=GetXmlHttpObject();
	
	var url="/agreer.php";
	var params="action="+x+"&id="+postID;
	xmlHttp.open("POST",url,true);

	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");

	xmlHttp.onreadystatechange=agreeStateChanged;

	xmlHttp.send(params);
}

function agreeStateChanged() {
	// processing
	if (xmlHttp.readyState==1) {
	}
	// done
	if (xmlHttp.readyState==4) {
		document.getElementById('agree-'+postID).innerHTML = xmlHttp.responseText;
	}
}
	

// ajax engine
function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

// OTHER STUFF

// count how many characters they have left
function limit(x, y) {
	if (x.value.length >= y) {
		x.value = x.value.substring(0, y);
		document.getElementById(x.name+"_limit").innerHTML = 0;
	}
	else
		document.getElementById(x.name+"_limit").innerHTML = y - x.value.length;
}

// display form
function showForm() {
	if (allowClick == true) {
		// array makes it easier to loop through tags
		var tagIDs = ['because', 'category', 'spamline', 'terms', 'submit'];
		var timecounter = 50;
		for (var x in tagIDs) {
			// set opacity for each tag as we loop through
			setTimeout("document.getElementById('"+tagIDs[x]+"').style.display='block';", timecounter);
			for (i = .2; i <= 1; i+=.1) {
				setTimeout("document.getElementById('"+tagIDs[x]+"').style.opacity='"+i+"';", timecounter);
				setTimeout("document.getElementById('"+tagIDs[x]+"').style.filter='alpha(opacity="+i*100+")';", timecounter);
				timecounter += 50;
			}
			// make text nice and pretty again for IE
			setTimeout("document.getElementById('"+tagIDs[x]+"').style.filter='';", timecounter-50);
			// reset counter + 50
			timecounter = (parseInt(x)+2)*50;
		}
		// also hide yellow tab
		setTimeout("document.getElementById('sticky').style.left = '-60px';", 50);
		setTimeout("document.getElementById('sticky').style.left = '-50px';", 100);
		setTimeout("document.getElementById('sticky').style.left = '-40px';", 150);
		setTimeout("document.getElementById('sticky').style.left = '-30px';", 200);
		setTimeout("document.getElementById('sticky').style.left = '-20px';", 250);
		setTimeout("document.getElementById('sticky').style.left = '-10px';", 300);
		setTimeout("document.getElementById('sticky').style.left = '0';", 350);
		
		allowClick = false;
	}
}

// send form off
function validate() {
		writeIt(document.formy.name.value, document.formy.descrip.value, document.formy.category.value, document.formy.spamtest.value, document.formy.otherbox.value);
}

