/**
 * Modal de informação de login.
 */
$(document).ready(function(){
	$(window).modal({
		url:'/afiliadas/templates/modal.jsp',
		autoOpen:true
	});
})

 

function checar()
{
	var mail = document.form_login.txt_email ;
	var senha = document.form_login.txt_senha ;
	var error = document.getElementById('erro_login');
	
	mail.onfocus = function () 
	{
		error.innerHTML = '' ;
		error.style.display = 'none' ;
	}
	
	senha.onfocus = function () 
	{
		error.innerHTML = '';
		error.style.display='none';
	}
		
	if ( mail.value == null || mail.value == "" || mail.value.length == '0' )
	{
		error.innerHTML = '<p>Insira um E-mail</p>';
		error.style.display = 'block';
		return false ;
	}
	
	if(!mail.value.match(/^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/))
	{
		error.innerHTML = '<p>E-mail invalido</p>';
		error.style.display = 'block';
		return false ;
	}
	
	if ( senha.value.length == '0' ) 
	{
		error.innerHTML = '<p>Insira sua senha</p>';
		error.style.display = 'block';
		return false ;
	}
	
	login() ;

	return false ;
}


function login()
{
	if ( !Sarissa || !document.getElementById ) return ;

	var oLogin = document.form_login.txt_email ;
	var oPassword = document.form_login.txt_senha ;
	var oErrorMessage = document.getElementById( 'erro_login' ) ;

	var userLogin = oLogin.value ;
	var userPassword = oPassword.value ;

	/* Set up the request */
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open( 'POST', 'login.do', true ) ;

	/* The callback function */
	xmlhttp.onreadystatechange = function() 
	{
		if (xmlhttp.readyState == 4) 
		{
			if (xmlhttp.status == 200) 
			{
				var resultNode = xmlhttp.responseXML.getElementsByTagName('result')[0] ;
				
				var status = resultNode.getAttribute( 'status' ) ;

				var message = xmlhttp.responseXML.getElementsByTagName('message')[0].firstChild.data ;
				

				if ( status == 'SUCCESS' )
				{
					if ( message == 'Atualiza Cadastro' )
						document.form_login.action = 'content.do?a=16&a2=16' ;
					else
						document.form_login.action = 'index.do' ;
				
					document.form_login.submit() ;
				}
				else
				{
					oErrorMessage.innerHTML = message ;
					oErrorMessage.style.display = 'block' ;
				}
			} 
			else 
			{
				oErrorMessage.innerHTML = xmlhttp.status + " ao tentar autenticar usuário." ;
				oErrorMessage.style.display = 'block' ;
			}
		}
	}

	/* Send the POST request */
	xmlhttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ) ;

	xmlhttp.send( 'login=' + escape( userLogin ) + '&password=' + userPassword ) ;
}


function abreEsqueciSenha()
{
	var oBoxLogin = document.getElementById( 'box_login' ) ;
	var cForms = oBoxLogin.getElementsByTagName( 'FORM' ) ;

	oBoxLogin.setAttribute( 'class', 'esq_senha' ) ;
	cForms[0].style.display = 'none' ;
	cForms[1].style.display = 'block' ;
}


function fechaEsqueciSenha()
{
	var oBoxLogin = document.getElementById( 'box_login' ) ;
	var cForms = oBoxLogin.getElementsByTagName( 'FORM' ) ;

	oBoxLogin.removeAttribute( 'class' ) ;
	cForms[1].style.display = 'none' ;
	cForms[0].style.display = 'block' ;
}


function checar_esqueci()
{
	var mail = document.form_senha.txt_email ;
	var error = document.getElementById('erro_senha');
	
	mail.onfocus = function () 
	{
		error.innerHTML = '' ;
		error.style.display = 'none' ;
	}
	
	if ( mail.value == null || mail.value == "" || mail.value.length == '0' )
	{
		error.innerHTML = '<p>Insira um E-mail</p>';
		error.style.display = 'block';
		return false ;
	}
	
	if(!mail.value.match(/^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/))
	{
		error.innerHTML = '<p>E-mail invalido</p>';
		error.style.display = 'block';
		return false ;
	}
	
	reminder() ;

	return false ;
}

function reminder() 
{
	if ( !Sarissa || !document.getElementById ) return ;

	var oLogin = document.form_senha.txt_email ;
	var oErrorMessage = document.getElementById( 'erro_senha' ) ;

	var userLogin = oLogin.value ;
	
	var enableButton = document.getElementById('bt_ok_senha');
	enableButton.setAttribute( 'disabled', 'disabled' );

	/* Set up the request */
	var xmlhttp = new XMLHttpRequest();
	xmlhttp.open( 'POST', 'reminder.do', true ) ;

	/* The callback function */
	xmlhttp.onreadystatechange = function() 
	{
		if (xmlhttp.readyState == 4) 
		{
			if (xmlhttp.status == 200) 
			{
				var resultNode = xmlhttp.responseXML.getElementsByTagName('result')[0] ;
				
				var status = resultNode.getAttribute( 'status' ) ;

				if ( status == 'SUCCESS' )
				{
					oErrorMessage.innerHTML = 'Uma nova senha foi enviada para o seu e-mail.' ;
					oErrorMessage.style.display = 'block' ;
				}
				else
				{
					var message = xmlhttp.responseXML.getElementsByTagName('message')[0].firstChild.data ;

					oErrorMessage.innerHTML = message ;
					oErrorMessage.style.display = 'block' ;
				}				
				
			} 
			else 
			{
				oErrorMessage.innerHTML = xmlhttp.status + " ao regerar senha." ;
				oErrorMessage.style.display = 'block' ;
			}
			enableButton.removeAttribute('disabled');
		}
	}

	/* Send the POST request */
	xmlhttp.setRequestHeader( 'Content-Type', 'application/x-www-form-urlencoded' ) ;

	xmlhttp.send( 'login=' + escape( userLogin ) ) ;
}

