/*
funções em Ajax para controle do Admin
*/


function getAjax()
{
	if (window.XMLHttpRequest)
	{
		return new XMLHttpRequest();
	}else if(window.ActiveXObject){
	      	try {
			var ajax=new ActiveXObject("Microsoft.XMLHTTP");
		}catch(e){
			var ajax=new ActiveXObject("Msxml2.XMLHTTP");
			}
		}else{
		  alert('Seu browser n&atilde;o tem suporte ao XMLHttpRequest');		
		}
		return ajax;
}

function mudaPaginaDiv(caminho, parametros, div)
{
	
	var ajax=getAjax();
	ajax.open('POST', caminho, true);
	ajax.onreadystatechange = function(){
		if (ajax.readyState == 4)
		{

            //document.removeChild(loading);
			document.getElementById(div).innerHTML=ajax.responseText;

		}else{
			document.getElementById(div).innerHTML= '';
			document.getElementById(div).innerHTML= '<div style="text-align:center; line-height:300%;"><br/><img src="imagens/layout/popup/loading.gif" style="vertical-align:middle;"/><br/></div>';
//            var loading = document.createElement('img');
//            loading.src="../imagens/layout/popup/loading.gif";
//            loading.id="../imagens/layout/popup/loading.gif";
//            document.getElementById(div).appendChild(loading);
           // document.getElementById(div).innerHTML= '<img src="../imagens/layout/popup/loading.gif"/>';
		}
	}
	
	parametros=setaParametros(parametros);
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
	ajax.send(parametros);

}
function setaParametros(parametros)
{
	if(parametros!==null)
	{
		var i;
		var caracter = null;

		for(chave in parametros)
		{
			if(caracter!==null)
			{
				retorno += caracter+chave+'='+parametros[chave];
			}else{
				var retorno;
				retorno = chave+'='+parametros[chave];
			}
			caracter = '&';
		}
	}else{
		return null;
	}
	return retorno;
}


//n&atilde;o usada
function addEvent(object, evType, func, useCapture)
{
    if(object.addEventListener)
    {
        object.addEventListener(evType, func, useCapture);
    } else
    if(object.attachEvent)
    {
        object.attachEvent("on" + evType, func);
    }
}

function valorRadio(radio_group)
{	
	var valor_opcao = "";
	if(radio_group != null)
	{
		var len = radio_group.length;

	}else{
		return 0;

	}
	var i;
	
	for (i = 0; i <len; i++) 
	{
		if (radio_group[i].checked) 
		{
			valor_opcao = radio_group[i].value;
		}
	}

	return valor_opcao;
	
}


function Trim(strTexto)
{
    return strTexto.replace(/^\s+|\s+$/g, '');
}


function isNum(str, msg, nome)
{
	var VBlnIsNum;
	VIntTam = str.length;
	VBlnIsNum = true;
	if (VIntTam == 0)
	{
		erro(msg,nome);
		return false;
	}
	else
	{
		for (i=0; i < VIntTam; i++)
		{
			if (str.substring(i,i+1) < '0' || str.substring(i,i+1) >'9')
			{
				VBlnIsNum = false;
			}
		}
		return VBlnIsNum;
	}
}

function verificaRadio(valor, nome)
{

	if (valor == false || valor =="" || valor ==0)
	{
		erro('Por favor, selecione uma op��es.', nome);
		return false;
		
	}else{
		return true;
		
	}
} 

function validaContato()
{
	limpaErros();
	var erros = new Array();
	erros[0] = preenchido(document.getElementById('nome').value, 'Por favor, digite o seu nome.', 'Nome');
	erros[1] = isEmail(document.getElementById('email').value, 'Este endereço não parece correto. Por favor, digite um endereço de e-mail válido.', 'E-Mail');
	erros[2] = isValorLista(document.getElementById('area').value, 'Área');

	var procura = array_search(erros, false);
	if(procura!==false)
	{
		return false;	
	}else{
		return true;	
	}
}

function array_search (array,val) 
{
	for (var i = 0; i < array.length; i++) 
	{
		if (array[i] == val) 
		{
			return i;
		}
	}
	return false;
}

