// Variables
var capaError = ".capaError"; //la capa de los errores
var textoError = ".textoError";
/*Values Contenidos*/
var nombre = "#nombre";
var textNombre = "#textNombre";
var apellidos = "#apellidos";
var textApellidos ="#textApellidos";
var email = "#email";
var textEmail = "#textEmail";
var empresa ="#empresa";
var textEmpresa = "#textEmpresa";
var telefono ="#telefono";
var textTelefono = "#textTelefono";
var cp ="#cp";
var textCP = "#textCP";
var asunto = "#asunto";
var textAsunto = "#textAsunto";
var localidad = "#localidad";
var textLocalidad = "#textLocalidad";
var direccion = "#direccion";
var textDireccion = "#textDireccion";
var contacta_fo = ".contacta_fo";
var botonEnviar = "#botonContacta";

//CARGA DEL DOCUMENTO
$(document).ready(function (){	

	//	$(capaError).hide();
	
	/*Llamada form contenidos*/
	$(contacta_fo).submit(function (){
		if(presionaBotonSubmitContacta())
		{	
			return true;
		}
		else{
			return false;	
			}
	});	
	
});	

//FUNCIONES

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

function compTodosContenidos(){
	//limpiarTextoError();	
	var ok = true;
	
	if(!compInputText(nombre,textNombre))
		ok = false;
	if(!compInputText(apellidos,textApellidos))
		ok = false;
	if(!compInputText(empresa,textEmpresa))
		ok = false;
	if(!compInputTelf(telefono,textTelefono))
		ok = false;
	if(!compInputEmail(email,textEmail))
		ok = false;
	if(!compInputText(direccion,textDireccion))
		ok = false;
	if(!compInputText(localidad,textLocalidad))
		ok = false;
	if(!compInputText(asunto,textAsunto))
		ok = false
		

	return ok;
}

/*VALIDACIONES CONTENIDOS*/
function compInputText(input,texto){
	if ($(input).attr("value") == null)
	{		
		//ponerError("Campo Requerido");
		//mostrarCapaError();
		pintarTextoError(texto);
		return false;
	}
	else
	{
		//limpiarTextoError();
		//ocultarCapaError();
		pintarTextoNormal(texto);
		return true;
	}
}

function compInputEmail(input,texto){
	var email = ($(input).val());
	var AtPos = email.indexOf("@")
	var StopPos = email.lastIndexOf(".")
	var ok = true

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

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

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

	if (StopPos - AtPos == 1) {
	ok = false;
	}
		if (ok){
			pintarTextoNormal(texto);
		//	limpiarTextoError();
		//	ocultarCapaError();
		}
		else{
		pintarTextoError(texto);
		//ponerError("E-mail no Válido");
		//mostrarCapaError();
		}
	return ok;
}

function compInputTelf(input,texto){
var telf = ($(input).val());
var stripped = telf.replace(/[\(\)\.\-\ ]/g, '');

	if (isNaN(parseInt(stripped)))
	{
		pintarTextoError(texto);
		//ponerError("Telefono no Válido");
		//mostrarCapaError();
		return false;	
	}
	else{
		pintarTextoNormal(texto);
		//limpiarTextoError();
		//ocultarCapaError();
		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', '#AE0C21');
}
