/**
* Mini-FrameWork desenvolvido por Wilian Fiabani - fiabani@gmail.com
* todos os direitos reservados ao autor
*/



function Ajax(){
	this.assincr = true;
	this.method = "GET";
	this.val = "";
	
	//carrega o conteudo de uma resposta ajax em uma div
	this.loadContent = function(url , div_name){
		if(xmlhttp) {
			xmlhttp.open(this.method, url , this.assincr);
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 4){
					if(xmlhttp.status == 200) {
						if(div_name != ""){
							w(div_name).setHTML(xmlhttp.responseText);
						}
					} else {
						alert(xmlhttp.statusText);
					}
				}
			}
			xmlhttp.send(null);
		}
	}
	
	//carrega o conteudo de uma ajax em uma var
	this.loadResponse = function(url){
		if(xmlhttp) {
			xmlhttp.open(this.method, url , this.assincr);
			xmlhttp.onreadystatechange = function() {
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200) {
						ajax.val = xmlhttp.responseText;
					} else {
						alert(xmlhttp.statusText);
					}
				}
			}
			xmlhttp.send(null);
		}
		return this.val;
	}
	
}

//retorna o objeto XMLHttp
function ajaxInit(){
	try {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(ex) {
			try {
				return new XMLHttpRequest();
			} catch(exc) {
				alert("Esse browser nÃ£o tem recursos para uso do Ajax");
				return null;
			}
		}
	}
}

//cria o objeto XMLHttp
var xmlhttp = ajaxInit();
//cria o objeto ajax
var ajax = new Ajax();


function ajaxInit(){
	try {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} catch(e) {
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch(ex) {
			try {
				return new XMLHttpRequest();
			} catch(exc) {
				alert("Esse browser nÃ£o tem recursos para uso do Ajax");
				return null;
			}
		}
	}
}


//cria o objeto XMLHttp
var xmlhttp = ajaxInit();
//cria o objeto ajax
var ajax = new Ajax();


//se tÃ¡ no IE, retorna true
function ie(){
	if(navigator.userAgent.indexOf("IE") >= 0){
		return true;
	}else{
		return false;
	}
}

function setCookie(cname, cvalue, days)
{
    var dtmData = new Date();
    if(days){
        dtmData.setTime(dtmData.getTime() + (days * 24 * 60 * 60 * 1000));
        var strExpires = "; expires=" + dtmData.toGMTString();
    }else{
        var strExpires = "";
    }
    document.cookie = cname+"="+cvalue+strExpires+"; path=/";
}

// FunÃ§Ã£o para ler o cookie.
function getCookie(cname){
    var strNomeIgual = cname + "=";
    var arrCookies = document.cookie.split(';');

    for(var i = 0; i < arrCookies.length; i++){
        var strValorCookie = arrCookies[i];
        while(strValorCookie.charAt(0) == ' '){
            strValorCookie = strValorCookie.substring(1, strValorCookie.length);
        }
        if(strValorCookie.indexOf(strNomeIgual) == 0){
            return strValorCookie.substring(strNomeIgual.length, strValorCookie.length);
        }
    }
    return null;
}


function w(obj){
	object = document.getElementById(obj);
	
	object.setVisible = function(st){
		if(st){
			this.style.display = "block";
		}else{
			this.style.display = "none";
		}
	}
	
	object.setClass = function(nclass){
		this.className = nclass;
	}
	
	object.setText = function(val){
		this.value = val;
	}
	
	object.getText = function(){
		return this.value;
	}
	
	object.setHTML = function(val){
		this.innerHTML = val;
	}
	
	object.getHTML = function(){
		return this.innerHTML;
	}
	
	object.setAlpha = function(alpha){
		this.style.filter = "alpha(opacity="+ alpha +")";
		this.style.opacity = alpha/100;
	}
	
	object.fadeIn = function(time) {
		alpha = 0;
		var i = setInterval(
				function() {
					if (alpha >= 100)
						clearInterval(i);
						object.setAlpha(alpha);
						alpha += 2;
				}, time);
	}
	
	object.fadeOut = function(time) {
		alpha = 100;
		var i = setInterval(
				function() {
					if (alpha <= 0){
						clearInterval(i);
					}
					object.setAlpha(alpha);
					alpha -= 2;
				}, time);
	}
	
	//aplica uma mascara de texto
	if(object.type && object.type == "text"){
		object.mask = function(ms){
			object.onkeydown = function(){
				setTimeout(function(){
					var val = object.value;
					var str = "";
					for(i=0 ; i<val.length ; i++){
						if(ms.charAt(i) == 9){
							var objStr = /^\d+$/;
							if(objStr.test(val.charAt(i))){
								str += val.charAt(i)
							}
						}else if(ms.charAt(i) == "a"){
							str += val.charAt(i);
						}else{
							str += ms.charAt(i)
						}
					}
					object.value = str;
				}, 2);
			}
		}
	}
	
	return object;
}

