function ValidaEmail(email)
{
	
	var CaracValid = "_-.@0123456789qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
	var tam = email.value.length;
	var valido = true;
	var pos1 = email.value.indexOf('@');
	var pos2 = email.value.indexOf('.');
	var pos3 = email.value.indexOf('.',pos1);
		
	for (i = 0;  i < tam;  i++)
	{
		ch = email.value.charAt(i);
		for (j = 0;  j < CaracValid.length;  j++)
		if (ch == CaracValid.charAt(j))
			break;
		if (j == CaracValid.length)
		{
			valido = false;
			break;
		}    
	}
			
	if((pos1 == -1) || (pos1 == 0) )
	{
		valido = false;
	}
	else
	{		
		if(email.value.indexOf('@',pos1+1)!=-1)
		{
			valido = false;
		}
	}
		
	if(tam<=pos1+1)
	{
		valido = false;
	}
		
	if(pos3 == -1)
	{
		valido = false;
	}
		
	if(tam<=pos3+1)
	{
		valido = false;
	}
		
	if(valido)
	{
		document.getElementById("msg_mail").innerHTML = "";
		document.getElementById("hd_erro_mail").value = "NO";
		return(true);
	}
	else
	{	
		if(document.getElementById("hd_erro_mail").value == 'NO'){
		   document.getElementById("hd_erro_mail").value = "SIM";
		   document.getElementById("msg_mail").innerHTML = "<b>ATENÇ&atilde;O:</b>  E-mail inválido!";
		}
			
	}
}

function empty_field(object,name)
{
	var value = object.value;
	var id = object.id;
	if(value == '')
	{
		document.getElementById("msg_"+id).innerHTML = "Campo <b>"+name+"</b> é obrigatório!";
	}else{
		document.getElementById("msg_"+id).innerHTML = "";
	}
}

function DigitaNumero(campo)
{
 var valid    = "0123456789/-";
 var numerook = "";
 var temp;
  
    
  for (var i=0; i< campo.value.length; i++) {
    temp = campo.value.substr(i, 1);
    
    if (valid.indexOf(temp) != "-1") 
      numerook = numerook + temp;
  }
   
 campo.value = numerook;

}

function erro_date(id,name)
{
	var name = name;
	var id = id;
	if(document.getElementById("hd_"+id).value == 'NO'){
	   document.getElementById("hd_"+id).value = "YES";
	   document.getElementById("msg_"+id).innerHTML = "<b>ATENÇ&atilde;O: </b>  "+name+" Inválida!";
	}
}		
function ValidaData(campo,name)
{
	var id = campo.id;
	var name = name;
	if( campo != '' )
	{
		var strData = campo.value;
		var pos = strData.indexOf("/",0);
		if (pos < 0){
			erro_date(id,name);
			return false
		}
		dia = strData.slice(0,pos);

		pos2 = strData.indexOf("/",pos+1);
		if (pos < 0){
			erro_date_birthday();
			return false
		}
		mes = strData.slice(pos+1,pos2);
	
		ano = strData.substr(pos2+1);

		dia= "00" + dia;
		dia = dia.substring(dia.length-2,dia.length);
   		mes= "00" + mes;
		mes = mes.substring(mes.length-2,mes.length);

		if (strData.value != '' && strData.length != 10) {
			erro_date_birthday();
			return false;
		}
			
		if (isNaN(dia) || isNaN(mes) || isNaN(ano)) {
			erro_date_birthday();
			return false;
		}
	
		if (ano.length < 4){
			erro_date_birthday();
			return false;
		}

		if (mes < 1 || mes > 12) {
			erro_date_birthday();
			return false;
		}

		if (dia < 1 || dia > 31) {
			erro_date_birthday();
			return false;
		}

		if ((mes==4 || mes==6 || mes==9 || mes==11) && dia==31) {
			erro_date_birthday();
			return false;
		}

		if (strData.indexOf("_") != -1) {
			erro_date_birthday();
			return false;
		}

		if (mes == 2) {
			var isleap = (ano % 4 == 0 && (ano % 100 != 0 || ano % 400 == 0))
			if (dia>29 || (dia==29 && !isleap)) {
				erro_date_birthday();
				return false;
			}
		}
	}
	document.getElementById("hd_"+id).value = "NO";
	document.getElementById("msg_"+id).innerHTML = "";
	return true;		
}

function MascaraData(campo,key){
	var strData = campo.value;
	
	if(key.keyCode == 8 || key.keyCode == 46)
	{
		return false;
	}
	
	if(strData.length == 2 )
	{
		campo.value = strData+"/";
	}
	
	if(strData.length == 5)
	{
		campo.value = strData+"/";
	}
}

function changeDisplay(state){
	$('div[@id=block]').css('display',state);
	$('div[@id=loading]').css('display',state);
}

function displayError(message){
	if(message == "clear"){
		$("div[@id=message]").html("");
	}else{
		var html = $("div[@id=message]").html();
		$("div[@id=message]").html(html + '<img src="../skins/basic/admin/icons/error.png" width="16" height="16"/> - '+message+'<br>');
	}
	

}