function trim(str){
	return str.replace(/^\s+|\s+$/g,"");
}

function geraSenha() {
	var currentTime = new Date()
	var month = currentTime.getMonth() + 1
	var day = currentTime.getDate()
	var year = currentTime.getFullYear()
	var hours = currentTime.getHours();
	var min = currentTime.getMinutes();
	var sec = currentTime.getSeconds();
	
	var source = "" + year + month + day + hours + min + sec;

	var senha = hex_md5(source).substr(0, 6);

	$('#senha').val(senha);
	//$('#senhaConfirma').val(senha);

	return(senha);
}

function mudaTipo(tipo) {
	var maskCNPJ = "99.999.999/9999-99";
	var maskCPF = "999.999.999-99"; 

	$("#cpf").unmask();
	
	//alert(tipo);
	
	if (tipo == "em" || tipo == "") {
		$("#labelNome").html("Nome Fantasia");
		$("#labelRazao").html("Razão Social");
		$("#labelDocumento1").html("CNPJ");
		$("#labelDocumento2").html("Insc. Estadual");
		$("#cpf").mask(maskCNPJ);
	}
	else {
		$("#labelNome").html("Nome");
		$("#labelRazao").html("Nome Fantasia");
		$("#labelDocumento1").html("CPF");
		$("#labelDocumento2").html("RG");
		$("#cpf").mask(maskCPF);
		//$("#rg").mask("99.999.999-9");
	}
}

function getTipoSelecionado() {
	var ret = "";
	for (var i=0; i < document.formulario.tipo.length; i++) {
		if (document.formulario.tipo[i].checked) {
			ret = document.formulario.tipo[i].value;
		}
	}
	
	return(ret);
}

function validaDoc() {
	var form = document.formulario;
	var ret = false;
	
	var cpf = trim(form.cpf.value);
	var tipo = getTipoSelecionado();
	
	if (cpf == "" || cpf == "___.___.___-__" || cpf == "__.___.___/____-__") {
		ret = true;
	}
	else if (tipo == "em") {
		ret = isCnpj(cpf);
	}
	else {
		ret = isCpf(cpf);
	}
	
	if (!ret) {
		alert("O " + $("#labelDocumento1").html() + " é inválido.");
	}
	
	return(ret);
}