function getControl(controlName){
	return document.getElementById(controlName);
}

function getOpenerControl(controlName){
	return window.opener.document.getElementById(controlName);
}

function getChoice(control){
	for(i=0;i<control.length;i++)
		if (control.options[i].selected) return control.options[i].value;
	return null;
}

function setSelectItem(control, value){
	for(i=0;i<control.length;i++){
		if (control.options[i].value == value){
			control.options[i].selected = true; 
			break;
		}
	}
}

// Funções de TRIM
    //retira caracteres em ranco à esquerda da string
    function leftTrim(sString)
    {
	    while (sString.substr(0,1) == ' ')
	    {
		    sString = sString.substr(1, sString.length);
	    }
	    return sString;
    }

    //retira caracteres em ranco à direita da string
    function rightTrim(sString)
    {
	    while (sString.substr(sString.length-1, sString.length) == ' ')
	    {
		    sString = sString.substr(0,sString.length-1);
	    }
	    return sString;
    }

    //retira caracteres em branco dos dois lados da string
    function trim(sString)
    {
	    while (sString.substr(0,1) == ' ')
	    {
		    sString = sString.substr(1, sString.length);
	    }
	    while (sString.substr(sString.length-1, sString.length) == ' ')
	    {
		    sString = sString.substr(0,sString.length-1);
	    }
	    return sString;
    }
//

function openWindow(url, windowName, w, h, windowFeatures)
{
	var winleft = (screen.width - w) / 2;
	var wintop = (screen.height - h) / 2;
	
	params = 'height=' + h + ',width=' + w + ',top=' + wintop + ',left=' + winleft;
	
	if (windowFeatures)
	{
		params = params + ',scrollbars=yes,resizable=yes';
	}
	
	return win = window.open(url, windowName, params);
}

function validateArgStr(){
	return ((arguments[0] != null) ? arguments[0] : "");
}

function validateArgInt(){
	return ((arguments[0] <= 0) ? arguments[1] : arguments[0]);
}

function showMessageBox() {
	var strPage = validateArgStr(arguments[0]);
	var strMessage = validateArgStr(arguments[1]);
	var strHeader = validateArgStr(arguments[2]);
	var strTitle = validateArgStr(arguments[3]);
	var intWidth = validateArgInt(arguments[4], 350);
	var intHeight = validateArgInt(arguments[5], 300);
	var intType = validateArgInt(arguments[6], 1);
	var strRedirect = validateArgStr(arguments[7]);
	
	url = strPage;
	url += "?msg=" + strMessage;
	url += "&header=" + strHeader;
	url += "&title=" + strTitle;
	url += "&type=" + intType;
	url += "&redirect=" + strRedirect;
	url += "&height=" + intHeight;
	
	try
	{
		var returnValue = "";
		if(window.showModalDialog)
			returnValue = window.showModalDialog(url,"","dialogHeight:" + intHeight.toString() + "px;dialogWidth:" + intWidth.toString() + "px;center:yes;status:no;");
		else
		{
			win = openWindow(url, "MessageBox", intWidth, intHeight, false);
			return false;
		}
		
		if (intType == 3){
			if (returnValue != null) 
				return (returnValue == "true");
		}
		else{
			if (window.showModalDialog && strRedirect != "") 
				window.location.href = strRedirect;
		}
	}
	catch(e)
	{
		//alert(e.name & " :: " & e.message);
	}
}

function doPostBackOpener(p_ButtonClientID)
{
	window.opener.__doPostBack(p_ButtonClientID,'');
}

function confirmMessageBox(p_Confirm, p_ButtonClientID, p_UrlRedirect)
{	
	if (window.showModalDialog)
		window.returnValue = (p_Confirm ? "true" : "false");
	else
		if (p_Confirm) doPostBackOpener(p_ButtonClientID);
	
	window.close();
	
	if (!window.showModalDialog && p_UrlRedirect != "")
		window.opener.location.href = p_UrlRedirect;
}

//exemplo de uso do noLetters: onKeyDown="return noLetters(event);"
function noLetters(e) {
    var tecla;
    if (!e)
        e = window.event;

    // verificação cross-browser
    if (e.which)
        tecla = e.which;
    else if (e.keyCode)
        tecla = e.keyCode;

    if ((tecla >= 48 && tecla <= 57) || (tecla >= 96 && tecla <= 105) || tecla == 8 || tecla == 37 || tecla == 39 || tecla == 46 || tecla == 9) {
        /*9: tab; 8 = backspace; 37 = seta pra esquerda; 39 = seta pra direita; 46 = delete*/
        return true;
    }
    else {
        return false;
    }
}

//formata uma data corretamente
function formataData(obj) {
    if (parseInt(obj.value.length) == 2)
        obj.value += "/";
    if (parseInt(obj.value.length) == 5)
        obj.value += "/";
}

// Formata o campo de CEP
function formataCep(obj) {
    if (parseInt(obj.value.length) == 5)
        obj.value += "-";
}