function emailCheck (emailStr) 
{
	var emailPat=/^(.+)@(.+)$/
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	var validChars="\[^\\s" + specialChars + "\]"
	var quotedUser="(\"[^\"]*\")"
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	var atom=validChars + '+'
	var word="(" + atom + "|" + quotedUser + ")"
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
		alert("Email incorrecto")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	if (user.match(userPat)==null) {
		alert("Email incorrecto")
		return false
	}
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
		  for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Email incorrecto")
			return false
			}
		}
		return true
	}

	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		alert("Email incorrecto")
		return false;
	}
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
		domArr[domArr.length-1].length>3) {
		 alert("Email incorrecto")
	   return false
	}

	if (len<2) {
	   var errStr="Email incorrecto"
	   alert(errStr)
	   return false
	}
}

function valida_dni(valor){
	checkOK = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	for (i = 1; i < valor.length; i++) {
		for (j = 0; j < checkOK.length; j++)
		{
			if (valor.charAt(i) == checkOK.charAt(j)){
				break;
			}
		}
		if (j == checkOK.length)
		{
			return false;
		}
	}
	return true;
}


function comprueba_dni_repe(dni)
{  
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}


	xmlHttp.onreadystatechange=function()
	{
		if(xmlHttp.readyState==4)
		{
			document.getElementById("dni_ok").value=xmlHttp.responseText;
			/*
			if((document.getElementById("dni_cif").value.length==9)&&(document.getElementById("dni_ok").value=='kko')){
				document.getElementById("tr_contrasena").style.display='block';
			}else{
				document.getElementById("tr_contrasena").style.display='none';
			}
			*/
		}
	}
	var url="ajax/comprueba_dni_repe.php" ;
	url=url+"?id=" + dni ;//+ "&idc=" + id_centro
	url=url+"&sid="+Math.random();
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);  
}


function validar(){
	if(document.getElementById("nombre_completo").value.length<1){
		alert("Rellene el campo Nombre completo de la empresa");
		document.getElementById("bnombre_completo").focus();
		return false;
	}

	if(document.getElementById("logo").value.length>5){
		if(!valida_fotos(document.getElementById("logo").value)){
			return false;
		}
	}

	if(document.getElementById("dni_cif").value.length<9){
		alert("Rellene el campo DNI/CIF");
		document.getElementById("dni_cif").focus();
		return false;
	}else if(!valida_dni(document.getElementById("dni_cif").value)){
		alert("Error DNI/CIF: Sólo letras y numeros");
		document.getElementById("dni_cif").focus();
		return false;
	}else if(document.getElementById("dni_ok").value!='ok'){//el dni está repetido -> pedimos la contraseña
		/*
		if((document.getElementById("contrasena").value.length<6)&&(document.getElementById("contrasena").value.length<15)){
			alert("Su centro ya está dado de alta. Para actualizar sus datos deberá introduci la contraseña (6-15 caracteres).");
			document.getElementById("dni_ok").focus();
			return false;
		}
		/*
		else{
		*/
			alert("Error DNI/CIF: Su DNI/CIF ya está dado de alta. Para actualizar sus datos deberá ponerse en contacto con FACEP.");
			//document.getElementById("dni_ok").focus();
			return false;
		/*
		}
		*/
	}
	if(document.getElementById("nombre_comercial").value.length<1){
		alert("Rellene el campo Nombre comercial");
		document.getElementById("nombre_comercial").focus();
		return false;
	}
	if(document.getElementById("domicilio").value.length<1){
		alert("Rellene el campo Domicilio");
		document.getElementById("domicilio").focus();
		return false;
	}
	if(document.getElementById("cp").value.length<5){
		alert("Rellene el campo Código Postal");
		document.getElementById("cp").focus();
		return false;
	}
	if(document.getElementById("poblacion").value.length<1){
		alert("Rellene el campo Población");
		document.getElementById("poblacion").focus();
		return false;
	}
	if(document.getElementById("provincia").value=='no'){
		alert("Rellene el campo Provincia");
		document.getElementById("provincia").focus();
		return false;
	}
	if(document.getElementById("telefono").value.length<1){
		alert("Rellene el campo Teléfono");
		document.getElementById("telefono").focus();
		return false;
	}
	/*
	if(document.getElementById("www").value.length<8){
		alert("Rellene el campo Web");
		document.getElementById("www").focus();
		return false;
	}
	*/
	if(document.getElementById("persona_contacto").value.length<1){
		alert("Rellene el campo Nombre de la Persona de contacto");
		document.getElementById("persona_contacto").focus();
		return false;
	}
	if(document.getElementById("cargo").value.length<1){
		alert("Rellene el campo cargo de la persona de contacto");
		document.getElementById("cargo").focus();
		return false;
	}

	if (document.getElementById("email_contacto").value.length < 6) {
		alert("Rellene el campo Email de contacto");
		document.getElementById("email_contacto").focus();
		return (false);
	}else{
	  var resu=emailCheck (document.getElementById("email_contacto").value);
	  if(resu ==false){
		 document.getElementById("email_contacto").focus();
		 return resu;
	  }
	}

	if(document.getElementById('seleccionados').value==0){
		alert("Debe indicarnos su oferta formativa");
		return false;
	}


}





function valida_fotos(foto){
	var str=foto;
	var punto=str.lastIndexOf(".");
	var ext=str.substr(punto+1);
	var ext=ext.toLowerCase();
	if(ext!="jpg" && ext!="jpeg" && ext!="pjpg"){
		alert("El formato del logo debe ser JGP");
		return (false);
	}
	return true;
}

 function despliega_subseccion(num){
	var tr="tr_subsec_" + num;
	var img="img_subsec_" + num;
	if(document.getElementById(tr).style.display=='none'){
		document.getElementById(img).src='imagenes/icono_menos.gif';
		document.getElementById(tr).style.display='block';
	} else {
		document.getElementById(img).src='imagenes/icono_mas.gif';
		document.getElementById(tr).style.display='none'; 
	}

}

function cuenta(campo){
	if(campo.checked==true){
		document.getElementById('seleccionados').value++;
	}else {
		document.getElementById('seleccionados').value--;
	}
}