// Scripts
// Autor Victor Braun Rodrigues (vbraun@gosites.com.br)
	// variaveis para rolagem no staus bar
	
	var Message="";
	var place=1;

	// variaveis para rolagem no titulo
	var messagetitle = Message +"          ";
	var titlei="0";	
	var temptitle="";

	// variaveis da função de data por extenso
        hoje = new Date();
        dia = hoje.getDate();
        dias = hoje.getDay();
        mes = hoje.getMonth();
        ano = hoje.getYear();
        if (ano < 2000) {
			ano = 1900 + ano};
        function CriaArray (n) {
        this.length = n };
        NomeDia = new CriaArray(7);
        NomeDia[0] = "Domingo";
        NomeDia[1] = "Segunda-feira";
        NomeDia[2] = "Ter&ccedil;a-feira";
        NomeDia[3] = "Quarta-feira";
        NomeDia[4] = "Quinta-feira";
        NomeDia[5] = "Sexta-feira";
        NomeDia[6] = "S&aacute;bado";
		NomeMes = new CriaArray(12);
        NomeMes[0] = "janeiro";
        NomeMes[1] = "fevereiro";
        NomeMes[2] = "março";
        NomeMes[3] = "abril";
        NomeMes[4] = "maio";
        NomeMes[5] = "junho";
        NomeMes[6] = "julho";
        NomeMes[7] = "agosto";
        NomeMes[8] = "setembro";
        NomeMes[9] = "outubro";
        NomeMes[10] = "novembro";
        NomeMes[11] = "dezembro";

function WriteDate(){
	document.write (NomeDia[dias]+", " + dia + " de " + NomeMes[mes] + " de "  + ano)
}

function ScrollTo(name){
  document.anchors.item(name).scrollIntoView();
}

function bookmark(url,titulo){
	window.external.AddFavorite('http://' + url, titulo);
}

function maxjan(){ 
  var window_height = window.screen.availHeight; 
  var window_width = window.screen.availWidth; 
  window.resizeTo(window_width,window_height); 
  var left_point = 0; 
  var top_point = 0; 
  window.moveTo(left_point, top_point); 
}

function SetHelp(campo,txt) { campo.innerText = txt ; }

function Legenda(campo,txt) { campo.innerText = txt ; }

function trimAll(sString) {
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);}
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);}
	return sString;
}

function antipopup(text){
            janela=window.open("","","menubar=no,scrollbars=no,status=no,toolbar=no,location=no,directories=no,top=20,resizable=no,width=1,height=1");
            //interceptacao de erro na abertura da janela            
            if(janela == null) { alert(text); return; }
            //fim
            janela.close();
 			}
 			
function mostra(bloco){ 
	 blocodiv 	= document.getElementById(bloco);    
     if (blocodiv.style.display== ''||blocodiv.style.display== 'block'){
  		 blocodiv.style.display	= 'none';}
     else {
  		blocodiv.style.display	= 'block';}
}

function AbrirJanela(pag,w,h,sb){
	window.open(pag,"WIN","toolbar=no,scrollbars="+ sb +",width="+ w +",height="+ h +",resizable=no,directories=no,location=no,menubar=no")
}

function AbreJanela(pag,nome,w,h,sb){
	window.open(pag,nome,"toolbar=no,scrollbars="+ sb +",width="+ w +",height="+ h +",resizable=no,directories=no,location=no,menubar=no")
}

function AbreGaleria(path,txt) 
{
	if(txt==undefined){
		txt='';
	}	

	window.open("galeria_path.asp?path=../galeria/" + path + "/&titulo="+txt,"galeria","toolbar=no,scrollbars=no,width=650,height=500,resizable=no,directories=no,location=no,menubar=no");
}


function popup(pag,w,h){
	window.open(pag,"popup","toolbar=no,scrollbars=no,width="+ w +",height="+ h +",resizable=no,directories=no,location=no,menubar=no")
}

