function  f_pessoa_juridica(){
	var nr_rg, nr_cpf, dt_nascimento;
	dt_nascimento = document.getElementById('dt_nascimento');
	nr_cpf = document.getElementById('nr_cpf');
	nr_rg = document.getElementById('nr_rg');
	if(dt_nascimento.value.length>0 || nr_cpf.value.length>0 || nr_rg.value.length>0)
	{
		if(confirm('os dados de pessoa fisica serão perdidos deseja continuar?'))
		{
			dt_nascimento.value = '';
			nr_cpf.value = '';
			nr_rg.value = '';
		}
		else
			return false;
	}

	var pesJ = document.getElementById('div_pessoa_juridica');
	var pesF = document.getElementById('div_pessoa_fisica');
	pesF.style.visibility='hidden';
	pesF.style.display='none';
	pesJ.style.visibility='visible';
	pesJ.style.display='block';	
	document.getElementById('pessoa_juridica').className="botaoOn";
	document.getElementById('pessoa_fisica').className="tipo_pessoa";
	return true;
}
function  f_pessoa_fisica(){
	var nr_cnpj, nm_fantasia;
	nr_cnpj = document.getElementById('nr_cnpj');
	nm_fantasia = document.getElementById('nm_fantasia');
	if(nr_cnpj.value.length>0 || nm_fantasia.value.length>0)
	{
		if(confirm('os dados de pessoa juridica serão perdidos deseja continuar?'))
		{
			nr_cnpj.value = '';
			nm_fantasia.value = '';
		}
		else
			return false;
	}
	var pesJ = document.getElementById('div_pessoa_juridica');
	var pesF = document.getElementById('div_pessoa_fisica');
	pesJ.style.visibility='hidden';
	pesJ.style.display='none';
	pesF.style.visibility='visible';
	pesF.style.display='block';	
	document.getElementById('pessoa_fisica').className="botaoOn";
	document.getElementById('pessoa_juridica').className="tipo_pessoa";
	return true;
}
function unicoChecked(obj) 
{
	var id, er,i;
	id=obj.id.replace(/[0-9]/g, '').replace(/_$/,'');
	er = new RegExp('^'+id);
	for(i=0;i<obj.form.elements.length;i++)
	{
		if(er.test(obj.form.elements[i].id))
			obj.form.elements[i].checked=false;
	}
	obj.checked=true;
}
function addTelefone()
{
	var pai=document.getElementById('telefones_0').parentNode;
	var id=pai.getElementsByTagName('tr').length;
	var input, select, check, opt, tr,td,i;
	tr = document.createElement('tr');
	td = document.createElement('td');
	input = document.createElement('input');
	input.type="text";
	input.className = "telefone";
	input.id = input.name = "nr_telefone_"+id;
	input.setAttribute('onkeyup', 'javascript: formataTel(this);');
	td.appendChild(input);
	tr.appendChild(td);
	td = document.createElement('td');
	select = document.createElement('select');
	select.id = select.name = "cd_telefone_tipo_"+id;
	for(i=0;i<document.getElementById('cd_telefone_tipo_0').options.length;i++)
	{
		option = document.createElement('option');
		option.value=document.getElementById('cd_telefone_tipo_0').options[i].value;
		option.innerHTML=document.getElementById('cd_telefone_tipo_0').options[i].innerHTML;
		select.appendChild(option);
	}
	td.appendChild(select);
	tr.appendChild(td);
	td = document.createElement('td');
	check = document.createElement('input');
	check.type="checkbox";
	check.value='S';
	check.id = check.name = "telefone_principal_"+id;
	check.setAttribute('onclick', 'javascript: unicoChecked(this);');
	i = document.createElement('center');
	i.appendChild(check);
	td.appendChild(i);
	tr.appendChild(td);
	pai.appendChild(tr);
}
function addEmail()
{
	var pai=document.getElementById('emails_0').parentNode;
	var id=pai.getElementsByTagName('tr').length;
	var input, select, check, opt, tr,td,i;
	tr = document.createElement('tr');
	td = document.createElement('td');
	input = document.createElement('input');
	input.type="text";
	input.className = "email";
	input.setAttribute("onblur", "javascript: validaEmail(this);");
	input.id = input.name = "nm_email_"+id;
	td.appendChild(input);
	tr.appendChild(td);
	td = document.createElement('td');
	select = document.createElement('select');
	select.id = select.name = "cd_email_tipo_"+id;
	for(i=0;i<document.getElementById('cd_email_tipo_0').options.length;i++)
	{
		option = document.createElement('option');
		option.value=document.getElementById('cd_email_tipo_0').options[i].value;
		option.innerHTML=document.getElementById('cd_email_tipo_0').options[i].innerHTML;
		select.appendChild(option);
	}
	td.appendChild(select);
	tr.appendChild(td);
	td = document.createElement('td');
	check = document.createElement('input');
	check.type="checkbox";
	check.value='S';
	check.id = check.name = "email_principal_"+id;
	check.setAttribute('onclick', 'javascript: unicoChecked(this);');
	i = document.createElement('center');
	i.appendChild(check);
	td.appendChild(i);
	tr.appendChild(td);
	td = document.createElement('td');
	check = document.createElement('input');
	check.type="checkbox";
	check.value='S';
	check.id = check.name = "email_cobranca_"+id;
	check.setAttribute('onclick', 'javascript: unicoChecked(this);');
	i = document.createElement('center');
	i.appendChild(check);
	td.appendChild(i);
	tr.appendChild(td);
	pai.appendChild(tr);
}
function addIm()
{
	var pai=document.getElementById('ims_0').parentNode;
	var id=pai.getElementsByTagName('tr').length;
	var input, select, check, opt, tr,td,i;
	tr = document.createElement('tr');
	td = document.createElement('td');
	input = document.createElement('input');
	input.type="text";
	input.className = "email";
	input.id = input.name = "nm_im_"+id;
	td.appendChild(input);
	tr.appendChild(td);
	td = document.createElement('td');
	select = document.createElement('select');
	select.id = select.name = "cd_im_tipo_"+id;
	for(i=0;i<document.getElementById('cd_im_tipo_0').options.length;i++)
	{
		option = document.createElement('option');
		option.value=document.getElementById('cd_im_tipo_0').options[i].value;
		option.innerHTML=document.getElementById('cd_im_tipo_0').options[i].innerHTML;
		select.appendChild(option);
	}
	td.appendChild(select);
	tr.appendChild(td);
	pai.appendChild(tr);
}
//formata telefone 
function formataTel(tel){
	var aux = tel.selectionEnd;
	tel.value = tel.value.replace(/[^0-9]/g, '');

	if(tel.value.length >= 1)
	{
		tel.value = '('+tel.value;
		aux++;
	}
	if(tel.value.length >= 4)
	{
		tel.value = tel.value.substring(0,3)+') '+tel.value.substring(3,13);
		aux+=2;
	}
	if(tel.value.length >= 10)
	{
		tel.value = tel.value.substring(0,9)+'-'+tel.value.substring(9,13);
		aux++;
	}
	tel.selectionEnd = aux;
}
function formataCpf(cpf) {
	var aux = cpf.selectionEnd;
	cpf.value = cpf.value.replace(/[^0-9]/g, '');
	if(cpf.value.length >= 4)
	{
		cpf.value = cpf.value.substring(0,3) + '.' + cpf.value.substring(3,13);
		aux++;
	}
	if(cpf.value.length >= 8)
	{
		cpf.value = cpf.value.substring(0,7) + '.' + cpf.value.substring(7,13);
		aux++;
	}
	if(cpf.value.length >= 12)
	{
		cpf.value = cpf.value.substring(0,11) + '-' + cpf.value.substring(11,13);
		aux++;
	}
	cpf.selectionEnd = aux;
}
function formataData(data) {
	var aux = data.selectionEnd;
	data.value = data.value.replace(/[^0-9]/g, '');
	if(data.value.length >= 3)
	{
		data.value = data.value.substring(0,2) + '/' + data.value.substring(2,9);
		aux++;
	}
	if(data.value.length >= 6)
	{
		data.value = data.value.substring(0,5) + '/' + data.value.substring(5,9);
		aux++;
	}
	data.selectionEnd = aux;
}

