// JavaScript Document
var oFunciones={
	enfocaCampo:function(elm,cadena){
		if(elm.value == cadena){
			elm.value="";
		}
		elm.style.border='1px solid #003298';
		elm.style.background='#efefef';
	},
	desenfocaCampo:function(elm,cadena){
		if(elm.value==""){
			elm.value=cadena;
		}
		elm.style.border='1px solid silver';
		elm.style.background='#fff';
	},
	valida_correo:function(campo){
		//cortamos el valor en trozos para operar con el
		array_campo=campo.split("@");
		tam_campo=array_campo.length;
		array_campo2=campo.split(".");
		tam_campo2=array_campo2.length;
		//aqui extraemos el contenido de la extension
		extension=array_campo2[tam_campo2-1];
		longitud=campo.length;
		espacios=campo.indexOf(" ");
		//fijamos una variable con el formato que queremos que tenga el campo
		var formato=/^[A-Za-z0-9\_\-\.]+@[A-Za-z0-9\-\_\.]+.[A-Za-z]$/;
		//comprobamos con la funcion test que el campo cumple el formato y le damos su resultado (true o false) a una variable
		form_valido=formato.test(campo);
		//Comprobamos que todo es correcto, de no ser asi devuelve falso
			if((tam_campo==2)&&(tam_campo2>=2)&&(longitud>5)&&(espacios==-1)&&((extension=="com")||(extension=="net")||(extension=="org")||(extension=="info")||(extension=="es")||(extension.length==2))&&(form_valido==true))
			{
			return(true);
		}
		return(false);
	},
	
	valida_telefono:function(campo){
		//var tamCampo=campo.length;
		//fijamos una variable con el formato que queremos que tenga el campo
		var formato=/^[0-9]{9}$/;
		//comprobamos con la funcion test que el campo cumple el formato y le damos su resultado (true o false) a una variable
		form_valido=formato.test(campo);
		//Comprobamos que todo es correcto, de no ser asi devuelve falso
			if(form_valido==true)
			{
				return(true);
		}
		return(false);
	},
	
	valida_formu_colaboradores:function(idForm){
		theForm=document.getElementById(idForm);
		
		if (theForm.nombrePartner.value==""){
			alert("First name is required.");
			theForm.nombrePartner.focus();
			return;
		}
		if (theForm.apellidosPartner.value==""){
			alert("Last name is required.");
			theForm.apellidosPartner.focus();
			return;
		}
		if(this.valida_correo(theForm.correoPartner.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correoPartner.focus();
			return;
		}
		if (theForm.empresa.value==""){
			alert("Company name is required.");
			theForm.empresa.focus();
			return;
		}
		/*if (theForm.dni.value==""){
			alert("IFA Number is required.");
			theForm.dni.focus();
			return;
		}*/
		if (theForm.telefonoFijo.value==""){
			alert("Telephone is required.");
			theForm.telefonoFijo.focus();
			return;
		}
		/*if (theForm.clave.value==""){
			alert("Password is required.");
			theForm.clave.focus();
			return;
		}*/
		/*if (theForm.validaCodigo.value==""){
			alert("Validation Code is required.");
			theForm.validaCodigo.focus();
			return;
		}*/
		
		theForm.submit();
	},
	
	valida_peticion_folleto:function(idForm){
		theForm=document.getElementById(idForm);
		
		if (theForm.nombre.value=="First Name"){
			alert("First name is required.");
			theForm.nombre.focus();
			return;
		}
		if (theForm.apellidos.value=="Last Name"){
			alert("Last name is required.");
			theForm.apellidos.focus();
			return;
		}
		if(this.valida_correo(theForm.correo.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correo.focus();
			return;
		}
		
		if (theForm.telefono.value=="Your Telephone"){
			alert("Telephone is required.");
			theForm.telefono.focus();
			return;
		}
		
		theForm.submit();
	},
	
	valida_formu_email:function(idForm){
		theForm=document.getElementById(idForm);
		
		if (theForm.nombreContacto.value==""){
			alert("First name is required.");
			theForm.nombreContacto.focus();
			return;
		}
		if (theForm.apellidosContacto.value==""){
			alert("Last name is required.");
			theForm.apellidosContacto.focus();
			return;
		}
	
		if (theForm.telefono.value==""){
			alert("Telephone is required.");
			theForm.telefono.focus();
			return;
		}

	
		if(this.valida_correo(theForm.correoContacto.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correoContacto.focus();
			return;
		}
		
		
		
		theForm.submit();
	},
	valida_formu_contacto:function(idForm){
		theForm=document.getElementById(idForm);
		
		if (theForm.nombreContacto.value==""){
			alert("First name is required.");
			theForm.nombreContacto.focus();
			return;
		}
		if (theForm.apellidosContacto.value==""){
			alert("Last name is required.");
			theForm.apellidosContacto.focus();
			return;
		}
		if(this.valida_correo(theForm.correoContacto.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correoContacto.focus();
			return;
		}
		
		if (theForm.comentarios.value==""){
			alert("Message is required.");
			theForm.comentarios.focus();
			return;
		}
		
		/*if (theForm.validaCodigo.value==""){
			alert("Validation Code is required.");
			theForm.validaCodigo.focus();
			return;
		}*/
		
		theForm.submit();
	},
	valida_formu_login:function(idForm){
		theForm=document.getElementById(idForm);
		
		if (theForm.nombrePartner.value==""){
			alert("Password is required.");
			theForm.nombrePartner.focus();
			return;
		}
		
		if(this.valida_correo(theForm.correoPartner.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correoPartner.focus();
			return;
		}
		
		/*if (theForm.validaCodigo.value==""){
			alert("Validation Code is required.");
			theForm.validaCodigo.focus();
			return;
		}*/
		
		theForm.submit();
	},
	valida_formu_request_data:function(idForm){
		theForm=document.getElementById(idForm);
		
		
		
		if(this.valida_correo(theForm.correoPartnerRequest.value)==false){
			alert("Wrong format for the email address, it must be like: [name + @ + domain + . + ext].");
			theForm.correoPartnerRequest.focus();
			return;
		}
		
		
		theForm.submit();
	},
	controlInsertarCliente:function(oForm){
		if((oForm.nombre.value=="")||(oForm.apellidos.value=="")||(oForm.telefonoUno.value=="")||(oForm.correo.value=="")){
			alert("Fields marked '*' are required.");
			
			return;
		}else{
			if(confirm("¿Confirm that you want to add the client " + oForm.nombre.value + " " + oForm.apellidos.value + "?")){
				this.sendForm(oForm);
			}else{
				return;	
			}
			
		}
	},
	controlModificarCliente:function(oForm){
		if((oForm.nombre.value=="")||(oForm.apellidos.value=="")||(oForm.telefonoUno.value=="")||(oForm.correo.value=="")){
			alert("Fields marked '*' are required.");
			return;
		}else{
			if(confirm("¿Confirm that you want to modify the client " + oForm.nombre.value + " " + oForm.apellidos.value + "?")){
				this.sendForm(oForm);
			}else{
				return;	
			}
		}
	},
	controlEliminarCliente:function(nombre,idCliente,idPartner){
		//alert(cDes);
		if(confirm("¿Confirm that you want to delete the client " + nombre + "?")){
			window.location.href="controlador.php?op=clientes&accion=bor&id=" + idCliente;
		}else{
			return;	
		}
	},
	muestraOculta:function(sElemento){
		var oElm=document.getElementById(sElemento);
		if(oElm.style.display=="none"){
			oElm.style.display="block";
		}else{
			oElm.style.display="none";
		}
	},
	llamaBuscador:function(qUrl,qCrit){
		qCriterio=document.getElementById(qCrit).value;
		if(qCriterio==""){
			coletilla="all";
		}else{
			coletilla=qCriterio;
		}
		//alert(coletilla);
		qUrlConc=qUrl + "&criterio=" + coletilla;
		window.location.href=qUrlConc;
	},
	sendForm:function(oForm){
		oForm.submit();
	}
}
