function enableObj(id) {	
	document.getElementById(id).disabled = false;
}
function disableObj(id) {
	document.getElementById(id).disabled = true;
}
function comuteObj(id) {
	document.getElementById(id).disabled = !document.getElementById(id).disabled;
}

function hideObj(id) {
	document.getElementById(id).style.visibility	= 'hidden';
	document.getElementById(id).style.display		= 'none';
}
function showObj(id) {
	document.getElementById(id).style.visibility	= 'visible';
	document.getElementById(id).style.display		= '';
}

function toggleVis(id) {
	/*
	alert(id);
	if (document.getElementById(id).style.visibility	== 'hidden')
		document.getElementById(id).style.visibility	= 'visible';
	else
		document.getElementById(id).style.visibility	= 'none';
	alert(document.getElementById(id).style.visibility);	
	
	if (document.getElementById(id).style.visibility	== 'hidden')
		document.getElementById(id).style.visibility	= '';
	else
		document.getElementById(id).style.visibility	= 'hidden';
	*/
	if (document.getElementById(id).style.visibility	== 'hidden')
		showObj(id);
	else
		hideObj(id);
}



function associateEvent() {
	document.getElementById("usr").onkeydown = defineEvent
	document.getElementById("pwd").onkeydown = defineEvent
}
 
function defineEvent(e) {
	if (!e)
		var e = window.event
	var code = e.keyCode
	if (code == 13) {
		// document.getElementById("foo2").focus()
		
		enableObj('login_submit');   
		if ( formlogin_check() ) {
			loginUsr(document.getElementById('usr').value , hex_md5(document.getElementById('pwd').value) );
		} 
	}
}

function EnterPressed(e) 
{
	// Code adapted from Jennifer Madden
	// http://jennifermadden.com/162/examples/stringEnterKeyDetector.html

	var characterCode
	if(e && e.which){           // NN4 specific code
		e = e
	characterCode = e.which
	}
	else {
    	e = event
		characterCode = e.keyCode // IE specific code
	}
	// Enter key is 13
	if (characterCode == 13) {
		enableObj('login_submit');   
		if ( formlogin_check() ) {
			loginUsr(document.getElementById('usr').value , hex_md5(document.getElementById('pwd').value) ); 
		}
	}
  	else return false
}

function formlogin_check()
{
	//alert ( document.getElementById('usr').value );
	//alert ( document.getElementById('pwd').value );
	
	if ( document.getElementById('usr').value != '' && document.getElementById('pwd').value != '' ) 
		{enableObj('login_submit');return true;}
	else
		{disableObj('login_submit');return false;}																		
}



function changepwd()
{
	document.getElementById("changepwd_question").style.visibility		='hidden';
	document.getElementById("changepwd_question").style.display			='none';
	document.getElementById('changepwd').style.display					='block';
	document.getElementById('changepwd').style.visibility				='visible'
	
	document.editUsr.submit.disabled									= 'disabled';
	//document.editUsr.submit_exit.disabled								= 'disabled';
}

function checkPasswords(pwd,pwd2)
{
	if ( (pwd != pwd2) || pwd == '')
	{
		document.getElementById('pwd').style.backgroundColor = '#fdcece';
		document.getElementById('pwd2').style.backgroundColor = '#fdcece';
	
		src = "images/delete.png";
 		document.getElementById('password_checked').src		= src;
		document.getElementById('password_checked').title = 'As passwords que inseriu não são iguais!';
		document.getElementById('password_checked').alt   = 'As passwords que inseriu não são iguais!';
		
		return('ko');
	}
	else if ( pwd == pwd2 && (pwd != '' && pwd2 != '') )
	{
		src = "images/accept.png";
 		document.getElementById('password_checked').src = src;
		document.getElementById('password_checked').title = 'Passwords coincidem!';
		document.getElementById('password_checked').alt   = 'Passwords coincidem!';
		
		document.getElementById('pwd').style.backgroundColor = '#ffffff';
		document.getElementById('pwd2').style.backgroundColor = '#ffffff';
		return ('ok');
		
	}	
}

