// Variables

/*Values Contenidos*/

var textNombre = "#textNombre";
var textApellidos = "#textApellidos";
var textEmail = "#textEmail";
var textTelefono = "#textTelefono";
var textDireccion = "#textDireccion";
var textPassword1 = "#textPassword1";
var textPassword2 = "#textPassword2";
var textPais = "#textPais";
var textCP = "#textCP";
var textPoblacion = "#textPoblacion";


var nombre = "#nombre";
var apellidos = "#apellidos";
var email = "#email";
var telefono = "#telefono";
var direccion = "#direccion";
var password1 = "#password1";
var password2 = "#password2";
var pais = "#pais";
var cp = "#cp";
var poblacion = "#poblacion";

var formulario = ".nuevo_user";

//CARGA DEL DOCUMENTO
$(document).ready(function (){
	/*Llamada form contenidos*/
	$(formulario).submit(function (){
		if(presionaBotonSubmitContenidos())
		{	
			return true;
		}
		else{
			return false;	
			}
	});	
		
});	

//FUNCIONES

function presionaBotonSubmitContenidos(){
	
	if(compTodosContenidos())
	{
		return true;
	}
	else
	{
		return false;
	}
}

function compTodosContenidos(){
	var ok = true;
	
	if(!compNombre())
		ok = false;	
	if(!compApellidos())
		ok = false;
	if(!compEmail())
		ok = false;	
	if(!compTelefono())
		ok = false;	
	if(!compDireccion())
		ok = false;	
	if(!compPais())
		ok = false;
	if(!compCP())
		ok = false;
	if(!compPoblacion())
		ok = false;	
	if(!compPassword1())
		ok = false;	
	if(!compPassword2())
		ok = false;	
		
	return ok;
}

/*VALIDACIONES*/
function compNombre(){
	if($(nombre).attr("value") == null){
		pintarTextoError(textNombre);
		return false;
	}
	else{
		pintarTextoNormal(textNombre);
		return true;
		}
}

function compApellidos(){
	if($(apellidos).attr("value") == null){
		pintarTextoError(textApellidos);
		return false;
	}
	else{
		pintarTextoNormal(textApellidos);
		return true;
		}
}

function compEmail(){
var email2 = ($(email).val());
	var AtPos = email2.indexOf("@")
	var StopPos = email2.lastIndexOf(".")
	var ok = true

	if (email2 == "") {
	ok = false;
	}

	if (AtPos == -1 || StopPos == -1) {
	ok = false;
	}

	if (StopPos < AtPos) {
	ok = false;
	}

	if (StopPos - AtPos == 1) {
	ok = false;
	}
	
	if(ok){
		pintarTextoNormal(textEmail);
	}
	else{
		pintarTextoError(textEmail);
	}
		
return ok;
}

function compTelefono(){
	if($(telefono).attr("value") == null){
		pintarTextoError(textTelefono);
		return false;
	}
	else{
		pintarTextoNormal(textTelefono);
		return true;
		}
}

function compDireccion(){
	if($(direccion).attr("value") == null){
		pintarTextoError(textDireccion);
		return false;
	}
	else{
		pintarTextoNormal(textDireccion);
		return true;
		}
}

function compPais(){
	if($(pais).attr("value") == null){
		pintarTextoError(textPais);
		return false;
	}
	else{
		pintarTextoNormal(textPais);
		return true;
		}
}

function compCP(){
	if($(cp).attr("value") == null){
		pintarTextoError(textCP);
		return false;
	}
	else{
		pintarTextoNormal(textCP);
		return true;
		}
}

function compPassword1(){
	if($(password1).attr("value") == null){
		pintarTextoError(textPassword1);
		return false;
	}
	else{
		pintarTextoNormal(textPassword1);
		return true;
		}
}

function compPassword2(){
	if($(password2).attr("value") == null){
		pintarTextoError(textPassword2);
		return false;
	}
	else{
		pintarTextoNormal(textPassword2);
		return true;
		}
}

function compPoblacion(){
	if($(poblacion).attr("value") == null){
		pintarTextoError(textPoblacion);
		return false;
	}
	else{
		pintarTextoNormal(textPoblacion);
		return true;
		}
}




/****Acciones Báscias****/

function limpiarTextoError(){
$(textoError).empty();	
}

function ponerError(string){
$(textoError).append(string+" <br> ");
}

function ocultarCapaError(){
$(capaError).hide();
}

function mostrarCapaError(){
$(capaError).show();
}

function pintarTextoError(text){ 
$(text).css('color', '#FF0000');
}

function pintarTextoNormal(text){
$(text).css('color', '#666666');
}
