function PopUp(url, nome, largura, altura, sroll)
{
	var esq = (screen.width - largura) / 2;
	var top = (screen.height - altura) / 2;
	window.open(''+ url +'',''+ nome +'','width=' + largura + ',height=' + altura + ',top=' + top + ',left=' + esq + ',scrollbars='+ sroll +'');
}

function isNumber(myfield, e) // onKeyPress="javascript:return isNumber(this, event);"
{
	var key;
	var keychar;

	if (myfield.length == 0)
		myfield.value = 0;
	if (window.event)
		key = window.event.keyCode;
	else if (e)
		key = e.which;
	else
		return true;
	keychar = String.fromCharCode(key);
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==13) || (key==27))
		return true;
	else if ((("0123456789").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

function bloquearEnter(campo, e) // onKeyPress="javascript:return bloquearEnter(this,event);"
{
	var tecla = (window.Event) ? e.which : e.keyCode;
	if ( tecla == 13 ) { return false; }
}

function mascaraEntrada(campo, mascara, e) // onKeyPress="javascript:return mascaraEntrada(this,'##/##/####',event);"
{
	var tecla = (window.Event) ? e.which : e.keyCode;
	var valorCampo = campo.value;
	var seps = ".-,:;/";
	var estaMascara = mascara.substring(valorCampo.length, valorCampo.length + 1);
	var proxMascara = mascara.substring(valorCampo.length + 1, valorCampo.length + 2);
	var ret = false;
	if ( tecla == 8 || tecla == 9 || tecla == 13 || tecla == 0 ) { return true; } // BackSpace, TAB ou ENTER
	if ( tecla > 47 && tecla < 58 && ( estaMascara == "#" || estaMascara == "?" ) == true ) { ret = true; } // Números
	if ( tecla > 96 && tecla < 123 && ( estaMascara == "@" || estaMascara == "?" ) == true ) { ret = true; } // Letras
	if ( tecla > 64 && tecla < 91 && ( estaMascara == "@" || estaMascara == "?" ) == true ) { eet = true; }
	if ( ret == true )
	{
		campo.value = campo.value + String.fromCharCode(tecla);
		if ( seps.indexOf(proxMascara) > -1 )
		{
			campo.value = campo.value + proxMascara;
		}
	}
	return false;
}

function mascaraMoeda(campo, sepMilesimo, sepDecimal, e, tam) // onKeyPress="javascript:return mascaraMoeda(this,'.',',',event, tam);"
{
	var strCheck = '0123456789';
	var tecla = (window.Event) ? e.which : e.keyCode;
	if ( tecla == 8 || tecla == 9 || tecla == 13 || tecla == 0 ) { return true; }
	var key = String.fromCharCode(tecla);
	if ( strCheck.indexOf(key) == -1 ) { return false; }
	var len = campo.value.length;
	if (len >= tam) { return false; }
	for( var i = 0; i < len; i++ )
	{
		if ( (campo.value.charAt(i) != '0') && (campo.value.charAt(i) != sepDecimal) ) break;
	}
	var aux = '';
	for(; i < len; i++)
		if (strCheck.indexOf(campo.value.charAt(i))!=-1) aux += campo.value.charAt(i);
	aux += key;
	len = aux.length;
	if (len == 0) campo.value = '';
	if (len == 1) campo.value = '0'+ sepDecimal + '0' + aux;
	if (len == 2) campo.value = '0'+ sepDecimal + aux;
	if (len > 2) {
		var aux2 = '';
		for (var j = 0, i = len - 3; i >= 0; i--) {
			if (j == 3) {
				aux2 += sepMilesimo;
				j = 0;
			}
			aux2 += aux.charAt(i);
			j++;
		}
		var valor = ''
		var len2 = aux2.length;
		for (i = len2 - 1; i >= 0; i--)
		valor += aux2.charAt(i);
		valor += sepDecimal + aux.substr(len - 2, len);
		campo.value = valor;
	}
	return false;
}

function validaEmail(email)
{
	var exp = /^([0-9,a-z,A-Z]+)([.,_]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])?$/;
	if (!exp.test(email))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function validaData(data)
{
	var aData = data.split('/');
	var dia = aData[0];
	var mes = aData[1];
	var ano = aData[2];

	var dataAux = dia + mes + ano
	for (var i = 0; i < dataAux.length; i++)
	{
		caracter = dataAux.substring(i,i+1);
		if ((("0123456789").indexOf(caracter) == -1))
		{
			return false;
		}
	}

	if ((dia > 31) || (dia.length != 2)) { return false; }
	if ((mes > 12) || (mes.length != 2)) { return false; }
	if ((ano < 1900) || (ano > 2100) || (ano.length != 4)) { return false; }
}

function validaHora(horario)
{
	var aHora = horario.split(':');
	var hora = aHora[0];
	var minuto = aHora[1];

	var horaAux = hora + minuto;
	for (var i = 0; i < horaAux.length; i++)
	{
		caracter = horaAux.substring(i,i+1);
		if ((("0123456789").indexOf(caracter) == -1))
		{
			return false;
		}
	}

	if ((hora > 24) || (hora.length != 2)) { return false; }
	if ((minuto > 59) || (minuto.length != 2)) { return false; }
	if ((hora >= 24) && (minuto >= 01)) { return false; }
}

function validarCPF(cpf)
{
	var soma;
	var valor;
	var digito_verificador;

	var aux;
	var validos = '0123456789'
	var numero = '';
	for (var i = 0; i < cpf.length; i++)
	{
		aux = validos.indexOf(cpf.substring(i, i+1));
		if (aux >= 0) { numero += aux }
	}

	if(numero.length != 11)
	{
		return false;
	}

	// Cálculo do Primeiro dígito do CPF
	soma = 0;
	for (var i = 0; i < 9; i++)
	{
		soma += (10-i) * parseInt(numero.charAt(i));
	}
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2) digito_verificador = 0;

	if (parseInt(numero.charAt(9)) != digito_verificador)
	{
		return false;
	}

	// Cálculo do Segundo dígito do CPF
	soma = 0;
	for (var i = 0; i < 10; i++)
	{
		soma += (11-i) * parseInt(numero.charAt(i));
	}
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2) digito_verificador = 0;

	if (parseInt(numero.charAt(10)) != digito_verificador)
	{
		return false;
	}

	return true;
}

