

$(document).ready(function (){
	$("#boton_aceptar").click(submitLogin);  
	$("#inputsubmitfirstlogin").click(submitFirstLogin);  
	
    $(".controlkeysubmitlogin").keypress(function(e){
        var keyCode= e.keyCode? e.keyCode: e.which;
        if(keyCode==13){
             submitLogin();
        }
    });	
    
    
    
}
);

//Remarca una seccion del menú lateral 
function RemarcaSeccion(id){
	
	
		if(id.length >0){
			$("#"+id).addClass("btactivo");
			
			
		}	
	
}


//Controlamos que un texto no sea mayor que 255
function LimitText255(field,maxlimit) {
	if (field.value.length > maxlimit){ 
		field.value = field.value.substring(0, maxlimit);
		alert('El tamaño máximo del campo es 255');
		field.focus();
		
	}	
}

function submitLogin(){
	
	
	
	$("#avisos1").css({ display: "none" });	
	$("#avisos8").css({ display: "none" });	
	$("#avisos3").css({ display: "none" });	
	$("#avisos4").css({ display: "none" });	
	$("#avisos10").css({ display: "none" });
	$("#avisos6").css({ display: "none" });
	$("#avisos12").css({ display: "none" });
	$("#avisos0").css({ display: "none" });		
	$("#avisos00").css({ display: "none" });		
	$("#avisos000").css({ display: "none" });		
	
	$("#avisos5").css({ display: "none" });
	
	
	var f = document.getElementById('loginInversiones');
	var _user = f.user.value;
	var _password = f.password.value;

	var data = {user: _user, 
					password: _password };
		
		document.getElementById('avisos5').style.display="inline";
   		document.getElementById('boton_aceptar').style.display="none";
   			
					
		$.ajax({
			
	           url: "checkLogin.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           alert('error : ' + err);
	           },
	           success: function(txt) {
	           		
				
		           	 if (txt=="OK"){
			        	self.location = "mis_inversiones.html";
			        	
		           	 }
		           	 else if (txt=="NOUSER"){
		           	 	$("#avisos10").css({ display: "inline" });	
		           	 }
		           	 
		           	 else if (txt=="ERROR_FORMATO_USER"){
		           	 	$("#avisos00").css({ display: "inline" });	
		           	 }
		           	 else if (txt=="ERROR_FORMATO_PASSWD"){
		           	 	$("#avisos0").css({ display: "inline" });	
		           	 }
		           	 
		           	 else if (txt=="INACTIVE"){
		           	 	$("#avisos4").css({ display: "inline" });	
		           	 }
		           	 
		           	 else if (txt=="ERROR"){
		           	 	$("#avisos3").css({ display: "inline" });		
		           	 }
		           	 else if (txt=="FIRST_LOGIN"){
			        	self.location = "first_login.html";
		           	 }
		           	 else if (txt=="0"){
		           	 	$("#avisos000").css({ display: "inline" });		
		           	 
		           	 }else if (txt=="2"){
		           	 	$("#avisos1").css({ display: "inline" });		
		           	 
		           	 }else if (txt=="1"){
		           	 	$("#avisos12").css({ display: "inline" });		
		           	 
		           	 }else{
		           	 	$("#avisos3").css({ display: "inline" });		
		           	 }  	
		           	 
		           	document.getElementById('avisos5').style.display="none";
           			document.getElementById('boton_aceptar').style.display="inline";
	        	}            
	        
	        
			});
			
			
			
		
	return false;
}

function submitRecuperar(){
	$("#avisos1").css({ display: "none" });	//FORMATO INCORRECTO
	$("#avisos2").css({ display: "none" });	//error en la llamada
	$("#avisos3").css({ display: "none" });	//error en el token
	$("#avisos4").css({ display: "none" });	//error en el token
	var f = document.getElementById('formRecuperar');
	var _mail = f.mail.value;
	var _paso = f.paso.value;
	
	if(!cw_es_email(f.mail)){
		$("#avisos1").css({ display: "inline" });
		return false;
	}		
	
	document.getElementById("boton_recuperar").style.display="none";
	document.getElementById("avisos2").style.display="inline";
	
	var data = {mail: _mail, paso: _paso};
					
		$.ajax({
	           url: "checkPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 if (txt=="OK"){
		           	 	f.result.value=0;
			        	f.submit();
		           	 }
		           	 
		           	 document.getElementById("boton_recuperar").style.display="inline-block";
					 document.getElementById("avisos2").style.display="none";
	        	}            
	        
	        
			});
	return false;
}


   

