﻿function ContaCharPost(campoTxt,campoCount,tamanho){
	var contador = document.getElementById(campoCount);
	var cont = tamanho - campoTxt.value.length
	var AtualNode = contador.firstChild;
	var novoNode = document.createTextNode(cont);
	contador.replaceChild(novoNode, AtualNode);
	if (campoTxt.value.length > tamanho){
		campoTxt.value = campoTxt.value.substring(0, tamanho);
		cont = tamanho - campoTxt.value.length
		AtualNode = contador.firstChild;
		novoNode = document.createTextNode(cont);
		contador.replaceChild(novoNode, AtualNode);
		return true;
	}
} 

function Chr(AsciiNum){
	return String.fromCharCode(AsciiNum)
}


function Modal(acao,id) {
	var telaCinza = document.getElementById("telaCinza");
	var Obj = document.getElementById(id);		
	if(acao == "show"){	
		telaCinza.style.display = "block";
		Obj.style.display = "block";
	} else {
		telaCinza.style.display = "none";
		Obj.style.display = "none";	
	}
}

function Acompanhar(id,id_div){
	var f = document.formMb;
	f.action = f.destino.value + "proc/acompanhar"
	f.hash_usuario.value = id;
	f.id_div.value = id_div;
	f.submit();	
}

function NaoAcompanhar(id,id_div){
	var f = document.formMb;
	f.action = f.destino.value + "proc/nao-acompanhar"
	f.hash_usuario.value = id;
	f.id_div.value = id_div;
	f.submit();	
}

function ExcluirAcompanhante(id,id_div){
	var f = document.formMb;
	f.action = f.destino.value + "proc/excluir-acompanhante"
	f.hash_usuario.value = id;
	f.id_div.value = id_div;
	f.submit();	
}

function AcompanharTag(id,id_div){
	var f = document.formMb;
	f.action = f.destino.value + "proc/acompanhar-tag"
	f.hash_tag.value = id;
	f.id_div.value = id_div;
	f.submit();	
}

function NaoAcompanharTag(id,id_div){
	var f = document.formMb;
	f.action = f.destino.value + "proc/nao-acompanhar-tag"
	f.hash_tag.value = id;
	f.id_div.value = id_div;
	f.submit();	
}

function Favorito(acao,id,id_div){
	var f = document.formMb;
	if(acao == "add"){
		f.action = f.destino.value + "proc/adcionar-favorito";
	} else {
		f.action = f.destino.value + "proc/excluir-favorito";
	}
	f.target = "frmPost";
	f.hash_post.value = id;
	f.id_div.value = id_div;
	f.submit();
}

function MBusca(){
	var f = document.frm_busca;
	error_found = false;
	CheckTextFieldMinimo(f.key_busca, jsRequired, 100,2,jsNoTags,"busca");
	if (!error_found) {
		f.submit();
	}
}

function VerificaLogin() {	
	var f = document.formLogin;
	error_found = false;
	CheckTextField(f.login, jsRequired, 30,jsNoTags,"login");
	CheckTextField(f.senha, jsRequired,30,jsNoTags,"senha");
	if (!error_found) {
		f.submit();
	}
}

 function VerificaCadastro() {	
	var f = document.frmCad;
	error_found = false;
	CheckTextField(f.nome, jsRequired,30,jsNoTags,"nome");
	CheckTextField(f.sobrenome, jsRequired,40,jsNoTags,"sobrenome");
	CheckEmailField(f.email, jsRequired, 60, "e-mail");
	CheckTextFieldMinimo(f.senha, jsRequired, 100,6,jsNoTags,"senha");
	CheckRadioField(f.sexo, "sexo");
	if (!error_found) {
		f.submit();
	}
}

function ResponderPost(login){
	var f = document.formMb;
	location = f.destino.value + login
}

function RPPermalink(){
	var f = document.formPost;
	f.submit();
}

function AddSugerido(id){
	var f = document.formSugeridos;
	f.action = f.base.value + "add-usuario-sugerido";
	f.hash_usuario.value = id;
	EnviarPostRetorno("formSugeridos","inc_amigos")
}

