var toggleModal = function(backgroundColour, options) {
    // modal view for the whole screen
    // ver 2.02 17/10/2008 03:42:06
    if ($("myModal")) {
        $("myModal").dispose();
        return false;
    }

    var options = $merge({
        zIndex: 10000000,
        opacity: .85,
        events: $empty()
    }, options);

    if (!$type(backgroundColour) && !$("modal"))
        return false;

    return new Element("div", {
        id: "myModal",
        styles: {
            position: "absolute",
            top: 0,
            left: 0,
            width: window.getScrollWidth(),
            height: window.getScrollHeight(),
            background: backgroundColour,
            "z-index": options.zIndex
        },
        opacity: options.opacity,
        events: options.events
    }).inject(document.body);
} // end toggleModal

/*
// example usage
toggleModal("#fff"); // create a white modal, default options.
// ... do stuff
toggleModal(); // close it.

// something more fancy...
toggleModal("#555", {
    zIndex: 1000,
    events: {
        click: function() {
            $("myModal").fade(0); // want to gently fade it out
            // do other stuff/cleanup here
            (function() { toggleModal(); }).delay(500); // removes the layer itself after fade done
        }
    }
});

// the function also returns the object so you can tweak it from the outside:
toggleModal("#000000").adopt(new Element("div", {"class": "myClass", text: "Hi from modal land!"})).addClass("modals");
*/

function doLogin(){
	var u = $('user').value;
	var p = $('pass').value;

	if(u.length > 4 && p.length != ''){
		var request = new Request.JSON({
			url: 'private/login.php',
			method: 'post',
			data: {user: u, pass: p},
			onComplete: function(jsonObj) {
				if(jsonObj.success){
					document.location = jsonObj.data.url;
				}
				else {
					showModal(jsonObj.data.errorMsg);
				}
			}
		}).send();
	}
	else {
		var msg = 'Insira o <strong>nome de utilizador</strong> e a <strong>password</strong>!<br>';
		showModal(msg);
	}
}

function showModalBox(msg){
	var closeStr = '&nbsp;<br><a href="javascript:closeModal();">fechar</a>';
	
	msg = msg+'<br>'+closeStr;
	var elem = new Element("div", {"class": "loginInfo", "html": msg});
	var m = toggleModal("#000000");
	m.adopt(elem);
	m.addClass("modals");
	//toggleModal("#000000").adopt(new Element("div", {"class": "loginInfo", "html": msg})).addClass("modals");
}

function closeModal(){
    $("myModal").fade(0); // want to gently fade it out
    // do other stuff/cleanup here
    (function() { toggleModal(); }).delay(500); // removes the layer itself after fade done
}
