function limitarTamanhoCampo(objeto, limite, tamanhoMaximo) {

    var tamanho = objeto.value.length;

    limite.value = (tamanhoMaximo - 1) - tamanho;


    if(tamanho >  tamanhoMaximo) {
    	alert('O valor do campo excedeu o valor máximo.');
    }

    return true;
}


// open a popUp window
function popUpWindow(url, target) {

    if (url == "") {
        return;
    }

    msgWin=window.open(url, target,"location=no,screenX=100,screenY=100,toolbar=no,directories=no,menubar=no,status=no,scrollbars=yes,resizable=yes,width=600,height=400");

}

// open a popUp window
function popUpWindowOpcoes(url, target,opcoes) {
    if (url == "") {
        return;
    }
    msgWin=window.open(url, target,opcoes);

}

// remove an item from select
function removeItemSelect(selectObject) {

    //var index = selectObject.selectedIndex;

    //if (index >= 0) {
        //selectObject.options[index] = null;
    //}

    if (selectObject.length > 0) {
        for (var i = 0; i < selectObject.options.length + 1; i++) {
            if (selectObject.options[i] != null && selectObject.options[i].selected)      {
                selectObject.options[i] = null;
                i = i - 1;
            }
        }
    }
}

// add a item of the input text into a select object
function addInputTextToSelect(inputObject, selectObject) {

    if(trim(inputObject.value) == '') {
    	alert('Informe um valor para ser adicionado.');
    	return;
    }
    var index = selectObject.length++;

    selectObject[index].text = inputObject.value;
    selectObject[index].value = inputObject.value;
    selectObject.options[index].className = "texto";

}

// add a item of the input text into a select object
function addValueToSelect(value, description,selectObject) {

    var index = selectObject.length++;

    selectObject[index].text = description;
    selectObject[index].value = value;
    selectObject.options[index].className = "texto";

}

// add a option select to another select object
function addSelectOptionToSelect(selectObjectOrig, selectObjectDest) {

    var index = selectObjectDest.length++;

    if (selectObjectOrig.selectedIndex > -1) {

    if(trim(selectObjectOrig[selectObjectOrig.selectedIndex].value) == '') {
    	alert('Informe um valor para ser adicionado.');
    	return;
    }
        selectObjectDest[index].text = selectObjectOrig[selectObjectOrig.selectedIndex].text;
        selectObjectDest[index].value = selectObjectOrig[selectObjectOrig.selectedIndex].value;
        selectObjectDest.options[index].className = "texto";

    }
}

// it adds an item of a select in another one select of the open window
function addItemToOpener(selectObject, selectOpener) {

    var form = document.form;
    var formOpener = window.opener.document.form;
    var optionSelected;

            if (selectObject.selectedIndex > -1) {

                for (var i = 0; i < selectObject.options.length; i++) {
                    if (selectObject.options[i].selected)      {

                        optionSelected = selectObject.options[i];

                        var index = selectOpener.length++;

                        selectOpener[index].text = optionSelected.text;
                        selectOpener[index].value = optionSelected.value;
                        selectOpener.options[index].className = "texto";

                    }
                }


	    } else {
	        return;
	    }
        window.close();

}

// add white spaces to a text
function addSpaces(text) {

    while (text.length < 50) {
        text += " ";
    }

    return text;
}

// refresh a select object
function refreshSelect(selectObject) {
    for (var i = 0; i < selectObject.length; i++) {
        selectObject.options[i].text = addSpaces(selectObject.options[i].text);
    }
}

// converts a select object into a hidden object
function convertSelectToHidden(selectObject, hiddenObject) {
    var values = "";

    for (var i = 0; i < selectObject.length; i++) {

        values += selectObject.options[i].value + "%";

    }
   hiddenObject.value = values;

}

function somenteNumeros(myfield,e)
{
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (((keycode>47) && (keycode<58) )  || (keycode==8)) { return true; }
	else return false;
}

function limparTextfield(checkComponent, textComponent) {


	if(!checkComponent.checked) {
		textComponent.value = '';
	}
}

function marcarCheckbox(checkComponent) {


	if(!checkComponent.checked) {
		checkComponent.checked = true;
	}
}

function existeRegistroSelect(codigoRegistro, descricaoRegistro, campo) {

	for(j=0 ; j < campo.length; j++ ){

		if(codigoRegistro == campo[j].value) {
			alert('O elemento ' + descricaoRegistro + ' já foi adicionado a lista.');
			return true;
		}

	}

	return false;
}