// JavaScript Document
// Validação de campo vazio. Param: campo, nome do campo para exibição
function validaBranco( campo, nome ) {	

	campo = document.getElementById( campo );

	if( trim( campo.value ) == "" ) {
		if( nome != null )
			alert( "O campo " + nome.toUpperCase() + " deve ser preenchido." );
		else
			alert( "O campo " + campo.name.toUpperCase() + " deve ser preenchido." );
		
		campo.focus();
		return false;
	}
	return true;
}

function validaBranco2( campoParam ){
	var campo = document.getElementById(campoParam);
	
	if( campo.value == "" ){
		return false;
	}
	return true;
}

var loginacess = 0;
var senhaacess = 0;

function limpaLogin() {
	if(loginacess == 0) {
		document.getElementById("nome_news").value = "";
		loginacess = 1;
	}
}

function limpaEmail() {
	if(senhaacess == 0) {
		document.getElementById("email_news").value = "";
		senhaacess = 1;
	}
}

function restituicampo(campo) {
	if(!document.getElementById(campo).value && campo == "nome_news") {
		document.getElementById(campo).value = "Nome"
		loginacess = 0;
	}
	if(!document.getElementById(campo).value && campo == "email_news") {
		document.getElementById(campo).value = "E-mail"
		//document.getElementById("senha").type = "text";
		senhaacess = 0;
	}
}
//Controles de mudança de cor de fundo de tabela
function mover(local){
	local.bgColor = "#FCE600";
}
function mout(local){
	local.bgColor = "";
}
//Link para o destino do flash
function destLogo(){
	document.location.href = "default.asp";
}

//Exibe ou esconde pelo Id
function mostraDados( pri ) {
  var element = document.getElementById(pri);

  if (element.style.display == 'none') 
    element.style.display = '';
  else
    element.style.display = 'none';
}

function CorFundo(destino, oquefazer) {
	if(oquefazer == 1){
		document.getElementById(destino).style.border = "1px solid #006600";
		document.getElementById(destino).style.backgroundColor = "#FDFDFD";
	} else {
		document.getElementById(destino).style.border = "0px solid #FFFFFF";
		document.getElementById(destino).style.backgroundColor = "#FFFFFF";
	}
}

