function inicializaCamposColor(formulario)
{
	if(eval('document.'+ formulario)) 
	{
		var ObjetosText = eval('document.'+formulario+'.getElementsByTagName("input")');
		var select      = eval('document.'+formulario+'.getElementsByTagName("select")');
		var textArea    = eval('document.'+formulario+'.getElementsByTagName("textarea")');
	
		for (i = 0; i < ObjetosText.length; i++)
		{
	
			if (((ObjetosText[i].type == 'text') || (ObjetosText[i].type == 'password')) && (ObjetosText[i].onblur == null) && (ObjetosText[i].onfocus == null))
			{
				ObjetosText[i].onfocus = function () { enterComponente(this,formulario);  }
				ObjetosText[i].onblur =  function () { exitComponente(this,formulario);  }
				ObjetosText[i].className  = formulario + '_exit';
			}
		}
	
		for (i = 0; i < select.length; i++)
		{
	
			if (  (select[i].onblur == null) && (select[i].onfocus == null))
			{
				select[i].onfocus = function () { enterComponente(this,formulario);  }
				select[i].onblur =  function () { exitComponente(this,formulario);  }
				select[i].className  = formulario + '_exit';
			}
		}
	
		for (i = 0; i < textArea.length; i++)
		{
			if ((textArea[i].onblur == null) && (textArea[i].onfocus == null))
			{
				textArea[i].onfocus = function () { enterComponente(this,formulario);  }
				textArea[i].onblur =  function () { exitComponente(this,formulario);  }
				textArea[i].className  = formulario + '_exit';
			}
		}
	}
}

function colorSelect(formulario)
{
	if(eval('document.'+ formulario)) 
	{
		var select = eval('document.'+ formulario + '.getElementsByTagName("select")');
		var cor = true;
	
		for( var j = 0; j < select.length; j++ )
		{
			cor = true;
		
			for( var i = 0; i < select[j].length; i++ )
			{
				if( cor )
				{
					select[j].options[i].style.background = "#F8FAF5";		
					cor = false;
				}
				else
				{
					select[j].options[i].style.background = "#FFFFFF";		
					cor = true;
				}
				
				select[j].options[i].style.marginLeft = "3px";
			}
		}
	}
}

function enterComponente(e,estilo) 
{
	e.className  = estilo + '_enter';
}

function exitComponente(e,estilo) 
{
	e.className  = estilo + '_exit';
}

function swfvars($arquivo,$largura,$altura,$bg,$wmode,$title,$flashvars)
{
	document.writeln('<object type="application/x-shockwave-flash" data="' + $arquivo + '" width="' + $largura + '" height="' + $altura + '" title="' + $title + '">');
	document.writeln('<param name="movie" value="' + $arquivo + '">');
	document.writeln('<param name="flashvars" value="' + $flashvars + '">');
	document.writeln('<param name="bgcolor" value="#' + $bg + '">');
	document.writeln('<param name="wmode" value="' + $wmode + '">');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('</object>');
}