function validarCNPJ(cnpj)
{
	var soma;
	var peso;
	var digito_verificador;

	var aux;
	var validos = '0123456789'
	var numero = '';
	for (var i = 0; i < cnpj.length; i++)
	{
		aux = validos.indexOf(cnpj.substring(i, i+1));
		if (aux >= 0) { numero += aux }
	}

	if(numero.length != 14)
	{
		return false;
	}

	// Cálculo do Primeiro dígito do CNPJ
	soma = 0;
	peso = 5;
	for (var i = 0; i < 12; i++)
	{
		soma += (peso * (parseInt(numero.charAt(i))));
		peso--;
		if (peso == 1) peso = 9;
	}
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2)
	{
		digito_verificador = 0;
	}

	if (parseInt(numero.charAt(12)) != digito_verificador)
	{
		return false;
	}

	// Calculo do Segundo dígito do CNPJ
	soma = 0;
	peso = 6;
	for (var i = 0; i < 12; i++)
	{
		soma += (peso * (parseInt(numero.charAt(i))));
		peso--;
		if (peso == 1) peso = 9;
	}
	soma += digito_verificador * 2;
	digito_verificador = 11 - (soma % 11);
	if ((soma % 11) < 2) digito_verificador = 0;

	if(parseInt(numero.charAt(13)) != digito_verificador)
	{
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// As funções abaixo movem os ítens entre os selects.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
function compareOptionValues(a, b)
{
	var sA = parseInt( a.value, 36 );
	var sB = parseInt( b.value, 36 );
	return sA - sB;
}

function compareOptionText(a, b)
{
	var sA = parseInt( a.text, 36 );
	var sB = parseInt( b.text, 36 );
	return sA - sB;
}

function moveDualList( srcList, destList, moveAll )
{
	if ((srcList.selectedIndex == -1 ) && (moveAll == false)) { return; }

	newDestList = new Array( destList.options.length );
	var len = 0;

	for( len = 0; len < destList.options.length; len++ )
	{
		if ( destList.options[ len ] != null )
		{
			newDestList[len] = new Option(destList.options[len].text, destList.options[len].value, destList.options[ len ].defaultSelected, destList.options[len].selected);
		}
	}

	for( var i = 0; i < srcList.options.length; i++ )
	{
		if (srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll))
		{
			newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
			len++;
		}
	}

	newDestList.sort( compareOptionValues );
	for ( var j = 0; j < newDestList.length; j++ )
	{
		if ( newDestList[ j ] != null )
		{
			destList.options[ j ] = newDestList[ j ];
		}
	}

	for( var i = srcList.options.length - 1; i >= 0; i-- )
	{
		if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
		{
			srcList.options[i] = null;
		}
	}
}