//Validação de email
function valida_email(nomeCampo)
{
	var elemento = document.getElementById(nomeCampo);

	var emailPattern = /^[\w-]+(\.[\w-]+)*@(([A-Za-z\d][A-Za-z\d-]{0,61}[A-Za-z\d]\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;

	var resultado = emailPattern.test(elemento.value); 
	
	if(!resultado) {
		alert("O e-mail digitado é inválido.");
		elemento.focus();
		return false
	}
	
	return true;	
}

//Validação de Telefone
function validaTelefone( campoPar ) {
	campo = document.getElementById( campoPar );
	var valor='';
	var digito = false;
	a = campo.value;

	if ( a != "" ) {
		num = a.length;
		for (f=0;f<num;f++)
		{
			if (parseInt(a.substr(f,1)) || a.substr(f,1)=='0') 
			if ((a.substr(f,1) != '0') || digito) valor = valor + '' + a.substr(f,1);
			if (parseInt(a.substr(f,1)) && a.substr(f,1) != '0') digito = true;
		};
		num = valor.length;
		if (num < 9 || num > 10)
		{
			alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
			campo.focus();
			return false;
		}
		else
		{
			if (num == 9) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-7, 3) + '-' + valor.substr(num-4, 4);
			if (num == 10) valor = '(' + valor.substr(0, 2) + ') ' + valor.substr(num-8, 4) + '-' + valor.substr(num-4, 4);
			campo.value = valor;
		}
	}
	else{
		alert ('- Número de telefone inválido. \nEntre com o DDD e o número do telefone.\nExemplo: (11) 1234-5678');
		campo.focus();
		return false;
	}
	return true;
}
//Função para validar CNPJ
function valida_cnpj(campoform) {
	num_cpf = '';
	campo = campoform.value;

	if (!campo) return true;
	
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 14)
		{
		d1= 0;
		d4= 0;
		xx= 1;
		for (nCount = 0;nCount< num_cpf.length-2;nCount++)
			{
			if (xx < 5) fator= 6 - xx; else fator= 14 - xx;
			d1 = d1 + num_cpf.charAt(nCount) * fator;
			if (xx < 6) fator = 7 - xx; else fator = 15 - xx;
			d4 = d4 + num_cpf.charAt(nCount) * fator;
	    	xx = xx+1;
			}
		resto = d1%11;
		if (resto < 2) digito1 = 0; else digito1 = 11 - resto;
		d4 = d4 + 2 * digito1;
		resto = d4%11;
		if (resto < 2) digito2 = 0; else digito2 = 11 - resto;
		Check = digito1+''+digito2;
        if (Check != num_cpf.substring(num_cpf.length-2,num_cpf.length))
			{
			alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else 
			{
			campoform.value= num_cpf.substring(0,2)
			+ '.' + num_cpf.substring(2,5) + '.' + num_cpf.substring(5,8) + '/' + num_cpf.substring(8,12)
			+ '-' + num_cpf.substring(12,14);
			return true;
			};
		}
	else
		{
		alert ('- CNPJ inválido. \nEntre com o CNPJ correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
		}
}
//Valida CPF
function valida_cpf(campoform) {
	num_cpf = '';
	campo = campoform.value;
	if (campo == "00000000000" || campo == "11111111111" || campo == "22222222222" || campo == "33333333333" || campo == "44444444444" || campo == "55555555555" || campo == "66666666666" || campo == "77777777777"  || campo == "88888888888"  || campo == "99999999999"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (campo == "000.000.000-00" || campo == "111.111.111-11" || campo == "222.222.222-22" || campo == "333.333.333-33" || campo == "444.444.444-44" || campo == "555.555.555-55" || campo == "666.666.666-66" || campo == "777.777.777-77"  || campo == "888.888.888-88"  || campo == "999.999.999-99"){
		alert ('- CPF inválido. \nEntre com o CPF correto.');
    	campoform.value = '';
    	campoform.focus();
		return false;
	}
	if (!campo) return true;
	for (i=0;i<campo.length;i++)
		{
		resposta=campo.charAt(i) ;
		num = parseFloat(resposta);
		if (resposta==''+num) num_cpf=num_cpf+resposta; 
		}

	if (num_cpf.length == 11)
		{
		soma = 0;
		for (i=0; i < 9; i ++) soma += parseInt(num_cpf.charAt(i)) * (10 - i);
		resto = 11 - (soma % 11);
		if (resto == 10 || resto == 11)	resto = 0;
		soma = 0;
		for (i = 0; i < 10; i ++) soma += parseInt(num_cpf.charAt(i)) * (11 - i);
		resto2 = 11 - (soma % 11);
		if (resto2 == 10 || resto2 == 11) resto2 = 0;
		if ((resto != parseInt(num_cpf.charAt(9))) || (resto2 != parseInt(num_cpf.charAt(10))))
			{
			alert ('- CPF inválido. \nEntre com o CPF correto.');
    		campoform.value = '';
    		campoform.focus();
			return false;
			}
		else
			{
			campoform.value= num_cpf.substring(0,3) + '.' + num_cpf.substring(3,6) + '.'
			+ num_cpf.substring(6,9) + '-' + num_cpf.substring(9,11);
			return true;
			}
		}
	else
		{
		alert ('- CPF inválido. \nEntre com o CPF correto.');
   		campoform.value = '';
   		campoform.focus();
		return false;
		}	
}
//Validação do cadastro de senha
function validaSenha(){
	if ((document.getElementById("senhaatual").value == document.getElementById("confSenha").value) && (document.getElementById("senhaatual").value.length == document.getElementById("confSenha").value.length)) {		
		return false;
	} else {
		return true;
		document.getElementById("senhaatual").value = "";
		document.getElementById("confSenha").value = "";
		document.getElementById("senha").focus();
	}
}
function validaData(campo){
	var data = document.getElementById(campo);

	hoje = new Date();
    anoAtual = hoje.getFullYear();
   	barras = data.value.split("/");
    if (barras.length == 3){
		dia = barras[0];
        mes = barras[1];
   	    ano = barras[2];
        resultado = (!isNaN(dia) && (dia > 0) && (dia < 32)) && (!isNaN(mes) && (mes > 0) && (mes < 13)) && (!isNaN(ano) && (ano.length == 4) );
   	    if (!resultado) {
			alert("O formato da data é invalido!");
            data.focus();
            return false;
		}
	}
	else {
		alert("O formato da data é invalido!");
		data.focus();
        return false;
   	}
	return true;
}

function trim(txt){  
	var retirar = ' ';
	var retorno = '';

	for(i=0;i<txt.length;i++){  
		if(retirar.indexOf(txt.substr(i,1)) == -1){  
			retorno += txt.substr(i,1);
		}
	}  
	return retorno;
}

function AjaxClass(url, local, metodo, params)
{
	this.url = url;
	this.local = local;
	this.metodo = metodo;
	this.params = params;
	this.objAjax = this.contruct();
}

AjaxClass.prototype.contruct = function(){
	var xml;
	if (window.XMLHttpRequest)  {
		xml = new XMLHttpRequest();
	} else {
		xml = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xml;
}

AjaxClass.prototype.formStandard = function(b){
	do {
		b = b.replace(/ /,"|!|");
	} while(b.search(/ /) > 0);
	return b;
}

AjaxClass.prototype.ajaxManual = function(){
	var local = this.local;
	var obj = this.objAjax;
	if(!this.params) this.params = null;
	if(!this.metodo) this.metodo = "GET";
	this.metodo = this.metodo.toUpperCase();	
	
	this.objAjax.open(this.metodo,this.url, true);	
	
	if(this.metodo == "POST" && this.params){
		this.objAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.objAjax.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
		this.objAjax.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
		this.objAjax.setRequestHeader("Pragma", "no-cache");
	}
	
	this.objAjax.send(this.params);
}

AjaxClass.prototype.ajax = function(){
	var local = this.local;
	var obj = this.objAjax;
	if(!this.params) this.params = null;
	if(!this.metodo) this.metodo = "GET";
	this.metodo = this.metodo.toUpperCase();
		
	this.objAjax.open(this.metodo,this.url, true);	
	
	if(this.metodo == "POST" && this.params){
		this.objAjax.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		this.objAjax.setRequestHeader("Cache-Control","no-store, no-cache, must-revalidate");
		this.objAjax.setRequestHeader("Cache-Control","post-check=0, pre-check=0");
		this.objAjax.setRequestHeader("Pragma", "no-cache");
	}
	
	this.objAjax.onreadystatechange = function(){	
		if (obj.readyState==4) {
			if (obj.status==200) {
				document.getElementById(local).innerHTML = obj.responseText;
			} else {
				document.getElementById(local).innerHTML = "Ocorreu um erro no momento de carregar a página.<br> Descrição do erro: " + obj.statusText;
			}
		}
	}
	this.objAjax.send(this.params);
}

function incluiItemLista(novoText, novoValue, local){
	var newOpt = document.createElement('option');
	newOpt.text = novoText;
	newOpt.value = novoValue;
	var LocalItemDest = local;
	if(window.navigator.appName == "Microsoft Internet Explorer") {
		document.getElementById(LocalItemDest).add(newOpt); // IE only
	} else {
		document.getElementById(LocalItemDest).options[document.getElementById(LocalItemDest).length] = new Option(novoText, novoValue);
	}
}

function barraProgressoUpload(progressId) {
	window.document.cookie = "RefreshProgressID=TRUE";

	Param = "scrollbars=0,resizable=0, toolbar=0, status=0, menubar=0, width=400, height=100, top= "+ String(window.screen.height / 2 - 50) +", LEFT="+ String(window.screen.width / 2 - 200);
	window.open("ProgressBar.asp?ProgressID="+ progressId , null, Param);
}


function inicializaBarraProgresso(progressId) {
	var CookiesOff = true;
	
	//if cookies are not enabled let user know that browser must be supporting cookies for this demo to run ok
	CookiesOff = verificaSuportaCookies();		
		
	if( progressId != -1 && String(window.document.cookie).indexOf("RefreshProgressID=TRUE") != -1 )
		window.location.reload(1);

	window.document.cookie = "RefreshProgressID=FALSE";

	//by setting the ProgressID to -1 we make sure progress bar will not be displayed by the Upload function below	
	if (CookiesOff == true){	
		progressId = -1;
	}	
}

function verificaSuportaCookies() {
	//checks to see if browser has cookie support enabled,
	//	if not an alert box is displayed, letting user know we need cookies
	//also returns true if cookies disabled, otherwise false is returned	
	var blnCookiesSupported = false;
	
	//set the cookie
	document.cookie = 'testit' + "=" + escape('hi') + ";"
	
	//now attempt to retrieve the cookie
	var aCookie = document.cookie.split(";");

	for (var i=0; i < aCookie.length; i++)
	{ 
		var aCrumb = aCookie[i].split("=");

		if ('testit' == aCrumb[0]) {
			blnCookiesSupported = true; 
		}	
	}
	
	return blnCookiesSupported
}

