﻿/**
 * AJAX idalem.com - Marlos Irapuan
 */
 

// --> Inicio do código

var d=document;
var xmlhttp = getXmlHttpRequest();
	
function getXmlHttpRequest() {
	if (window.XMLHttpRequest) // Mozila, Safari
	{
		return new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject)  // IE
	{
		try {
			return new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				return new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) { }
		}
	}
}


// ENVIAR VOTO

// DIV da enquete, FORM da votacao, SELECAO da enquete,, ID enquete
//function enviarVoto(gdiv, gform, gcheckradio) {
function enviarVoto(gdiv, gcomando, gid) {
	
	var tdiv		= gdiv;
	var tcomando	= gcomando;
	var tid			= gid;
	
	
	// se for a votacao
	if (tcomando == 'votar')
	{
		for (i=0;i<d.frmEnquete.elements.length;i++) {
			if ((d.frmEnquete.elements[i].type == "checkbox") || (d.frmEnquete.elements[i].type == "radio")) {
				var marcado = d.frmEnquete.elements[i].checked;
				var valor	= d.frmEnquete.elements[i].value;
				if (marcado) 
				{
					//window.alert('id da enquete: ' + tid);
					var valor_sel = d.frmEnquete.elements[i].value;
					var ok = true;
				}
			}
		}
		if (!ok) 
		{
			window.alert('Selecione uma opcao de voto!');
			return false;
		}
	}
	
	if (ok != true)
	{
		// Se for listar a enquete
		if (tcomando == 'listar') acao = 'listarEnquetes';
		// Ver resultado parcial
		if (tcomando == 'resultado') acao = 'verResultado';
		
	} else {
		acao = 'votar';
	}
	if (valor_sel) valor = valor_sel;	
	//window.alert('acao: ' + acao + ' id da enquete: ' + tid);
	
	xmlhttp.open("GET", "__voto.php?acao="+acao+"&valor="+valor+"&idenquete="+tid+"&" + Math.ceil( Math.random() * 100000 ), true);
	xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=ISO-8859-1");
	xmlhttp.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
	xmlhttp.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
	xmlhttp.setRequestHeader("Pragma", "no-cache");
	
	xmlhttp.onreadystatechange = function() {
	
		if (xmlhttp.readyState == 1) 	// Carregando
		{
			d.getElementById(tdiv).innerHTML = "Processando ... <br/><div align='center'><img src='loading.gif' /></div>";
		}
	
		if (xmlhttp.readyState == 4) 	// Completado
		{
			if(xmlhttp.status == 200) 	// Confirmado
			{
				d.getElementById(tdiv).innerHTML = xmlhttp.responseText;
			} else { // Erro
				d.getElementById(tdiv).innerHTML = "Erro ao processar conteúdo: " + xmlhttp.status;
			}	
	   }
	}	
	xmlhttp.send(null);
	//d.getElementById(gtext).value = "";
}


// FAVORITOS

function addFav(){
    var url      = "http://www.metalro.com.br/";
    var title    = "METALRO.COM.BR - O Maior Portal de Heavy Metal da Amazônia";
    if (window.sidebar) window.sidebar.addPanel(title, url,"");
    else if(window.opera && window.print){
        var mbm = document.createElement('a');
        mbm.setAttribute('rel','sidebar');
        mbm.setAttribute('href',url);
        mbm.setAttribute('title',title);
        mbm.click();
    }
    else if(document.all){window.external.AddFavorite(url, title);}
}


// MOSTRA DATA