function submitRecuperarOK(){
	$("#avisos10").css({ display: "none" });
	$("#avisos11").css({ display: "none" });
	$("#avisos1").css({ display: "none" });
	$("#avisos2").css({ display: "none" });
	var f = document.getElementById('formRecuperarOK');
	var _respuesta = f.respuesta.value;
	var _paso = f.paso.value;
		
	var data = {respuesta: _respuesta, paso: _paso};
					
		$.ajax({
	           url: "checkPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 if (txt=="OK"){
		           	 	f.result.value=0;
			        	f.submit();
		           	 }
		           	 if (txt=="NOPASS"){
		           	 	$("#avisos10").css({ display: "inline" });
		           	 }
		           	 if (txt=="2"){
		           	 	$("#avisos1").css({ display: "inline" });
		           	 }
		           	 if (txt=="1"){
		           	 	$("#avisos12").css({ display: "inline" });
		           	 }
		           	 
		           	 if (txt=="0"){
		           	 	$("#avisos2").css({ display: "inline" });
		           	 }
		           	 
		           	 if (txt=="INACTIVE"){
		           	 	$("#avisos2").css({ display: "inline" });
		           	 }
		           	 if (txt=="INVALID_CODE" ||txt=="ERROR"){
		           	 	f.result.value=1;
		           	 	f.submit();
		           	 }
		           	 
	        	}            
	        
	        
			});
	return false;
}



function submitRecuperarOK2(){
	
	$("#avisos1").css({ display: "none" });
	$("#avisos2").css({ display: "none" });
	$("#avisos3").css({ display: "none" });
	$("#avisos4").css({ display: "none" });
	$("#avisos10").css({ display: "none" });
	var f = document.getElementById('formRecuperarOK2');
	
	var _password1 = f.password1.value;
	var _password2 = f.password2.value;
	var _paso = f.paso.value;
	
	if (_password1=='' || _password2==''){
		$("#avisos10").css({ display: "inline" });
		return false;
	}
	
	var data = {password1: _password1, password2: _password2, paso: _paso};
					
		$.ajax({
	           url: "checkPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 
	           		
		           	if(txt=="DISTINTAS"){
					 	$("#avisos3").css({ display: "inline" });
					 	return false;
					 }
					
					if (txt=="INVALID_PASSWORD_FORMAT"){
						$("#avisos1").css({ display: "inline" });
						return false;
					}
					
					if(txt=="INVALID_CODE"){
					 	$("#avisos4").css({ display: "inline" });
					 	return false;
					 }
					 
					 if(txt=="ERROR"){
					 	f.resultOK2.value=1;
					 	f.submit();
					 }
					 if(txt=="OK"){
					 	f.resultOK2.value=0;
					 	f.submit();
					 }
	        	}            
			});
		/*
		_llamada="pass2";
		data = {password2: _password2, paso: _paso, llamada: _llamada};
					
		$.ajax({
	           url: "checkPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           		 
		           	 if (txt=="INVALID_PASSWORD_FORMAT"){
			        	$("#avisos2").css({ display: "inline" });
			        	return false;
		           	 }
		           	 if(txt=="ERROR"){
		           	 	$("#avisos4").css({ display: "inline" });
		           	 	return false;
		           	 }
	        	}            
			});
			
		_llamada = "comp";
		data = {password1: _password1, password2: _password2, paso: _paso, llamada: _llamada};
					
		$.ajax({
	           url: "checkPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 if (txt=="OK"){
			        	//f.resLogin.value=txt;
			        	f.submit();
			        	//self.location = "recuperar_password_ok3.html";
		           	 }
		           	 if(txt=="ERROR"){
		           	 	$("#avisos3").css({ display: "inline" });
		           	 	return false;
		           	 }
	        	}            
			});
			*/
	return false;
}