// retorna os tamanhos da pagina
function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//retorna um arquivo xml
function xmlLoader(url){
    //by Micox: micoxjcg@yahoo.com.br.
    if(window.XMLHttpRequest){
        var Loader = new XMLHttpRequest();
        Loader.open("GET", url ,false);
        Loader.send(null);
        return Loader.responseXML;
    }else if(window.ActiveXObject){
        var Loader = new ActiveXObject("Msxml2.DOMDocument.3.0");
        Loader.async = false;
        Loader.load(url);
        return Loader;
    }
}











function votaEnquete(codEnquete) {
	//
	var x = document.getElementsByName("enquete");
	//
	var codigo = 0;
	//
	var msg = document.getElementById("msgEnquete");
	msg.innerHTML = "";
	for(i=0;i<x.length;i++) {
		if(x[i].checked == true) {
			codigo = x[i].value;
		}
	}
	//
	if(codigo == 0) {
		msg.innerHTML = "<font color='#FF0000'>Selecione uma opção</font>";
	} else {
		var acde = new xmlBool("votaEnquete.php?codigo="+codigo+"&codEnquete="+codEnquete,"GET",msg,"mostraEnquete("+codEnquete+");","Você já votou nessa enquete!");
	}
}
function mostraEnquete (codigo) {
	//
	var ab = new xml("mostraEnquete.php?codigo="+codigo+"&m="+(Math.random()*10000),"GET",document.getElementById("mEnquete"),false,null);
}
function mostraOpcoes (codigo) {
	//
	var ab = new xml("mostraOpcoes.php?codigo="+codigo+"&m="+(Math.random()*10000),"GET",document.getElementById("mEnquete"),false,null);
}



var xml = function  (arquivo, metodo, local, added, funcao) {
	// carregando o objeto XML HttpRequest
	var xmlhttp;
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	//
	var resposta;
	// Verificando o Objeto XML Request
	if(xmlhttp) {
	// fazendo a requisição do arquivo
		xmlhttp.open(metodo,arquivo,true);
	// definindo a funcão
		xmlhttp.onreadystatechange = function () {
	/* redyState = 4  - Carregamento Completo */
			if(!document.getElementById("carregador"+local.id)) {
				local.innerHTML = "<div align='center' id='carregador"+local.id+"'></div>";
			}
			if(xmlhttp.readyState == 4) {
	// Status == 200 carregamento CORRETO
				if(xmlhttp.status == 200) {
					// armazendo os dados
					resposta = encodeURI(xmlhttp.responseText);
					//alert(resposta);
					// Verificando se há ou não a substituição
					if(added == false) {
						local.innerHTML = "";
					}
					local.innerHTML += decodeURI(resposta);
					if(funcao != null)
						eval(funcao);
				} else {
					// Inserindo a MSG de Erro
					local.innerHTML = encodeURI("Problemas em carregar o arquivo");
				}
			} else {
				// Mostrando a Mensagem de Carregamento
				if(xmlhttp.readyState == 1) {
					document.getElementById("carregador"+local.id).innerHTML = "<div class='textoDefault'>aguarde...</div>";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
					if(xmlhttp.readyState == 3) {
						document.getElementById("carregador"+local.id).innerHTML = "<div class='textoDefault'>processando...</div>";
					}
				}
			}

		}
		xmlhttp.send(null);
	}
}
var xmlBool = function  (arquivo, metodo, local, funcao,msg, txt) {
	// carregando o objeto XML HttpRequest

	var xmlhttp;
	try{
		xmlhttp = new XMLHttpRequest();
	}catch(ee){
		try{
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		}catch(e){
			try{
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			}catch(E){
				xmlhttp = false;
			}
		}
	}
	//
	if(!txt) {
		txt = "carregando...";
	}
	var resposta;
	// Verificando o Objeto XML Request
	if(xmlhttp) {
	// fazendo a requisição do arquivo
		xmlhttp.open(metodo,arquivo,true);
	// definindo a funcão
		xmlhttp.onreadystatechange = function () {
	/* redyState = 4  - Carregamento Completo */
			if(!document.getElementById("carregador"+local.id)) {
				local.innerHTML = "<div align='center' id='carregador"+local.id+"'></div>";
			}
			if(xmlhttp.readyState == 4) {
	// Status == 200 carregamento CORRETO
				if(xmlhttp.status == 200) {
					// armazendo os dados
					resposta = xmlhttp.responseText;
					// Verificando se há ou não a substituição
					if(eval(resposta)) {

						eval(funcao);
					}
					else
						local.innerHTML = "<span class='texto_normal' style='color:#FF0000;text-align:left'>"+msg+"</span>";
				} else {
					// Inserindo a MSG de Erro
					local.innerHTML = encodeURI("Problemas em carregar o arquivo");
				}
			} else {
				// Mostrando a Mensagem de Carregamento
				if(xmlhttp.readyState == 1) {
					document.getElementById("carregador"+local.id).innerHTML = "<div align='left' style='linkPequeno1;text-align:left'>"+txt+"</div>";
				} else {
					if(xmlhttp.readyState == 2) {
						document.getElementById("carregador"+local.id).innerHTML = "";
					}
				}
			}

		}
		xmlhttp.send(null);
	}
}
