function isFieldBlank(theField)
{
  var inStr = theField.value;
  var inLen = inStr.length;
      
  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)

    if (ch != " ")
      return (false);   
  }

  //Permissoes de seguranca nao deixavam a atribuicao funcionar 
  inStr = "";
  return (true);
}

function isNumber(theNumber)
{
  inStr = theNumber.value;
  inLen = inStr.length;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)
  
    if ((ch < "0") || (ch > "9"))
      return (false);
  }
  
  return (true);
}

function isColor(theColor)
{
  inStr = theColor.value;
  inLen = inStr.length;
  
  if (inLen != 7)
    return(false);

  var hexa = '0123456789ABCDEFabcdef';
  
  var ch = inStr.substring(0,1);
  
  if (ch != '#')
    return(false)
    
  for(var i = 1; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)
  
    if (hexa.indexOf(ch,0) == -1)
      return (false);
  }
  
  return (true);
}

function isTime(theTime)
{
  var i = 0;
  var qcaracter = 2;
  inStr = theTime.value;
  if (inStr.length > 5){
   return false;
  }
  var ch =  inStr.substring(0,2)
  while ( i < 4)
  {
    i++;
    if (i == 1)  
      if ((ch < "00")  || (ch > "23"))
        return (false)
      else
        ch = inStr.substring(2,3);

    if (i == 2)
      if (ch != ":")
        return (false)
      else
        ch = inStr.substring(3,5);

    if (i == 3) 
      if ((ch < "00")  || (ch > "59"))
        return (false);
  }

  return (true);
}

function isFone(theFone)
{
  inStr = theFone.value;
  inLen = inStr.length;

  var ContAbr = 0;
  var ContFec = 0;
  var PosAbr  = 0;
  var PosFec  = 0;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1);
  
    if ((ch != "0") && (ch != "1") && (ch != "2") && (ch != "3") && (ch != "4") && (ch != "5") && (ch != "6") && (ch != "7") && (ch != "8") && (ch != "9") && (ch != "(") && (ch != ")") && (ch != " "))
      return (false);

    if (ch == "(")
    {
      ContAbr = ContAbr + 1;
      PosAbr  = i;
    }
    
    if (ch == ")")
    {
      ContFec = ContFec + 1;
      PosFec  = i;
    }
  }
  
  if (ContAbr == 0)
    return (false);
  
  if (ContAbr > 0 || ContFec > 0)
  {
//    if (ContAbr > 1 || ContFec > 1)
//      return (false);

    if (ContAbr != ContFec)
      return (false);
  }

  if (PosAbr > PosFec)
    return (false);

  if ((PosFec + 1 )== inLen)
    return (false);

  return (true);
}

function dataValida(data,formato)
{
  if (data.length < 10) 
  {   
    return (false);
  }
  myRe = new RegExp;

  if (formato == 1)
    myRe=/([0-2][0-9]|3[01])\/(0[1-9]|1[0-2])\/([12][0-9]{3,3})/ig;
  else if (formato == 2)
    myRe=/(0[1-9]|1[0-2])\/([0-2][0-9]|3[01])\/([12][0-9]{3,3})/ig;
  else if (formato == 3)
    myRe=/([12][0-9]{3,3})\/(0[1-9]|1[0-2])\/([0-2][0-9]|3[01])/ig;
  else
    return(false);

  if (! eval("myRe.exec(data)"))
  {
    return false;
  }
  myRe.exec(data)
  
  if (formato == 1)
  {
    var dia = RegExp.$1;
    var mes = RegExp.$2;
    var ano = RegExp.$3;
  }
  else if (formato == 2)
  {
    var mes = RegExp.$1;
    var dia = RegExp.$2;
    var ano = RegExp.$3;
  }
  else if (formato == 3)
  {
    var ano = RegExp.$1;
    var mes = RegExp.$2;
    var dia = RegExp.$3;
  }
  
  if (dia.charAt(0) == "0")
  {
    dia = dia.charAt(1);
  }  
  
  if (mes.charAt(0) == "0")
  {
    mes = mes.charAt(1);
  }  
  
  dia = parseInt(dia);  
  mes = parseInt(mes);    
  ano = parseInt(ano);     

  if ((ano%400==0) || ((ano%4==0) && (ano%100!=0))) 
  {
    var DiasMes=new Array(31,29,31,30,31,30,31,31,30,31,30,31);
  }
  else
  {
    var DiasMes=new Array(31,28,31,30,31,30,31,31,30,31,30,31);
  }

  if (dia > DiasMes[mes-1])
  {
    return (false);
  }
  return (true);
}  

function horaValida(hora)
{
  if (hora.length < 5)
  {
    return (false);
  }
  myRe = new RegExp;

  myRe = /^([0-1][0-9]|2[0-4]):([0-5][0-9])$/ig;

  if (! eval("myRe.exec(hora)"))
  {
    return false;
  }
  myRe.exec(hora);
  myRe = null;
  return (true);
}