function rolarodape() {
	window.status=Message.substring(0, place);
	if (place >= Message.length) 
	{
		place=1;
		window.setTimeout("scrollOut()",300); 
	} else 
	{
		place++;
		window.setTimeout("rolarodape()",100); 
   	} 
}
		
function scrollOut() {
	window.status=Message.substring(place, Message.length);
	if (place >= Message.length) 
	{
		place=1;
		window.setTimeout("rolarodape()", 100);
	} else
	{
		place++;
		window.setTimeout("scrollOut()", 100);
   	}
}

function rolartitulo(){
	document.title=temptitle+ messagetitle.charAt(titlei)  
	temptitle=temptitle+ messagetitle.charAt(titlei)       
	titlei++					    
	if(titlei== messagetitle.length) {
	titlei="0"					  
	temptitle=""}
	setTimeout("rolartitulo()",90) 		   		
}

function checaCPF (CPF) {
	if (CPF.length != 11 || CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;
	soma = 0;
	for (i=0; i < 9; i ++)
		soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(9)))
		return false;
	soma = 0;
	for (i = 0; i < 10; i ++)
		soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		resto = 0;
	if (resto != parseInt(CPF.charAt(10)))
		return false;
	return true;
 }

function isCPF (CPF) {
	if (CPF.length < 11) return false;
	if (CPF.length == 14) {
	if ((CPF.charAt(3) != ".") || (CPF.charAt(7) != ".") || (CPF.charAt(11) != "-")) return false;
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CPF.substring(0,3);
			x += CPF.substring(4,7);
			x += CPF.substring(8,11);
			x += CPF.substring(12,14);
			CPF = x;	
		} else {
			CPF = CPF.replace(".","");
			CPF = CPF.replace(".","");
			CPF = CPF.replace("-","");
		}
	}	
	if (CPF == "00000000000" || CPF == "11111111111" ||
		CPF == "22222222222" ||	CPF == "33333333333" || CPF == "44444444444" ||
		CPF == "55555555555" || CPF == "66666666666" || CPF == "77777777777" ||
		CPF == "88888888888" || CPF == "99999999999")
		return false;		
	var nonNumbers = /\D/;
	if (nonNumbers.test(CPF)) return false;
	soma = 0;
	for (i=0; i < 9; i ++) soma += parseInt(CPF.charAt(i)) * (10 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)	resto = 0;
	if (resto != parseInt(CPF.charAt(9))) return false;
	soma = 0;
	for (i = 0; i < 10; i ++) soma += parseInt(CPF.charAt(i)) * (11 - i);
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)	resto = 0;
	if (resto != parseInt(CPF.charAt(10))) return false;
	return true;
 }

function isCNPJ(CNPJ) {
		if (CNPJ.length < 14)	return false;
		if (CNPJ.length == 18){
			if ((CNPJ.charAt(2) != ".") || (CNPJ.charAt(6) != ".") || (CNPJ.charAt(10) != "/") || (CNPJ.charAt(15) != "-"))	return false;
			//substituir os caracteres que nao sao numeros
			if(document.layers && parseInt(navigator.appVersion) == 4){
				x = CNPJ.substring(0,2);
				x += CNPJ.substring(3,6);
				x += CNPJ.substring(7,10);
				x += CNPJ.substring(11,15);
				x += CNPJ.substring(16,18);
				CNPJ = x;	
			} else {
				CNPJ = CNPJ.replace(".","");
				CNPJ = CNPJ.replace(".","");
				CNPJ = CNPJ.replace("-","");
				CNPJ = CNPJ.replace("/","");
			}
		}
		
		var nonNumbers = /\D/;
		if (nonNumbers.test(CNPJ)) return false;	
		var a = [];
		var b = new Number;
		var c = [6,5,4,3,2,9,8,7,6,5,4,3,2];
		for (i=0; i<12; i++){
			a[i] = CNPJ.charAt(i);
			b += a[i] * c[i+1];
		}
		if ((x = b % 11) < 2) { a[12] = 0 } else { a[12] = 11-x }
		b = 0;
		for (y=0; y<13; y++) {
			b += (a[y] * c[y]); 
		}
		if ((x = b % 11) < 2) { a[13] = 0; } else { a[13] = 11-x; }
		if ((CNPJ.charAt(12) != a[12]) || (CNPJ.charAt(13) != a[13])) return false;
		return true;
}