//12.456.890/2345-67
function formataCnpj(cnpj) {
	var aux = cnpj.selectionEnd;
	cnpj.value = cnpj.value.replace(/[^0-9]/g, '');
	if(cnpj.value.length >= 3)
	{
		cnpj.value = cnpj.value.substring(0,2) + '.' + cnpj.value.substring(2,17);
		aux++;
	}
	if(cnpj.value.length >= 7)
	{
		cnpj.value = cnpj.value.substring(0,6) + '.' + cnpj.value.substring(6,17);
		aux++;
	}
	if(cnpj.value.length >= 11)
	{
		cnpj.value = cnpj.value.substring(0,10) + '/' + cnpj.value.substring(10,17);
		aux++;
	}
	if(cnpj.value.length >= 16)
	{
		cnpj.value = cnpj.value.substring(0,15) + '-' + cnpj.value.substring(15,17);
		aux++;
	}
	cnpj.selectionEnd = aux;
}
function validaEmail(email){
	email.value = email.value.replace('^ | $','').toLowerCase();
	if(arguments.length == 2 && email.value.length==0)
	{
		showHidde(email, true);
		return true;
	}
	
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    var address = email.value;
   
    if(reg.test(address) == false) {
       alert('email invalido!');
	  // email.focus();
      return false;
   }
}
function formataCep(cep){
	var aux = cep.selectionEnd;
	cep.value = cep.value.replace(/[^0-9]/g, '');
	if(cep.value.length >= 5)
		cep.value = cep.value.substring(0,5)+'-'+cep.value.substring(5,8);
	cep.selectionEnd = aux;
}
function validaUsuario(usuario)
{
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}
	var aux="cd_pessoa="+(usuario.form.cd_pessoa.value*1)+"&nm_usuario="+usuario.value;
	http.open("POST", "usuario_ajax.php" ,true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno = http.responseXML;
			try
			{
				if(retorno.getElementsByTagName('usuario').item(0).firstChild.nodeValue > 0)
				{
					alert('usuario já utilizado!');
					usuario.focus();
				}
			}
			catch(E)
			{}
			}
		}
	}
	http.send(aux);
}
function validaUsuario2(usuario)
{
	try
	{
		var http = new XMLHttpRequest()
	}
	catch(ee)
	{
		try
		{
		 	var http = new ActiveXObject("Msxml2.XMLHTTP")
		}
		catch(e)
		{
			try
			{
				var http = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch(E)
			{
				var http = false
			}
		}
	}
	var aux="cd_pessoa="+1+"&nm_usuario="+usuario.value;
	http.open("POST", "../usuario_ajax.php" ,true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	http.setRequestHeader("Content-length", aux.length);
    	http.setRequestHeader("Connection", "close");
	http.onreadystatechange=function() {
		if (http.readyState==4)
		{ 
			if (http.status == 200) {
				var retorno = http.responseXML;
			try
			{
				if(retorno.getElementsByTagName('usuario').item(0).firstChild.nodeValue > 0)
				{
					alert('usuario já utilizado!');
					usuario.focus();
				}
			}
			catch(E)
			{}
			}
		}
	}
	http.send(aux);
}
function validaFormulario(frm)
{
	if (frm.conheceu.selectedIndex == 0)
	{
		frm.conheceu.focus();
		alert('selecione como conheceu a powermidia.net');
		return false;
	}
	else if(frm.nm_pessoa.value.length == 0)
	{
		frm.nm_pessoa.focus();
		alert('o nome é obrigatorio!');
		return false;
	}
	else if(frm.cd_cidade.value.length == 0)
	{
		frm.cd_estado.focus();
		alert('a cidade é obrigatoria!');
		return false;
	}
	else if(frm.nm_usuario.value.length == 0)
	{
		frm.nm_usuario.focus();
		alert('usuario e senha é obrigatoria!');
		return false;
	}
	else
		return true;
}

