function createXMLHTTP() 
{
	var ajax;
	try 
	{
		ajax = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	catch(e) 
	{
		try 
		{
			ajax = new ActiveXObject("Msxml2.XMLHTTP");
			alert(ajax);
		}
		catch(ex) 
		{
			try 
			{
				ajax = new XMLHttpRequest();
			}
			catch(exc) 
			{
				 alert("Esse browser não tem recursos para uso do Ajax");
				 ajax = null;
			}
		}
		return ajax;
	}
	   var arrSignatures = ["MSXML2.XMLHTTP.5.0", "MSXML2.XMLHTTP.4.0",
							"MSXML2.XMLHTTP.3.0", "MSXML2.XMLHTTP",
							"Microsoft.XMLHTTP"];
	   for (var i=0; i < arrSignatures.length; i++) 
	   {
			try 
			{
				var oRequest = new ActiveXObject(arrSignatures[i]);
				return oRequest;
			} 
			catch (oError) 
			{
			}
	   }
		   throw new Error("MSXML is not installed on your system.");
}

addEvent = function(o, e, f, s){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e){
		try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
	for(var i = (e = o["_on" + e] || []).length; i;)
		if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
			return delete e[i];
	return false;
};

MaskInput = function(f, m){
    function mask(e){
        var patterns = {"1": /[A-Z]/i, "2": /[0-9]/, "4": /[\xC0-\xFF]/i, "8": /./ },
            rules = { "a": 3, "A": 7, "9": 2, "C":5, "c": 1, "*": 8};
        function accept(c, rule){
            for(var i = 1, r = rules[rule] || 0; i <= r; i<<=1)
                if(r & i && patterns[i].test(c))
                    break;
                return i <= r || c == rule;
        }
        var k, mC, r, c = String.fromCharCode(k = e.key), l = f.value.length;
        (!k || k == 8 ? 1 : (r = /^(.)\^(.*)$/.exec(m)) && (r[0] = r[2].indexOf(c) + 1) + 1 ?
            r[1] == "O" ? r[0] : r[1] == "E" ? !r[0] : accept(c, r[1]) || r[0]
            : (l = (f.value += m.substr(l, (r = /[A|9|C|\*]/i.exec(m.substr(l))) ?
            r.index : l)).length) < m.length && accept(c, m.charAt(l))) || e.preventDefault();
    }
	//alert(f)
    for(var i in !/^(.)\^(.*)$/.test(m) && (f.maxLength = m.length), {keypress: 0, keyup: 1})
        addEvent(f, i, mask);
};


function valformatamarcado(campo) {
	campo.style.backgroundColor="#ffbbbb";
	campo.style.border="solid 2px #FF0000";
}
function valformatanormal(campo) {
	campo.style.backgroundColor="";
	campo.style.border="1px solid #090";
}

function valtexto(campo) {
	valformatanormal(campo);
	if (campo.value == ""){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
}
function valselect(campo) {
	valformatanormal(campo);
	if (campo.selectedIndex == 0){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
}
function valemail(campo) {
	valformatanormal(campo);
	var mail=campo.value;
    apos=mail.indexOf("@");
	dotpos=mail.lastIndexOf(".");
	if (apos<1||dotpos-apos<2) {
		valformatamarcado(campo);

		campo.focus();
		return 1;
	}
}
function valradio(campo) {
	for (var x=campo.length-1; x > -1; x--) {
			valformatanormal(campo[x]);
    }
    var cnt = -1;
    for (var i=campo.length-1; i > -1; i--) {
        if (campo[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) {
		return 0;
	}
    else {
		for (var v=campo.length-1; v > -1; v--) {
			valformatamarcado(campo[v]);
    	}
		return 1;
	}
}
function valnumero(campo) {
	valformatanormal(campo);
	var i;
	valor = 0
 for (i = 0; i < campo.value.length; i++){ 
 var c = campo.value.charAt(i);
 if (((c < "0") || (c > "9"))) valor = 1;
 }
	if (valor == 0){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
}

function valselectmult(campo) {
	valformatanormal(campo);
	for (var i = 0; i < campo.options.length; i++) {
		if (campo.options[i].selected) {
			return 
		}
	}
		valformatamarcado(campo);
	campo.focus();
	return 1;
}

function valcheckboxmulti(campo) {
	valformatanormal(campo);
	for (var i = 0; i < campo.options.length; i++) {
		if (campo.options[i].checked) {
			return 
		}
	}
		valformatamarcado(campo);
	campo.focus();
	return 1;
}


var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
 for (i = 0; i < s.length; i++){ 
 // Check that current character is number.
 var c = s.charAt(i);
 if (((c < "0") || (c > "9"))) return false;
 }
 // All characters are numbers.
 return true;
}

function stripCharsInBag(s, bag){
	var i;
 var returnString = "";
 // Search through string's characters one by one.
 // If character is not in bag, append to returnString.
 for (i = 0; i < s.length; i++){ 
 var c = s.charAt(i);
 if (bag.indexOf(c) == -1) returnString += c;
 }
 return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
 // EXCEPT for centurial years which are not also divisible by 400.
 return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
 } 
 return this
}

function valhora(campo){
	valformatanormal(campo);	
	horario = campo.value.split(':')
	if (horario.length == 2) {
	tamanho = horario[0].length
	tamanho2 = horario[1].length
	hora = Number(horario[0])
	minuto = Number(horario[1])
	
		if (tamanho < 2 && tamanho != 0) {
			campo.value = '0' + campo.value
			horario = campo.value.split(':')
			tamanho = horario[0].length
			tamanho2 = horario[1].length
			hora = Number(horario[0])
			minuto = Number(horario[1])
		}

		if (tamanho == 2 && tamanho2 == 2) {
			if (hora >= 0 && hora <= 23 && minuto >= 0 && minuto <= 59){
				return	
			}		
		}
	}
/*	if (horario.length > 1){
		if (Number(horario[0]) >= 0 && Number(horario[0]) <= 23){
			if (Number(horario[1]) >= 0 && Number(horario[1]) <= 59) {
				return; 
			}
		}
	}
}*/
	valformatamarcado(campo);
	campo.focus();
	return 1;	
}

function valdata(campo){
	valformatanormal(campo);
	var dtStr = campo.value
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
	if (strMonth.length<1 || month<1 || month>12){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		valformatamarcado(campo);
		campo.focus();
		return 1;
	}
}

function valcpf(campo) {	
    valformatanormal(campo)	
    var CPF = campo.value; // Recebe o valor digitado no campo
	
	// Verifica se o campo é nulo
	if (CPF != '') {
		//alert('Este campo é de preenchimento obrigatório!');
		if (CPF.length != 11) {
			valformatamarcado(campo);
			return 1;}
		// Aqui começa a checagem do CPF
		var POSICAO, I, SOMA, DV, DV_INFORMADO;
		var DIGITO = new Array(10);
		DV_INFORMADO = CPF.substr(9, 2); // Retira os dois últimos dígitos do número informado
		
		// Desemembra o número do CPF na array DIGITO
		for (I=0; I<=8; I++) {
			DIGITO[I] = CPF.substr( I, 1);
		}
		
		// Calcula o valor do 10º dígito da verificação
		POSICAO = 10;
		SOMA = 0;
		for (I=0; I<=8; I++) {
			SOMA = SOMA + DIGITO[I] * POSICAO;
			POSICAO = POSICAO - 1;
		}
		DIGITO[9] = SOMA % 11;
		if (DIGITO[9] < 2) {
			DIGITO[9] = 0;
		}
		else{
			DIGITO[9] = 11 - DIGITO[9];
		}
		// Calcula o valor do 11º dígito da verificação
		POSICAO = 11;
		SOMA = 0;
		for (I=0; I<=9; I++) {
			SOMA = SOMA + DIGITO[I] * POSICAO;
			POSICAO = POSICAO - 1;
		}
		DIGITO[10] = SOMA % 11;
		if (DIGITO[10] < 2) {
			DIGITO[10] = 0;
		}
		else {
			DIGITO[10] = 11 - DIGITO[10];
		}
		
		// Verifica se os valores dos dígitos verificadores conferem
		DV = DIGITO[9] * 10 + DIGITO[10];
		if (DV != DV_INFORMADO) {
			valformatamarcado(campo);
			return 1;
		} 
	}
}


function valcnpj(campo) {
	valformatanormal(campo)
	s = campo.value;
	if (isNaN(s)) {
		valformatamarcado(campo);			
		return 1;
	}
	var i;
	var c = s.substr(0,12);
	var dv = s.substr(12,2);
	var d1 = 0;
	for (i = 0; i <12; i++){
		d1 += c.charAt(11-i)*(2+(i % 8));
	}
	if (d1 == 0){
		valformatamarcado(campo);
		return 1;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9) d1 = 0;
	if (dv.charAt(0) != d1){
		valformatamarcado(campo);
		return 1;
	}
	d1 *= 2;
	for (i = 0; i < 12; i++){
		d1 += c.charAt(11-i)*(2+((i+1) % 8));
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9)
	d1 = 0;
	if (dv.charAt(1) != d1){
		valformatamarcado(campo);
		return 1;
	}
}

function arruma() {
	alvo = f_scrollTop() - 270
	if (alvo < 0 ) alvo = 0;
	atual = Number(document.getElementById("mapaEstados").style.top.replace("px",""));
	
  mover = Math.round((atual*20+alvo)/21)
	//document.getElementById("mapaEstados").innerHTML = mover;
	
	document.getElementById("mapaEstados").style.top = mover + "px";
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}