function NegarSugerido(id){
	var f = document.formSugeridos;
	f.action = f.base.value + "negar-usuario-sugerido";
	f.hash_usuario.value = id;
	EnviarPostRetorno("formSugeridos","inc_amigos")
}

function EnviarFaleConosco(){
	var f = document.formFale;
	error_found = false;
	CheckTextField(f.nome, jsRequired,100,jsNoTags,"nome");
	CheckEmailField(f.email, jsRequired, 100, "e-mail");
	CheckTextField(f.mensagem, jsRequired,5000,jsNoTags,"mensagem");
	if (!error_found) {
		var objMsgErro = document.getElementById("msgErro");
		var objMsgSucesso = document.getElementById("msgSucesso");
		objMsgErro.style.display = "none";
		objMsgSucesso.style.display = "none";
		f.submit();
	}
}

function Ajax(){
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	return xmlhttp
}

function EnviarPost(idForm){
	var http = Ajax();
	var url = document.getElementById(idForm).action
	var params = BuscaElementosForm(idForm);
	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");			
	http.send(params);
}


function EnviarPostRetorno(idForm,IdDiv){
	var http = Ajax();
	var url = document.getElementById(idForm).action
	var params = BuscaElementosForm(idForm);
	http.open("POST", url, true);
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.onreadystatechange = function() {
		if (http.readyState==4){
	    	var mensagem    = http.responseText;
	        mensagem        = mensagem.replace(/\+/g," ");
	        mensagem        = unescape(mensagem);
	        document.getElementById(IdDiv).innerHTML=mensagem;
	     }
    }
	http.setRequestHeader("Connection", "close");			
	http.send(params);
}


function Carregar(url,IdDiv){
	var Conexao = Ajax();
	Conexao.open("GET",url,true);
   	Conexao.onreadystatechange = function() {
		if (Conexao.readyState==4){
	    	var mensagem    = Conexao.responseText;
	        mensagem        = mensagem.replace(/\+/g," ");
	        mensagem        = unescape(mensagem);
	        document.getElementById(IdDiv).innerHTML=mensagem;
	     }
    }
	Conexao.send(null);
}

// Pega os elementos de um form e monta uma querystring
function BuscaElementosForm(idForm) { 
    var elementosFormulario = document.getElementById(idForm).elements; 
    var qtdElementos = elementosFormulario.length; 
    var queryString = ""; 
    var elemento; 

    //Cria uma funcao interna para concatenar os elementos do form 
    this.ConcatenaElemento = function(nome,valor) { 
    	if (queryString.length>0) { 
			queryString += "&"; 
		} 
		queryString += encodeURIComponent(nome) + "=" + encodeURIComponent(valor); 
	 }; 

    //Loop para percorrer todos os elementos 
    for (var i=0; i<qtdElementos; i++) { 
        //Pega o elemento 
        elemento = elementosFormulario[i]; 
        if (!elemento.disabled) { 
            //Trabalha com o elemento caso ele nao esteja desabilitado 
            switch(elemento.type) { 
                //Realiza a acao dependendo do tipo de elemento 
                case 'text': case 'password': case 'hidden': case 'textarea': 
                    this.ConcatenaElemento(elemento.name,elemento.value); 
                    break; 
                case 'select-one': 
                    if (elemento.selectedIndex>=0) { 
                        this.ConcatenaElemento(elemento.name,elemento.options[elemento.selectedIndex].value); 
                    } 
                    break; 
                case 'select-multiple': 
                    for (var j=0; j<elemento.options.length; j++) { 
                        if (elemento.options[j].selected) { 
                            this.ConcatenaElemento(elemento.name,elemento.options[j].value); 
                        } 
                    } 
                    break; 
                case 'checkbox': case 'radio': 
                    if (elemento.checked) { 
                        this.ConcatenaElemento(elemento.name,elemento.value); 
                    } 
                    break; 
            } 
        } 
    } 
    return queryString; 
} 

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

