/*
**************************************
* formatCurrency Function v1.1       *
* Autor: Carlos R. L. Rodrigues      *
**************************************
*/


function formatCurrency(o, n, dig, dec){
	o.c = !isNaN(n) ? Math.abs(n) : 2;
	o.dec = typeof dec != "string" ? "," : dec, o.dig = typeof dig != "string" ? "." : dig;
	addEvent(o, "keypress", function(e){
		if(e.key > 47 && e.key < 58){
			var o, s, l = (s = ((o = this).value.replace(/^0+/g, "") + String.fromCharCode(e.key)).replace(/\D/g, "")).length, n;
			if(o.maxLength + 1 && l >= o.maxLength) return false;
			l <= (n = o.c) && (s = new Array(n - l + 2).join("0") + s);
			for(var i = (l = (s = s.split("")).length) - n; (i -= 3) > 0; s[i - 1] += o.dig);
			n && n < l && (s[l - ++n] += o.dec);
			o.value = s.join("");
		}
		e.key > 30 && e.preventDefault();
	});
}



//========================================================
// REQUIRES http://www.jsfromhell.com/geral/event-listener
//========================================================

addEvent = function(o, e, f, s){
    var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
    r[r.length] = [f, s || o], o[e] = function(e){
        try{
            (e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
            e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
            e.target || (e.target = e.srcElement || null);
            e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
        }catch(f){}
        for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
        return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
    for(var i = (e = o["_on" + e] || []).length; i;)
        if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
            return delete e[i];
    return false;
};



function adjustFormatCurrency(){
	inicio = "valor";
	for(x in document.all){

		// aplicando funcao de formatação de valores
		if(x.indexOf(inicio)>-1){
			formatCurrency(document.getElementById(x), 2);
		 }
	}
}


/***************************************************////
var qtdAreas = new Number(0)
function AdicionaArea(idArea,titulo){
	qtdAreas++;
	tabela = document.getElementById("tblInclusao"+idArea);
	var newRow = tabela.tBodies[0].rows[1].cloneNode(true);
	tabela.tBodies[0].appendChild(newRow);
	newRow.cells[0].innerHTML = '';
	resposta = '';

	/** MONTA HTML POR AJAX **/
	if(xmlhttp){
		var url="orcamento.divisao.ajax.php?idArea="+idArea+"&qtdAreas="+qtdAreas+"&"+Math.random();
//		alert(url);
		xmlhttp.open("GET", url,true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4){
				resposta = xmlhttp.responseText;
				newRow.cells[0].innerHTML = resposta;
				newRow.cells[0].innerHTML = "<hr/><br/><br/><a href='javascript:void(0);' class='lnkDuplicar' onclick='RemoveArea(this,"+idArea+");'>["+titulo+"]</a><br/>"+newRow.cells[0].innerHTML;
			}
		}
		//Enquanto ocorre a conexao e busca, mostra o texto nos campos...
		newRow.cells[0].innerHTML = "<span class='imgWait'>aguarde</span>";
		xmlhttp.send(null)
	}else{
		//Caso o navegador nao suporte XMLHttpRequest
		alert("Seu navegador não suporta algumas funcionalidades deste site.")
	}
	/** FIM DA MONTAGEM DE HTML POR AJAX **/
}

function RemoveArea(obj,idArea){
	tabela = document.getElementById("tblInclusao"+idArea);
	rowIndex = obj.parentNode.parentNode.rowIndex;
	try {
		tabela.deleteRow(rowIndex);
	} catch (ex) {
		alert(ex);
	}
}


function abre(IdTabela,fecha){
	if (document.getElementById(IdTabela).style.display == "none"){
		acao = 'abrir';
	} else {
		acao = 'fechar';
	}
	if(fecha){
		inicio = IdTabela.split('-');
		for(x in document.all){
			if(x.indexOf(inicio[0])>-1){
				document.getElementById(x).style.display = "none";
			 }
		}
	}

	if (acao == 'abrir'){
		document.getElementById(IdTabela).style.display = "block";
	} else {
		document.getElementById(IdTabela).style.display = "none";
	}
}
