function enterCms(e) {
	if(e.ctrlKey) {
		if(e.keyCode == 90) {
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if (e.stopPropagation) e.stopPropagation();
			showLogin();
		}
	}
}

function submitEnter(myfield,e)
{
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else {
		if (e) {
			keycode = e.which;
		}
	}
	if (keycode == 13) {
		login();
	}
}

function showLogin()
{
	var html = loginHtml();
	document.getElementById("toolbar").innerHTML = html;
	document.getElementById("cmspanel").style.display = "block";
	document.getElementById("loginname").value = "";
	document.getElementById("password").value = "";
	if(!document.getElementById("loginname").focus()) {
		document.getElementById("loginname").focus();
	}
	moveSite();
}

function moveSite()
{
	// Move site content down
	document.getElementById("sitecontent").style.paddingTop = "60px";

	document.getElementById("sitecontent").style.width = (screen.width - 30) + "px";
	document.getElementById("sitecontent").style.height = (parseInt(document.body.offsetHeight) - 60);
}

function login()
{
	obj = document.getElementById("toolbar");

	var url = cms_path + 'app/controller/actions/authorize.php';
	var success	= function(t){ loginComplete(t, obj); }
	var failure	= function(t){ loginFailed(t, obj); }
	
	var loginname = document.getElementById("loginname").value;
	var password = document.getElementById("password").value;
	var language_id = 0;
	if(document.getElementById("language_1").checked) {
		language_id = document.getElementById("language_1").value;
	} else {
		language_id = document.getElementById("language_2").value;
	}

	var pars = 'loginname='+loginname+'&password='+password+'&language_id='+language_id;
	var html = '<div class="cms_loginbar">';
	html += '<div style="float:left; width:175px; height:22px; padding-top:8px;"><span class="cms_formlabel">Logging in...</span></div>';
	html += '</div>';
	
	obj.innerHTML	= html;
	var myAjax = new Ajax.Request(url, {method:'post', postBody:pars, onSuccess:success, onFailure:failure});
}

function loginComplete(t, obj) {
	if(t.responseText == "0") {
		alert("Sorry, the login failed. please try again");
		showLogin();
	} else {
		// create dynamic script adding later on baz..... 
		document.location.href = window.location.search;
	}
}

function loginFailed(t, obj){
	alert("Sorry, the login failed. please try again");
	showlogin();
}

function loginHtml() {
	var html = '<div class="cms_loginbar">';
	html += '<div style="float:left; width:50px;">&nbsp;</div>';
	html += '<div style="float:left; width:50px; height:22px; padding-top:8px;"><span class="cms_formlabel">Login:</span></div>';
	html += '<div style="float:left; width:150px; height:22px; padding-top:4px;"><input type="text" id="loginname" size="25" maxlength="255" class="cms_forminput" onKeyPress="return submitEnter(this, event);"></div>';
	html += '<div style="float:left; width:75px; height:22px; padding-top:8px;"><span class="cms_formlabel">Password:</span></div>';
	html += '<div style="float:left; width:150px; height:22px; padding-top:4px;"><input type="password" id="password" size="25" maxlength="255" class="cms_forminput" onKeyPress="return submitEnter(this, event);"></div>';
	html += '<div style="float:left; width:75px; height:22px; padding-top:8px;"><span class="cms_formlabel">Language:</span></div>';
	html += '<div style="float:left; width:250px; height:22px; padding-top:4px;">nl: <input type="radio" class="cms_forminput" id="language_1" name="language_id" value="1" checked> en: <input type="radio" class="cms_forminput" id="language_2" name="language_id" value="2"></div>';
	html += '<div style="float:left; width:100px; height:24px; padding-top:8px;"><a onclick="login();" class="cms_formbutton" style="cursor:pointer; cursor:hand;">GO</a></div>';
	html += '</div>';
	return html;
}