window.onload = darFoco;

function darFoco(){
	if(document.frmSend.focoParNick != undefined && document.frmSend.focoParNick.value == "true")
		document.frmSend.parNick.focus();
	else
		document.frmSend.parEmail.focus();
}

function esEmail(argvalue) {

  if (argvalue.indexOf(" ") != -1)
    return false;
  else if (argvalue.indexOf("@") == -1)
    return false;
  else if (argvalue.indexOf("@") == 0)
    return false;
  else if (argvalue.indexOf("@") == (argvalue.length-1))
    return false;

  return true;

}

function estaVacio(texto){
	if (texto == null || texto == ""){ 
		return true;
	}
	return false;
}

function validarDatos(theForm){
	
	if(estaVacio(theForm.parEmail.value)){
		alert("Ingrese correo electronico");
		theForm.parEmail.focus();
		return false;		
	}else if(!esEmail(theForm.parEmail.value)){
		alert("Correo electronico no valido");
		theForm.parEmail.focus();
		return false;		
	}/*else if(estaVacio(theForm.parNick.value)){
		alert("Ingrese Alias");
		theForm.parNick.focus();
		return false;		
	}*/else if(estaVacio(theForm.parPass.value)){
		alert("Ingrese contrase\u00F1a");
		theForm.parPass.focus();
		return false;		
	}else if(estaVacio(theForm.parPass2.value)){
		alert("Ingrese contrase\u00F1a por segunda vez");
		theForm.parPass2.focus();
		return false;		
	}else if(theForm.parPass.value != theForm.parPass2.value){
		alert("Las dos contrase\u00F1as ingresadas no coinciden");
		theForm.parPass.value = "";
		theForm.parPass2.value = "";
		theForm.parPass.focus();
		return false;
	}else if(estaVacio(theForm.parNombre.value)){
		alert("Ingrese Nombre");
		theForm.parNombre.focus();
		return false;		
	}else if(estaVacio(theForm.parApellido.value)){
		alert("Ingrese Apellido");
		theForm.parApellido.focus();
		return false;
	}else if(estaVacio(theForm.parSexo.value)){
		alert("Ingrese su sexo");
		theForm.parSexo.focus();
		return false;
	}else if(estaVacio(theForm.parDiaNacimiento.value)){
		alert("Seleccione un d\u00EDa de Nacimiento");
		theForm.parDiaNacimiento.focus();
		return false;
	}else if(estaVacio(theForm.parMesNacimiento.value)){
		alert("Seleccione un mes de Nacimiento");
		theForm.parMesNacimiento.focus();
		return false;
	}else if(estaVacio(theForm.parAnoNacimiento.value)){
		alert("Seleccione un a\u00F1o de Nacimiento");
		theForm.parAnoNacimiento.focus();
		return false;
	}else if(estaVacio(theForm.parTelefono.value)){
		alert("Ingrese un numero de tel\u00E9fono");
		theForm.parTelefono.focus();
		return false;			
	/*}else if(validarNumero(theForm.parTelefono.value)){
		alert("Ingrese un n\u00FAmero de tel\u00E9fono");
		theForm.parTelefono.focus();
		return false;
	}else if(validarNumero(theForm.parTelefono0.value)){
		alert("Ingrese un n\u00FAmero de telefono");
		theForm.parTelefono0.focus();
		return false;
	}else if(validarNumero(theForm.parTelefono1.value)){
		alert("Ingrese un n\u00FAmero de telefono");
		theForm.parTelefono1.focus();
		return false;*/
	}else if(estaVacio(theForm.parTipoDoc.value)){
		alert("Seleccione un tipo de documento");
		theForm.parTipoDoc.focus();
		return false;
	}else if(estaVacio(theForm.parNumeroDoc.value)){
		alert("Ingrese el n\u00FAmero de documento");
		theForm.parNumeroDoc.focus();
		return false;
	}else if(estaVacio(theForm.parPaisResidencia.value)){
		alert("Seleccione su pa\u00EDs de residencia");
		theForm.parPaisResidencia.focus();
		return false;}

	function validarNumero(valor)
	{ 
	     	//Compruebo si es un valor numérico 
	     	if (isNaN(valor)) { 
	           	 //entonces (no es numero) devuelvo el valor cadena vacia 
	     		return false;
	     	}else{ 
	           	 //En caso contrario (Si era un número) devuelvo el valor 
	     		return true;
	     	} 
	}
	
	if(!validaFecha(theForm.parDiaNacimiento.value,theForm.parMesNacimiento.value,theForm.parAnoNacimiento.value)){
		alert("La fecha ingresada debe ser anterior a la fecha actual");
		return false;
	}
	
	theForm.debeActualizar.value = "N";
	
	theForm.parEmail.value = theForm.parEmail.value.toLowerCase();
	return true;
	
}

//Funcion que dado un string con el siguiente formato: "dd/mm/
function convertirAFecha(string) {
	  var date = new Date()
	  mes = parseInt(string.substring(3, 5));
	  date.setMonth(mes - 1); //en javascript los meses van de 0 a 11
	  date.setDate(string.substring(0, 2));
	  date.setYear(string.substring(6, 10));
	  return date;
}

function validaFecha(d,m,a){
    
	var fechaHoy = new Date();
	var fechaIngresada = convertirAFecha(d+"/"+m+"/"+a);
	if (fechaIngresada > fechaHoy){		
		return false;
	}
	
	nRes = 0;
    bisiesto = false;
    
    if(a % 4 == 0){
    	bisiesto = true;	
    }
       
    switch (m){
     case '01': nRes = 31; break;
     case '02': 
     if(bisiesto){
      	nRes = 29;
     }else{
     	nRes = 28;
     }
     break;
     case '03': nRes = 31; break;
     case '04': nRes = 30; break;
     case '05': nRes = 31; break;
     case '06': nRes = 30; break;
     case '07': nRes = 31; break;
     case '08': nRes = 31; break;
     case '09': nRes = 30; break;
     case '10': nRes = 31; break;
     case '11': nRes = 30; break;
     case '12': nRes = 31; break;
    }
    
    if (d > 0 && d <= nRes){
    	return true;
    }else{    
   		alert("La fecha seleccionada no es valida.");
   		document.frmSend.parDiaNacimiento.focus();
   		return false;
   	}
}

function registrar(theForm){
    if(validarDatos(theForm)){
         theForm.submit();
    }
}


