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�a");
		theForm.parPass.focus();
		return false;		
	}else if(estaVacio(theForm.parPass2.value)){
		alert("Ingrese contrase�a por segunda vez");
		theForm.parPass2.focus();
		return false;		
	}else if(theForm.parPass.value != theForm.parPass2.value){
		alert("Las dos contrase�as 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.parDiaNacimiento.value)){
		alert("Seleccione un d�a 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�o de Nacimiento");
		theForm.parAnoNacimiento.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�mero de documento");
		theForm.parNumeroDoc.focus();
		return false;
	}else if(estaVacio(theForm.parPaisResidencia.value)){
		alert("Seleccione su pa�s de residencia");
		theForm.parPaisResidencia.focus();
		return false;
	}
	 
	if(!validaFecha(theForm.parDiaNacimiento.value,theForm.parMesNacimiento.value,theForm.parAnoNacimiento.value)){
		return false;
	}
	
	theForm.debeActualizar.value = "N";
	
	theForm.parEmail.value = theForm.parEmail.value.toLowerCase();
	return true;
	
}
function validaFecha(d,m,a){
    
    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();
    }
}

