function createXMLHttpRequest() {
   try { return new XMLHttpRequest(); } catch(e) {}
   try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
   alert("XMLHttpRequest not supported");
   return null;
}
var xhReq = createXMLHttpRequest();

function doCmd($cmdStr,$handleWith){
   xhReq.open("post", "do.php");
   xhReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
   xhReq.onreadystatechange = $handleWith == null ? returned : $handleWith;
   xhReq.send($cmdStr);
}
function vote(){
	
}

function login(){
	var username = document.getElementById("username").value;
	var password = document.getElementById("password").value;
	doCmd("cmd=login&username="+username+"&password="+password,incLogin);
}
function incLogin(){
	if (xhReq.readyState == 4) {
		if (xhReq.status == 200) {
			var xmldoc = xhReq.responseXML;
			if (getError(xmldoc) != null){
				showLoginError(getError(xmldoc));
			}else{
				window.location = "/new";
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}
function requestPassword(){
	hideLostError();
	var email = document.getElementById("lost_email").value;
	document.getElementById("rbutton").disabled = true;
	doCmd("cmd=lost&email="+email,incLost);
}
function incLost(){
	if (xhReq.readyState == 4) {
		if (xhReq.status == 200) {
			var xmldoc = xhReq.responseXML;
			if (getError(xmldoc) != null){
				showLostError(getError(xmldoc));
				document.getElementById("rbutton").disabled = false;
			}else{
				showLostError('Thank you, please check your email');
			}
		} else {
			//alert('There was a problem with the request.');
		}
	}
}
function resetPassword(){
	hideLostError();
	var pass1 = document.getElementById("newPass01").value;
	var pass2 = document.getElementById("newPass02").value;
	
	var key = document.getElementById("key").value;
	var username = document.getElementById("rusername").value;
	
	if (pass1.length < 5){
		showLostError('New password is too short');
	}else if (pass1 != pass2){
		showLostError("Your passwords don't match");
	}else{
		//Submit;
		doCmd("cmd=reset&username="+username+"&key="+key+"&password="+pass1+"&templogin=true",incReset);
		document.getElementById("resetSub").disabled = true;
	}
}
function incReset(){
	if (xhReq.readyState == 4) {
		if (xhReq.status == 200) {
			var xmldoc = xhReq.responseXML;
			if (getError(xmldoc) != null){
				showLostError(getError(xmldoc));
				document.getElementById("resetSub").disabled = false;
			}else{
				showLostError('Thank you, you can now login');
			}
		} else {
			//alert('There was a problem with the request.');
		}
	}
}
function register(){
	var username = document.getElementById("reg_username").value;
	var password = document.getElementById("reg_password").value;
	var email = document.getElementById("reg_email").value;
	doCmd("cmd=register&username="+username+"&password="+password+"&email="+email,incReg);
}
function getError($doc){
	return $doc.getElementsByTagName('error').length == 0 ? null : $doc.getElementsByTagName('error').item(0).getElementsByTagName('title').item(0).childNodes[0].nodeValue;	
}
function incReg(){
	if (xhReq.readyState == 4) {
		if (xhReq.status == 200) {
			var xmldoc = xhReq.responseXML;
			if (getError(xmldoc) != null){
				alert(getError(xmldoc));
			}else{
				window.location = "submit";
			}
		} else {
			alert('There was a problem with the request.');
		}
	}
}
function returned(){
	
}
function dodelete($id){
	doCmd("cmd=remove&vid_id="+$id,returned);
	removeDiv("cell_"+$id);		
}
//PASSWORD ERROR DIV;
function showLostError($content){
	var ele = document.getElementById('password_error');
	if (ele){
		ele.style.visibility = "visible";
		var txt_ele = document.getElementById('password_div_text');
		if(txt_ele)txt_ele.innerHTML = $content;
	}else{
		
	}
}
function hideLostError(){
	var ele = document.getElementById('password_error');
	if (ele){
		ele.style.visibility = "hidden";
	}else{
		
	}
}
//LOGIN ERROR DIV;
function hideLoginError(){
	var ele = document.getElementById('loginError');
	if (ele){
		ele.style.visibility = "hidden";
	}else{
		
	}
}
function showLoginError($content){
	var ele = document.getElementById('loginError');
	if (ele){
		ele.style.visibility = "visible";
		var txt_ele = document.getElementById('login_div_text');
		if(txt_ele)txt_ele.innerHTML = $content;
	}else{
		
	}
}