function submitFirstLogin(){
	$("#avisos1").css({ display: "none" });//Campos vacíos
	$("#avisos2").css({ display: "none" });//Error mail
	$("#avisos3").css({ display: "none" });//Error Password1
	$("#avisos4").css({ display: "none" });//Error Password2
	$("#avisos5").css({ display: "none" });//Error password1!=password2
	$("#avisos6").css({ display: "none" });//Error condiciones
	$("#avisos7").css({ display: "none" });//Error llamada
	$("#avisos11").css({ display: "none" });//Error llamada
	$("#avisos12").css({ display: "none" });//Error llamada
	$("#avisosna").css({ display: "none" });//Error llamada
	$("#avisossec").css({ display: "none" });//Error llamada
	//alert("ENTRO");
	var f = document.getElementById('formFirstLogin');
	
	var _usuario = f.usuario.value;
	var _password1 = f.password1.value;
	var _password2 = f.password2.value;
	//var _pregunta = f.pregunta.value;
	var _respuesta = f.respuesta.value;
	
	var _accept_news = 0;
	var _accept_email = 0;
	
	if(f.accept_news.checked) _accept_news=1;
	if(f.accept_email.checked) _accept_email=1;
	
		
	//Comprobar formato email
	/*if(!cw_es_email(f.usuario)){
		$("#avisos2").css({ display: "inline" });
		return false;
	}*/
	
	var _pregunta = document.getElementById('selectivo').options[document.getElementById('selectivo').selectedIndex].value;
	
	
	/*alert("USUARIO="+_usuario);
	alert("PASS="+_password1);
	alert("PASS2="+_password2);
	alert("PREG="+_pregunta);
	alert("RESP="+_respuesta);
	alert("PAPEL="+_papel);
	alert("ABANT="+_abantropia);
	return false;
	*/
	if (!_password1.length>0){
		$("#avisos3").css({ display: "inline" });
		return false;
	}
	if (!_password2.length>0){
		$("#avisos4").css({ display: "inline" });
		return false;
	}
	if (!_pregunta.length>0){
		$("#avisos11").css({ display: "inline" });
		return false;
	}
	
	if (!_respuesta.length>0){
		$("#avisos12").css({ display: "inline" });
		return false;
	}
	
	
	var data = {usuario: _usuario,
				password1: _password1,
				password2: _password2,
				pregunta: _pregunta,
				respuesta: _respuesta,
				accept_news: _accept_news,
				accept_email: _accept_email
				};
					
				 if(!f.papel.checked){
						$("#avisos6").css({ display: "inline" });
						return false;
					}
		$.ajax({
	           url: "checkFirstLogin.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           	//alert("RES="+txt);
		           	 if (txt=="VACIO"){
			        	$("#avisos1").css({ display: "inline" });
		           	 }
		           	 else if (txt=="INVALID_USER"){
		           	 	$("#avisos2").css({ display: "inline" });
		           	 }
		           	 else if (txt=="INVALID_PASSWORD_FORMAT_1"){
		           	 	$("#avisos3").css({ display: "inline" });
		           	 }
		           	 else if (txt=="INVALID_SECURITY"){
		           	 	$("#avisossec").css({ display: "inline" });
		           	 }
		           	 else if (txt=="INVALID_PASSWORD_FORMAT_2"){
		           	 	$("#avisos4").css({ display: "inline" });
		           	 }
		           	 else if (txt=="DISTINTAS"){
		           	 	$("#avisos5").css({ display: "inline" });
		           	 }
		           	 else if (txt=="USER_CODE_NOT_AVAILABLE"){
		           	 	$("#avisosna").css({ display: "inline" });
		           	 }
		           	 
		           	 else if(txt=="OK"){
		           	 	f.error.value=0;
		           	 	//f.submit();
		           	 	self.location = "first_login_ok.html";
		           	 }
		           	 
		           	 else
		           	 	$("#avisos7").css({ display: "inline" });
		           	 
		           	 
	        	}            
			});
	return false;
}


