function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;
    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || 
				(numstr.charAt(i) == "-") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "-" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
			  (numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
	//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   

   	return isValid;
}  // end IsNum
function esDigito(sChr){
var sCod = sChr.charCodeAt(0);
return ((sCod > 47) && (sCod < 58));
}

function valSep(oTxt){
var bOk = false;
//bOk = bOk || ((oTxt.value.charAt(2) == "-") && (oTxt.value.charAt(5) == "-"));
bOk = bOk || ((oTxt.value.charAt(2) == "/") && (oTxt.value.charAt(5) == "/"));
return bOk;
}

function finMes(oTxt){
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
var nRes = 0;
switch (nMes){
case 1: nRes = 31; break;
case 2: nRes = 29; break;
case 3: nRes = 31; break;
case 4: nRes = 30; break;
case 5: nRes = 31; break;
case 6: nRes = 30; break;
case 7: nRes = 31; break;
case 8: nRes = 31; break;
case 9: nRes = 30; break;
case 10: nRes = 31; break;
case 11: nRes = 30; break;
case 12: nRes = 31; break;
}
return nRes;
}

function valDia(oTxt){
var bOk = false;
var nDia = parseInt(oTxt.value.substr(0, 2), 10);
bOk = bOk || ((nDia >= 1) && (nDia <= finMes(oTxt)));
return bOk;
}

function valMes(oTxt){
var bOk = false;
var nMes = parseInt(oTxt.value.substr(3, 2), 10);
bOk = bOk || ((nMes >= 1) && (nMes <= 12));
return bOk;
}

function valAno(oTxt){
var bOk = true;
var nAno = oTxt.value.substr(6);
bOk = bOk && ((nAno.length == 2) || (nAno.length == 4));
if (bOk){
for (var i = 0; i < nAno.length; i++){
bOk = bOk && esDigito(nAno.charAt(i));
}
}
return bOk;
}

function valFecha(oTxt){
	var bOk = true;
	if (oTxt.value != "")
	{
		bOk = bOk && (valAno(oTxt));
		bOk = bOk && (valMes(oTxt));
		bOk = bOk && (valDia(oTxt));
		bOk = bOk && (valSep(oTxt));
		if (!bOk)
			return false;
		else
			return true;
	}
}



function ValidacionFormParticulares(formulario)
{
	var flag=true;
	var reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	var reg_dni = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Za-z]$/;
	var reg_cif = /^[A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	
	with(formulario)
	{
		if(NOMBRE.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NOMBRE");
			NOMBRE.focus();
		}
		else if (APELLIDOS.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo APELLIDOS");
			APELLIDOS.focus();
		}
		/*else if (NIF_CIF.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NIF/CIF");
			NIF_CIF.focus();
		}
		else if (!(reg_dni.test(NIF_CIF.value)))
		{
			flag=false;
			alert("El DNI debe constar de 8 dígitos y una letra");
			NIF_CIF.focus();
		}*/
		else if (FECHA_NACIMIENTO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo FECHA NACIMIENTO");
			FECHA_NACIMIENTO.focus();
		}
		else if (valFecha(FECHA_NACIMIENTO) == false)
		{
			flag=false;
			alert("La FECHA NACIMIENTO es incorrecta.");
			FECHA_NACIMIENTO.focus();
		}
		else if (DIRECCION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo DIRECCIÓN");
			DIRECCION.focus();
		}
		else if (POBLACION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo POBLACIÓN");
			POBLACION.focus();
		}
		else if (PROVINCIA.value == "")
		{
			flag=false;
			alert("Debe seleccionar una PROVINCIA");
			PROVINCIA.focus();
		}
		else if (CODIGO_POSTAL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CÓDIGO POSTAL");
			CODIGO_POSTAL.focus();
		}
		else if (TELEFONO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo TELÉFONO");
			TELEFONO.focus();
		}
		else if (EMAIL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo EMAIL");
			EMAIL.focus();
		}
		else if (!(reg_mail.test(EMAIL.value)))
		{
			flag=false;
			alert("El formato del EMAIL es incorrecto\nidentificador@tuemail.dominio")
			EMAIL.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA")
			PWD.focus();
		}
		else if (PWD.value != PWD2.value)
		{
			flag=false;
			alert("Debe confirmar su contraseña")
			PWD2.focus();
		}
		else if (ACEPTO.checked == false)
		{
			flag=false;
			alert("Debe aceptar las condiciones de uso de la web")
			ACEPTO.focus();
		}
	}
	return flag;
}

