// JavaScript Document
function mascara_data(data){ 
  var mydata = ''; 
  mydata = mydata + data; 
  
  if (mydata.length == 2){ 
	  mydata = mydata + '/'; 
	  document.cadescritores.data_nascimento.value = mydata; 
  } 
  if (mydata.length == 5){ 
	  mydata = mydata + '/'; 
	  document.cadescritores.data_nascimento.value = mydata; 
  } 
  if (mydata.length == 10){ 
	  verifica_data(); 
  } 
} 

function verifica_data () { 

	dia = (document.cadescritores.data_nascimento.value.substring(0,2)); 
	mes = (document.cadescritores.data_nascimento.value.substring(3,5)); 
	ano = (document.cadescritores.data_nascimento.value.substring(6,10)); 
	
	situacao = ""; 
	// verifica o dia valido para cada mes 
	if ((dia < 01)||(dia < 01 || dia > 30) && (  mes == 04 || mes == 06 || mes == 09 || mes == 11 ) || dia > 31) { 
		situacao = "falsa"; 
	} 
	
	// verifica se o mes e valido 
	if (mes < 01 || mes > 12 ) { 
		situacao = "falsa"; 
	} 
	
	// verifica se e ano bissexto 
	if (mes == 2 && ( dia < 01 || dia > 29 || ( dia > 28 && (parseInt(ano / 4) != ano / 4)))) { 
		situacao = "falsa"; 
	} 
	
	if (document.cadescritores.data_nascimento.value == "") { 
		situacao = "falsa"; 
	} 
	
	if (situacao == "falsa") { 
		alert("Data inválida!"); 
		document.cadescritores.data_nascimento.focus(); 
	} 
} 

function checkForm_escritores(form) {
	d = document.cadescritores;
	nome			= document.getElementById('nome').value;
	telefone 		= document.getElementById('telefone').value;
	endereco 		= document.getElementById('endereco').value;
	bairro			= document.getElementById('bairro').value;
	cidade			= document.getElementById('cidade').value;
	uf				= document.getElementById('uf').value;
	email			= document.getElementById('email').value;	
	tamanho_email   = document.getElementById('email').value.length;	
	senha			= document.getElementById('senha_atual').value;	
	c_senha			= document.getElementById('c_senha').value;	
	var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
	
	if (nome == "") {
		alert("O nome  é obrigatorio!");
		d.nome.focus();
		return false;
	}
	
	if (tamanho_email <= 0) {
		alert("O campo e-mail deve ser conter um endereço eletronico valido 1 !");
		d.email.focus();
		return false;
	}
	
	if (!(er.test(d.email.value))) { 
		alert("O campo e-mail deve ser conter um endereço eletronico valido!");
		d.email.focus();
		return false;
	}
	
	if (telefone == "") {
		alert("O telefone é obrigatorio!");
		d.telefone.focus();
		return false;
	}

	if (endereco == "") {
		alert("O endereço é obrigatorio!");
		d.endereco.focus();
		return false;
	}

	if (bairro == "") {
		alert("O bairro é obrigatorio!");
		d.bairro.focus();
		return false;
	}

	if (cidade == "") {
		alert("A cidade é obrigatorio!");
		d.cidade.focus();
		return false;
	}

	if (uf == "") {
		alert("A uf é obrigatorio!");
		d.uf.focus();
		return false;
	}

	if (senha == "") {
		alert("O senha do usuário e obrigatorio!");
		d.senha_atual.focus();
		return false;
	}
	
	if (senha != c_senha) {
		alert("A confirmação da senha está da errada!");
		d.c_senha.focus();
		return false;
	}
	return true;
}

function fococadescritores(){
	d = document.cadescritores;
	d.nome.focus();
}

function createXMLHttpRequestInstance(){                  
	if (window.XMLHttpRequest) {           
		req = new XMLHttpRequest();    
	} else if (window.ActiveXObject) {
		isIE = true;
		req = new ActiveXObject("Microsoft.XMLHTTP");              
	}        
}

function validaEmail(cliente) {
	createXMLHttpRequestInstance();
	d = document.cadescritores;
	var email			= d.email.value;
	if (cliente == null) {
		cliente = "";
	}
	document.getElementById("carregando").style.display='block';
	req.onreadystatechange = processaDados;
	req.open("GET","buscaemail.php?email=" + email +"&cliente=" + cliente, true);  
	req.setRequestHeader("Content-Type", "application/x-www-urlencoded");
	req.send(null);
}

function processaDados() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			if (!req.responseXML) {
				document.cadescritores.button.disabled = false;
				document.getElementById("carregando").style.display ="none";
			} else {
				processaXMLCLIENTE(req.responseXML);
			}
		}
	}
}

function processaXMLCLIENTE(obj) {
	d = document.cadescritores;
	var nos 	= obj.getElementsByTagName("dadoscliente");
	if (nos.length > 0 ) {
		alert("E-mail já cadastrado. Verifique");
		d.email.focus();
		document.getElementById("carregando").style.display = "none";
		document.cadescritores.button.disabled = true;
	} else {
		document.cadescritores.button.disabled = false;
		document.getElementById("carregando").style.display ="none";
	}
}