function isEmail(mail)
{
  if (mail.value.length < 6)
  {
    return (false);
  }

//O codigo abaixo é um teste de melhoras na validação de email
var pais, generico, nome_dominio, O_FF, nome_mail , str_regex
RegExp.lastIndex = 0;
pais ='(ac|ad|ae|af|ag|ai|al|am|an|ao|aq|ar|as|at|au|aw|ax|az|ba|bb|bd|be|bf|bg|bh|bi|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|cr|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gg|gh|gi|gl|gm|gn|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|im|in|io|iq|ir|is|it|je|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|mv|mw|mx|my|mz|na|nc|ne|nf|ng|ni|nl|no|np|nr|nu|nz|om|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|ps|pt|pw|py|qa|re|ro|rs|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tl|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)'
generico= '(aero|biz|com|coop|info|museum|name|net|org|pro|gov|edu|mil|int)'
nome_dominio= '([A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9])'
O_FF ='([01]?[0-9]?[0-9]|2[0-4][0-9]|25[0-5])'
nome_mail='([a-zA-Z0-9][_a-zA-Z0-9\-\.]*)'
str_regex='^'+nome_mail+'@((\\['+O_FF+'(\.'+O_FF+'){3}\\])|('+nome_dominio+'\.)+('+generico+'|'+pais+'|'+generico+'\.+'+pais+'))$'

myRe = new RegExp(str_regex);

// O codigo abaixo é o original  
//  myRe = new RegExp;
//  
//  RegExp.lastIndex = 0;
//
//  myRe = /([_a-zA-Z\d\-\.][_a-zA-Z\d\-\.]+)@([_a-zA-Z\d\-][_a-zA-Z\d\-]+(\.[_a-zA-Z\d\-][_a-zA-Z\d\-]+))/;

  if (! myRe.test(mail.value))
  {
    return false;
  }
  
  myRe.exec(mail);

  myRe = null;

  return (true);
}

function ContEmail(theMail)
{
  inStr = theMail.value;
  inLen = inStr.length;

  var Cont = 0;

  for(var i = 0; i < inLen; i++)
  {
    var ch = inStr.substring(i, i+1)
  
    if (ch == "@")
      Cont = Cont + 1;
  }

  if (Cont != 1)
    return (false);

  return (true);
}

function ComparaData(CampoDataInicio, CampoDataFim)
{
  Dia = CampoDataInicio.value.substring(0,2);
  Mes = CampoDataInicio.value.substring(3,5);
  Ano = CampoDataInicio.value.substring(6,10);
  var DataInicioConvert = Ano + Mes + Dia;

  Dia = CampoDataFim.value.substring(0,2);
  Mes = CampoDataFim.value.substring(3,5);
  Ano = CampoDataFim.value.substring(6,10);
  var DataFimConvert = Ano + Mes + Dia;        
  
  if (DataFimConvert < DataInicioConvert){
    return (false);
  }
  return (true);
}  


function contar(campo,rest,limite, mens)
{
  if (campo.value.length > limite)
  {
    campo.value = campo.value.substring(0, limite);
    alert(mens.replace(/%char%/g, limite));
  }
  else
  {
    rest.value = limite - campo.value.length;
  }
}

function TamanhoCampo(theField, Tamanho, mens)
{
  if (theField.value.length > Tamanho)
  {
    theField.value = theField.value.substring(0, Tamanho-1);
    alert(mens.replace(/%char%/g, Tamanho));
	 theField.focus();
  }
}


// Procura o Ch1 e troca por Ch2 na string de entrada
function Replace(theString, Ch1, Ch2)
{
  var strAux = theString;
  var strLen = strAux.length;
  var saida  = "";
  var chAux  = ""; 
  var P = 0;

  while (P < strLen)
  {
    chAux = strAux.substring(P, P + 1);

    if (chAux == Ch1) 
      saida = saida + Ch2
    else
      saida = saida + chAux;
 
    P++;
  }

  return(saida);
}

