/*function GerarSWF($arquivo,$altura,$largura){
    document.writeln('    <object type="application/x-shockwave-flash" data="'+ $arquivo +'" width="'+ $largura +'" height="'+ $altura +'">');
    document.writeln('        <param name="movie" value="' + $arquivo + '" />');
    document.writeln('        <param name="menu" value="false" />');
    document.writeln('        <param name="quality" value="high" />');
	document.writeln('        <param name="wmode" value="transparent" />');
    document.writeln('    </object>');
}*/

function abre_pop(url,largura,altura) {
	window.open(url, "pop", "toolbar=0,location=0,directories=0,status=0,menubar=no,scrollbars=no,resizable=no,width="+largura+",height="+altura);
}

function imprimir(){
	window.focus();
	window.print();
}

function isQtdeCaracter(iCampo, iTamanho, iNomeCampo) {
	if (iCampo.value.length > iTamanho){
		alert(iNomeCampo + ' só aceita ' + iTamanho + ' caracteres.');
		return false;
	}
	return true;
}

function isVazio(iCampo,iNomeCampo) {
	if (iCampo.value == '') {
		alert(iNomeCampo + ' é obrigatório.');
		iCampo.focus();
		return false;
	}
	return true;
}

function isChecado(iCampo,iNomeCampo) {
	var checado = false;
	for (i=0;i<iCampo.length;i++){
		if (iCampo[i].checked==true){
			checado = true;
		}
	}
	if (checado != true) {
		alert( 'Selecione o campo ' + iNomeCampo + '.');
		return false;
	} else {
		return true;
	}
}

