function check_form() {
	var str;
	str = document.getElementById('email').value;
	if ((str.indexOf(".") < 3) || (str.indexOf("@") < 0)) {
		alert("You must enter valid email address.");
		return false;
	}
	str = document.getElementById('zip').value;
	if (str.length < 4) {
		alert("You must enter a valid zip code.");
		return false;
	}
	str = document.getElementById('display').value;
	if (str.length < 4) {
		alert("Your display name must be longer than 3 characters.");
		return false;
	}
	var alphaExpression = /^[a-zA-Z 0-9-_]+$/;

	if (!(str.match(alphaExpression))){
		alert("You may only use letters and numbers in your username.");
		return false;
	}	
	str = document.getElementById('password').value;
	if (str.length < 4) {
		alert("Your password must be longer than 3 characters.");
		return false;
	}
	var str2 = document.getElementById('password2').value;	
	if (str != str2) {
		alert("The passwords you entered do not match.");
		return false;
	}
	return true;
}

function forgot_pass() {
	document.getElementById('forgotpassword').value="true";
	document.login_form.submit();
}
function getXMLHTTPRequest() {
	try {
		req = new XMLHttpRequest();
	}
	catch(err1) {
		try {
			req = new ActiveXObject("Mxxml2.XMLHTTP");
		}
		catch(err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (err3) {
				req = false;
			}
		}
	}
	return req;
}
var http = getXMLHTTPRequest();

function updateTwitter() {
	http.open("POST", "ajax/updatetwitter.php", true);
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = useHttpResponseTwitter;
	if (document.getElementById('newfeed')) {
		thestr = "new_feed=" + document.getElementById('newfeed').value;
	}
	else {
		thestr = "new_feed=";
	}
	http.send(thestr);
}	

function vote_up(questionid) {
		http.open("POST", "/ajax/vote.php", true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = useHttpResponseUp;
		thestr = "vote=up&questionid=" + questionid;
		http.send(thestr);
}
function vote_down(questionid) {
		http.open("POST", "/ajax/vote.php", true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = useHttpResponseDown;
		thestr = "vote=down&questionid=" + questionid;
		http.send(thestr);
}
function remove_vote(questionid) {
		http.open("POST", "/ajax/vote.php", true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = useHttpResponseRemove;
		thestr = "vote=remove&questionid=" + questionid;
		http.send(thestr);
}
function useHttpResponseUp() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var questionid = http.responseText;
			questionid = (questionid.replace(/^\W+/,'')).replace(/\W+$/,'');
			var data_ary = questionid.split(";");
			if (data_ary[1].indexOf("Error") < 0) {
				var curTotal = document.getElementById('votetotal' + data_ary[0]).innerHTML;
				var amount;
				if (data_ary[1] == "true") {
					amount = "2";
					document.getElementById('votewarning' + data_ary[0]).innerHTML = "Your vote has been changed";
				}
				else {
					amount = "1";
					document.getElementById('votewarning' + data_ary[0]).innerHTML = "";
				}
				var newTotal = parseInt(curTotal) + parseInt(amount);
				document.getElementById('votetotal' + data_ary[0]).innerHTML = newTotal;
				document.getElementById('voteup' + data_ary[0]).style.backgroundPosition = "left bottom";
				document.getElementById('votedown' + data_ary[0]).style.backgroundPosition = "left top";
				document.getElementById('removevote' + data_ary[0]).innerHTML = "Undo Vote";
			}
			else {
				document.getElementById('votewarning' + data_ary[0]).innerHTML = data_ary[1];
			}
		}
	}
}
function useHttpResponseDown() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var questionid = http.responseText;
			questionid = (questionid.replace(/^\W+/,'')).replace(/\W+$/,'');
			var data_ary = questionid.split(";");
			if (data_ary[1].indexOf("Error") < 0) {
				var curTotal = document.getElementById('votetotal' + data_ary[0]).innerHTML;
				var amount;
				if (data_ary[1] == "true") {
					amount = "2";
					document.getElementById('votewarning' + data_ary[0]).innerHTML = "Your vote has been changed";
				}
				else {
					amount = "1";
					document.getElementById('votewarning' + data_ary[0]).innerHTML = "";
				}
				var newTotal = parseInt(curTotal) - parseInt(amount);
				document.getElementById('votetotal' + data_ary[0]).innerHTML = newTotal;
				document.getElementById('votedown' + data_ary[0]).style.backgroundPosition = "left bottom";
				document.getElementById('voteup' + data_ary[0]).style.backgroundPosition = "left top";
				document.getElementById('removevote' + data_ary[0]).innerHTML = "Undo Vote";
			}
			else {
				document.getElementById('votewarning' + data_ary[0]).innerHTML = data_ary[1];
			}
		}
	}
}
function useHttpResponseRemove() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var questionid = http.responseText;
			questionid = (questionid.replace(/^\W+/,'')).replace(/\W+$/,'');
			var data_ary = questionid.split(";");
			if (data_ary[1].indexOf("Error") < 0) {
				var curTotal = document.getElementById('votetotal' + data_ary[0]).innerHTML;
				document.getElementById('votewarning' + data_ary[0]).innerHTML = "Your vote has been removed";
				var newTotal;
				if (data_ary[1] == "add") {
					newTotal = parseInt(curTotal) + 1;
				}
				else {
					newTotal = parseInt(curTotal) - 1;
				}
				document.getElementById('votetotal' + data_ary[0]).innerHTML = newTotal;
				document.getElementById('voteup' + data_ary[0]).style.backgroundPosition = "left top";
				document.getElementById('votedown' + data_ary[0]).style.backgroundPosition = "left top";
				document.getElementById('removevote' + data_ary[0]).innerHTML = "";
			}
			else {
				document.getElementById('votewarning' + data_ary[0]).innerHTML = data_ary[1];
			}
		}
	}
}

function useHttpResponseTwitter() {
	if (http.readyState == 4) {
		if (http.status == 200) {
			var twitterText = http.responseText;
			if (twitterText) {
				document.getElementById('twitter_text').innerHTML = twitterText;
			}
		}
	}
}