function mostraDataAtual() {
	mydate 		= new Date();
	var myday 		= mydate.getDay();
	var mymonth 	= mydate.getMonth();
	var myweekday	= mydate.getDate();
	var myyear		= mydate.getFullYear();
	var weekday		= myweekday; 
	var day;
	
	if(myday == 0)
	day = " Domingo, "; 
	
	else if(myday == 1)
	day = " Segunda-feira, "; 
	
	else if(myday == 2)
	day = " Terça-feira, "; 
	
	else if(myday == 3)
	day = " Quarta-feira, "; 
	
	else if(myday == 4)
	day = " Quinta-feira, "; 
	
	else if(myday == 5)
	day = " Sexta-feira, "; 
	
	else if(myday == 6)
	day = " Sábado, "; 
	
	if(mymonth == 0)
	month = "Janeiro "; 
	
	else if(mymonth ==1)
	month = "Fevereiro "; 
	
	else if(mymonth ==2)
	month = "Março "; 
	
	else if(mymonth ==3)
	month = "Abril "; 
	
	else if(mymonth ==4)
	month = "Maio "; 
	
	else if(mymonth ==5)
	month = "Junho "; 
	
	else if(mymonth ==6)
	month = "Julho "; 
	
	else if(mymonth ==7)
	month = "Agosto "; 
	
	else if(mymonth ==8)
	month = "Setembro "; 
	
	else if(mymonth ==9)
	month = "Outubro "; 
	
	else if(mymonth ==10)
	month = "Novembro "; 
	
	else if(mymonth ==11)
	month = "Dezembro "; 
	
	document.write(day);
	document.write(myweekday+" de "+month+ " de "+myyear);
	// -->
}


// Confere Busca
function confereBusca() {
	d = document.pesquisar;
	if (!d.campo_pesquisa.value) {
		window.alert('Preencha o campo de busca!');
		d.campo_pesquisa.focus();
		return false;
	} 
	else if (d.campo_pesquisa.value.length < 3) {
		window.alert('Preencha com mais de 3 caracteres a busca!');
		d.campo_pesquisa.focus();
		return false;
	}
	
	return true;
}

// Confere cadastro de evento
function confereCadastroEvento() {
	
	
	d = document.form_cadastro_evento;
	var valido = validarData(d.txtData);
	
	if (!d.txtNome.value) {
		window.alert('Preencha o nome do evento!');
		d.txtNome.focus();
		return false;
	} 
	
	if (!d.txtData.value) {
		window.alert('Preencha a data do evento!');
		d.txtData.focus();
		return false;
	}
	else if (valido == false) {
		window.alert('Data invalida!');
		d.txtData.focus();
		return false;
	}
	else if (d.txtNome.value.length < 3) {
		window.alert('Preencha um nome com mais de 2 caracteres!');
		d.txtNome.focus();
		return false;
	}
	else if (!d.txtLocal.value) {
		window.alert('Preencha com um local!');
		d.txtLocal.focus();
		return false;
	}
	else if (!d.txtCidade.value) {
		window.alert('Preencha a cidade do evento!');
		d.txtCidade.focus();
		return false;
	}
	else if (d.txtHorario.value==false) {
		window.alert('Coloque o horario da festa!');
		d.txtHorario.focus();
		return false
	}
	else if (d.txtValor.value==false) {
		window.alert('Coloque um valor do ingresso!');
		d.txtValor.focus();
		return false
	}
	else if (d.txtDescricao.value==false) {
		window.alert('Preencha a descricao do evento!');
		d.txtDescricao.focus();
		return false
	}
	else if (d.txtResp.value==false) {
		window.alert('Informe as letras do quadrado!');
		d.txtResp.focus();
		return false
	}

	
	return true;
}

// Confere cadastro de classificado
function confereCadastroClassificado() {
	d = document.form_cadastro_classificado;
	if (!d.txtNome.value) {
		window.alert('Preencha o seu nome!');
		d.txtNome.focus();
		return false;
	} 
	else if (d.txtNome.value.length < 3) {
		window.alert('Preencha um nome com mais de 2 caracteres!');
		d.txtNome.focus();
		return false;
	}
	else if (!d.txtEmail.value) {
		window.alert('Preencha o seu email!');
		d.txtEmail.focus();
		return false;
	}
	else if (isEmail(d.txtEmail.value)==false) {
		window.alert('Entre com um email valido!');
		d.txtEmail.focus();
		return false
	}
	else if (!d.txtFone.value) {
		window.alert('Preencha com um telefone!');
		d.txtFone.focus();
		return false;
	}
	else if (!d.txtLocal.value) {
		window.alert('Preencha com um local!');
		d.txtLocal.focus();
		return false;
	}
	else if (d.txtNomeProduto.value==false) {
		window.alert('Preencha o nome do produto!');
		d.txtNomeProduto.focus();
		return false
	}
	else if (d.txtValor.value==false) {
		window.alert('Coloque um valor!');
		d.txtValor.focus();
		return false
	}
	else if (d.txtDescricao.value==false) {
		window.alert('Preencha a descricao do produto!');
		d.txtDescricao.focus();
		return false
	}
	else if (d.ffImagem.value==false) {
		window.alert('Escolha uma imagem do produto!');
		d.ffImagem.focus();
		return false
	}
	else if (d.txtResp.value==false) {
		window.alert('Informe as letras do quadrado!');
		d.txtResp.focus();
		return false
	}
	
	return true;
}

