/**
 * loadCombo- Cleyton 18/6/2004 10:37
 *
 * Envia os dados de um form, que contenha combos, para o um iframe. 
 * 
 * Obs: Esta função é usada somente para quando for carregar combos.
**/
function loadCombo(objCaller, cboName4Load, text4Empty) {
	var obj4Load = eval("document.forms[0]." + cboName4Load);
	var objIframe = GetObj('ifrLoadCombo');
	var oldAction = document.forms[0].action;
	
	if (objCaller.selectedIndex != -1) {
		obj4Load.options[0].text = "Carregando...";
		
		document.forms[0].action = objIframe.src;
		document.forms[0].target = objIframe.name;
		document.forms[0].cbo.value = cboName4Load;
		document.forms[0].submit();
		
		document.forms[0].action = oldAction;
		document.forms[0].target = '_self';
	} else {
		ClearCombo(obj4Load);
		objCombo.options[0] = new Option("Selecione " + text4Empty, "0");
	}
}

/**
 * ClearCombo - Cleyton 18/6/2004 12:26
 *
 * Elimina todas as options de uma combo.
**/
function ClearCombo(objCbo) {
	objCbo.options.length = 0;
}
		
/**
 * GetObj- Cleyton 19/mai/2004
 *
 * Retorno: objeto
**/
function GetObj(id){
	if(document.getElementById){
		//NE
		return document.getElementById(id);
	}else{
		//IE
		if (document.all) {
			return document.all(id);	
		} else {
			return document(id);	
		}
	}
}

/**
 * chkEmail- Cleyton 18/mai/2004
 *
 * Retorno: true se o email estiver correto, false se contrário.
**/
function chkEmail(campo) {
	var email = eval('document.forms[0].' + campo + '.value');
	var ok = true;
	
	if (email.length == 0) {
		ok = false;
	}else if (email.indexOf("@") == -1) {
		ok = false;
	}else if (email.indexOf(".") == -1) {
		ok = false;
	}
	
	if (!ok) {
		alert("Email Inválido!");
		document.frm.Email.focus();
		return ok;			
	}
	
	return ok;
}


/**
 * RadioValue - Cleyton 17/set/2003
 * Parâmetro: ctrlradio - elemento
 *
 * Retorno: Quando se tem vários elementos RADIO, lhe retorna o VALUE
 *			do indice checado, caso contrário retorna "-".
**/
function radioValue(ctrlradio) {
	for(var i=0; i < ctrlradio.length; i++) {
		if (ctrlradio[i].checked){
			return ctrlradio[i].value;
		}
	}
}

function formAction(URL) {
	var frm = document.forms[0];
	frm.action = URL;
	frm.submit();
}

function visivel(obj,v) {
	if (v) {
		//obj.style.visibility='visible';
		//obj.style.position='relative';
		obj.style.display='block';
	} else {
		//obj.style.visibility='hidden';
		//obj.style.position='absolute';
		obj.style.display='none';
	}
	//alert(obj.name);
	return true;
}

function visivelById(id,v) {
	return visivel(document.getElementById(id),v);
}

function onlyNumber (ctrl, e, max) {
	var key = e.keyCode;

	//rotulo.innerHTML=key;

	//if (e.shiftKey) return false;

	// cima 38, baixo 40
	// enter, tab, backspace, delete, ?, 0..9, 0..9 (numpad), <, >, home, end
	if (!(key==13 || key==9 || key==8 || key==46 || key==191 || (key>47 && key<58)  || (key>95 && key<106)||
		key==37 || key==39 || key==35 || key==36)) return false;
	
	// vírgula
	//if (!(key==188 && ctrl.value.indexOf(',') == -1)) return false;

	//FormatarNumero(ctrl, e, n);
	
	if ((key>47 && key<58) && (ctrl.value.length >= max)) return false;
	
	return true;
}

function FormatarMascara(ctrl, e, mascara, valor, direcao) {
	//var valor = NumeroPuro(ctrl.value); alert(valor);
	var ini; // posição do primeiro caractere a ser analisado
	var saida = '';
	var m; // posição na string de Máscara
	var n; // posição na string de Número (Valor)
	
	var key = e.keyCode;
	if ((key>=35 && key<=40) || e.shiftKey) return false

	if (direcao == 1) { // esquerda para direita
		m = 0;
		n = 0;
		while (m < mascara.length && n < valor.length) {
			if (mascara.charAt(m) == '#') {
				saida = saida + valor.charAt(n);
				n++;
			} else {
				saida = saida + mascara.charAt(m);
			}
			m++;
		}
		
	} else { // direita para esquerda
		m = mascara.length - 1;
		n = valor.length - 1;
		while (m >= 0 && n >= 0 ) {
			if (mascara.charAt(m) == '#') {
				saida = valor.charAt(n) + saida;
				n--;
			} else {
				saida = mascara.charAt(m) + saida;
			}
			m--;
		}
	}
	ctrl.value = saida;
}

function NumeroPuro(s) {
	var str_numero = '';
	for (var i = 0; i < s.length; i++)
		if (s.charCodeAt(i)>47 && s.charCodeAt(i)<58)
			str_numero = str_numero + s.charAt(i);
	
	// tirar zeros a esquerda
	//numero = Math.ceil(s).toString();
	//if (s == '0') s = '';
	/*
	var numero = parseInt(texto); alert('numero: ' + numero);
	if (parseInt(texto) == 0)
		return '';
	else
		return texto;
	*/
	return str_numero
}

function CleanString (str, str_re) {
	var saida = '';
	var re = new RegExp(str_re);
	
	for (var i = 0; i < str.length; i++)
		if (re.test(str.substr(i,1)))
			saida = saida + str.charAt(i);
	
	return saida
}

