function getHTTPObject() {
	var xhr = false;
	if (window.XMLHttpRequest) {
		xhr = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xhr = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				xhr = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				xhr = false;
			}
		}
	}
	return xhr;
}

function grabFile(file) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			displayResponse(request);
		};
		request.open("GET", file, true);
		request.send(null);
	}
}

function displayResponse(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var potd_div = document.getElementById("potd");
			var potd_contents = document.createTextNode(request.responseText);
			while (potd_div.hasChildNodes()) {
				potd_div.removeChild(potd_div.lastChild);
			}
			potd_div.appendChild(potd_contents);
		}
	}
}

function grabHtmlChunk(file, mode, data) {
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			displayHtmlChunk(request);
		};
		request.open(mode, file, true);
		if (mode) {request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); }
		request.send(data);
	}
}

function displayHtmlChunk(request) {
	if (request.readyState == 4) {
		if (request.status == 200 || request.status == 304) {
			var potd_div = document.getElementById("potd");
			potd_div.innerHTML = request.responseText;
		}
	}
}

function submitReg() { 
	// Grab the data from the form by element id:
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var password2 = document.getElementById('password2').value;
	var email = document.getElementById('email').value;
	
	// Validate data is the correct format
	if (username.length < 5) { alert('Username must be at least 5 characters'); return false; }
	if (username.length > 20) { alert('Username must be less than 20 characters'); return false; }
	if (password != password2) { alert('Passwords do not match'); return false; }
	if (password.length < 6) { alert('Passwords must be at least 6 characters'); return false; }
	if (password.length > 20) { alert('Passwords must be less than 20 characters'); return false; }
	if (!(isValidEmail(email))) { alert('Invalid email address'); return false; }
	// Build the data string for the POST submission to PHP
	data = "username="+username+"&password="+password+"&email="+email;		
	grabHtmlChunk("./TedPOTD/process_reg.php","POST",data);
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Checking Information... Please Wait...</strong>";
	
}

function submitAnswer() {
	// If user was logged in, Send answer to PHP script which records it in database
	// Otherwise, PHP will display login form
	var answer = document.getElementById('answer').value;
	var problemID = document.getElementById('problemID').value;
	data = "answer="+answer+"&problemID="+problemID;
	
	var request = getHTTPObject();
	if (request) {
		request.onreadystatechange = function() {
			displayHtmlChunk(request);
		};
		request.open("POST", "./TedPOTD/check_answer.php", true);
		request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		request.send(data);
	}
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Submitting Answer... Please Wait...</strong>";
}

function submitLogin() {
	// See where we're headed next:
	var destination = document.getElementById('destination').value;
		
	// PHP will verify login credentials and record the user's answer
	var answer = document.getElementById('users_answer').value;
	var username = document.getElementById('username').value;
	var password = document.getElementById('password').value;
	var problemID = document.getElementById('problemID').value;
	
	// Build the POST data to submit the user information along with his answer and problemID 
	// (if provided) and destination
	data = "answer="+answer+"&username="+username+"&password="+password+"&problemID="+problemID+
			"&destination="+destination;
	grabHtmlChunk("./TedPOTD/check_login.php","POST",data);
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Logging In... Please Wait...</strong>";
}

function LogOut() {	
	grabHtmlChunk('./TedPOTD/logout.php', "GET", null); 
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Logging Out... Please Wait...</strong>";
	
}

function isValidEmail(str) { return (str.indexOf(".") > 2) && (str.indexOf("@") > 0); }

function ForgotPassword() {
	var username = document.getElementById('username').value;
	var email = document.getElementById('email').value;
	
	// Build the POST data to submit the user information along with his answer and problemID 
	// (if provided) and destination
	data = "username="+username+"&email="+email;
	grabHtmlChunk("./TedPOTD/reset_password.php","POST",data);
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Resetting Your Password... Please Wait...</strong>";
}

function ChangePassword() {
	var username = document.getElementById('username').value;
	var oldpassword = document.getElementById('oldpassword').value;
	var newpassword = document.getElementById('newpassword').value;
	var newpassword2 = document.getElementById('newpassword2').value;
	
	// Validate data is the correct format
	if (newpassword != newpassword2) { alert('Passwords do not match'); return false; }
	if (newpassword.length < 6) { alert('Passwords must be at least 6 characters'); return false; }
	if (newpassword.length > 20) { alert('Passwords must be less than 20 characters'); return false; }
	
	// Build the POST data to submit the user information along with his answer and problemID 
	// (if provided) and destination
	data = "username="+username+"&oldpassword="+oldpassword+"&newpassword="+newpassword;
	grabHtmlChunk("./TedPOTD/change_password.php","POST",data);
	var potd_div = document.getElementById("potd");
	potd_div.innerHTML = "<strong>Resetting Your Password... Please Wait...</strong>";
}