function ValidacionFormActParticulares(formulario)
{
	var flag=true;
	var reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	var reg_dni = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Za-z]$/;
	var reg_cif = /^[A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	
	with(formulario)
	{
		if(NOMBRE.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NOMBRE");
			NOMBRE.focus();
		}
		else if (APELLIDOS.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo APELLIDOS");
			APELLIDOS.focus();
		}
		/*else if (NIF_CIF.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NIF/CIF");
			NIF_CIF.focus();
		}
		else if (!(reg_dni.test(NIF_CIF.value)))
		{
			flag=false;
			alert("El DNI debe constar de 8 dígitos y una letra");
			NIF_CIF.focus();
		}*/
		else if (FECHA_NACIMIENTO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo FECHA NACIMIENTO");
			FECHA_NACIMIENTO.focus();
		}
		else if (valFecha(FECHA_NACIMIENTO) == false)
		{
			flag=false;
			alert("La FECHA NACIMIENTO es incorrecta.");
			FECHA_NACIMIENTO.focus();
		}
		else if (DIRECCION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo DIRECCIÓN");
			DIRECCION.focus();
		}
		else if (POBLACION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo POBLACIÓN");
			POBLACION.focus();
		}
		else if (PROVINCIA.value == "")
		{
			flag=false;
			alert("Debe seleccionar una PROVINCIA");
			PROVINCIA.focus();
		}
		else if (CODIGO_POSTAL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CÓDIGO POSTAL");
			CODIGO_POSTAL.focus();
		}
		else if (TELEFONO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo TELÉFONO");
			TELEFONO.focus();
		}
		else if (EMAIL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo EMAIL");
			EMAIL.focus();
		}
		else if (!(reg_mail.test(EMAIL.value)))
		{
			flag=false;
			alert("El formato del EMAIL es incorrecto\nidentificador@tuemail.dominio");
			EMAIL.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA");
			PWD.focus();
		}
		else if (PWD.value != PWD2.value)
		{
			flag=false;
			alert("Debe confirmar su contraseña");
			PWD2.focus();
		}
		/*else if (!validarNumeroCuentaBancaria(ENTIDAD_BANCARIA,OFICINA_BANCARIA,DIGITO_CONTROL,CUENTA_BANCARIA))
		{
			flag=false;
		}*/
	}
	
	return flag;
}


/*function ValidacionFormEmpresas(formulario)
{
	var flag=true;
	var reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	var reg_dni = /^[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][A-Za-z]$/;
	var reg_cif = /^[A-Za-z][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$/;
	
	with(formulario)
	{
		
		/*if (NIF_CIF.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NIF/CIF")
			NIF_CIF.focus();
		}
		else if (!(reg_dni.test(NIF_CIF.value)))
		{
			flag=false;
			alert("El CIF debe constar de letra + 8 dígitos");
			NIF_CIF.focus();
		}
		 if (EMPRESA.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo EMPRESA")
			EMPRESA.focus();
		}
		else if (DIRECCION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo DIRECCIÓN")
			DIRECCION.focus();
		}
		else if (POBLACION.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo POBLACIÓN")
			POBLACION.focus();
		}
		else if (PROVINCIA.value == "")
		{
			flag=false;
			alert("Debe seleccionar una PROVINCIA")
			PROVINCIA.focus();
		}
		else if (CODIGO_POSTAL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CÓDIGO POSTAL")
			CODIGO_POSTAL.focus();
		}
		else if (TELEFONO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo TELÉFONO")
			TELEFONO.focus();
		}
		else if (EMAIL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo EMAIL")
			EMAIL.focus();
		}
		else if (!(reg_mail.test(EMAIL.value)))
		{
			flag=false;
			alert("El formato del EMAIL es incorrecto\nidentificador@tuemail.dominio")
			EMAIL.focus();
		}
		else if(NOMBRE.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NOMBRE")
			NOMBRE.focus();
		}
		else if (APELLIDOS.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo APELLIDOS")
			APELLIDOS.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA")
			PWD.focus();
		}
		else if (PWD.value != PWD2.value)
		{
			flag=false;
			alert("Debe confirmar su contraseña")
			PWD2.focus();
		}
		else if (ACEPTO.checked == false)
		{
			flag=false;
			alert("Debe aceptar las condiciones de uso de la web")
			ACEPTO.focus();
		}
	}
	return flag;
}
*/