function isCEP(CEP) {
	if (CEP.length < 8 ) return false;
	if (CEP.length == 9 ) {
		if (CEP.charAt(5) != "-") return false;
		if(document.layers && parseInt(navigator.appVersion) == 4){
			x = CEP.substring(0,5);
			x += CEP.substring(6,3);
			CEP = x;	
		} else {
			CEP = CEP.replace("-","");
		}
	}
	var nonNumbers = /\D/;
	if (nonNumbers.test(CEP)) return false;
	return true;
}

function formataCNPJ(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr	= vr.replace(".", "");
   vr 	= vr.replace(".", "");
   vr 	= vr.replace("/", "");
   vr 	= vr.replace("-", "");
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 6)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2, tam);
      if (tam >= 6 && tam < 9)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,tam-5);
      if (tam >= 9 && tam < 13)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,tam-8);
      if (tam >= 13 && tam < 15)
         Campo.value = vr.substr(0,2) + '.' + vr.substr(2,3) + '.' + vr.substr(5,3) + '/' + vr.substr(8,4)+ '-' + vr.substr(12,tam-12);
      }
}

function formataCPF(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr 	= vr.replace(".", "");
   vr 	= vr.replace(".", "");
   vr 	= vr.replace("-", "");
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam > 3 && tam < 7)
         Campo.value = vr.substr(0,3) + '.' + vr.substr(3, tam);
      if (tam >= 7 && tam < 10)
         Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);
      if (tam >= 10 && tam < 14)
         Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9); 
      }
}


function formataData(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr 	= vr.replace("/", "");
   vr 	= vr.replace("/", "");
   vr 	= vr.replace("/", "");
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam > 2 && tam < 5)
         Campo.value = vr.substr(0,2) + '/' + vr.substr(2, tam);
      if (tam >= 5 && tam < 10)
         Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,tam-4);

      }
}

function formataDataHora(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr 	= vr.replace("/", "");
   vr 	= vr.replace("/", "");
   vr 	= vr.replace("/", "");
   vr 	= vr.replace(" ", "");
   vr 	= vr.replace(":", "");
   
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam >= 11){ Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + " " + vr.substr(8,2) + ":" + vr.substr(10,2);}
	  else{
	  if (tam >= 9){Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4) + " " + vr.substr(8,tam);}
	  else{
	  if (tam >= 5){Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,tam);}
	  else{if (tam > 3){Campo.value = vr.substr(0,2) + '/' + vr.substr(2, tam);}		
	}}}}
}

function formataFone(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr 	= vr.replace("(", "");
   vr 	= vr.replace(")", "");
   vr 	= vr.replace("-", "");
   vr 	= vr.replace(" ", "");
   
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam >= 7){ Campo.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,4) + '-' + vr.substr(6,4);}
	  else { if (tam >= 3){Campo.value = '(' + vr.substr(0,2) + ') ' + vr.substr(2,4) + '-' + vr.substr(6,4)}}
	}
}

function formataCEP(Campo, teclapres){
   var tecla = teclapres.keyCode;
   var vr = new String(Campo.value);
   vr 	= vr.replace(".", "");
   vr 	= vr.replace(".", "");
   vr 	= vr.replace("-", "");
   tam 	= vr.length + 1 ;
   if (tecla != 9 && tecla != 8){
      if (tam > 5 && tam < 8)
         Campo.value = vr.substr(0,5) + '-' + vr.substr(5, tam-5);
      }
}

