function $(id) {
    return document.getElementById(id)
}

function $tags(tagName,obj) {
    if(typeof obj=="undefined")obj=document
    return obj.getElementsByTagName(tagName)
}

function $new(tagName){
    return document.createElement(tagName)
}

function $before(newo,oldo){
    oldo.parentNode.insertBefore(newo,oldo)
}

function $replace(newo,oldo){
    oldo.parentNode.replaceChild(newo,oldo)
}

function $remove(oldo){
    if(oldo.length){
        //quando se passa uma lista
        for(var i=0;i<oldo.length;i++){
            if(oldo[i]!=null)
                oldo[i].parentNode.removeChild(oldo[i])}
    }
    else{
        //quando passa um unico valor
        if(oldo!=null)
            oldo.parentNode.removeChild(oldo)}
}

$del=$remove

function $child(pai,filho){
    if(filho.length){
        for(var i=0;i<filho.length;i++)
            pai.appendChild(filho[i])}
    else
        pai.appendChild(filho)
}

function $delChild(pai,filho){
    if(typeof filho=="string")
        pai.removeChild($(filho))
    else{
        for(var i=0;i<filho.length;i++)
            pai.removeChild($(filho[i]))}
}

function $criaCarregando(pai){
    if(navigator.appName=="Microsoft Internet Explorer"){
	var browser = navigator.appName 
	var ver = navigator.appVersion 
	var thestart = parseFloat(ver.indexOf("MSIE"))+1 
	var brow_ver = parseFloat(ver.substring(thestart+4,thestart+7)) 
	if (brow_ver <= 8){
	    var novoFilho = document.createElement("<img id ='carrega' alt='Carregando' src='../imagens/loading.gif' style='margin-left:20px;' />")
	}else{
	    var novoFilho = $new('img')
	    novoFilho.setAttribute("id","carrega")
	    novoFilho.setAttribute("alt","Carregando")
	    novoFilho.setAttribute("src","../imagens/loading.gif")
	    novoFilho.setAttribute("style","margin-left:20px;")
	}
    }else{
        var novoFilho = $new('img')
        novoFilho.setAttribute("id","carrega")
        novoFilho.setAttribute("alt","Carregando")
        novoFilho.setAttribute("src","../imagens/loading.gif")
        novoFilho.setAttribute("style","margin-left:20px;")}
    $child($(pai),novoFilho)
}

function doObj(o){
    if(typeof o=="string")o=$(o)
    return o
}

function getElementsByClassName(classname, node) {
    if(!node) node = document.getElementsByTagName("body")[0];
    var a = [];
    var re = new RegExp('\\b' + classname + '\\b');
    var els = node.getElementsByTagName("*");
    for(var i=0;i< els.length;i++)
        if(re.test(els[i].className))a.push(els[i]);
    return a;
}

//Tratamento de eventos

function addEvent(o,evtype,fn){
    o=doObj(o)
    if(o.addEventListener){
        o.addEventListener(evtype,fn,true)
    }else{
        o.attachEvent("on"+evtype,fn)
    }
}

function getSource(e){
    if(typeof e=="undefined")e=window.event
    var target=e.target ? e.target : e.srcElement
    if(target.nodeType==3)target=target.parentNode
    return target
}

function cancelEvent(e){
    try{e.preventDefault()}catch(E){}
    return false
}

function cria_cookie(nome, valor, lista, expira_ano, expira_mes, expira_dia, path, domain, secure ){
    if(!pega_cookie(nome))
        var cookie_string = nome + "=" + valor
    else{
        igual=0
        valor_old = pega_cookie(nome).split(",")
        for(var i=0;i<valor_old.length;i++){
            if(valor_old[i]==valor)igual=1
        }
        if(igual==0){
            if(lista)var cookie_string = nome + "=" + (valor_old + "," + valor);
            else var cookie_string = nome + "=" + valor;
        }
    }

    if (expira_ano){
        var expires = new Date (expira_ano,expira_mes,expira_dia);
        cookie_string += "; expires=" + expires.toGMTString();
    }

    if (path)cookie_string += "; path=" + escape(path);
    if (domain)cookie_string += "; domain=" + escape(domain);
    if (secure)cookie_string += "; secure";
      
    document.cookie = cookie_string;
}

function delete_cookie(cookie_name){
	//if(pega_cookie(cookie_name)){
	var cookie_date = new Date ( );  // current date & time
	cookie_date.setTime ( cookie_date.getTime() - 1 );
	document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();//}
}

function tira_valor(cookie_name,valor){
    valor_cookie = pega_cookie(cookie_name);
    if(valor_cookie != null){
        valor_cookie = valor_cookie.split(",");
        delete_cookie(cookie_name)
        for(var i=0;i<valor_cookie.length;i++){
            if(valor_cookie[i]!=valor)cria_cookie(cookie_name,valor_cookie[i],true)
        }
    }
}

function pega_cookie(cookie_name){
    var results = document.cookie.match(cookie_name + '=(.*?)(;|$)');
    if (results)
        return (results[1]);
    else
        return null;
}

