function urlDecode( value )
{
	var texto = value ;
	texto = texto.replace( /\+/g, " " ) ;
	texto = unescape( texto ) ;

	return texto ;
}

function ajaxLogin()
{
	var errMsg = "" ;

	if ( document.login.userLogin.value == '' || 
		 document.login.userPassword.value == '' )
	{
		errMsg = "Por favor, preencha" ;

		if ( document.login.userLogin.value == '' )
		{
			errMsg += " o seu login" ;
			if ( document.login.userPassword.value == '' )
				errMsg += " e sua senha" ;
		}
		else
		{
			errMsg += " a sua senha" ;
		}
		
		errMsg += "." ;
	}
	
	if ( errMsg == "" )
	{
		if ( !Sarissa || !document.getElementById ) return ;
		
		/* Set up the request */
		var xmlhttp = new XMLHttpRequest() ;
		xmlhttp.open( 'POST', 'login', true ) ;

		/* The callback function */
		xmlhttp.onreadystatechange = function() 
		{
			if ( xmlhttp.readyState == 4 )
			{
				if ( xmlhttp.status == 200 )
				{
					feedbackStatus = xmlhttp.responseXML.getElementsByTagName( 'status' )[0].firstChild.data ;
					
					if ( feedbackStatus == 'OK' )
					{
						userId = xmlhttp.responseXML.getElementsByTagName( 'userId' )[0].firstChild.data ;
						userName = urlDecode( xmlhttp.responseXML.getElementsByTagName( 'userName' )[0].firstChild.data ) ;
						userLogin = urlDecode( xmlhttp.responseXML.getElementsByTagName( 'userLogin' )[0].firstChild.data ) ;
												
						if ( xmlhttp.responseXML.getElementsByTagName( 'userProducerCode' )[0].firstChild )
							userProducerCode = urlDecode( xmlhttp.responseXML.getElementsByTagName( 'userProducerCode' )[0].firstChild.data ) ;
						else
							userProducerCode = '' ;

						if ( xmlhttp.responseXML.getElementsByTagName( 'resumeId' )[0].firstChild )
							resumeId = xmlhttp.responseXML.getElementsByTagName( 'resumeId' )[0].firstChild.data ;
						else
							resumeId = '' ;
						
						if ( xmlhttp.responseXML.getElementsByTagName( 'restrictedAccess1' )[0].firstChild )
							restrictedAccess1 = xmlhttp.responseXML.getElementsByTagName( 'restrictedAccess1' )[0].firstChild.data ;
						else
							restrictedAccess1 = '0' ;
							
		               	setCookie( 'userId', userId, '', '/', '', '' ) ;
		               	setCookie( 'userName', userName, '', '/', '', '' ) ;
		               	setCookie( 'userLogin', userLogin, '', '/', '', '' );
		               	setCookie( 'userProducerCode', userProducerCode, '', '/', '', '' ) ;
		               	setCookie( 'resumeId', resumeId, '', '/', '', '' ) ;
		               	setCookie( 'restrictedAccess1', restrictedAccess1, '', '/', '', '' ) ;
		               	
		               	window.location = 'documentos.jsp';
					}
					else if ( feedbackStatus == 'Invalid' )
					{
						alert( 'Login ou senha incorretos.' ) ;
					}
					else
					{
						alert( 'Erro ao acessar autenticador.' ) ;
					}
				} 
				else 
				{
					alert( 'Erro ao acessar autenticador.' ) ;
				}
			}
		}

		/* Send the POST request */
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
		xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		xmlhttp.setRequestHeader("Pragma", "no-cache");

		var sentData = "userLogin=" + document.login.userLogin.value
					 + "&userPassword=" + document.login.userPassword.value ;

		
		xmlhttp.send( sentData ) ;
	}
	else
	{
		alert( errMsg ) ;
	}
}

function ajaxLogout( oDiv )
{
	
	while ( oDiv.firstChild )
		oDiv.removeChild( oDiv.firstChild ) ;
	
	oDiv.innerHTML = "<form style=\"margin:0\" name=\"login\">\n"
               	   + " Usu&aacute;rio "
               	   + "<input type=\"text\" name=\"userLogin\" class=\"usuario\"/>"
               	   + " Senha "
               	   + "<input class=\"senha\" name=\"userPassword\" type=\"password\"/>"
               	   + "<input type=\"button\" value=\"ok\" class=\"botao\" onClick=\"ajaxLogin( this.parentNode.parentNode )\">\n"
               	   + "</form>\n" ;

	deleteCookie( 'userId', '/', '' ) ;
	deleteCookie( 'userName', '/', '' ) ;
	deleteCookie( 'userLogin', '/', '' ) ;
	deleteCookie( 'userProducerCode', '/', '' ) ;
	deleteCookie( 'resumeId', '/', '' ) ;
	deleteCookie( 'restrictedAccess1', '/', '' ) ;
	
	window.location = 'index.jsp';
	
}


function setCookie( name, value, expires, path, domain, secure ) 
{
	var today = new Date() ;
	today.setTime( today.getTime() ) ;

	if ( expires )
		expires = expires * 1000 * 60 * 60 * 24 ;

	var expires_date = new Date( today.getTime() + (expires) ) ;

	document.cookie = name + "=" + escape( value )
					+ ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) 
					+ ( ( path ) ? ";path=" + path : "" )
					+ ( ( domain ) ? ";domain=" + domain : "" ) 
					+ ( ( secure ) ? ";secure" : "" ) ;
}

function getCookie( name ) 
{
	var start = document.cookie.indexOf( name + "=" ) ;
	var len = start + name.length + 1 ;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
		return null ;

	if ( start == -1 ) return null ;

	var end = document.cookie.indexOf( ";", len ) ;

	if ( end == -1 ) 
		end = document.cookie.length ;

	return unescape( document.cookie.substring( len, end ) ) ;
}


function deleteCookie( name, path, domain ) 
{
	if ( getCookie( name ) ) 
		document.cookie = name + "="
						+ ( ( path ) ? ";path=" + path : "" )
						+ ( ( domain ) ? ";domain=" + domain : "" )
						+ ";expires=Thu, 01-Jan-1970 00:00:01 GMT" ;
}