function isCnpj(iCampo, iNomeCampo) {
	var iCnpj = null;
	iCnpj = iCampo.value;
	invalidChars = " /:,;-..";
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		iCnpj = iCnpj.replace(badChar,"");
	}
	if (EhIgual(iCnpj)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	dac1 = 0; 
	dac2 = 0; 
	dac = ""; 
	dact = ""; 
	sum = 0;
	iCampo1="";
	dac = iCnpj.substring(iCnpj.length-2, iCnpj.length);
	iCampo1 = iCnpj.substring(0,iCnpj.length-2);
	for (i=iCampo1.length-1, j=2; i >= 0 ; i--, j++) {	
		j = (j == 10) ? 2 : j; 
		sum += (iCampo1.substring(i, i+1) * j); 
	}
	dac1 = ((sum * 10) % 11); 
	dac1 = (dac1 == 10) ? 0 : dac1; 
	sum = 0;
	for (i=iCampo1.length-1, j=3; i >= 0 ; i--, j++){	
		j = (j == 10) ? 2 : j; 
		sum += (iCampo1.substring(i, i+1) * j); 
	}
	sum  += (dac1 * 2); 
	dac2 = ((sum * 10) % 11); 
	dac2 = (dac2 == 10) ? 0 : dac2; 
	dact = dac1+""+dac2;
	if (dact == dac) {
		iCampo.value = iCnpj;
		return true;
	} else {	
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
}

function isCpf(iCampo, iNomeCampo){
	var iCpf = null;
	iCpf = iCampo.value;
	invalidChars = " /:,;-..";
	for (i=0; i < invalidChars.length; i++) {
		badChar = invalidChars.charAt(i);
		iCpf = iCpf.replace(badChar,"");
	}
	if (EhIgual(iCpf)==true) { 
		iCampo.focus();
		alert(iNomeCampo + ' é inválido.');
		return false; 
	}
	x=0; 
	soma=0; 
	dig1=0; 
	dig2=0; 
	texto=""; 
	iCampo1="";
	len = iCpf.length; 
	x = len -1;
	for (var i=0; i <= len - 3; i++) {
		y = iCpf.substring(i,i+1); 
		soma = soma + ( y * x);
		x = x - 1; 
		texto = texto + y;
	}
	dig1 = 11 - (soma % 11);
	if (dig1 == 10) dig1=0 ; 
	if (dig1 == 11) dig1=0 ;
	iCampo1 = iCpf.substring(0,len - 2) + dig1 ;
	x = 11; soma=0;
	for (var i=0; i <= len - 2; i++) {
		soma = soma + (iCampo1.substring(i,i+1) * x); 
		x = x - 1;
	}
	dig2= 11 - (soma % 11);
	if (dig2 == 10) dig2=0; 
	if (dig2 == 11) dig2=0;
	if ((dig1 + "" + dig2) == iCpf.substring(len,len-2)) {
		iCampo.value = iCpf;
		return true;
	}
	iCampo.focus();
	alert(iNomeCampo + ' é inválido.');
	return false; 
}

function isData(iData,iNomeCampo){
var Data = iData.value;
if (Data == ""){
 return false }

Data_valida = true
 
if (Data.length > 10){
 Data_valida = false
 }
 
//Se a Data for apenas números retorna inválido
if (!isNaN(Data)){
 Data_valida = false
}

for (i=0;i<=Data.length;i++){
 if (isNaN(Data.charAt(i))){
  Separador = Data.charAt(i) 
  
  //SE O SEPARADOR FOR DIRERENTE DE "/" , RETORNA DATA INVALIDA.
  if (Separador != "/"){
   Data_valida = false;
   
   }
     
  Dia = Data.substring(0,Data.indexOf(Separador)) 
  Mes = Data.substring(eval(Data.indexOf(Separador) + 1),Data.lastIndexOf(Separador)) 
  Ano = Data.substring(eval(Data.lastIndexOf(Separador) + 1),Data.length) 
 } //End if
} // End For


if (isNaN(Dia)){
 Data_valida = false  }
 
if (Dia.length < 1 || Dia.length > 2){ 
 Data_valida = false }
 
if (isNaN(Mes)){
 Data_valida = false }
 
if (Mes.length < 1 || Mes.length > 2 ){ 
 Data_valida = false }
 
if (isNaN(Ano)){
 Data_valida = false }
 
if (Ano.length < 2 || Ano.length > 4){ 
 Data_valida = false } 
 
if (Dia < 1 || Dia > 31 || Mes < 1 || Mes > 12){
 Data_valida = false }

if (Data_valida == false){
	alert(iNomeCampo + ' é inválido.');
	return false;
} else {
	return true;
}
}

function isDataVal(iCampo, iNomeCampo){
	var barra = '/';
	var iCampo1 = iCampo;
	var pos1 = iCampo1.indexOf(barra);
	var pos2 = iCampo1.indexOf(barra,pos1+1)
	var dia = iCampo1.substring(0,pos1)
	var mes = iCampo1.substring(pos1+1,pos2)
	var ano = iCampo1.substring(pos2+1)
	var now = new Date();
	var MaxAno = now.getYear()+50;
	if (!(isFormatoDataVal(iCampo))) {
		alert(iNomeCampo + ' formato inválido.');
		return false;
	}

	if ((eval(dia) > 31) || (eval(mes) > 12) || (eval(ano) < 1900))	{
		alert(iNomeCampo + ' é inválido.');
		return false;
	}
	
	if((eval(mes) == 1) || (eval(mes) == 3) || (eval(mes) == 5) || (eval(mes) == 7) || (eval(mes) == 8) || (eval(mes) == 10) || (eval(mes) == 12))	{
		if(eval(dia) > 31)	{
			alert(iNomeCampo + ' é inválido.');
			return false;
		}				
	}
	
	if((eval(mes) == 4) || (eval(mes) == 6) || (eval(mes) == 9) || (eval(mes) == 11))	{
		if(eval(dia) > 30)		{
			alert(iNomeCampo + ' é inválido.');
			return false;
		}
	}
	
	if(eval(mes) == 2)	{
		var MaxDia = 0
		if(eval(ano) % 4 == 0)		{
			MaxDia = 29
		} else {
			MaxDia = 28
		}
		if(eval(dia) > MaxDia)		{
			alert(iNomeCampo + ' é inválido.');
			return false;
		}
	}
	return true;
}

function isEmail(iCampo,iNomeCampo) {
	var str = iCampo.value;
	var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
	var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
	if ( (!r1.test(str) && r2.test(str)) ) {
		return true;
	} else {
		iCampo.focus();
		alert(iNomeCampo+" inválido.");
		return false;
	}
	return true;
}

function isFormatoData(iCampo){
	var barra = '/';
	var iCampo1 = iCampo.value;
	var pos1 = iCampo1.indexOf(barra);
	var pos2 = iCampo1.indexOf(barra,pos1+1);
	if (pos1 != 2 || pos2 != 5){
		return false;
	}
	var dia = iCampo1.substring(0,pos1);
	var mes = iCampo1.substring(pos1+1,pos2);
	var ano = iCampo1.substring(pos2+1);
	if (!(isInteiro(dia) && isInteiro(mes) && isInteiro(ano))){
		return false;
	}
	return true;
}

function isFormatoDataVal(iCampo){
	var barra = '/';
	var iCampo1 = iCampo;
	var pos1 = iCampo1.indexOf(barra);
	var pos2 = iCampo1.indexOf(barra,pos1+1);
	if (pos1 != 2 || pos2 != 5){
		return false;
	}
	var dia = iCampo1.substring(0,pos1);
	var mes = iCampo1.substring(pos1+1,pos2);
	var ano = iCampo1.substring(pos2+1);
	if (!(isInteiro(dia) && isInteiro(mes) && isInteiro(ano))){
		return false;
	}
	return true;
}


function isIgual(iCampo1, iCampo2, iNomeCampo1, iNomeCampo2) {
	if (iCampo1.value != iCampo2.value){
		iCampo1.focus();
		alert('O campo '+iNomeCampo1+' não é igual ao campo '+iNomeCampo2);
		return false;
	}
	return true;
}

function isInteiro(iCampo){
	var i;
    for (i = 0; i < iCampo.length; i++){   
        var c = iCampo.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}


function isNumerico(iCampo, iNomeCampo) {
	var CaractereInvalido = false;
	if (!(isInteiro(iCampo.value))){
		CaractereInvalido = true;
	}
	for (i=0; i < iCampo.value.length; i++) {
		var Caractere = iCampo.value.charAt(i);
		if(Caractere != "." && Caractere != "," && Caractere != "-"){
         	if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
		}
	}
	if (!CaractereInvalido == true) {
		return true;
   	} else {
		iCampo.focus();
		alert(iNomeCampo + ' deve conter apenas números.');
		return false;
	}
}

function isSelecionado(iCampo, iNomeCampo) {
	if (iCampo.selectedIndex==0) {
		iCampo.focus();
		alert(iNomeCampo + ' não foi selecionado.');
		return false;
	} else {
		return true;
	}
}

function isTamanhoDefinido(iCampo, iMinimo, iMaximo, iNomeCampo){
	if (eval(iCampo.value.length) < iMinimo || eval(iCampo.value.length) > iMaximo){
		iCampo.focus();
		alert(iNomeCampo + ' deve ter no mínimo ' + iMinimo + ' e no máximo ' + iMaximo + ' caracteres.');
		return false;
	} else {
		return true;
	}
}

function isTamanho(iCampo, iTamanho, iNomeCampo){
	if (eval(iCampo.value.length) != iTamanho){
		iCampo.focus();
		alert(iNomeCampo + ' deve conter ' + iTamanho + ' caracteres.');
		return false;
	} else {
		return true;
	}
}

function isValor(iCampo, iNomeCampo){
	var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
 	if (!(objRegExp.test(iCampo.value))) {
		alert(iNomeCampo + ' não é um valor válido.');
		return false;
	} else {
		return true;
	}

}

function isAreaMinima(iCampo, iMinimo, iNomeCampo){
	if (eval(iCampo.value) >= iMinimo){
		return true;
	} else {
		iCampo.focus();
		alert(iNomeCampo + ' deve ter no mínimo ' + iMinimo + ' m².');
		return false;
	}
}

// Rotinas antigas
function isChecked(opcao){
	if (opcao.checked == true){
		return true;
	}
	return false;
}

function isNumber(numero)
{
   var CaractereInvalido = false;
   for (i=0; i < numero.length; i++){
      var Caractere = numero.charAt(i);
      if(Caractere != "." && Caractere != "," && Caractere != "-"){
         if (isNaN(parseInt(Caractere))) CaractereInvalido = true;
      }
   }

   return !CaractereInvalido;
}

// Verifica se todos os valores são iguais
function EhIgual(campo) { 
	var i = 0
	var j = 1
	var igual = true;
	// Todos os caracteres devem ser diferentes
	while (i < campo.length-1 && igual == true) {
		while (j < campo.length && igual == true) {
			if (campo.charAt(i) != campo.charAt(j)) {
				igual = false;
			}
			else j++;
		}
		i++;
	}
	return igual;
}
