function abrePopup(url, name, altura, largura, top, left){
	window.open(url, name, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, top='+top+', left='+left+', width='+largura+', height='+altura);
}

function abrePopup2(url, name, toolbar, location, directories, status, menubar, scrollbars, resizable, altura, largura, top, left){
	window.open(url, name, 'toolbar=' + toolbar + ', location=' + location + ', directories=' + directories + ', status=' + status + ', menubar=' + menubar + ', scrollbars=' + scrollbars + ', resizable=' + resizable + ', top=' + top + ', left=' + left + ', width=' + largura + ', height=' + altura);
}

function abrePopup3(url, name, toolbar, location, directories, status, menubar, scrollbars, resizable, fullscreen, altura, largura, top, left){
	window.open(url, name, 'toolbar=' + toolbar + ', location=' + location + ', directories=' + directories + ', status=' + status + ', menubar=' + menubar + ', scrollbars=' + scrollbars + ', resizable=' + resizable + ', fullscreen=' + fullscreen + ', top=' + top + ', left=' + left + ', width=' + largura + ', height=' + altura);
}

function abreDialog(url, w, h, s){
	showModelessDialog(url, window, "dialogWidth:" + w + "px; dialogHeight:" + h + "px; scroll:" + s + ";status:no;help:no;");
}

function fecharJanela(){
	window.close();
}

function imprimir(){
	window.print();
}

function verificaNumeric(campo,texto){
	if(isNaN(document.getElementById(campo).value)){
		alert("O campo \"" + texto + "\" deve conter apenas números!");
		marcaInputText(campo);
		erro=true;
	}
}

function verificaAlpha(campo,texto){
	if(!isNaN(document.getElementById(campo).value)){
		alert("O campo \"" + texto + "\" deve conter apenas letras!");
		marcaInputText(campo);
		erro=true;
	}
}

function verificaPhone(campo,texto)
{
  var error = ""; 

  var digits = "0123456789()-";

  for (var i = 0; i < document.getElementById(campo).value.length; i++)
  {
    temp = document.getElementById(campo).value.substring(i, i+1)

    if (digits.indexOf(temp) == -1 && 
     document.getElementById(campo).value != "")
    {
      error += "O campo \"" + texto + "\" informado não é válido. Tente novamente!";
	  marcaInputText(campo);
      break;
    }
  }

  if (error != "")
  {
    alert(error);
    erro=true;
  } else {
    return !erro;
  } 
}

//validaçao de data

function verificaData(campo, texto)
{
var tam, sep1, sep2, verAno

tam = document.getElementById(campo).value.length;
sep1 = parseInt(document.getElementById(campo).value.indexOf("/", 0));
sep2 = parseInt(document.getElementById(campo).value.indexOf("/", sep1+1));
verAno = tam-sep2;

if (sep1<0 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

if (sep2<5 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

if(verAno < 5 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido.\n\nO ano deve ter 4 dígitos.");
marcaInputText(campo);
erro = true;
return false;
}
else
return !erro;
} 

//termina a validação de data

//validaçao de tempo

function verificaTempo(campo, texto)
{
var tam, sep1, sep2, verAno

tam = document.getElementById(campo).value.length;
sep1 = parseInt(document.getElementById(campo).value.indexOf(":", 0));
sep2 = parseInt(document.getElementById(campo).value.indexOf(":", sep1+1));
verSeg = tam-sep2;

if (sep1<0 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

if (sep2<5 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

if(verAno < 7 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}
else
return !erro;
} 

//termina a validação de tempo

function verificaHora(campo, texto)
{
var tam, sep1, sep2

tam = document.getElementById(campo).value.length;
sep1 = parseInt(document.getElementById(campo).value.indexOf(":", 0));

if (sep1<0 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

if (sep2<5 && document.getElementById(campo).value!="")
{
alert("O campo \"" + texto + "\" informado não é valido. Tente novamente!");
marcaInputText(campo);
erro = true;
return false;
}

else
return !erro;
} 

function verificaPreco(campo, texto)
{

  var error = ""; 

  var digits = "0123456789,";
  numero_casas = 2;  // numero de casas decimais

  for (var i = 0; i < document.getElementById(campo).value.length; i++)
  {
    temp = document.getElementById(campo).value.substring(i, i+1)

    if (digits.indexOf(temp) == -1 && 
     document.getElementById(campo).value != "")
    {
      error += "O campo \"" + texto + "\" informado não é válido. Tente novamente!";
      break;
    }
  }
  
  if (document.getElementById(campo).value.indexOf(',') != -1){
	dectext = document.getElementById(campo).value.substring(document.getElementById(campo).value.indexOf(',')+1, document.getElementById(campo).value.length)
    if (dectext.length > numero_casas){
      error += "Digite apenas " + numero_casas + " casas decimais.";
      marcaInputText(campo);
    }
  }
  
  if (error != "")
  {
    alert(error);
    erro=true;
    marcaInputText(campo);
  } else {
    return !erro;
  } 
}


function verificaPreco2(campo, texto)
{

  var error = ""; 

  var digits = "0123456789,";
  numero_casas = 2;  // numero de casas decimais

  for (var i = 0; i < document.getElementById(campo).value.length; i++)
  {
    temp = document.getElementById(campo).value.substring(i, i+1)

    if (digits.indexOf(temp) == -1 && 
     document.getElementById(campo).value != "")
    {
      error += "O campo \"" + texto + "\" informado não é válido. Tente novamente!";
      break;
    }
  }
  
  if (document.getElementById(campo).value.indexOf(',') != -1){
	dectext = document.getElementById(campo).value.substring(document.getElementById(campo).value.indexOf(',')+1, document.getElementById(campo).value.length)
    if (dectext.length > numero_casas){
      error += "Digite apenas " + numero_casas + " casas decimais.";
      marcaInputText(campo);
    }
  }
  
  if (error != "")
  {
    alert(error);
    erro=true;
    marcaInputText(campo);
  } 

}
function verificaEmail(campo,texto)
{
	 if (document.getElementById(campo).value != "")
        {
          if (document.getElementById(campo).value.indexOf("@") == -1 || document.getElementById(campo).value.indexOf("@") == document.getElementById(campo).value.length - 1 || document.getElementById(campo).value.indexOf("@") == 0 ||
              document.getElementById(campo).value.indexOf(".") == -1 || document.getElementById(campo).value.indexOf(".") == document.getElementById(campo).value.length - 1 || document.getElementById(campo).value.indexOf(".") == 0 ||
              document.getElementById(campo).value.length < 6)
              {
               alert("O campo \"" + texto + "\" informado não é válido. Tente novamente!");
			   marcaInputText(campo);
               erro=true;
              }
         }
}

function marcaInputText(campo){
	document.getElementById(campo).style.borderStyle="dashed";
	document.getElementById(campo).style.borderColor="FF0000";
	document.getElementById(campo).style.borderWidth="1px";
}

function desmarcaInputText(campo){
	document.getElementById(campo).style.borderStyle="solid";
	document.getElementById(campo).style.borderColor="336699";
	document.getElementById(campo).style.borderWidth="1px";
}

function alinhaValor(campo,posicao){
	document.getElementById(campo).style.textAlign=posicao;
}

function verificaSenha(campo1,campo2){
	if(document.getElementById(campo1).value != document.getElementById(campo2).value){
		alert('As senhas informadas não conferem. Regedite-as!');
		marcaInputText(campo1);
		marcaInputText(campo2);
        erro=true;
	}else{
		if(document.getElementById(campo1).value!=""){
			if(document.getElementById(campo1).value.length < 6){
				alert('Sua senha deve conter 6 dígitos ou mais!');
				marcaInputText(campo1);
				marcaInputText(campo2);
				erro=true;
			}
		}
	}
}

function verificaCpf(campo,texto){
	
	var lngContador
	var lngSoma	
	var lngDAC1
	var lngDAC2
	var strZeros
	var cont_erro = "";
	
	//Não pode ter mais que onze caracteres
	if (document.getElementById(campo).value.length > 11){
		cont_erro = cont_erro + 1;
	}
	
		if(document.getElementById(campo).value.charAt(0) == document.getElementById(campo).value.charAt(1) && document.getElementById(campo).value.charAt(1) == document.getElementById(campo).value.charAt(2) && document.getElementById(campo).value.charAt(0) == document.getElementById(campo).value.charAt(2)){
			if (document.getElementById(campo).value.charAt(6) == document.getElementById(campo).value.charAt(7) && document.getElementById(campo).value.charAt(7) == document.getElementById(campo).value.charAt(8) && document.getElementById(campo).value.charAt(8) == document.getElementById(campo).value.charAt(6)){
				if (document.getElementById(campo).value.charAt(6) == document.getElementById(campo).value.charAt(0) && parseInt(document.getElementById(campo).value.charAt(0)) + parseInt(document.getElementById(campo).value.charAt(6)) == 6){
					if(document.getElementById(campo).value.charAt(3) == document.getElementById(campo).value.charAt(4) && document.getElementById(campo).value.charAt(4) == document.getElementById(campo).value.charAt(5) && document.getElementById(campo).value.charAt(3) == document.getElementById(campo).value.charAt(5)){
						if(document.getElementById(campo).value.charAt(9) == document.getElementById(campo).value.charAt(10) && document.getElementById(campo).value.charAt(3) == document.getElementById(campo).value.charAt(4)){
							if(parseInt(document.getElementById(campo).value.charAt(3)) + parseInt(document.getElementById(campo).value.charAt(9)) == 2){
								return true;
							}
						}
					}
				}
			}
		}


	//Verifica se todos os dígitos são iguais	
	for(lngContador=2; lngContador<document.getElementById(campo).value.length; lngContador++)
		if(document.getElementById(campo).value.charAt(lngContador) != document.getElementById(campo).value.charAt(lngContador-1)) break;
	
	//Se chegou no final, eram todos iguais	
	if(lngContador == document.getElementById(campo).value.length){
		cont_erro = cont_erro + 1;
	}
	
  //Primeiro dígito verificador (lngDAC1) :
  lngSoma = 0;
  for(lngContador = 0; lngContador<9; lngContador++)
		lngSoma = lngSoma + (document.getElementById(campo).value.charAt(lngContador) * (10 - lngContador));  
  lngDAC1 = (lngSoma * 10) % 11;
  if(lngDAC1==10) lngDAC1 = 0; 	

  //Já verifica se o primeiro dígito está certo
  if(lngDAC1!=document.getElementById(campo).value.charAt(document.getElementById(campo).value.length-2)){
		cont_erro = cont_erro + 1;
  }
  
  //Segundo dígito verificador (lngDAC2) :
  lngSoma = 0;
  for(lngContador = 0; lngContador<9; lngContador++)
		lngSoma = lngSoma + (document.getElementById(campo).value.charAt(lngContador) * (11 - lngContador));
		
  lngDAC2 = ((lngSoma + (2 * lngDAC1)) * 10) % 11;
  if(lngDAC2==10) lngDAC2 = 0;

  //Verifica se o segundo dígito está certo
  if(lngDAC2!=document.getElementById(campo).value.charAt(document.getElementById(campo).value.length-1)){
		cont_erro = cont_erro + 1;
  }
  
  if(cont_erro >= 1){
	alert("O campo \"" + texto + "\" informado não é válido. Tente novamente!");
	marcaInputText(campo);
    erro=true;
  }
  
}

function verificaCgc(campo,texto){
	if(VerifyCNPJ(document.getElementById(campo).value) != 1){
		alert("O campo \"" + texto + "\" informado não é válido. Tente novamente!");
		marcaInputText(campo);
		erro=true;
	}
}

// Aqui inicia as funções de teste do CNPJ

function isNUMB(c)
	{
	if((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+"."+c.substring(cx+1);
		}
	if((parseFloat(c) / c != 1))
		{
		if(parseFloat(c) * c == 0)
			{
			return(1);
			}
		else
			{
			return(0);
			}
		}
	else
		{
		return(1);
		}
	}

function LIMP(c)
	{
	while((cx=c.indexOf("-"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("/"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(","))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("."))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf("("))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(")"))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	while((cx=c.indexOf(" "))!=-1)
		{		
		c = c.substring(0,cx)+c.substring(cx+1);
		}
	return(c);
	}

function VerifyCNPJ(CNPJ)
	{
	CNPJ = LIMP(CNPJ);
	if(isNUMB(CNPJ) != 1)
		{
		return(0);
		}
	else
		{
		if(CNPJ == 0)
			{
			return(0);
			}
		else
			{
			g=CNPJ.length-2;
			if(RealTestaCNPJ(CNPJ,g) == 1)
				{
				g=CNPJ.length-1;
				if(RealTestaCNPJ(CNPJ,g) == 1)
					{	
					return(1);
					}
				else
					{
					return(0);
					}
				}
			else
				{
				return(0);
				}
			}
		}
	}
function RealTestaCNPJ(CNPJ,g)
	{
	var VerCNPJ=0;
	var ind=2;
	var tam;
	for(f=g;f>0;f--)
		{
		VerCNPJ+=parseInt(CNPJ.charAt(f-1))*ind;
		if(ind>8)
			{
			ind=2;
			}
		else
			{
			ind++;
			}
		}
		VerCNPJ%=11;
		if(VerCNPJ==0 || VerCNPJ==1)
			{
			VerCNPJ=0;
			}
		else
			{
			VerCNPJ=11-VerCNPJ;
			}
	if(VerCNPJ!=parseInt(CNPJ.charAt(g)))
		{
		return(0);
		}
	else
		{
		return(1);
		}
	}
	
//verifica se tem o texto

function verificaTexto(campo,txt,texto){
	if(document.getElementById(campo).value != ""){
		if(document.getElementById(campo).value.indexOf(txt) == -1){
			alert(texto);
			marcaInputText(campo);
			erro=true;
		}
	}
}

//conta caracters do textarea
function textCounter(campo,contador,total){
	if(document.getElementById(campo).value.length > total){
		document.getElementById(campo).value = document.getElementById(campo).value.substring(0, total);
	}else{
		document.getElementById(contador).value = total - document.getElementById(campo).value.length;
	}
}

//conta caracters do textarea
function textCounter2(campo, contador, total){
	if(document.getElementById(campo).value.length > total){
		document.getElementById(campo).value = document.getElementById(campo).value.substring(0, total);
	}else{
		document.getElementById(contador).innerHTML = total - document.getElementById(campo).value.length;
	}
}

//apaga o texto do text
function apagaText(campo,texto){
	if(document.getElementById(campo).value = texto){
		document.getElementById(campo).value = "";
	}
}


function verificaNomeCompleto(campo,texto){

	var elemento = document.getElementById(campo);

	if(document.getElementById(campo).value != ""){
		if(elemento.value.split(" ").length <= 1){
			alert(texto);
			marcaInputText(campo);
			erro=true;
		}
	}
}

var submitou = false;
function submita(campo,texto,formName) {
	document.getElementById(campo).value = texto;
	if(!submitou){
		submitou = true;
		//formName.submit();
	}else{
		alert(texto);
	}
}

//funcao q deixa a 1ª letra em maiusculo
function primeiraMaiuscula(str){
	return (str.substring(0,1).toUpperCase() + str.substring(1,str.length).toLowerCase())
}

function primeiraMaiuscula2(str){
	var resultado = "";
	var splt = str.split(" ");
	for(var i = 0; i < splt.length; i++){
		resultado == ""?resultado = primeiraMaiuscula(splt[i]):resultado += " " + primeiraMaiuscula(splt[i])
	}
	return resultado;
}

//muda o focus automatico
function setaFocus(campo1, campo2, tamanho){

	var campo_txt = document.getElementById(campo1).value;

	if(campo_txt.length == tamanho){
		document.getElementById(campo2).focus();
		return false;
	}
	
}

function replaceChar(texto,char,troca){
	for (h = 0; h < texto.length; h++){
		if (texto.indexOf(char) != -1){
			texto = texto.replace(char,troca);
		}else{
			texto = texto;
		}
	}
	return texto;
}

function clicaApaga(campo,valor){
	if(document.getElementById(campo).value == valor){
		document.getElementById(campo).value = "";
	}
}

function verificaTelHome(campo, texto){
	var campo2 = document.getElementById(campo).value;
	if (campo2 != ""){
		if (campo2.length >= 7){
			if (campo2.charAt(0) == "9" || campo2.charAt(0) == "8" || campo2.charAt(0) == "7"){
				alert("O " + texto + " não pode ser um telefone móvel!");
				marcaInputText(campo);
				erro=true;
			}
		}else{
			alert("O " + texto + " não é um nº de telefone válido!");
				marcaInputText(campo);
				erro=true;
		}
	}
}

function selecionaValor(campo){
	document.getElementById(campo).select();
	document.getElementById(campo).focus();
}

function mostraC(span){
	if(document.getElementById(span).style.display == "none"){
		document.getElementById(span).style.display = "block";
	}else{
		document.getElementById(span).style.display = "none";
	}
}

function BreakItUp(campo){
  
  var FormLimit = 102399

  var TempVar = new String
  TempVar = document.getElementById(campo).value

  if (TempVar.length > FormLimit)
  {
    document.getElementById(campo).value = TempVar.substr(0, FormLimit)
    TempVar = TempVar.substr(FormLimit)

    while (TempVar.length > 0){
      var objTEXTAREA = document.createElement("TEXTAREA")
      objTEXTAREA.name = campo
      objTEXTAREA.value = TempVar.substr(0, FormLimit)
      document.forms[0].appendChild(objTEXTAREA)

      TempVar = TempVar.substr(FormLimit)
    }
  }
  
}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}

function preview_images(selFile,selFigura){
	if (document.all[selFile].value!="")
		document.all[selFigura].src=document.all[selFile].value;	
}

function formatCurrency(number){
   var num = new String (number);
   if (num.indexOf (".") == -1)
      {
      intLen = num.length;
      toEnd = intLen;
      var strLeft = new String (num.substring (0, toEnd));
      var strRight = new String ("00");
      }
   else
      {
      pos = eval (num.indexOf ("."));
      var strLeft = new String (num.substring (0, pos));
	  
      intToEnd = num.length;
      intThing = pos + 1;
      var strRight = new String (num.substring (intThing, intToEnd));
	  
      if (strRight.length > 2)
         {
         nextInt = strRight.charAt(2);
		 
         if (nextInt >= 5)
            {
            strRight = new String (strRight.substring (0, 2));
            strRight = new String (eval ((strRight * 1) + 1));
			
            if((strRight * 1) >= 100)
               {
               strRight = "00";
               strLeft = new String (eval ((strLeft * 1) + 1));
               }
			   
            if (strRight.length <= 1)
               {
               strRight = new String ("0" + strRight);
               }
            }
         else
            {
            strRight = new String (strRight.substring (0, 2));
            }
         }
      else
         {
         if (strRight.length != 2)
            {
            strRight = strRight + "0";
            }
         }
      }
	  
   if (strLeft.length > 3)
      {
      var curPos = (strLeft.length - 3);
	  
      while (curPos > 0)
         {
         var remainingLeft = new String (strLeft.substring (0, curPos));
         var strLeftLeft = new String (strLeft.substring (0, curPos));
         var strLeftRight = new String (strLeft.substring (curPos, strLeft.length));
		 
         strLeft = new String (strLeftLeft + "." + strLeftRight);
         curPos = (remainingLeft.length - 3);
         }
      }

   strWhole = strLeft + "," + strRight;
   finalValue = strWhole;
   return (finalValue);
}