function isData(vData)
{
	if (vData==0) return true;
	var Parte=vData.split("/"); 
	Ano=Parte[2];
	Mes=Parte[1];
	Dia=Parte[0];
	if (Parte.join("").length != 8) return false;
	if (isNaN(Parte.join(""))) return false;
	if (Ano < 1500) return false;
	Data1 = new Date(Ano,Mes-1,Dia);
	if (Mes < 1 || Mes > 12) return false;
	if (Dia < 1 || Dia > 31) return false;
	if (Dia > 30 || (Dia > 28 && Mes == 02 )) {
 		D = Data1.getDate();
 		if (D < Dia) return false;
	}
	return true;
}

function isDataHora(vData)
{
	if (vData.length == 10) return isData(vData);
	if (vData==0) return true;
	var vTodo=vData.split(" ");
	var vHora=vTodo[1].split(":");
	Hora=vHora[0];
	Min =vHora[1];
	var Parte=vTodo[0].split("/"); 
	Ano=Parte[2];
	Mes=Parte[1];
	Dia=Parte[0];
	if (Parte.join("").length != 8) return false;
	if (vHora.join("").length != 4) return false;
	if (isNaN(Parte.join(""))) return false;
	if (isNaN(vHora.join(""))) return false;
	if (Hora>24 || Min>60) return false;
	if (Ano < 1500) return false;
	Data1 = new Date(Ano,Mes-1,Dia);
	if (Mes < 1 || Mes > 12) return false;
	if (Dia < 1 || Dia > 31) return false;
	if (Dia > 30 || (Dia > 28 && Mes == 02 )) {
 		D = Data1.getDate();
 		if (D < Dia) return false;
	}
	return true;
}

function qPalavras(txt){
	var formcontent=txt;

	formcontent=formcontent.split(" ");
	return formcontent.length;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}

function isValidEmail(email, required) {
    email=email.replace(/^\s*|\s*$/g,"");
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    }
	
    return true;
}

function limpaValor(valor, validos) {
	// retira caracteres invalidos da string
	var result = "";
	var aux;
	for (var i=0; i < valor.length; i++) {
		aux = validos.indexOf(valor.substring(i, i+1));
		if (aux>=0) {
			result += aux;
		}
	}
	return result;
}

function formataValor(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = limpaValor(campo.value,"0123456789");
	tam = vr.length;
	if (tam < tammax && tecla != 8){ tam = vr.length + 1 ; }
	if (tecla == 8 ) { tam = tam - 1 ; }
	if ( tecla == 8 || tecla >= 48 && tecla <= 57 || tecla >= 96 && tecla <= 105 ){
		dec=2
		if ( tam <= dec ){ campo.value = vr ; }

		if ( (tam > dec) && (tam <= 5) ){
			campo.value = vr.substr( 0, tam - 2 ) + "," + vr.substr( tam - dec, tam ); }
		if ( (tam >= 6) && (tam <= 8) ){
			campo.value = vr.substr( 0, tam - 5 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); }
		if ( (tam >= 9) && (tam <= 11) ){
			campo.value = vr.substr( 0, tam  - 8 ) + "." + vr.substr( tam  - 8, 3 ) +  "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); }
		if ( (tam >= 12) && (tam <= 14) ){
			campo.value = vr.substr( 0, tam - 11 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - dec, tam ); }
		if ( (tam >= 15) && (tam <= 17) ){
			campo.value = vr.substr( 0, tam - 14 ) + "." + vr.substr( tam - 14, 3 ) + "." + vr.substr( tam - 11, 3 ) + "." + vr.substr( tam - 8, 3 ) + "." + vr.substr( tam - 5, 3 ) + "," + vr.substr( tam - 2, tam ); }
	} 

}

function maxLength(textAreaField, limit) {
	var ta = document.getElementById(textAreaField);
	if (ta.value.length >= limit) {
		ta.value = ta.value.substring(0, limit-1);
 	}
}