// JavaScript Document
function RegistroCliente(id){
	var ajax=nuevoAjax();
	ajax.open("POST", "gestor/ajax.php", true);
	var param2='id='+id+'&pagajax=paginas/clientes/RegistroLogCliente.php&ms='+new Date().getTime();
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==1){
			
		}
		if (ajax.readyState==4){            	
			//alert('Resultado: '+ajax.responseText);p  	
		}
		return false;            
	}
					
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.setRequestHeader("Content-length", param2.length);
	ajax.setRequestHeader("Connection", "close");
	ajax.send(param2);
	return;
}

function ListarTContactoCliente(tc){
	var ajax=nuevoAjax();
	ajax.open("POST", "gestor/ajax.php", true);
	var param2='tc='+encodeURIComponent(tc)+'&pagajax=paginas/clientes/listar_tcontacto_clientes_web.php&ms='+new Date().getTime();
	
	ajax.onreadystatechange=function(){
		if (ajax.readyState==1){
			//getObj('divaviso').style.display='inline';
		}
		if (ajax.readyState==4){            	
			//alert('Resultado: '+ajax.responseText);  	
			document.getElementById('tipocontacto').innerHTML=ajax.responseText;
		}
		return false;            
	}
					
	ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	ajax.setRequestHeader("Content-length", param2.length);
	ajax.setRequestHeader("Connection", "close");
	ajax.send(param2);
	return;
}

function ListarActividades(tc, act){
	if(act==null) act="";
	if(tc!=-1){
		var ajax=nuevoAjax();
		ajax.open("POST", "gestor/ajax.php", true);
		var param2='contacto='+encodeURIComponent(tc)+'&actividad='+encodeURIComponent(act)+'&pagajax=paginas/clientes/listar_actividades_web.php&ms='+new Date().getTime();
		
		ajax.onreadystatechange=function(){
			if (ajax.readyState==1){
				//getObj('divaviso').style.display='inline';
			}
			if (ajax.readyState==4){            	
				//alert('Resultado: '+ajax.responseText);  	
				document.getElementById('actividades').innerHTML=ajax.responseText;
				document.getElementById('comboactividades').disabled=false;
				
			}
			return false;            
		}
						
		ajax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		ajax.setRequestHeader("Content-length", param2.length);
		ajax.setRequestHeader("Connection", "close");
		ajax.send(param2);
	}else{			
		document.getElementById('actividades').innerHTML="";
		document.getElementById('actividades').disabled=true;	
	}
	return;
}

function ValidarDatosCliente(){
	var r=true;
	var txt="";
	if(document.getElementById('cliente').value==""){
		r=false;
		txt+="Nombre\n";
	}
	if(document.getElementById('nif').value==""){
		r=false;
		txt+="No ha indicado Número de Identificación Fiscal\n";
	}else if(document.getElementById('nif').length<5){
		r=false;
		txt+="Longitud de NIF Incorrecta\n";
	}
	
	if(document.getElementById('direccion').value==""){
		r=false;
		txt+="Dirección\n";
	}
	if(document.getElementById('codigopostal').value==""){
		r=false;
		txt+="Código Postal\n";
	}
	if(document.getElementById('provincia').value==""){
		r=false;
		txt+="Provincia\n";
	}
	if(document.getElementById('poblacion').value==""){
		r=false;
		txt+="Población\n";
	}
	if(document.getElementById('telefono1').value==""){
		r=false;
		txt+="Teléfono\n";
	}else if(document.getElementById('telefono1').value.length<9 ){
		r=false;
		txt+="Formato de Teléfono incorrecto\n";
	}
	if(ValidarEmail(document.getElementById('email'))!=true){
		r=false;
		txt+=ValidarEmail(document.getElementById('email'))+"\n";
	}
	if(document.getElementById('combotipocontacto')==null || document.getElementById('combotipocontacto').value<0){
		r=false;
		txt+="Sector\n";
	}
	if(document.getElementById('comboactividades')==null || document.getElementById('comboactividades').value<0){
		r=false;
		txt+="Actividad\n";
	}
	
	if(!r){
		alert("Debe completar los siguientes campos:\n" + txt);
	}
	return(r);
}

function ValidarEmail(cntrl) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	var Resultado=true;
	if ((cntrl.value==null)||(cntrl.value=='')){
		Resultado = 'No ha Introducido el Correo Electrónico\n';
	}else if (!reg.test(cntrl.value)){
		Resultado = 'No ha introducido un Correo Electrónico válido\n';
	}
	return Resultado;
}