function checkForm()
{
	errorSubmit = '0';
	
	if (document.getElementById('chkusr').value == 'ko')
		errorSubmit = '1';
		
	if ( !checkNome(document.editUsr.nome.value) )
		errorSubmit = '1';
	
	// Checking Passwords!
	//var chkpwd = checkPasswords( document.editUsr.pwd.value , document.editUsr.pwd2.value);
	//if ( chkpwd <= 0 ) 
		//errorSubmit = '1';
	
	//Checking Nome Utilizador
	
	
	/*
	//Checking Data Nascimento	
	if (document.editUsr.datanasc.value == '') {
		document.getElementById('datanasc').style.backgroundColor	= '#FDCECE';
		errorSubmit = '1';
	}
	else
		document.getElementById('datanasc').style.backgroundColor	= '#FFFFFF';	
	*/
	
	// Disabling / Enabling Submit Button
	if(errorSubmit == '1')
		turnOff_submit();
	else
		turnOn_submit();
}

function checkNome(nome)
{
	if (nome == '') {
		document.getElementById('nome').style.backgroundColor	= '#FDCECE';
		return false;
	}
	else {
		document.getElementById('nome').style.backgroundColor	= '#FFFFFF';		
		return true;
	}
}

function checkEmail(email)
{
		//return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (email == '' || !filter.test(email)) {
		document.getElementById('email').style.backgroundColor	= '#FDCECE';
		document.getElementById('submit').disabled					= 'disabled';
		//document.getElementById('submit_exit').disabled				= 'disabled';	
		return false;
	}
	else
	{
		document.getElementById('email').style.backgroundColor	= '#FFFFFF';
		checkForm();
		return true;
	}
}



function checkTelemovel(number)
{
	var MinDigits		= 5;

	if ( number.length > MinDigits && IsNumeric(number))
	{
		document.getElementById('telemovel').style.backgroundColor	= '#FFFFFF';
		checkForm();
		return true;
	}
	else
	{
		document.getElementById('telemovel').style.backgroundColor	= '#FDCECE';
		document.getElementById('submit').disabled					= 'disabled';
		//document.getElementById('submit_exit').disabled				= 'disabled';	
		
		return false;
	}
}

function IsNumeric(sText)
{
	var ValidChars	= "0123456789";
	var Operator	= "+";
	var IsNumber=true;
	var Char;

	for (i = 0; i < sText.length && IsNumber == true; i++) 
   	{ 
		Char = sText.charAt(i); 
		
		if ( i > 0 ) 
		{
			if (ValidChars.indexOf(Char) == -1) 
				IsNumber = false;
		}
		else
		{
			if (ValidChars.indexOf(Char) == -1 && Operator.indexOf(Char) == -1)
				IsNumber = false;
		}
	}
	return IsNumber;
}


function turnOff_submit()
{
	document.editUsr.submit.disabled				= 'disabled';
	//document.editUsr.submit_exit.disabled			= 'disabled';
}

function turnOn_submit()
{
	document.editUsr.submit.disabled				= '';
	//document.editUsr.submit_exit.disabled			= '';
}

function addFav(){
    var url        = "http://www.adesportiva.com";
    var title    = "A Desportiva";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}


var popWindow;
function popWind(caminho ){
	if ( popWindow ){
		popWindow.close();
	}
	popWindow = window.open(caminho, "Morada", "width=450, height=375, scrollbars=no" ); //, 'toolbar,width=150,height=100')
}

var popUpWindow;
function popUpWindow(caminho , width , height ){
	if ( popWindow ){
		popWindow.close();
	}
	popWindow = window.open(caminho, "Morada", "width="+width+" , height="+height+", scrollbars=no" ); //, 'toolbar,width=150,height=100')
}