var ie = false;

if (document.all) {
	ie = true;
}

function showPopup(linkobj, text) {

	var popupobj = document.getElementById('popup');
	if (!popupobj) {
		popupobj = createPopupElement();
	}
	document.getElementById('popup-text').innerHTML = text;
	popupobj.style.top = getAbsoluteOffsetTop(linkobj);
	popupobj.style.left = getAbsoluteOffsetLeft(linkobj)
	popupobj.style.display = 'block';
}

function hidePopup() {
	var popupobj = document.getElementById('popup');
	popupobj.style.display = 'none';
}

function createPopupElement() {
	var elemDiv = document.createElement('div');
	if (typeof(elemDiv.innerHTML) != 'string') { return null; }
	elemDiv.id = 'popup';
	elemDiv.style.zIndex = 0;
	elemDiv.style.position = 'absolute';
	elemDiv.style.display = 'none';
	elemDiv.style.padding = '5px';
	elemDiv.style.width = '220px';
	elemDiv.style.border = '1px solid black';
	elemDiv.style.background = 'white';
	elemDiv.innerHTML = "<table border='0' cellpadding='0' cellspacing='0'><tr><\/tr><tr><td class='pop-text'><div id='popup-text' class='ll-popup-text'><\/div><\/td><\/tr><\/table>";
	document.body.appendChild(elemDiv);
	return elemDiv;
}

function getAbsoluteOffsetTop(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	} else {
		if (obj.y) {
			curtop += obj.y;
		}
	}
	if (ie) {
		curtop += 26;
	} else {
		curtop += 10;
	}
	return curtop + "px";
}

function getAbsoluteOffsetLeft(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	} else {
		if (obj.x) {
			curleft += obj.x;
		}
	}
	if (ie) {
		curleft += 11;
	}
	return curleft + "px";
}

function checkdata() {
	var retval = false;
	if (document.getElementById("accname").value.length == 0) {
		alert("Please supply an email account name.");
	} else if (document.getElementById("password").value.length == 0) {
		alert("Please supply a password.");
	} else if (document.getElementById("confpassword").value.length == 0) {
		alert("Please confirm your password.");
	} else if (document.getElementById("sumanswer").value.length == 0) {
		alert("To prove you are not a bot, please answer the addition problem.");
	} else if (!document.getElementById("accept").checked) {
		alert("Please accept the terms and conditions. Youu can read the terms and conditions by clicking the Terms button.");
	} else if (document.getElementById("confpassword").value != document.getElementById("password").value) {
		alert("Your passwords do not match.");
	} else if (!document.getElementById("accname").value.match(/^[0-9a-zA-Z]+$/)) {
		alert("Your account name may only contain numbers and letter in the range a-z, A-Z and/or 0-9.");
	} else if (!document.getElementById("password").value.match(/^[0-9a-zA-Z]+$/)) {
		alert("Your password may only contain numbers and letter in the range a-z, A-Z and/or 0-9.");
	} else if (document.getElementById("accname").value.length < 5 || document.getElementById("accname").value.length > 20) {
		alert("Your account name must be between 5 and 20 characters long.");
	} else if (document.getElementById("password").value.length < 6) {
		alert("Your password must be at least 6 characters long.");
	} else {
		retval = true;
	}
	return retval;
}

function createacc() {
	if (checkdata()) {
		document.getElementById("createbutton").disabled = true;
		xmlhttpRequest("Create.php?uname=" + document.getElementById("accname").value + "&pword=" + document.getElementById("password").value + "&cans=" + document.getElementById("sumanswer").value);
	}
}

function xmlhttpRequest(strURL) {
	var xmlHttpReq = false;
	var self = this;
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	self.xmlHttpReq.open("GET", strURL, true);
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			var reply = self.xmlHttpReq.responseText;
			if (reply == "0") {
				document.getElementById("createbutton").disabled = false;
				alert("The answer to the addition problem is incorrect.");
			} else if (reply == "1") {
				document.getElementById("createbutton").disabled = false;
				alert("Sorry, the name " + document.getElementById("accname").value + " is already in use. Please use another email account name.");
			} else if (reply == "3") {
				location.href='UserHome.php';
			}
		}
	}
	self.xmlHttpReq.send(null);
}