function doit(){if (!window.print) {alert("Desculpe, mas seu browser nao suporta esta função. Por favor utilize a barra de trabalho para imprimir a página.");return}window.print();}
function CaixaAlta(obj) {s = obj.value.toUpperCase();obj.value = s;}
function showWait(id){$(id).innerHTML = "<span style='color:#FF0000'>Aguarde...</span>";}
function load(id){$(id).innerHTML = ajax.getContent();}
function envia(){document.form.submit();}
function formSubmit(campo, codigo, url) {if (campo!='' && codigo!='') {document.form[campo].value = codigo;}document.form.action=url;document.form.submit();}
function exibe(id) {element = $(id);if (element.style.display=='none') {element.style.display='block';}else {element.style.display='none';}}
function isNumber(theElement, theElementName) {s = theElement.value;if (isNaN(Math.abs(theElement.value)) && (s.charAt(0) != '#')) {if (isNumber.arguments.length  == 1)alert("O valor do campo deve ser um número.");else alert( theElementName +  "O valor do campo deve ser um número.");theElement.focus();	s = s.substring(0,0);theElement.value = s;return false;}return true;}
function isEmail(theElement, theElementName) {
	var s = theElement.value.toLowerCase();
	theElement.value=s;
	var filter=/^[A-Za-z0-9][A-Za-z0-9_.-]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return true;
	if (filter.test(s))
		return true;
	else
		alert("O e-mail informado não é válido.");
		theElement.focus(); 
		s = s.substring(0,0); 
		theElement.value = s;
		return false;
}

function verificaCpf(element) {
	var i;
	var s = element.value;
	if (s == ''){ return; }
	var c = s.substr(0,9);
	var dv = s.substr(9,2);
	var d1 = 0;
	for (i = 0; i < 9; i++) { d1 += c.charAt(i)*(10-i); }

	if (d1 == 0) {
		alert('O número do CPF informado é inválido!');
	    element.focus();
	    s = s.substring(0,0);
	    element.value = s;
		return false;
	}
	d1 = 11 - (d1 % 11);
	if (d1 > 9)
		d1 = 0;
	if (dv.charAt(0) != d1) {
		alert('O número do CPF informado é inválido!');
        element.focus();
		s = s.substring(0,0);
		element.value = s;
		return false;
	}

	d1 *= 2;
	for (i = 0; i < 9; i++) { d1 += c.charAt(i)*(11-i); }
	d1 = 11 - (d1 % 11);
	if (d1 > 9)
		d1 = 0;
	if (dv.charAt(1) != d1) {
		alert("O número do CPF informado é inválido!");
   		element.focus();
    	s = s.substring(0,0);
    	element.value = s;
		return false;
	}
}

num_digitos_cpf  = 11;
num_digitos_cnpj = 14;
num_digitos_cnpj_base = 8;

String.prototype.lpad = function(pSize, pCharPad) {var str = this;var dif = pSize - str.length;var ch = String(pCharPad).charAt(0);for (; dif>0; dif--) str = ch + str;return (str);}

String.prototype.trim = function() {return this.replace(/^\s*/, "").replace(/\s*$/, "");}
function setColor(el, bg) { if (el.style) el.style.backgroundColor = bg;}
function FormataCep(Campo,teclapres) {var tecla = teclapres.keyCode;vr = document.form[Campo].value;vr = vr.replace( ".", "" );vr = vr.replace( "-", "" );vr = vr.replace( "-", "" );tam = vr.length + 1;if ( tecla != 9 && tecla != 8 ) {if ( tam > 5 && tam < 9 )document.form[Campo].value = vr.substr( 0, 5 ) + '-' + vr.substr( 5, tam );}}

function FormataTelefone(campo,tammax,teclapres) {
	var tecla = teclapres.keyCode;

	//if (tecla < 48 || tecla > 57) { event.keyCode = 0; return false; }
	var telefone = campo.value;
	telefone = telefone.replace( "(", "" );
	telefone = telefone.replace( ")", "" );
	telefone = telefone.replace( " ", "" );
	telefone = telefone.replace( "-", "" );
	telefone = telefone.replace( "-", "" );

	if (telefone.length >= 10) { event.keyCode = 0; return false; }
	var tamanho = telefone.length;

	if (tamanho == 6) {
		var aux01 = telefone.substr(0, 3);
		var aux02 = telefone.substr(3, 4);
		campo.value = aux01 + "-" + aux02; return;
	}	
	if (tamanho == 7) { // Numeros de 8 digitos ou celulares. Ex.: 9986-6372
		var aux01 = telefone.substr(0, 4);
		var aux02 = telefone.substr(4, 4);
		campo.value = aux01 + "-" + aux02; return;
	}
	if (tamanho == 8) { // Numeros convencionais com prefixo. Ex.: (41) 524-4331
		var aux01 = telefone.substr(0, 2);
		var aux02 = telefone.substr(2, 3);
		var aux03 = telefone.substr(5, 4);
		campo.value = "(" + aux01 + ") " + aux02 + "-" + aux03;
		return;
	}
	if (tamanho == 9) { // Numeros de oito digitos e celulares com prefixo, ou 0800 de dez digitos. Ex.: (41) 3072-3035, 0800-41-3556
		var aux01 = telefone.substr(0, 4);
		if (aux01 == "0800") {
			var aux02 = telefone.substr(4, 2);
			var aux03 = telefone.substr(6, 4);
			campo.value = aux01 + "-" + aux02 + "-" + aux03;
			return;
		}
		aux01 = telefone.substr(0, 2);
		var aux02 = telefone.substr(2, 4);
		var aux03 = telefone.substr(6, 4);
		campo.value = "(" + aux01 + ") " + aux02 + "-" + aux03;
		return;
	}
	if (tamanho == 10) { // Numeros 0800 com 11 digitos. Ex.: 0800-555-0457
		var aux01 = telefone.substr(0, 4);
		var aux02 = telefone.substr(4, 3);
		var aux03 = telefone.substr(7, 4);
		campo.value = aux01 + "-" + aux02 + "-" + aux03;
		return;
	}
}