function ValidacionFormAcceso(formulario)
{
	flag=true;
	with(formulario)
	{
		if (USUARIO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo USUARIO")
			USUARIO.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA");
			PWD.focus();
		}
	}
	return flag;
}

function ValidacionFormUsuario(formulario)
{
	flag=true;
	with(formulario)
	{
		if (NOMBRE.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo NOMBRE")
			NOMBRE.focus();
		}
		else if (APELLIDOS.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo APELLIDOS")
			APELLIDOS.focus();
		}
		else if (USUARIO.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo USUARIO")
			USUARIO.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA");
			PWD.focus();
		}
	}
	return flag;
}


function ValidacionFormLogin(formulario)
{
	flag=true;
	var reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	with(formulario)
	{
		if (EMAIL.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo EMAIL")
			EMAIL.focus();
		}
		else if (!(reg_mail.test(EMAIL.value)))
		{
			flag=false;
			alert("El formato del EMAIL es incorrecto\nidentificador@tuemail.dominio")
			EMAIL.focus();
		}
		else if (PWD.value == "")
		{
			flag=false;
			alert("Debe cubrir el campo CONTRASEÑA");
			PWD.focus();
		}
	}
	return flag;
}

function validarFormEmail(formulario)
{
	reg_mail = /^[\.A-Za-z0-9_-]+\@[\.A-Za-z0-9_-]+\.[\.A-Za-z0-9_-]+$/;
	
	with(formulario)
	{
		if (EMAIL.value=="")
		{
			alert("Por favor, rellene el dato de e-mail");
			EMAIL.focus();
			return false;
		}
		else
		{
			if (!(reg_mail.test(EMAIL.value)))
			{
				alert("Correo electrónico incorrecto");
				EMAIL.focus();
				return false;
			}
			else
			  return true;
		}	 				
	}	
}

function validarNumeroCuentaBancaria(Entidad,Oficina,DC,Numero_Cuenta)
{
	if(Entidad.value=="" && Oficina.value=="" && DC.value=="" && Numero_Cuenta.value=="")
	{
		return true;
	}
	else if (Entidad.value == "")
	{
		alert("Número de cuenta incompleto")
		Entidad.focus();
		return false;
	}
	else if(!IsNum(Entidad.value))
	{
		alert("Los datos del número de cuenta deben ser numéricos")
		Entidad.focus();
		return false;
	}
	else if(Entidad.value.length < 4)
	{
		alert("Número de cuenta incompleto")
		Entidad.focus();
		return false;
	}
	else if (Oficina.value == "")
	{
		alert("Número de cuenta incompleto")
		Oficina.focus();
		return false;
	}
	else if(!IsNum(Oficina.value))
	{
		alert("Los datos del número de cuenta deben ser numéricos")
		Oficina.focus();
		return false;
	}
	else if(Oficina.value.length < 4)
	{
		alert("Número de cuenta incompleto")
		Oficina.focus();
		return false;
	}
	else if (DC.value == "")
	{
		alert("Número de cuenta incompleto")
		DC.focus();
		return false;
	}
	else if(!IsNum(DC.value))
	{
		alert("Los datos del número de cuenta deben ser numéricos")
		DC.focus();
		return false;
	}
	else if(DC.value.length < 2)
	{
		alert("Número de cuenta incompleto")
		DC.focus();
		return false;
	}
	else if (Numero_Cuenta.value == "")
	{
		alert("Número de cuenta incompleto")
		Numero_Cuenta.focus();
		return false;
	}
	else if(!IsNum(Numero_Cuenta.value))
	{
		alert("Los datos del número de cuenta deben ser numéricos")
		Numero_Cuenta.focus();
		return false;
	}
	else if(Numero_Cuenta.value.length < 10)
	{
		alert("Número de cuenta incompleto")
		Numero_Cuenta.focus();
		return false;
	}
	else
		return true;
}

