// funcoes d validação

function FormataCampo(Campo,teclapres,mascara){

	//pegando o tamanho do texto da caixa de texto com delay de -1 no event

	//ou seja o caractere que foi digitado não será contado.

	strtext = Campo.value

	tamtext = strtext.length

	//pegando o tamanho da mascara

	tammask = mascara.length

	//criando um array para guardar cada caractere da máscara

	arrmask = new Array(tammask)	

	//jogando os caracteres para o vetor

	for (var i = 0 ; i < tammask; i++){

		arrmask[i] = mascara.slice(i,i+1)

	} 

	//alert (teclapres.keyCode)

	//começando o trabalho sujo

	if (((((arrmask[tamtext] == "#") || (arrmask[tamtext] == "9"))) || (((arrmask[tamtext+1] != "#") || (arrmask[tamtext+1] != "9"))))){

		if ((teclapres.keyCode >= 37 && teclapres.keyCode <= 40)||(teclapres.keyCode >= 48 && teclapres.keyCode <= 57)||(teclapres.keyCode >= 96 && teclapres.keyCode <= 105)||(teclapres.keyCode == 8)||(teclapres.keyCode == 9) ||(teclapres.keyCode == 46) ||(teclapres.keyCode == 13)){

			Organiza_Casa(Campo,arrmask[tamtext],teclapres.keyCode,strtext)		

		}

		else{

			Detona_Event(Campo,strtext)

		}

	}

	else{//Aqui funcionaria a mascara para números mas eu ainda não implementei

		if ((arrmask[tamtext] == "A"))	{

			charupper = event.valueOf()

			//charupper = charupper.toUpperCase()

			Detona_Event(Campo,strtext)

			masktext = strtext + charupper 

			Campo.value = masktext

		}

	}

}



// Função abre nova janela

// utilização: 											PROPRIEDADES

//				abrejanela('PAGINA.htm','TITULO','scrollbars=yes,resizable=no,width=500,height=400')"



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 rightTrim(sString) 

{

   while (sString.substring(sString.length-1, sString.length) == ' ')

    {

      sString = sString.substring(0,sString.length-1);

    }

      return sString;

}







function leftTrim(sString) 

{

   while (sString.substring(0,1) == ' ')

     {

       sString = sString.substring(1, sString.length);

     }

   return sString;

}





function formatar(src, mask) 

{

  var i = src.value.length;

  var saida = mask.substring(0,1);

  var texto = mask.substring(i)

if (texto.substring(0,1) != saida) 

  {

	src.value += texto.substring(0,1);

  }

}



function abrejanela(url,nome,propriedades){ 

   window.open(url,nome,propriedades);

}



//Checa o campo E-Mail

function verifica_mail(email){

	var campo = email;

	//se a "@" não existe e é o primeiro caractere 

	if (campo.indexOf("@") < 1) return false

	//se o caracter antes da "@" é "." 

	if(campo.charAt(campo.indexOf("@")-1) == ".") return false;

	//se a última incidência de "." está antes da @ 

	if (campo.lastIndexOf(".") <= campo.indexOf("@")) return false;

	//se o último caracter é ponto,

	if (campo.lastIndexOf(".")  == (campo.length-1)) return false; 

	

	return true;

}



function val_cnpj(numero) {

	//numero = numero1.value;

	dig_1 = 0;

	dig_2 = 0;

	controle_1 = 5;

	controle_2 = 6;



if ( (numero.length != 18)  || (numero.substring(2, 3) != ".") || (numero.substring(6, 7) != ".") || (numero.substring(10, 11) != "/") || (numero.substring(15, 16) != "-") ) {

     return false;

}else{ 

  	   numero = (numero.substring(0, 2)) + "" + (numero.substring(3, 6)) + "" + (numero.substring(7, 10))+ "" + (numero.substring(11, 15)) + "" + (numero.substring(16, 18)) 

	   for ( i=0 ; i < 12 ; i++) {

	        dig_1 = dig_1 + parseFloat(numero.substring(i, i+1) * controle_1);

	          controle_1 = controle_1 - 1;

	        if (i == 3) {

	           controle_1 = 9;

	        }

	   }

	   

	   resto = dig_1 % 11;

	   dig_1 = 11 - resto;

	   

	   if ((resto == 0) || (resto == 1)){

	        dig_1 = 0;

	   }

	   

	   for ( i=0 ; i < 12 ; i++) {

	        dig_2 = dig_2 + parseInt(numero.substring(i, i+1) * controle_2);

	          controle_2 = controle_2 - 1;

	        if (i == 4) {

	           controle_2 = 9;

	        }

	   }

	   

	   dig_2 = dig_2 + (2 * dig_1);

	   resto = dig_2 %11;

	   dig_2 = 11 - resto;

	   

	   if ((resto == 0) || (resto == 1)){

	        dig_2 = 0;

	   }

	   

	   dig_ver = (dig_1 * 10) + dig_2;

	   

	   if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {

	          return false;

	   }

 }

 return true;

}