function SaltaCampo (campo,prox,tammax,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	if( tecla == 109 || tecla == 188 || tecla == 110 || tecla == 111 || tecla == 223 || tecla == 108 ) {
		document.form[campo].value = vr.substr( 0, vr.length - 1 );
	}
	else {
	 	vr = vr.replace( "-", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( "/", "" );
	 	vr = vr.replace( ",", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	vr = vr.replace( ".", "" );
	 	tam = vr.length;	
		
	 	if (tecla != 0 && tecla != 9 && tecla != 16 ) {
			if ( tam == tammax ) {
				if ( document.form[prox] )
					document.form[prox].focus();
			}
		}
	}
}

function FormataData(campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ) {
		if ( tam > 2 && tam < 5 )
			document.form[campo].value = vr.substr( 0, tam - 2  ) + '/' + vr.substr( tam - 2, tam );
		if ( tam >= 5 && tam <= 10 )
			document.form[campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, 2 ) + '/' + vr.substr( 4, 4 );
	}	
}

function FormataMesAno(campo,teclapres) {
	var tecla = teclapres.keyCode;
	vr = document.form[campo].value;
	vr = vr.replace( ".", "" );
	vr = vr.replace( "/", "" );
	vr = vr.replace( "/", "" );
	tam = vr.length + 1;

	if ( tecla != 9 && tecla != 8 ){
		if ( tam > 2 && tam < 7 )
			document.form[campo].value = vr.substr( 0, 2 ) + '/' + vr.substr( 2, tam ); }
}

function validaData(theElement, theElementName) {
	str = theElement.value;
	len = str.length;
	if(len != 10) {
    	if(len > 0) {
			alert("A data deve ser informada no formato DD/MM/AAAA.");
			s = str.substring(0,0); 
			theElement.value = s;
			return false;
		}
	}
	if(len == 10) {  
		strday = str.substring(0, 2);
		strmonth = str.substring(3, 5);
		stryear = str.substring(7, 10);
		if( isNaN(strday) || (strday < 0) || isNaN(stryear) || (stryear < 0)) {
			alert( "O valor especificado não é uma data válida.");
			s = str.substring(0,0); 
			theElement.value = s;
			theElement.focus();
			return false;
		} 
		if( (strmonth == "01") || (strmonth == "03") || (strmonth == "05") || (strmonth == "07") || (strmonth == "08") || (strmonth == "10") || (strmonth == "12") ) { monthdays = 31 }
		else if( (strmonth == "04") || (strmonth == "06") || (strmonth == "09") || (strmonth == "11") ) { monthdays = 30 }
		else if(strmonth == "02") { monthdays = ((stryear % 4) == 0) ? 29 : 28; }
		else {
			alert( "O valor especificado não é uma data válida.");			
			s = str.substring(0,0); 
			theElement.value = s;
			theElement.focus(); 
			return false;
		}
		if(strday > monthdays) {
			alert( "O valor especificado não é uma data válida.");			
			s = str.substring(0,0); 
			theElement.value = s;
			theElement.focus(); 
			return false;
		}
		if(theElement.value.substring(6, 10) > 2100) {
			alert( "O ano informado ("+theElement.value.substring(6, 10)+") não é aceito pelo sistema. \nVerifique se ele foi informado corretamente.");			
			s = str.substring(0,0); 
			theElement.value = s;
			theElement.focus(); 
			return false;
		}  
		if(theElement.value.substring(6, 10) < 1900) {
			alert( "O ano informado ("+theElement.value.substring(6, 10)+") não é aceito pelo sistema. \nVerifique se ele foi informado corretamente.");
			s = str.substring(0,0); 
			theElement.value = s;
			theElement.focus();
			return false;
		}
	}
	return true;
}