function isData(data)
{

	var objER = /^((0?[1-9]|[12]\d)\/(0?[1-9]|1[0-2])|30\/(0?[13-9]|1[0-2])|31\/?(0?[13578]|1[02]))\/(19|20)?\d{2}$/;	
	
	if(data.length>0){
		
		if(objER.test(data))
		{
			return true;
		}else{
			erro('Essa n�o � uma data v�lida.', 'Data do Evento');
			return false;
		}
		
	}else{
		erro('Por favor, digite a data do evento.', 'Data do Evento');
		return false;	
	}
}


function isHora(hora)
{
		
	var objER = /^([0-1]\d|2[0-3]):[0-5]\d$/;
	
	if(hora.length>0){
		
		if(objER.test(hora))
		{
			return true;
		}else{
			erro('Essa n�o � uma hora v�lida.', 'Hora');
			return false;
		}
		
	}else{
		erro('Por favor, digite a hora do evento.', 'Hora');
		return false;	
	}
}

function isEmail(Email, msg)
{
	var objEm = /^[\w-]+(\.[\w-]+)*@(([\w-]{2,63}\.)+[A-Za-z]{2,6}|\[\d{1,3}(\.\d{1,3}){3}\])$/;
	Email = Trim(Email);
	var teste = preenchido(Email, 'Por favor, digite o seu endereço de e-mail.', 'E-Mail');
	if(teste!==false)
	{
		if(objEm.test(Email))
		{
			return true;
		}else{
			erro(msg, 'E-Mail');
			return false;
		}
	}
	else
	{
		return false;

	}
}

function isTelefone(Tel, msg, nome)
{
	var objTl = /^[0-9]?[0-9]?\s?[0-9]{4}\s?[0-9]{4}$/;
	Tel = Trim(Tel);
	
	if(Tel.length > 0)
	{
		if(objTl.test(Tel))
		{
				//alert("é valido");
			return true;
		}else{
				//alert("n&atilde;o é valido");
				erro(msg, nome);
				return false;
		}
	}else{
		erro('O campo n&atilde;o foi preenchido.', nome);
		return false;
	}
}

function preenchido(Campo, msg, nome)
{
	if(Campo.length == 0)
	{
		erro(msg, nome);
		return false;
	}
}

function isCEP(CEP)
{
	var objER = /^[0-9]{5}-[0-9]{3}$/;
	//var objER = /^[0-9]{2}\.[0-9]{3}-[0-9]{3}$/
	
	CEP = Trim(CEP)
	if(CEP.length > 0)
	{
		if(objER.test(CEP))
		{
			return true;
		}else{
			erro('O n�mero do CEP informado n�o � v�lido.', 'CEP');
			return false;
		}
	}

	
}

function setaFocus(elemento)
{
	elemento.focus();
}


function mostraImagem(cod_imagem, categoria, event)
{
	

	var div = document.getElementById('preview_imagem');
	var imagem = document.getElementById('imagem_preview');

	imagem.src = '../imagens/ilustrativas/'+categoria+'/grandes/'+cod_imagem+'_0.jpg';
	
	//alert(imagem.src);
/*
	x=event.pageX;
	y=event.pageY;
*/
    	x = event.screenX + document.body.scrollLeft;
    	y = event.screenY + document.body.scrollTop;
	//alert(document.body.scrollTop);
	div.style.top = String(y - imagem.height/2)+'px';
	div.style.left = String(x - 20 - imagem.width)+'px';
	div.style.visibility = 'visible';
	
}

function esconde(id)
{
	document.getElementById(id).style.visibility = 'hidden';
}

function moveDiv(event)
{
	var div = document.getElementById('preview_imagem');
	var imagem = document.getElementById('imagem_preview');
	
	x = event.clientX + document.body.scrollLeft;
	y = event.clientY + document.body.scrollTop;
	
	div.style.top = String(y - imagem.height/2 + 5)+'px';
	div.style.left = String(x - 20 - imagem.width)+'px';
	
}