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

function vazio(campo, nome, tamanho) {

	if (tamanho == null)
		tamanho = 3;

	if (trim(campo.value) == "") {
		alert("Por favor preencha o campo " + nome + "!");
		campo.focus();
		return true;
	}
	/*
	 * else if (trim(campo.value).length < 3) { alert("O campo " + nome + " deve
	 * ter ao menos 3 caracteres!"); campo.focus(); return true; }
	 */

	//campo.value = trim(campo.value);
	return false;
}

function parado(campo, nome) {
	if (campo.selectedIndex == 0) {
		alert("Por favor selecione algo no campo " + nome + "!");
		campo.focus();
		return true;
	}

	return false;
}

function checkMail(mail) {
	var er = new RegExp(
			/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);

	if (typeof (mail) == "string") {
		if (er.test(mail))
			return true;
	} else if (typeof (mail) == "object") {
		if (er.test(mail.value))
			return true;
	}

	if (typeof (mail) == "object")
		mail.focus();

	alert('E-mail inválido!');
	return false;
}

function checkDate(pObj, greater_today, short) {

	if( short == null )
		var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/(19|20)?\d{2}$/;
	else
		var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))$/;

	var aRet = true;
	if ((pObj) && (pObj.value.match(expReg)) && (pObj.value != '')) {
		var dia = pObj.value.substring(0, 2);
		var mes = pObj.value.substring(3, 5);

		if( short == null )
			var ano = pObj.value.substring(6, 10);
		else
			var ano = new Date().getFullYear();

		if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30)
			aRet = false;
		else if ((ano % 4) != 0 && mes == 2 && dia > 28)
			aRet = false;
		else if ((ano % 4) == 0 && mes == 2 && dia > 29)
			aRet = false;

		if (greater_today != null) {
			hoje = new Date();

			h_dia = hoje.getDate();
			h_mes = hoje.getMonth();
			h_ano = hoje.getFullYear();

			if (h_dia < 10)
				h_dia = "0" + h_dia;

			if (h_ano < 2000)
				h_ano = "19" + h_ano;

			if (parseInt(ano.toString() + mes.toString() + dia.toString()) <= parseInt(h_ano
					.toString()
					+ h_mes.toString() + h_dia.toString()))
				aRet = false;
		}
	} else
		aRet = false;
	return aRet;
}

function ValidaTelefone(campo, nome){
	exp = /\(\d{2}\)\ \d{4}\-\d{4}/
    if(!exp.test(campo.value))
    {
    	campo.focus();
    	alert('Número de Telefone Inválido!');
    	return false;
    }

	return true;
}

function number_format(number, decimals, dec_point, thousands_sep) {
	// % nota 1: Para 1000.55 retorna com precisão 1 no FF/Opera é 1,000.5, mas
	// no IE é 1,000.6
	// * exemplo 1: number_format(1234.56);
	// * retorno 1: '1,235'
	// * exemplo 2: number_format(1234.56, 2, ',', ' ');
	// * retorno 2: '1 234,56'
	// * exemplo 3: number_format(1234.5678, 2, '.', '');
	// * retorno 3: '1234.57'
	// * exemplo 4: number_format(67, 2, ',', '.');
	// * retorno 4: '67,00'
	// * exemplo 5: number_format(1000);
	// * retorno 5: '1,000'
	// * exemplo 6: number_format(67.311, 2);
	// * retorno 6: '67.31'

	var n = number, prec = decimals;
	n = !isFinite(+n) ? 0 : +n;
	prec = !isFinite(+prec) ? 0 : Math.abs(prec);
	var sep = (typeof thousands_sep == "undefined") ? ',' : thousands_sep;
	var dec = (typeof dec_point == "undefined") ? '.' : dec_point;

	var s = (prec > 0) ? n.toFixed(prec) : Math.round(n).toFixed(prec); // fix
	// for
	// IE
	// parseFloat(0.55).toFixed(0)
	// = 0;

	var abs = Math.abs(n).toFixed(prec);
	var _, i;

	if (abs >= 1000) {
		_ = abs.split(/\D/);
		i = _[0].length % 3 || 3;

		_[0] = s.slice(0, i + (n < 0))
				+ _[0].slice(i).replace(/(\d{3})/g, sep + '$1');

		s = _.join(dec);
	} else {
		s = s.replace('.', dec);
	}

	return s;
}

function isDecimal(pVal, have_value) {
	if ((have_value != null)
			&& ((pVal == '0') || (pVal == '00') || (pVal == '0,00')))
		return false;
	else if ((have_value == null)
			&& ((pVal == '') || (pVal == '0') || (pVal == '00') || (pVal == '0,00')))
		return true;

	if (pVal.indexOf(',') == -1)
		pVal = pVal.toString() + ',00';

	var reTipo = /^[+-]?((\d+|\d{1,3}(\.\d{3})+)(\,\d*)?|\,\d+)$/;
	return reTipo.test(pVal);
}

function isCheckedGroup(check, name) {
	var fields = document.getElementsByName(check + '[]');

	if( fields.length != 0 )
	{
		for ( var i = 0; i < fields.length; i++) {
			if (fields.item(i).checked)
				return true;
		}
	
		alert('Favor selecionar um item em "' + name + '" !');
		fields.item(0).focus();
	}
	else
		alert('Favor cadastrar ao menos um "' + name + '" !');

	return false;
}

function limpa(texto) {
	array1 = new Array("á", "à", "â", "ã", "ä", "é", "è", "ê", "ë", "í", "ì",
			"î", "ï", "ó", "ò", "ô", "õ", "ö", "ú", "ù", "û", "ü", "ç", "Á",
			"À", "Â", "Ã", "Ä", "É", "È", "Ê", "Ë", "Í", "Ì", "Î", "Ï", "Ó",
			"Ò", "Ô", "Õ", "Ö", "Ú", "Ù", "Û", "Ü", "Ç", "-", "?", "!", "º",
			"$", "@", "%", "&", "*", "(", ")", ":", ";", ",", " ", "–", "#");
	array2 = new Array("a", "a", "a", "a", "a", "e", "e", "e", "e", "i", "i",
			"i", "i", "o", "o", "o", "o", "o", "u", "u", "u", "u", "c", "A",
			"A", "A", "A", "A", "E", "E", "E", "E", "I", "I", "I", "I", "O",
			"O", "O", "O", "O", "U", "U", "U", "U", "C", "_", "_", "_", "_",
			"_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_", "_");

	texto_limpo = "";

	for( var i=0; i < texto.length; i++ )
	{
		encontrado = false;
		for( var i1 =0; i1 < array1.length; i1++ )
		{
			if( texto.charAt(i) == array1[i1] )
			{
				texto_limpo += array2[i1];
				encontrado = true;
			}
		}

		if( !encontrado )
			texto_limpo += texto.charAt(i);
	}

	return texto_limpo.substring( 0, 25 );
}