function swf($arquivo,$largura,$altura,$bg,$wmode,$title)
{
	document.writeln('<object type="application/x-shockwave-flash" data="' + $arquivo + '" width="' + $largura + '" height="' + $altura + '" title="' + $title + '">');
	document.writeln('<param name="movie" value="' + $arquivo + '">');
	document.writeln('<param name="bgcolor" value="#' + $bg + '">');
	document.writeln('<param name="wmode" value="' + $wmode + '">');
	document.writeln('<param name="menu" value="false">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('</object>');
}


function trim(str)
{        
        return str.replace(/^\s*|\s*$/g,"");
}

function validaData(data) 
{
	var dia = data.substr(0,2);
	var mes = data.substr(3,2);
	var ano = data.substr(6,4);
							  
	// Checa se eh uma data valida
	if ((isNaN(parseInt(dia)) || isNaN(parseInt(mes)) || isNaN(parseInt(ano))) ||(ano < 1900 || ano > 9999) ||(mes < 1 || mes > 12) ||(dia < 1 || dia > 31) ||(mes == 2 && dia > 28 && (ano % 4 != 0)) ||(mes == 2 && dia > 29 && (ano % 4 == 0)) ||(dia > 30 && (mes == 4 || mes == 6 || mes == 9 || mes== 11)))return false;
	else
	return true;
}

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 Limpar(fld)
{
	fld.value = '';
	fld.focus();
}

function validaDinheiro(fld,e)
{
	var milSep = ".";
	var decSep = ",";
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13)
		return true;

	key = String.fromCharCode(whichCode);

	if (strCheck.indexOf(key) == -1)
		return false;

	len = fld.value.length;

	for (i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
			break;

	aux = '';

	for (; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1)
			aux += fld.value.charAt(i);

	aux += key;
	len = aux.length;

	if (len == 0)
		fld.value = '';
	if (len == 1)
		fld.value = '0'+ decSep + '0' + aux;
	if (len == 2)
		fld.value = '0'+ decSep + aux;
	if (len > 2)
	{
		aux2 = '';
		for (j = 0, i = len - 3; i >= 0; i--)
		{
			if (j == 3)
			{
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		fld.value = '';
		len2 = aux2.length;

		for (i = len2 - 1; i >= 0; i--)
			fld.value += aux2.charAt(i);
		fld.value += decSep + aux.substr(len - 2, len);
	}
	return false;
}

//###################################################################################
function validaPeso(fld,e)
{
	var milSep = "";
	var decSep = ".";
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.Event) ? e.which : e.keyCode;

	if (whichCode == 13)
		return true;

	key = String.fromCharCode(whichCode);

	if (strCheck.indexOf(key) == -1)
		return false;

	len = fld.value.length;

	for (i = 0; i < len; i++)
		if ((fld.value.charAt(i) != '0') && (fld.value.charAt(i) != decSep))
			break;

	aux = '';

	for (; i < len; i++)
		if (strCheck.indexOf(fld.value.charAt(i))!=-1)
			aux += fld.value.charAt(i);

	aux += key;
	len = aux.length;

	if (len == 0)
		fld.value = '';

	if (len == 1)
		fld.value = '0'+ decSep + '00' + aux;

	if (len == 2)
		fld.value = '0'+ decSep + '0' + aux;

	if (len == 3)
		fld.value = '0'+ decSep + aux;

	if (len > 3)
	{
		aux2 = '';

		for (j = 0, i = len - 3; i >= 0; i--)
		{
			if (j == 3)
			{
				aux2 += milSep;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}	

		fld.value = '';
		len2 = aux2.length;

		for (i = len2 - 1; i >= 1; i--)
			fld.value += aux2.charAt(i);
			
		fld.value += decSep + aux.substr(len - 3, len);
	}
	return false;
}

function ChecaNumero(num) 
{
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<num.length; i++) 
	{
		temp = "" + num.substring(i, i+1);		
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") 
		return false;

	return true;
}

function validaCEP(cep) 
{
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	if(cep.length != 8)
		return false;
	
	for (var i=0; i<cep.length; i++) 
	{
		temp = "" + cep.substring(i, i+1);		
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") 
		return false;

	return true;
}

function validaNumero(field) 
{
	var valid = "0123456789"
	var ok = "yes";
	var temp;
	
	for (var i=0; i<field.value.length; i++) 
	{
		temp = "" + field.value.substring(i, i+1);		
		if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if (ok == "no") 
	{
		alert("Válido somente números");
		field.focus();
		field.select();
	}
}

function validaemail(email)
{
	p=email.indexOf("@");
	z=email.indexOf(".");
	if ((p<1 || p==(email.length-1)) || (z<1 || z==(email.length-1))) 
		return false;
	return true;
}

function ChecaCNPJ(strCGC) {
	    strCGC = strCGC.replace(".", "");
     	strCGC = strCGC.replace(".", "");
	    strCGC = strCGC.replace("-", "");
	    strCGC = strCGC.replace("/", "");
	
	if (strCGC == "00000000000000" || strCGC == "11111111111111" ||
		strCGC == "22222222222222" || strCGC == "33333333333333" || strCGC == "44444444444444" ||
		strCGC == "55555555555555" || strCGC == "66666666666666" || strCGC == "77777777777777" ||
		strCGC == "88888888888888" || strCGC == "99999999999999")
		{ return false; }
	var NewCGC = "";
	var DV = "";
	var DVCGC;
	var s1 = 0;
	var s2 = 0;
	var NewDV;
	
	//Verifica tamanho do CGC
	if ((strCGC.length != 14) || (isNaN(strCGC)))
	{
		return false;
	}
	
	//Calcula os dígitos verificadores
	//Guarda os 12 primeiros digitos
	DVCGC = strCGC.substring(0,12);
	
	//calcula o primeiro digito verificador
	for (i=1;i<=4;i++)
		s1 = s1 + (parseInt(DVCGC.charAt(i-1))*(6-i));

	for (i=5;i<=12;i++)
		s1 = s1 + (parseInt(DVCGC.charAt(i-1))*(14-i));
	
	r1 = s1 % 11;
	if (r1 < 2)
		dv1 = 0;
	else 
		dv1 = 11 - r1;
	
	//calcula o segundo digito verificador
	s2 = dv1 * 2;
	
	for (i=1;i<=5;i++)
		s2 = s2 + (parseInt(DVCGC.charAt(i-1))*(7-i));
	for (i=6;i<=12;i++)
		s2 = s2 + (parseInt(DVCGC.charAt(i-1))*(15-i));
	r2 = s2 % 11;
	if (r2 < 2)
		dv2 = 0;
	else
		dv2 = 11 - r2;
	
	//junta os digitos verificadores
	DV = DV + dv1 + dv2;
	//guarda os digitos verificadores do CGC digitado (últimas duas posições no string)
	NewDV = strCGC.substring(12,14)
	if (NewDV == DV) //se o DV calculado for igual ao digitado, retorna true
		return true;
	else
	{
		return false;
	}
}

function ChecaCPF(strCPF) {
	
	
	
	
  	if (strCPF.length != 11 || strCPF == "00000000000" || strCPF == "11111111111" ||
		strCPF == "22222222222" ||	strCPF == "33333333333" || strCPF == "44444444444" ||
		strCPF == "55555555555" || strCPF == "66666666666" || strCPF == "77777777777" ||
		strCPF == "88888888888" || strCPF == "99999999999")
		{ return false; }
	soma = 0;
	for (i=0; i < 9; i ++)
		{soma += parseInt(strCPF.charAt(i)) * (10 - i);}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{resto = 0;}
	if (resto != parseInt(strCPF.charAt(9)))
		{ return false; }
	soma = 0;
	for (i = 0; i < 10; i ++)
		{soma += parseInt(strCPF.charAt(i)) * (11 - i);}
	resto = 11 - (soma % 11);
	if (resto == 10 || resto == 11)
		{resto = 0;}
	if (resto != parseInt(strCPF.charAt(10)))
		{ return false; }
	return true;
}



function FormataCNPJ(Campo, teclapres){

   if(window.event){
    var tecla = teclapres.keyCode;
   }else  tecla = teclapres.which;

   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 populaCidades(estado,selecionado) {
	$.ajax({
		type: "POST",
		url: "populaCidades.php",
		data: "uf=" + estado + "&selecionado=" + selecionado,
		success: function(txt) {
			$('#ajax_cidades').html(txt);
		}
	});
}