function DeleteAspas(theString)
{
  theString.value = theString.value.replace(/\"|\'/g, "");
}

// ZL - Para pegar o value de um combo
function SelectedCombo(Objeto)
{
  return Objeto[Objeto.selectedIndex].value
}

function GetColorBar(value)
{
  if (parseFloat(value) < 30)
  {
    return('PixAzul1.gif')
  }
  else if (parseFloat(value) < 60)
  {
    return('PixAzul2.gif')
  }
  else if (parseFloat(value) < 100)
  {
    return('PixAzul3.gif')
  }
  else 
  {
    return('PixVerde.gif')
  }
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function GetDatePart(inStr,Part,Formato) {
  // Part = 1 - dd
  // Part = 2 - mm
  // Part = 3 - yyyy
  // Formato 1 dd/mm/yyyy
  // Formato 1 mm/dd/yyyy
  // Formato 1 yyyy/mm/dd
  if (Formato == 1)
  {
    if (Part == 1)
     return(eval(inStr.substring(0,2)));
    else if (Part == 2)
     return(eval(inStr.substring(3,5)));
    else if (Part == 3)
     return(eval(inStr.substring(6,10)));
  }
  else if (Formato == 2)
  {
    if (Part == 1)
     return(eval(inStr.substring(3,5)));
    else if (Part == 2)
     return(eval(inStr.substring(0,2)));
    else if (Part == 3)
     return(eval(inStr.substring(6,10)));
  }
  else if (Formato == 3)
  {
    if (Part == 1)
     return(eval(inStr.substring(8,10)));
    else if (Part == 2)
     return(eval(inStr.substring(5,7)));
    else if (Part == 3)
     return(eval(inStr.substring(0,4)));
  }
}

// copyright 1999 Idocs, Inc. http://www.idocs.com
// Distribute this script freely but keep this notice in place
// USO <INPUT NAME="dollar" SIZE=5 MAXLENGTH=5 onKeyPress="return numbersonly(this, event)">

function numbersonly(myfield, e, dec)
{

  var key;
  var keychar;

  if (window.event)
    key = window.event.keyCode;
  else if (e)
    key = e.which;
  else
    return true;
  keychar = String.fromCharCode(key);

  // control keys
  if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27) )
    return true;

  // numbers
  else if ((("0123456789").indexOf(keychar) > -1))
    return true;

  // decimal point jump
  else if (dec && (keychar == "."))
  {
    myfield.form.elements[dec].focus();
    return false;
  }
  else
    return false;
}

function floatFix(Val, Decimais)
{
  var Res = "" + Math.round(Val * Math.pow(10, Decimais));
  var Dec = Decimais - Res.length;
  var Zer = Res.length - Decimais;
  var StrZero = "0.";

  if (Decimais != 0)
  {
    if (Val < 1)
    {
   	  for (Count=0; Count<=Dec-1; Count+= 1) 
      {	
    	  StrZero += "0";
      }
      if (Res < 1 * Math.pow(10, Decimais))
      {
        OutString = StrZero + Res;
      }
      else
      {
        OutString = Res.substring(0, Zer) + "." + Res.substring(Zer, Res.length);
      }
    }
    else
    {
      OutString = Res.substring(0, Zer) + "." + Res.substring(Zer, Res.length);
    }
  }
  else
  {
      OutString = Res.substring(0, Zer);
  }

  return (OutString);
}

function valida_CPF(obj)
{
	var s;	
	s = obj.value;
	while (s.indexOf(".") >-1) s=s.replace(".","");
	while (s.indexOf("-") >-1) s=s.replace("-","");
	while (s.indexOf(" ") >-1) s=s.replace(" ","");
	obj.value = s;
		cpf = s;
		valor = true;
		erro = new String;
		if (cpf.length < 11) erro += "Sao necessarios 11 digitos para verificacao do CPF! \n\n"; 
		var nonNumbers = /\D/;
		if (nonNumbers.test(cpf)) erro += "A verificacao de CPF suporta apenas numeros! \n\n";	
		if (cpf == "00000000000" || cpf == "11111111111" || cpf == "22222222222" || cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" || cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" || cpf == "99999999999"){
			  erro += "Numero de CPF invalido!"
		}
		var a = [];
		var b = new Number;
		var c = 11;
		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i);
			if (i < 9) b += (a[i] *  --c);
		}
		if ((x = b % 11) < 2) { a[9] = 0 } else { a[9] = 11-x }
		b = 0;
		c = 11;
		for (y=0; y<10; y++) b += (a[y] *  c--); 
		if ((x = b % 11) < 2) { a[10] = 0; } else { a[10] = 11-x; }
		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])){
			erro +="Digito verificador com problema!";
		}
		if (erro.length > 0){
//			alert(erro);
			return false;
		}
		return true;
}
function createCookie(name,value,days) 
{
	if (days) 
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function link_correio_el(nome, dominio, estilo, texto)
{
	
	document.write ( "<a href=\"mai"+"lto:" + nome + "&#64;" + dominio+ "\" class=\""+estilo+"\" >"+texto+"</a>")
}
function ContaPalavras(campo)
{
	var formcontent, i
	formcontent=campo.value
	
	for (i=0;i<10; i++)
	{
		formcontent = formcontent.replace(/[\t\n\r\f\v]/, " ")
		formcontent = formcontent.replace(/ [.,!?:;]/, " ")
	}
	formcontent=formcontent.split(" ")
	for(i=formcontent.length;i>=0;i--)
	{
		if(""==formcontent[i])
		{ 
			formcontent.splice(i, 1);
			i=formcontent.length;			
		}
  	}

	return (formcontent.length);
}
