String.prototype.trim = function(){
	return this.replace(/(^\s*)|(\s*$)/g,"");
}
function FormatName(sName){
	var formatedArray = new Array();
	// Retira os pontos
	sName = sName.replace(/\./g," ");
	//Retira os espaços iniciais e finais (Trim)
	sName = sName.replace(/(^\s*)|(\s*$)/g,"");
	// Remove espaços duplicados
	sName = sName.replace(/\s+/g," ");
	// Quebra as palavras
	sName = sName.split(" ");
	for(var i = 0; i < sName.length;i++){
		formatedArray[i] = sName[i].toLowerCase();
		//Tem uma letra solta... possivelmente um sobrenome abreviado
		if(formatedArray[i].length == 1){
			formatedArray[i] = formatedArray[i].toUpperCase();
			continue;
		}
		//Exceções
		switch(formatedArray[i]){
			case "da": 
			case "de":
			case "do":
			case "das":
			case "dos":
			case "d'":
			case "d`": 
			case "di":break;
			default:
				formatedArray[i] = (formatedArray[i].substr(0,1)).toUpperCase() + formatedArray[i].substr(1);
				break;
		}
	}
	return formatedArray.join(" ");
}
function cadastroNivel_1(oForm){
	var reGeneral = /[^\w\xC0-\xFC\d\s\-\,\.\\\/\`\']/gi
	var reNumeric = /[^\d\.\,\-]/gi
	var reCEP = /\D/gi
	var reTel = /[^\d\-\(\)\s]/gi
	with(oForm){
		var sPais = pais.value;
		if (cpf.value == '' || (sPais == 'BR' && !checaCPF(cpf.value)) ||  isNaN(parseInt(cpf.value)) ){
			if(sPais == 'BR'){
				alert('Preencha corretamente o seu CPF.');
			}
			else {
				alert('Preencha corretamente o seu Documento Pessoal.');
			}
			cpf.focus();
			return false;
		}
		cep.value = cep.value.trim();
		if (cep.value == ''){
			alert('Preencha o CEP');
			cep.focus();
			return false;
		}
		if (sPais == 'BR' && cep.value.match(reCEP) != null)	
		{
			alert('Utilize apenas números para o CEP');
			cep.focus();
			return false;
		}
		/*if(pais.options[pais.selectedIndex].value == ''){
			alert('Escolha o país onde você reside.');
			pais.focus();
			return false;
		}*/
		cidade.value = cidade.value.trim();
		if(cidade.value == '' || cidade.value.match(reGeneral) != null){
			alert('Preencha a cidade onde você reside.');
			cidade.focus();
			return false;
		}
		if(bairro.value != '' && bairro.value.match(reGeneral) != null){
			alert('Preencha corretamente o bairro onde você reside.');
			bairro.focus();
			return false;
		}
		if(estado.options[estado.selectedIndex].value == '' && sPais == 'BR'){
			alert('Escolha o estado que você reside.');
			estado.focus();
			return false;
		}
		else if (estado.options[estado.selectedIndex].value == '' && sPais != 'BR' && outroestado.value == ''){
			alert('Preencha corretamente o estado que você reside.');
			outroestado.focus();
			return false;
		}
		if(ds_address_type.options[ds_address_type.selectedIndex].value != '' && ds_address_type.options[ds_address_type.selectedIndex].value.match(reGeneral) != null){
			alert('Preencha corretamente o campo logradouro.');
			ds_address_type.focus();
			return false;
		}
		if (year_birthday.options[year_birthday.selectedIndex].value != '' || month_birthday.options[month_birthday.selectedIndex].value != '' || day_birthday.options[day_birthday.selectedIndex].value != ''){
			var dtData = day_birthday.options[day_birthday.selectedIndex].value + '/' + month_birthday.options[month_birthday.selectedIndex].value + '/' + year_birthday.options[year_birthday.selectedIndex].value;
			if (!isEuDate(dtData)){
				alert("Preencha corretamente sua data de nascimento !");
				oForm.day_birthday.focus();
				return false;
			}
		}
		
		if (year_birthday.options[year_birthday.selectedIndex].value == '' && month_birthday.options[month_birthday.selectedIndex].value == '' && day_birthday.options[day_birthday.selectedIndex].value == ''){
				alert("Preencha sua data de nascimento !");
				oForm.day_birthday.focus();
				return false;
			}
		if(telefone1.value != '' && telefone1.value.match(reTel) != null){
			alert('Só são aceitos os seguintes caracteres no campo telefone:\n - Dígitos 0-9\n - Parênteses, hífens e espaços\n\nPor favor verifique se há carateres inválidos.');
			telefone1.focus();
			return false;
		}
		if(telefone2.value != '' && telefone2.value.match(reTel) != null){
			alert('Só são aceitos os seguintes caracteres no campo telefone:\n - Dígitos 0-9\n - Parênteses, hífens e espaços\n\nPor favor verifique se há carateres inválidos.');
			telefone2.focus();
			return false;
		}
		if(ds_address_complement.value != '' && ds_address_complement.value.match(reGeneral) != null){
			alert('Preencha corretamente o campo complemento.');
			ds_address_complement.focus();
			return false;
		}
		if(ds_address_type_outro.value != '' && ds_address_type_outro.value.match(reGeneral) != null){
			alert('Preencha corretamente o campo tipo de logradouro.');
			ds_address_type_outro.focus();
			return false;
		}
		
		
	}
	return true;
}
function cadastroNivel_0(oForm){
	var selEspecialidade	= oForm.id_main_speciality;
	var selCategoria		= oForm.id_cadaster_category;
	var re					= /[^a-z0-9_\.\-]/g
	with(oForm){

		ds_user_login.value = ds_user_login.value.trim();
		ds_user_pass.value  = ds_user_pass.value.trim();
		ds_email.value = ds_email.value.trim();

		/* Campo profissão = selCategoria */
		if (selCategoria.options[selCategoria.selectedIndex].value == "6")
		{
		  alert("O Medcenter.com criou um site especialmente para você que não trabalha na área de saúde.");
		  window.open("http://www.medcenter.com/saude","_top");
		  return false;
		}
		/* Campo nm_first_name */
		var reName = /[^\'\w\s\-\xC0-\xFC]|\d/gi
		var reTrat= /^Dr(a)?(\.)?\s/i
				
		nm_first_name.value = nm_first_name.value.trim();		
		
		if (nm_first_name.value == '' || nm_first_name.value.match(reName) != null)
		{
		  alert("Preencha o seu nome corretamente (somente caracteres alfabéticos).");
		  nm_first_name.focus();
		  return false;
		}
		nm_last_name.value = nm_last_name.value.trim();
		if (nm_last_name.value == '' || nm_last_name.value.match(reName) != null){
			alert('Preencha o seu sobrenome corretamente (somente caracteres alfabéticos).');
			nm_last_name.focus();
			return false;
		}
		
		/* Campo sexo */
		if (!cd_gender[0].checked && !cd_gender[1].checked)
		{
     	  alert("Marque uma opção em sexo");
		  cd_gender[0].focus();
		  return false;
		}

		/* Campo profissão = selCategoria */
		if (selCategoria.options[selCategoria.selectedIndex].value == "")
		{
		  alert("Por favor, escolha uma ocupação.");
		  selCategoria.focus();
		  return false;
		}
		
		/* Especialidade / Atuação */
		if (id_main_speciality.options[id_main_speciality.selectedIndex].value == "") {
			alert("Selecione a sua Especialidade ou Área de Atuação Principal");
			id_main_speciality.focus();
			return false;
		}
				
		/* Campo ds_email */

		if (!isEmail(ds_email.value))
		{
		  alert("Preencha corretamente o seu email.\n O email deve conter no mínimo 5 e no máximo 80 caracteres, e deve estar no formato joao_silva@seuprovedor.com.br\nUtilize apenas letras minúsculas.");
		  ds_email.focus();
		  return false;
		}
		if (ds_email.value.indexOf('@medcenter.com')!= -1){	
		  alert("Você ainda não tem um email @medcenter.com\nPor favor, forneça outro endereço.");
		  ds_email.focus();
		  return false;
		}
		if(pais.options[pais.selectedIndex].value == ''){
			alert('Escolha o país onde você reside.');
			pais.focus();
			return false;
		}		
		/* Campos Usuário e Senha */
		if (ds_user_login.value == "")
		{
		  alert("Preencha o campo usuário.");
		  ds_user_login.focus();
		  return false;
		}
		if (ds_user_pass.value == "")
		{
		  alert("Preencha o campo senha.");
		 ds_user_pass.focus();
		  return false;
		}
		
		if (ds_user_login.value.length < 2 || ds_user_login.value.length > 20){
			alert('O nome de usuário deve conter de 2 a 20 caracteres válidos.');
			ds_user_login.focus();
			return false;
		}
		if(ds_user_pass.value.length < 4 || ds_user_pass.value.length > 8)
		{
			alert("A senha deve conter de 4 a 8 caracteres válidos")
			ds_user_pass.focus();
			return false;
		}

		if (ds_user_login.value.match(re) != null) {
			alert("No nome de usuário e na senha não são permitidos espaços nem caracteres especiais\n(Ex.: #,$,%,!)\nUtilize apenas letras minúsculas.");
			ds_user_login.focus();
			return false;
		}
		if (ds_user_pass.value.match(re) != null) {
			alert("No nome de usuário e na senha não são permitidos espaços nem caracteres especiais\n(Ex.: #,$,%,!)\nUtilize apenas letras minúsculas.");
			ds_user_pass.focus();
			return false;
		}
		
		if(ds_user_pass.value == ds_user_login.value){
			alert("O nome do usuário e a senha não devem ser iguais.");
			ds_user_pass.focus();
			return false;
		}
		if (ds_user_pass.value != ds_user_pass_confirm.value){
			alert("Confirmação de senha incorreta.\n Lembre-se de que deve ter 4 a 8 caracteres válidos.");
			ds_user_pass.value = "";
			ds_user_pass_confirm.value = "";
			ds_user_pass.focus();
			return false;
		}
		nm_first_name.value = nm_first_name.value.replace(reTrat,"");
		nm_first_name.value = FormatName(nm_first_name.value);
		nm_last_name.value = FormatName(nm_last_name.value);
  		return true;
  	}
}

function alterarcadastroNivel_0(oForm){
	var reName = /[^\'\w\s\-\xC0-\xFC]|\d/gi
	var reTrat= /^Dr(a)?(\.)?\s/i
	var selEspecialidade = oForm.id_main_speciality;
	var selCategoria = oForm.id_cadaster_category;
	var re = /[^a-z0-9_\.\-]/g
	with(oForm){
		/* Campo profissão = selCategoria */
		if (selCategoria.options[selCategoria.selectedIndex].value == "6")
		{
		  alert("O Medcenter.com criou um site especialmente para você que não trabalha na área de saúde.");
		  window.open("http://www.medcenter.com/saude","_top");
		  return false;
		}
		/* Campo nm_first_name */

		nm_first_name.value = nm_first_name.value.trim();		
		
		if (nm_first_name.value == '' || nm_first_name.value.match(reName) != null)
		{
		  alert("Preencha o seu nome corretamente (somente caracteres alfabéticos).");
		  nm_first_name.focus();
		  return false;
		}
		nm_last_name.value = nm_last_name.value.trim();
		if (nm_last_name.value == '' || nm_last_name.value.match(reName) != null){
			alert('Preencha o seu sobrenome corretamente (somente caracteres alfabéticos).');
			nm_last_name.focus();
			return false;
		}
		// Validação de senha no caso do usuário não ter webmail (pode-se alterar a senha)
		if(fl_webmail && fl_webmail.value == 0){
			if(ds_user_pass.value.length < 4 || ds_user_pass.value.length > 8)
			{
				alert("A senha deve conter de 4 a 8 caracteres válidos")
				ds_user_pass.focus();
				return false;
			}
			if (ds_user_pass.value.match(re) != null) {
				alert("No nome de usuário e na senha não são permitidos espaços nem caracteres especiais\n(Ex.: #,$,%,!)\nUtilize apenas letras minúsculas.");
				ds_user_pass.focus();
				return false;
			}
			if(ds_user_pass.value == ds_user_login.value){
				alert("O nome do usuário e a senha não devem ser iguais.");
				ds_user_pass.focus();
				return false;
			}
			if (ds_user_pass.value != ds_user_pass_confirm.value){
				alert("Confirmação de senha incorreta.\n Lembre-se de que deve ter 4 a 8 caracteres válidos.");
				ds_user_pass.value = "";
				ds_user_pass_confirm.value = "";
				ds_user_pass.focus();
				return false;
			}			
		}
		
		/* Campo sexo */
		if (!cd_gender[0].checked && !cd_gender[1].checked)
		{
     	  alert("Marque uma opção em sexo");
		  cd_gender[0].focus();
		  return false;
		}

		/* Campo profissão = selCategoria */
		if (selCategoria.options[selCategoria.selectedIndex].value == "")
		{
		  alert("Selecione a sua Profissão.");
		  selCategoria.focus();
		  return false;
		}
		
		/* Especialidade / Atuação */
		if (id_main_speciality.options[id_main_speciality.selectedIndex].value == "") {
			alert("Selecione a sua Especialidade ou Área de Atuação Principal");
			id_main_speciality.focus();
			return false;
		}
		/* Campo ds_email */
		if (ds_email.value.indexOf('@medcenter.com')!= -1) {	
			if (ds_user_login.value != ds_email.value.substr(0,(ds_email.value.indexOf('@'))) || fl_webmail.value != 1 ){
				alert("Você não tem um email @medcenter.com para este nome de usuário.\nPor favor, forneça outro endereço.\nQualquer dúvida, entre em contato com sac@medcenter.com");
				ds_email.focus();
				return false;
			}
		}		
		if (!isEmail(ds_email.value))
		{
		  alert("Preencha corretamente o seu email.\nO email deve conter no mínimo 5 e no máximo 80 caracteres, e deve estar no formato joao_silva@seuprovedor.com.br\nUtilize apenas letras minúsculas.");
		  ds_email.focus();
		  return false;
		}
		nm_first_name.value		=	nm_first_name.value.replace(reTrat,"");
		nm_first_name.value		=	FormatName(nm_first_name.value);
		nm_last_name.value		=	FormatName(nm_last_name.value);
  		return true;
  	}
}
function alterarCadastroNivel_1(oForm){
	var reGeneral = /[^\w\xC0-\xFC\d\s\-\,\.\\\/\`\']/gi
	var reNumeric = /[^\d\.\,\-]/gi
	var reCEP = /\D/gi
	var reTel = /[^\d\-\(\)\s]/gi
	with(oForm){
		var sPais = cd_country.value;
		if ( nr_cpf_personal_unique_code.value == '' || (sPais == 'BR' && !checaCPF(nr_cpf_personal_unique_code.value)) ){
			if(sPais == 'BR'){
				alert('Preencha corretamente o seu CPF.');
			}
			else {
				alert('Preencha corretamente o seu Documento Pessoal.');
			}
			nr_cpf_personal_unique_code.focus();
			return false;
		}
		nr_zip_code.value = nr_zip_code.value.trim();
		if (nr_zip_code.value == ''){
			alert('Preencha o CEP');
			nr_zip_code.focus();
			return false;
		}
		if (sPais == 'BR' && nr_zip_code.value.match(reCEP) != null)	
		{
			alert('Utilize apenas números para o CEP');
			nr_zip_code.focus();
			return false;
		}
		ds_city.value = ds_city.value.trim();
		if(ds_city.value == '' || ds_city.value.match(reGeneral) != null){
			alert('Preencha a cidade onde você reside.');
			ds_city.focus();
			return false;
		}
		if(ds_address_neighborhood.value != '' && ds_address_neighborhood.value.match(reGeneral) != null){
			alert('Preencha corretamente o bairro onde você reside.');
			ds_address_neighborhood.focus();
			return false;
		}
		if(cd_state.options[cd_state.selectedIndex].value == '' && !cd_state.disabled){
			alert('Escolha o estado que você reside.');
			cd_state.focus();
			return false;
		}
		else if (cd_state.disabled && cd_state_other.value == ''){
			alert('Preencha corretamente o estado que você reside.');
			cd_state_other.focus();
			return false;
		}
		/*if(ds_address_type.options[ds_address_type.selectedIndex].value == ''){
			alert('Preencha corretamente o campo tipo de logradouro.');
			ds_address_type.focus();
			return false;
		}*/
		if (year_birthday.options[year_birthday.selectedIndex].value != '' || month_birthday.options[month_birthday.selectedIndex].value != '' || day_birthday.options[day_birthday.selectedIndex].value != ''){
			var dtData = day_birthday.options[day_birthday.selectedIndex].value + '/' + month_birthday.options[month_birthday.selectedIndex].value + '/' + year_birthday.options[year_birthday.selectedIndex].value;
			if (!isEuDate(dtData)){
				alert("Preencha corretamente sua data de nascimento !");
				day_birthday.focus();
				return false;
			}
		}
		if (year_birthday.options[year_birthday.selectedIndex].value == '' && month_birthday.options[month_birthday.selectedIndex].value == '' && day_birthday.options[day_birthday.selectedIndex].value == ''){
				alert("Preencha sua data de nascimento !");
				oForm.day_birthday.focus();
				return false;
			}
		if(cd_area_code.value != '' && cd_area_code.value.match(reTel) != null){
			alert('Só são aceitos os seguintes caracteres no campo DDD:\n - Dígitos 0-9\n - Parênteses, hífens e espaços\n\nPor favor verifique se há carateres inválidos.');
			cd_area_code.focus();
			return false;
		}
		if(nr_phone.value != '' && nr_phone.value.match(reTel) != null){
			alert('Só são aceitos os seguintes caracteres no campo telefone:\n - Dígitos 0-9\n - Parênteses, hífens e espaços\n\nPor favor verifique se há carateres inválidos.');
			nr_phone.focus();
			return false;
		}
		if(ds_address_complement.value != '' && ds_address_complement.value.match(reGeneral) != null){
			alert('Preencha corretamente o campo complemento.');
			ds_address_complement.focus();
			return false;
		}
		if(ds_address_type_other.value != '' && ds_address_type_other.value.match(reGeneral) != null && ds_address_type_other.disabled){
			alert('Preencha corretamente o campo tipo de logradouro.');
			ds_address_type_other.focus();
			return false;
		}
	}
	return true;
}
/*********************************************************************************
*	FUNCTION:		isBetween
*	PARAMETERS:		val		AS any value
*					lo		AS Lower limit to check
*					hi		AS Higher limit to check
*	CALLS:			NOTHING
*	RETURNS:		TRUE if val is between lo and hi both inclusive, otherwise false.
**********************************************************************************/
function isBetween (val, lo, hi) {
	if ((val < lo) || (val > hi)) { return(false); }
	else { return(true); }
}

/*********************************************************************************
*	FUNCTION:		isDate checks a valid date
*	PARAMETERS:		theStr		AS String
*	CALLS:			isBetween, isInt
*	RETURNS:		TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isDate (theStr) {
	var the1st = theStr.indexOf('/');
	var the2nd = theStr.lastIndexOf('/');
	
	if (the1st == the2nd) { return(false); }
	else {
		var m = theStr.substring(0,the1st);
		var d = theStr.substring(the1st+1,the2nd);
		var y = theStr.substring(the2nd+1,theStr.length);
		var maxDays = 31;
		
		if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
			return(false); }
		else if (y.length < 4) { return(false); }
		else if (!isBetween (m, 1, 12)) { return(false); }
		else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
		else if (m==2) {
			if (y % 4 > 0) maxDays = 28;
			else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
           	else maxDays = 29;
		}
			
		if (isBetween(d, 1, maxDays) == false) { return(false); }
		else { return(true); }
	}
}
/*********************************************************************************
*	FUNCTION:		isTime validates TIME in HH:MM format.
*	PARAMETERS:		theStr		AS String
*	CALLS:			isBetween, isInt
*	RETURNS:		TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isTime (theStr) {
	var colonDex = theStr.indexOf(':');

	if ((colonDex<1) || (colonDex>2)) { return(false); }
	else {
		var hh = theStr.substring(0,colonDex);
		var ss = theStr.substring(colonDex+1, theStr.length);

		if ((hh.length<1) || (hh.length>2) || (!isInt(hh))) { return(false); }
		else if ((ss.length<1) || (ss.length>2) || (!isInt(ss))) { return(false); }
		else if ((!isBetween(hh,0,23)) || (!isBetween(ss,0,59))) { return(false); }
		else { return(true); }
	}
}
/*********************************************************************************
*	FUNCTION:		isDigit
*	PARAMETER:		Any Chracter
*	CALLS			isEmpty to check the NULL
*	RETURNS:		TRUE if the passed chracter is a digit, otherwise FALSE
**********************************************************************************/
function isDigit(theNum) {
	var theMask = '0123456789';
	
	if (isEmpty(theNum)) return(false);
	else if (theMask.indexOf(theNum) == -1) return(false);
	
	return(true);
}
/*********************************************************************************
*	FUNCTION:		

*	PARAMETER:		String (Email Address)
*	RETURNS:		TRUE if the String is a valid Email address
*					FALSE if the passed string is not a valid Email Address
*	EMAIL FORMAT:	AnyName@EmailServer e.g; webmaster@hotmail.com
*					@ sign can appear only once in the email address.
**********************************************************************************/
function isEmail (theStr) {
	var atIndex = theStr.indexOf('@');
	var dotIndex = theStr.indexOf('.', atIndex);
	var flag = true;
	var reEmail = /[^a-z0-9_\.\-\@\&]/g
	theSub = theStr.substring(0, dotIndex+1)
	
	if ( (atIndex < 1 || atIndex != theStr.lastIndexOf('@') || dotIndex < atIndex + 2 || theStr.length <= theSub.length) || theStr.search(reEmail) != -1)
	{	flag = false; }
	else { flag = true; }
	
	return(flag);
}

/*********************************************************************************
*	FUNCTION	isEmpty checks if the parameter is empty or null
*	PARAMETER	str		AS String
**********************************************************************************/
function isEmpty (str) {
	if ((str==null)||(str.length==0)) return true;
	else return(false);
}

/*********************************************************************************
*	FUNCTION:		isInt
*	PARAMETER:		theStr	AS String 
*	RETURNS:		TRUE if the passed parameter is an integer, otherwise FALSE
*	CALLS:			isDigit
**********************************************************************************/
function isInt (theStr) {
	var flag = true;

	if (isEmpty(theStr)) { flag=false; }
	else
	{	for (var i=0; i < theStr.length; i++) {
			if (isDigit(theStr.substring(i,i+1)) == false) {
				flag = false; break;
			}
		}
	}
	return(flag);
}

/*********************************************************************************
*	FUNCTION:		isReal
*	PARAMETER:		theStr	AS String 
					decLen	AS Integer (how many digits after period)
*	RETURNS:		TRUE if theStr is a float, otherwise FALSE
*	CALLS:			isInt
**********************************************************************************/
function isReal (theStr, decLen) {
	var dot1st = theStr.indexOf('.');
	var dot2nd = theStr.lastIndexOf('.');
	var OK = true;
	
	if (isEmpty(theStr)) return false;

	if (dot1st == -1) {
		if (!isInt(theStr)) return(false);
		else return(true);
	}
	
	else if (dot1st != dot2nd) return (false);
	else if (dot1st==0) return (false);
	else {
		var intPart = theStr.substring(0, dot1st);
		var decPart = theStr.substring(dot2nd+1);

		if (decPart.length > decLen) return(false);
		else if (!isInt(intPart) || !isInt(decPart)) return (false);
		else if (isEmpty(decPart)) return (false);
		else return(true);
	}
}


/*********************************************************************************
*	FUNCTION:		isEuDate checks a valid date in British format
*	PARAMETERS:		theStr		AS String
*	CALLS:			isBetween, isInt
*	RETURNS:		TRUE if theStr is a valid date otherwise false.
**********************************************************************************/
function isEuDate (theStr) {
	if (isBetween(theStr.length, 8, 10) == false) { return(false); }
	else {
		var the1st = theStr.indexOf('/');
		var the2nd = theStr.lastIndexOf('/');
		
		if (the1st == the2nd) { return(false); }
		else {
			var m = theStr.substring(the1st+1,the2nd);
			var d = theStr.substring(0,the1st);
			var y = theStr.substring(the2nd+1,theStr.length);
			var maxDays = 31;

			if (isInt(m)==false || isInt(d)==false || isInt(y)==false) {
				return(false); }
			else if (y.length < 4) { return(false); }
			else if (isBetween (m, 1, 12) == false) { return(false); }
			else if (m==4 || m==6 || m==9 || m==11) maxDays = 30;
			else if (m==2) {
				if (y % 4 > 0) maxDays = 28;
				else if (y % 100 == 0 && y % 400 > 0) maxDays = 28;
            	else maxDays = 29;
			}
			
			if (isBetween(d, 1, maxDays) == false) { return(false); }
			else { return(true); }
		}
	}
}
function checaCPF (CPF) {
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
 }


function outroPais() {
	with(document.cadastro_n1) 	{	
		var bBrasil = (pais.options[pais.selectedIndex].value == "BR")
		estado.disabled = !bBrasil;
		estado.value = (bBrasil) ? estado.value : "";
		ds_address_type.value = (bBrasil) ? ds_address_type.value : "";
		ds_address_type.disabled = !bBrasil;
		outroestado.disabled = bBrasil
		outroestado.value = (bBrasil) ? "" : outroestado.value;
		ds_address_type_outro.disabled = bBrasil;
		ds_address_type_outro.value = (bBrasil) ? "" : ds_address_type_outro.value;
		(!bBrasil) ? outroestado.focus() : estado.focus();
	}

}

function outroTipoLogradouro() {
	with(document.cadastro_n1) {
		if (ds_address_type.options[ds_address_type.selectedIndex].value != "") {
			ds_address_type_outro.value = "";
			ds_address_type_outro.disabled = true;
		}
		else
			ds_address_type_outro.disabled = false;
	}
}

function outroEstado() {
	with(document.cadastro_n1) {
		if (estado.options[estado.selectedIndex].value != "") {
			outroestado.value = "";
			outroestado.disabled = true;
		}
		else {
			outroestado.disabled = false;
		}	
	}
}

function usuarioExistente() {
	var re = /[^a-z0-9_\.\-]/g
	with (document.form2) {
		ds_user_login.value = ds_user_login.value.trim();
		if (ds_user_login.value == ""){
			alert("Preencha o campo novo usuário.");
			ds_user_login.focus();
			return false;
		}
		if (ds_user_login.value.length < 2 || ds_user_login.value.length > 20){
			alert('O nome de usuário deve conter de 2 a 20 caracteres válidos.');
			ds_user_login.focus();
			return false;
		}
		if (ds_user_login.value.match(re) != null) {
			alert("No nome de usuário não são permitidos espaços nem caracteres especiais\n(Ex.: #,$,%,!)\nUtilize apenas letras minúsculas.");
			ds_user_login.focus();
			return false;
		}
	}
}