function submitModUser(){
	$("#avisos1").css({ display: "none" });
	$("#avisos2").css({ display: "none" });
	$("#avisos3").css({ display: "none" });
	$("#avisos4").css({ display: "none" });
	$("#avisos5").css({ display: "none" });
	$("#avisos6").css({ display: "none" });
	
	
	var f = document.getElementById('formModUser');
	
	var _usuario = f.usuario.value;
	var _password = f.password.value;
	
	
	/*
	if(!cw_es_email(f.usuario)){
		$("#avisos2").css({ display: "inline" });
		return false;
	}*/
	document.getElementById('boton_usuario').style.display='none';
	document.getElementById('avisos6').style.display='inline';
	
	var data = {usuario: _usuario, password: _password};
					
		$.ajax({
	           url: "check_modUser.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 if (txt=="USER_CODE_NOT_AVAILABLE"){
			        	$("#avisos1").css({ display: "inline" });
		           	 }
		           	 
		           	 if(txt=="INVALID_USER_CODE_FORMAT"){
		           	 	$("#avisos2").css({ display: "inline" });
		           	 }
		           	 
		           	 if (txt=="INVALID_PASSWORD"){
		           	 	$("#avisos3").css({ display: "inline" });
		           	 }
		           	 
		           	 if (txt=="VACIO"){
		           	 	$("#avisos4").css({ display: "inline" });
		           	 }
		           	 
		           	  if (txt=="ERROR" || txt=="INVALID_USER_ID"){
		           	 	$("#avisos5").css({ display: "inline" });
		           	 }
		           	 
		           	 if(txt=="OK"){
		           	 	f.result.value=0;
		           	 	f.submit();
		           	 }
		           	 document.getElementById('boton_usuario').style.display='inline';
					 document.getElementById('avisos6').style.display='none';
		           	 
		           	 
	        	}            
			});
	return false;
}

function submitModPassword(){
	$("#avisos1").css({display:"none"});
	$("#avisos2").css({display:"none"});
	$("#avisos3").css({display:"none"});
	$("#avisos4").css({display:"none"});
	$("#avisos10").css({display:"none"});
	
	var f = document.getElementById('formModPassword');

	var _password1 = f.password1.value;
	var _password2 = f.password2.value;
	var _password3 = f.password3.value;
	
	var data = {password1: _password1, password2: _password2, password3: _password3};
	
	document.getElementById("boton_modPass").style.display="none";
	document.getElementById("avisos5").style.display="inline";
	
	$.ajax({
	           url: "check_modPassword.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {

	           		 if(txt=="MISSING"){
		           	 	$("#avisos10").css({display:"inline"});
		           	 	 document.getElementById("boton_modPass").style.display="inline";
					 document.getElementById("avisos5").style.display="none";
		           	 	return false;
		           	 }
		           	 
		           	 if(txt=="DISTINTAS"){
		           	 	$("#avisos4").css({display:"inline"});
		           	 	 document.getElementById("boton_modPass").style.display="inline";
					 document.getElementById("avisos5").style.display="none";
		           	 	return false;
		           	 }
		           	 
		           	 if(txt=="VACIA1" || txt=="INVALID_PASSWORD"){
		           	 	$("#avisos1").css({display:"inline"});
		           	 	 document.getElementById("boton_modPass").style.display="inline";
					 document.getElementById("avisos5").style.display="none";
		           	 	return false;
		           	 }
		           	 
		           	 if(txt=='VACIA2' || txt=="INVALID_PASSWORD_FORMAT"){
		           	 	$("#avisos2").css({display:"inline"});
		           	 	 document.getElementById("boton_modPass").style.display="inline";
					 document.getElementById("avisos5").style.display="none";
		           	 	return false;
		           	 }
		           	 
		           	 if(txt=='ERROR' || txt=='INVALID_USER_ID'){
		           	 	f.result.value=1;
		           	 	f.submit();
		           	 }
		           
		           	 if(txt=='OK'){
		           	 	f.result.value=0;
		           	 	f.submit();
		           	 }
		           	 
		           	 document.getElementById("boton_modPass").style.display="inline";
					 document.getElementById("avisos5").style.display="none";
		           	 
	        	}            
			});
			return false;
}

function submitModQuestion(){
	$("#avisos1").css({display:"none"});
	$("#avisos2").css({display:"none"});
	$("#avisos3").css({display:"none"});
	$("#avisos5").css({display:"none"});
	$("#avisos11").css({display:"none"});
	
	

	var f = document.getElementById('formModQuestion');
	//var _pregunta = f.pregunta.value;
	var _respuesta = f.respuesta.value;
	var _password = f.password.value;
	
	var _pregunta = document.getElementById('selectivo').options[document.getElementById('selectivo').selectedIndex].value;
	
	if (!_pregunta.length>0){
		$("#avisos11").css({ display: "inline" });
		return false;
	}
	if (!_respuesta.length>0){
		$("#avisos2").css({ display: "inline" });
		return false;
	}
	if (!_password.length>0){
		$("#avisos3").css({ display: "inline" });
		return false;
	}
	
	document.getElementById('avisos5').style.display="inline";
	document.getElementById('boton_question').style.display="none";
	
	var data = {pregunta: _pregunta, respuesta: _respuesta, password: _password}
	$.ajax({
	           url: "check_modQuestion.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           	
		           	 if(txt=="VACIA1"){
		           	 	$("#avisos1").css({display:"inline"});
		           	 }
		           	 
		           	 if(txt=="VACIA2"){
		           	 	$("#avisos2").css({display:"inline"});
		           	 }
		           	 
		           	 if(txt=="INVALID_PASSWORD"){
		           	 	$("#avisos3").css({display:"inline"});
		           	 }
		           	
		           	 if(txt=="OK"){
		           	 	f.result.value=0;
		           	 	f.submit();
		           	 }
		           	 	
		           	 if(txt == "INVALID_USER_ID" || txt == "ERROR")
		           	 	f.submit();
		           	 
		           	 document.getElementById('avisos5').style.display="none";
					 document.getElementById('boton_question').style.display="inline";
		           	 	
	        	}            
			});
			return false;

}