function FormataCPF(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 >= 10 && tam < 12) {

			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,3) + '-' + vr.substr(9,tam-9);

		} else if (tam >= 7 && tam < 10) {

			Campo.value = vr.substr(0,3) + '.' + vr.substr(3,3) + '.' + vr.substr(6,tam-6);

		} else if (tam > 3 && tam < 7) {

			Campo.value = vr.substr(0, 3) + '.' + vr.substr(3, tam);

		}

	}

}



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);

		}

}



//Valida o cpf 

function val_cpf(numero2) {

  dig_1 = 0;

  dig_2 = 0;

  controle_1 = 10;

  controle_2 = 11;

  lsucesso = 1;

  

  var numero = numero2.substr(0,3) + numero2.substr(4,3) + numero2.substr(8,3) + numero2.substr(12,2);

  

  if (numero == "00000000000" || numero == "11111111111" || numero == "22222222222" || numero == "33333333333" || numero == "44444444444" || numero == "55555555555" || numero == "66666666666" || numero == "77777777777" || numero == "88888888888" || numero == "99999999999") {

  	return false;

  }

  

  if ((numero.length != 11) && (numero.length != 0)) {

     return false;

  }

  else {

     for ( i=0 ; i < 9 ; i++) {

        dig_1 = dig_1 + parseInt(numero.substring(i, i+1) * controle_1);

          controle_1 = controle_1 - 1;

     }

    

     resto = dig_1 % 11;

     dig_1 = 11 - resto;

     if ((resto == 0) || (resto == 1)) {

          dig_1 = 0;

     }

     for ( i=0 ; i < 9 ; i++) {

          dig_2 = dig_2 + parseInt(numero.substring(i, i + 1) * controle_2);

        controle_2 = controle_2 - 1;

     }

     dig_2 = dig_2 + 2 * dig_1;

     resto = dig_2 % 11;

     dig_2 = 11 - resto;

     if ((resto == 0) || (resto == 1)) {

        dig_2 = 0;

     }

     dig_ver = (dig_1 * 10) + dig_2;

     if (dig_ver != parseFloat(numero.substring(numero.length-2,numero.length))) {

          return false;

        }

  }



return true;

}



function abre_impressao(url) {

	window.open(url, "impressao", "width=600,height=450");

}

//Utilização: onkeypress="evita_letra(event)" onKeydown="FormataHora('NOMEDOCAMPO','NOMEDOFORM',event)" maxlength="5"

function FormataHora(campo,formname,teclapres){

	var tecla = teclapres.keyCode;

	vr = document[formname][campo].value;

	vr = vr.replace( ":", "" );

	tam = vr.length + 1;

	

	if ( tecla != 9 && tecla != 8 ){

		if ( tam > 2 && tam < 5 ){

			document[formname][campo].value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );

		}

	}

}

// FUNÇÃO QUE AUTOCOMPLETA DATA COM '/'

// COMO UTILIZAR:    onkeydown="FormataData(this.name,this.form.name,event);" 

function FormataData(campo,formname,teclapres) { // Máscara para os campos de data

	var tecla = teclapres.keyCode;

	vr = document[formname][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 )

			document[formname][campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );

		if ( tam >= 5 && tam <= 10 )

			document[formname][campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 ); 

	}

}

// COMO UTILIZAR: onkeypress="evita_letra2(event)"

// Não permite digitar letras em um input type=text

function evita_letra2(tecla) {

	if (tecla.keyCode < 48 || tecla.keyCode > 57) 

		tecla.returnValue = false;

}

function MM_openBrWindow(theURL,winName,features) { //v2.0

  window.open(theURL,winName,features);

}



function ToggleAll(checked) {

    len = document.listagem.elements.length;

    var i = 0;

    for(i = 0; i < len; i++) {

        document.listagem.elements[i].checked = checked;

    }

}



function in_array(s,a) {

  for (i = 0; i < a.lenght; i++) {

	  /*if (a[i] == s) {

	  	return true;

	  }*/

	  alert(a[i]+'=='+s.name);

  }

  //return false;

}





// HINT

// Example:

// onMouseOver="hint('tool tip text here')";

// onMouseOut="hint()";

// -or-

// onMouseOver="hint('more good stuff', '#FFFF00', 'orange')";

// onMouseOut="hint()"; 