// Confere Comentario
function confereComentario() {
	d = document.form_comentario;
	if (!d.txtNome.value) {
		window.alert('Preencha o seu nome!');
		d.txtNome.focus();
		return false;
	} 
	else if (d.txtNome.value.length < 3) {
		window.alert('Preencha um nome com mais de 3 caracteres!');
		d.txtNome.focus();
		return false;
	}
	else if (!d.txtEmail.value) {
		window.alert('Preencha o seu email!');
		d.txtEmail.focus();
		return false;
	}
	else if (isEmail(d.txtEmail.value)==false) {
		window.alert('Entre com um email valido!');
		d.txtEmail.focus();
		return false
	}
	else if (d.txtResp.value==false) {
		window.alert('Informe as letras do quadrado!');
		d.txtResp.focus();
		return false
	}
	else if (!d.txtCorpo.value) {
		window.alert('Preencha o comentario!');
		d.txtCorpo.focus();
		return false;
	}
	
	return true;
}
// Confere Comentario Classificado
function confereComentarioClass() {
	d = document.form_comentario;
	if (!d.txtNome.value) {
		window.alert('Preencha o seu nome!');
		d.txtNome.focus();
		return false;
	} 
	else if (d.txtNome.value.length < 3) {
		window.alert('Preencha um nome com mais de 2 caracteres!');
		d.txtNome.focus();
		return false;
	}
	else if (!d.txtEmail.value) {
		window.alert('Preencha o seu email!');
		d.txtEmail.focus();
		return false;
	}
	else if (isEmail(d.txtEmail.value)==false) {
		window.alert('Entre com um email valido!');
		d.txtEmail.focus();
		return false
	}
	else if (!d.txtFone.value) {
		window.alert('Preencha com um telefone!');
		d.txtFone.focus();
		return false;
	}
	else if (d.txtResp.value==false) {
		window.alert('Informe as letras do quadrado!');
		d.txtResp.focus();
		return false
	}
	else if (!d.txtCorpo.value) {
		window.alert('Preencha o comentario!');
		d.txtCorpo.focus();
		return false;
	}

	
	return true;
}
// Valida data
function validarData(campo) {
	var expReg = /^(([0-2]\d|[3][0-1])\/([0]\d|[1][0-2])\/[1-2][0-9]\d{2})$/;
	if ((campo.value.match(expReg)) && (campo.value!='')) {
		return true;
	} else {
		return false;
	} 
}



// Valida horario
function maskHour(Hora, campo){  
   var hora01 = '';  
   hora01 = hora01 + Hora;  
   if (hora01.length == 2){  
      hora01 = hora01 + ':';  
      campo.value = hora01;  
   }  
   if (hora01.length == 5){  
      checkHour(campo);  
   }  
}  
  
function checkHour(campo){  
   hrs = (campo.value.substring(0,2));  
   min = (campo.value.substring(3,5));  
   estado = "";  
   if ((hrs < 00 ) || (hrs > 23) || ( min < 00) ||( min > 59)){  
      estado = "errada";  
   }  
  
   if (campo.value == "") {  
      estado = "errada";  
   }  
   if (estado == "errada") {  
      alert("Hora invalida!");  
      campo.focus();  
   }  
}   

// Valida email
function isEmail(email) {
	parte1 = email.indexOf("@");
	parte2 = email.indexOf(".");
	parte3 = email.length;
	
	if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
		return false;
	}
	return true;
}





// Conta Caracteres
function contaCaracteres(campo, limite) {
	texto_entrada = document.getElementById(campo).value;
	tamanho_digitado = texto_entrada.length;

	if (tamanho_digitado > limite) {
		document.getElementById(campo).value = document.getElementById(campo).value.substring(0, limite);
	} else {
		if (tamanho_digitado) document.getElementById('limite_caracteres').innerHTML = 'Digitado: ' + tamanho_digitado;
		else document.getElementById('limite_caracteres').innerHTML = "";
	}
}

/**
 * EOF
 */