function submitComunicaciones(){
	
	//<!-- action="check_modify_ok.html" -->
	var f = document.getElementById('formComunicaciones');
	var _comunicaciones=0;
	var _papel = 0;
	var _abantropia=0;
	if(f.comunicaciones.checked) _comunicaciones=1;
	if(f.abantropia.checked) _abantropia=1;
	//if(f.papel.checked) _papel=1;
	
	//alert("1="+_comunicaciones+" 2="+_papel+" 3="+_abantropia);
	document.getElementById('boton_com').style.display="none";
	document.getElementById('avisos1').style.display="inline";
	
	var data = {comunicaciones: _comunicaciones, abantropia: _abantropia, papel: _papel}
	$.ajax({
	           url: "setCommunication.php",
	           type: "POST",
	           dataType: "text",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
		           	 	f.result.value=txt;
		           	 	f.submit();	 	
		           	 	document.getElementById('boton_com').style.display="inline-block";
		           	 	document.getElementById('avisos1').style.display="none";
	        	}            
			});
			return false;

}

function closeSession(){
	
	
				$.ajax({
	           url: "closeSession.php",
	           type: "POST",
	           dataType: "text",
	           
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           		           	
		           	self.location = "index.html?exit=1";
		           
	        	}            
	        
	        
			});
	
	return false;
}



function blockSession(){
	var casilla=document.getElementById('papel').checked;

	if(casilla){
		$("#avisos1").css({ display:"inline"});
	}
	else
		$("#avisos1").css({ display:"none"});
	return false;
}

function bloquearSesion(){
	
	
				$.ajax({
	           url: "closeSession.php",
	           type: "POST",
	           dataType: "text",
	           
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(txt) {
	           		           	
		           	self.location = "index.html";
		           
	        	}            
	        
	        
			});
	
	return false;
}

function mostrarCuenta(view,code,account){
	
	
	var data = {vista: view, 
				codigo: code,
				cuenta: account };
					
		$.ajax({
	           url: "changeAccount.php",
	           type: "POST",
	           dataType: "xml",
	           data: data,	           		
	           error: function(req, err, obj) {
	           	alert('error : ' + err);
	           },
	           success: function(xml) {
	           	
	           	    var frame = $("frame",xml).text();
	           		 
	           		//alert(txt);
	           		//alert(account);
	           		for (var i=1;i<=5;i++)
	           			document.getElementById(code+'_tab_'+i).className="";
	           		
	           		document.getElementById(code+'_tab_'+view).className="pestanaactiva";
	           		
	           		var iframe = parent.frames['frame_'+code];

	           		iframe.window.location=frame;
	           		//resize('frame_'+account);
	           		document.getElementById('frame_'+code).height=500;
	           		iframe.window.location.reload();
	           		
	           		if (view=='2' || view=='5'){
	           			var frame2 = $("frame2",xml).text();
	           			var iframe2 = parent.frames['frame2_'+code];

		           		iframe2.window.location=frame2;
		           		//resize('frame_'+account);
		           		document.getElementById('frame2_'+code).height=500;
		           		iframe2.window.location.reload();
	           		}else{
	           			var iframe2 = parent.frames['frame2_'+code];

		           		iframe2.window.location="";
		           		//resize('frame_'+account);
		           		document.getElementById('frame2_'+code).height=0;
		           		iframe2.window.reload();
	           		}
	           		
	           		
		           	
	        	}            
	        
	        
			});
	return false;
}