/*



MOVE this to the <body>:

<div id="hintLayer" style="position:absolute; visibility: hidden"></div>

<script language="JavaScript"><!--

initToolTips(); //--></script>

*/
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var ie4 = document.all;
offsetX = 0;
offsetY = 20;
var hintSTYLE="";
function initToolTips(){
  if(ns4||ns6||ie4)
  {
    if(ns4) hintSTYLE = document.hintLayer;
    else if(ns6) hintSTYLE = document.getElementById("hintLayer").style;
    else if(ie4) hintSTYLE = document.all.hintLayer.style;
    if(ns4) document.captureEvents(Event.MOUSEMOVE);
    else
    {
      hintSTYLE.visibility = "visible";
      hintSTYLE.display = "none";
    }
    document.onmousemove = moveToMouseLoc;
  }
}

function hint(msg, fg, bg)
{
  if(hint.arguments.length < 1) // hide
  {
    if(ns4) hintSTYLE.visibility = "hidden";
    else hintSTYLE.display = "none";
  }
  else // show
  {
    if(!fg) fg = "#777777";
    if(!bg) bg = "#FFFFFF";
    var content =
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + fg + '"><td>' +
    '<table border="0" cellspacing="0" cellpadding="1" bgcolor="' + bg +
    '"><td align="center"><font face="sans-serif" color="' + fg +
    '" size="-2">&nbsp\;' + msg +
    '&nbsp\;</font></td></table></td></table>';
    if(ns4)
    {
      hintSTYLE.document.write(content);
      hintSTYLE.document.close();
      hintSTYLE.visibility = "visible";
    }
    if(ns6)
    {
      document.getElementById("hintLayer").innerHTML = content;
      hintSTYLE.display='block'
    }
    if(ie4)
    {
      document.all("hintLayer").innerHTML=content;
      hintSTYLE.display='block'
    }
  }
}

function moveToMouseLoc(e){
  if(ns4||ns6){
    x = e.pageX;
    y = e.pageY;
  }
  else
  {
    x = event.x + document.body.scrollLeft;
    y = event.y + document.body.scrollTop;
  }
  hintSTYLE.left = x + offsetX;
  hintSTYLE.top = y + offsetY;
  return true;
}

// formulario , 'array de valores obrigatorios '
function obrigatorio(ffff,ele)
{//começa valida
  var c=ele.split(',');
  for(x = 0; x<ffff.elements.length; x++)//varre o formulário
  {
	for(j=0;j<c.length;j++){//varre os obrigatórios
	var formul=ffff.elements[x];
		if(formul.name==c[j] &&  leftTrim(formul.value)==''){//se tem o elemento no formulário
			alert('O campo '+document.getElementById(formul.name+"_label").innerHTML+' é obrigatorio');
			ffff.elements[formul.name].focus();
			return false;
		}
	}//fim for
  }//fim for
  return true;
}//fim valida


// formata campo valor
function FormataValor(tammax,teclapres, tf) {
	var tecla = teclapres.keyCode;
	vr = tf.value;
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( ",", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	vr = vr.replace( ".", "" );
	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 ){
		if ( tam <= 2 ){
	 		tf.value = vr ; }
	 	if ( (tam > 2) && (tam <= 5) ){
	 		tf.value = vr.substr( 0, tam - 2 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 6) && (tam <= 8) ){
	 		tf.value = vr.substr( 0, tam - 5 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 9) && (tam <= 11) ){
	 		tf.value = vr.substr( 0, tam - 8 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 12) && (tam <= 14) ){
	 		tf.value = vr.substr( 0, tam - 11 ) + '.' + vr.substr( tam - 11, 3 ) + '.' + vr.substr( tam - 8, 3 ) + '.' + vr.substr( tam - 5, 3 ) + ',' + vr.substr( tam - 2, tam ) ; }
	 	if ( (tam >= 15) && (tam <= 17) ){
	 		tf.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 ) ;}
	}
}

//FUNÇÃO PARA MOSTRAR OU ESCONDER DIV OU ID
function mostra(div){
	if(document.getElementById(div).className=='mostra'){
		document.getElementById(div).className = 'esconde';
	}else{
		document.getElementById(div).className = 'mostra';
	}

}  

// logar no webmail... multidominio
function webmail(form){
	var url="http://"+document.getElementById('dominiomail').value+":2095/login/";
	form.action=url;
	form.submit();
}
// logar no cpanel... multidominio
function cpanel(form){
	var url="http://"+document.getElementById('dominiocpanel').value+":2082/login/";
	form.action=url;
	form.submit();
}


//funcao de abrir chat
function abrechat(){
	abrejanela('http://sitefique.com/chat.html','Galeria','width=220,height=230,screenX=100,screenY=50,scrollbars=no,resizable=no');
}