/**
 * @author basti
 */
function showLogin() {
	refInputLoginNick = document.getElementById("inputloginnick");
	refInputLoginPw = document.getElementById("inputloginpw");
	refFormLogin = document.getElementById("formLogin");
	refLinkRegister = document.getElementById("linkRegister");
	
	//hide Register Link
	 //backupRegister = new Element();
	 //backupRegister.replaceNode(refLinkRegister);
	refLinkRegister.parentNode.removeChild(refLinkRegister);
	
	refFormLogin.style.visibility = "visible";
	if ((refInputLoginNick.value.length != 0) && (refInputLoginPw.value.length != 0)) {
		//login fields are filled, although hidden, seems filled by Password Manager, so submit login form
		//do not call form.submit, call asynclogin() directly!
		
		//asyncLogin(refFormLogin);
	}
}

function showRegister() {
	//document.getElementById("linkShowLogin").style.visibility = "hidden";
	document.getElementById("linkRegister").style.visibility = "hidden";
	document.getElementById("formLogin").innerHTML = "not yet implemented :P";	
	document.getElementById("formLogin").style.visibility = "visible";
	document.getElementById("formLogin").style.width = "100%";
}

//pre-prototype of my AJAX funcs
/**
 * XML http Request (sync) to given Url. Returns ResponseText.
 * @author basti
 * @param {String} URI relative or absolute Http URI
 * @param {String} params: key=value&key2
 * @param {String} httpMethod = ("GET", "POST")
 */
function ajaxReturn(URI, params, httpMethod){
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
    }
    else 
        if (window.ActiveXObject) {
            req = new ActiveXObject("Microsoft.XMLHTTP");
        }
    if (req) {
		if (httpMethod == "GET") {
			if (params != "")
				req.open("GET", URI+"?"+params, false);
			else
				req.open("GET", URI, false);
			req.send(null);
			if (req.readyState == READY_STATE_COMPLETE) {
				if (req) {
					return req.responseText;
				}
				else {
					return "Error!";
				}
			}
		} else if (httpMethod == "POST") {
			req.open("POST", URI, false);
			//Send the proper header information along with the request
			req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			req.setRequestHeader("Content-length", params.length);
			req.setRequestHeader("Connection", "close");
			req.send(params);
			if (req.readyState == READY_STATE_COMPLETE) {
				if (req) {
					return req.responseText;
				}
				else {
					return "Error!";
				}
			}
		}
	} else {
		return "Error!";
	}
}

/*
function innerload(URI,TargetID) {
	document.getElementById(TargetID).innerHTML = ajaxReturn(URI, "", "GET");
	return false;
}
*/

function asyncLogin(currURL) {
	document.getElementById("submitlogin").disabled = true;
	var loginName = document.getElementById("inputloginnick").value;
	var loginPw = document.getElementById("inputloginpw").value;
	result = ajaxReturn(currURL,"inputloginnick="+loginName+"&inputloginpw="+loginPw, "POST");
	result = eval(result);
	if (result[0] != 0) {
		refUserbar = document.getElementById("userbar");
		refUserbar.innerHTML = 'Hello ' + result[result.length-1] + '. <li><a href="?logout" onclick="return asyncLogout('+ currURL +');" id="linkLogout">Logout</a></li>';
	} else {
		alert("Wrong login credentials. Try it again or remind if you already registered.");
	}
	//document.getElementById("submitlogin").disabled = false; //already replaced!
	return false; //prevent send form, just do it async
}

function asyncLogout(currURL) {
	var result = ajaxReturn(currURL+"inc/login.php","logout", "GET");
	if (result == 1) {
		refUserbar = document.getElementById("userbar");
		tempstr = '<li><a href="#" onclick="return showLogin();" id="linkShowLogin">Login</a></li>';
		tempstr += '<li><a href="#" onclick="return showRegister();" id="linkRegister">Register</a></li>';
		tempstr += '<li><div id="formLogin" style="display:inline-block; visibility:hidden;">';
		tempstr += '	<form method="post" action="'+currURL+'" onsubmit="return asyncLogin(' + currURL + 'inc/login.php?cdata\');">';
		tempstr += '		<input type="text" id="inputloginnick" name="loginnick" onfocus="tmpbg1 = this.style.background; this.style.background=\'white\';"  onblur="if(this.value.length==0) this.style.background=tmpbg1;"></input>';
		tempstr += '		<input type="password" id="inputloginpw" name="loginpw" onfocus="tmpbg2 = this.style.background; this.style.background=\'white\';" onblur="if(this.value.length==0) this.style.background=tmpbg2;"></input>';
		tempstr += '		<input type="submit" name="submitlogin" id="submitlogin" value="ok"></input>';
		tempstr += '	</form>';
		tempstr += '</div></li>';
		refUserbar.innerHTML = tempstr;
		return false;
	} else alert("logout error");
}
