//data:		JUL 2010
//autor:	[Rafael Wahasugui] 
//email:	[rafael@waio.com.br] 
//website:	[www.eletroperez.com.br] 
//-----------------------------------------------------------------------------



//SCRIPTS
//-----------------------------------------------------------------------------

// Preloader
<!--//--><![CDATA[//><!--
	var images = new Array()
	function preload() {
		for (i = 0; i < preload.arguments.length; i++) {
			images[i] = new Image()
			images[i].src = preload.arguments[i]
		}
	}
	preload(
		"http://www.eletroperez.com.br/images/bt-home.png",
		"http://www.eletroperez.com.br/images/bt-produtos.png",
		"http://www.eletroperez.com.br/images/bt-servicos.png",
		"http://www.eletroperez.com.br/images/bt-contato.png",
		"http://www.eletroperez.com.br/images/bt-extra.png",
		"http://www.eletroperez.com.br/images/bt-common01.png",
		"http://www.eletroperez.com.br/images/bt-common02.png",
		"http://www.eletroperez.com.br/images/bg-home.jpg",
		"http://www.eletroperez.com.br/images/bg-produtos.jpg",
		"http://www.eletroperez.com.br/images/bg-servicos.jpg",
		"http://www.eletroperez.com.br/images/bg-contato.jpg",
		"http://www.eletroperez.com.br/images/bg-extra.jpg"
	)
//--><!]]>


// header.php - Menu principal
//-----------------------------------------------------------------------------

$(document).ready(function(){
	$('#menu li a').click(function(){
		$('#menu li a').removeClass('active');
		$(this).addClass('active');
	});
});



// iluminacao.php - Fancybox
//-----------------------------------------------------------------------------
$(document).ready(function(){
	$("a[rel=group01]").fancybox({
		'type'				: 'ajax',
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group02]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	/*
	$("a[rel=group03]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group04]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group05]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group06]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group07]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group08]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	$("a[rel=group09]").fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over',
		'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over"> ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});
	*/
});

// iluminacao.php - TABS
//-----------------------------------------------------------------------------

$(document).ready(function(){
	$('ul.menu li a').click(function(){
		$('ul.menu li a').removeClass('highlight');
		$(this).addClass('highlight');
	});
});



//AJAXSIDE

/*------------- função padrão que inicia o ajax ------------*/
function ajaxInit() {
var xmlhttp;
try {
xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
xmlhttp = false;
}
}	
if (!xmlhttp && typeof XMLHttpRequest != 'undefined' ) {
try {
xmlhttp = new XMLHttpRequest();
} catch (e) {
xmlhttp = false ;
}
}
return xmlhttp;
}
/*------------------- end ajaxInit ------------------------*/


function setBusca(campo,valor,tipo,value){
	if (valor == '') {
		campo.value = '';	
	}
	
	if ((tipo == 'sair') && (campo.value == '')) {
		campo.value='SEU ' + value;
	}
}


function jsScript(texto){
	var ini = 0;
	while (ini!=-1){
		ini = texto.indexOf('<script', ini);
		if (ini >=0){
			ini = texto.indexOf('>', ini) + 1;
			var fim = texto.indexOf('</script>', ini);
			codigo = texto.substring(ini,fim);
			novo = document.createElement("script");
			novo.text = codigo;
			document.body.appendChild(novo);
		}
	}
}

// ********************  início das funções - Waio Soluções em TI ******************** //


/************ função que carrega uma determinada pagina dentro de uma layer ************/
function getSections(page,layer) {
	var layer = document.getElementById(layer);
	var ajax = ajaxInit();
	ajax.open("GET",page, true);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			var result = ajax.responseText;
			result=result.replace(/\+/g," ");
			result = unescape(result);
			layer.innerHTML = result;
			jsScript(result);
		} 
	};
	ajax.send(null);
}
/******************** end getSections ********************/


/************** função para a busca dos campos de endereço atraves de um cep **************/
function getCep(pagina, pesquisa) {
	//var carregando = document.getElementById("carregando");
	//carregando.style.display = 'block';
	ajax = ajaxInit();
	ajax.open("GET",pagina+"?pesquisa="+pesquisa, true);
	ajax.onreadystatechange = function() {
	if(ajax.readyState == 4 && ajax.status == 200) {
		var resultado = ajax.responseText;
		resultado=resultado.replace(/\+/g," ");
		resultado = unescape(resultado);
		//carregando.style.display = 'none';
		//layer.innerHTML = resultado;
		jsScript(resultado);
		document.getElementById('endereco').value = unescape(resultadoCEP['tipo_logradouro'])+" "+unescape(resultadoCEP['logradouro']);
		//document.getElementById('estado').selectedIndex = unescape(resultadoCEP['uf'])-1;
		//document.getElementById('cidade').value = unescape(resultadoCEP['cidade']);
		document.getElementById('bairro').value = unescape(resultadoCEP['bairro']);
	}};
	ajax.send(null);
}
/*-------------------------- end getCep -----------------------------------------------------------*/


// ********************  função para envio dos formulários por ajax ******************** //
function sendForm ( formID, formProcessor){
	var ajax = ajaxInit();
	var form = document.getElementById(formID);
	var elements = form.elements;
	query = "";
	for ( i = 0; i < elements.length-1; i++ ){
		if(elements.item(i).type == "radio" || elements.item(i).type == "checkbox") {
		if(!query) {
			if(elements.item(i).checked == true){
				query = elements.item(i).name + '=' + encodeURIComponent(escape(elements.item(i).value))+"&";
			}
			else{
				query = query;
			}
			} 
			else {
				if(elements.item(i).checked == true){
					query = query + elements.item(i).name + '=' + encodeURIComponent(escape(elements.item(i).value))+"&";
				}
			else{
				query = query;
			}
			}
		}
		else{
			query = query + elements.item(i).name + "=" + escape(elements.item(i).value) + "&";
		}
	}
	query = query + elements.item(i).name + "=" + escape(elements.item(i).value);
	ajax.open("POST", formProcessor,true );
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajax.onreadystatechange = function(){
	if ( ajax.readyState == 4 && ajax.status == 200) {
		var result = ajax.responseText;
		result=result.replace(/\+/g," ");
		result = unescape(result);
		alert(result);
		//se o formulario for frm_socios cada vez q salvar um socio ele aparece na div ajaxcontent//
		if (formID == 'form_agende' || formID =='form_news' || formID == 'form_contato' ) {
			if (result == 'E-mail enviado com sucesso!'){
				parent.location='index.php';
			}
		} // formID == frm_socios
	
		
	} // ajax.readyState
	};
	ajax.send(query);
}
/********** end sendForm **********/


/* --------------- Função para realizar pesquisas e jogar os dados dentro de uma layer ----------*/

function getPesquisa ( formID, formProcessor,layer){
	document.getElementById("loader").style.visibility = "visible";
	var ajax = ajaxInit();
	var form = document.getElementById(formID);
	var elements = form.elements;
	query = "";
	for ( i = 0; i < elements.length-1; i++ ){
		if(elements.item(i).type == "radio" || elements.item(i).type == "checkbox") {
		if(!query) {
			if(elements.item(i).checked == true){
				query = elements.item(i).name + '=' + encodeURIComponent(escape(elements.item(i).value))+"&";
			}
			else{
				query = query;
			}
			} 
			else {
				if(elements.item(i).checked == true){
					query = query + elements.item(i).name + '=' + encodeURIComponent(escape(elements.item(i).value))+"&";
				}
			else{
				query = query;
			}
			}
		}
		else{
			query = query + elements.item(i).name + "=" + escape(elements.item(i).value) + "&";
		}
	}
	query = query + elements.item(i).name + "=" + escape(elements.item(i).value);
	ajax.open("POST", formProcessor,true );
	ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	ajax.onreadystatechange = function(){
	if ( ajax.readyState==4 && ajax.status == 200) {
		var resultado = ajax.responseText;
		resultado=resultado.replace(/\+/g," ");
		resultado = unescape(resultado);
		document.getElementById(layer).innerHTML = resultado;
		document.getElementById("loader").style.visibility = "hidden";
		jsScript(resultado);				
		
	}
	};
	ajax.send(query);
}
/*---------------------------------------------------------------------------------------------------------------------*/



/* --------------- função que verifica se o usuario deseja realmente excluir um determinado registro; ----------*/

//pagina: pagina que processa o ajax, ou seja, faz a exclusão do registro
//label(rótulo): é o nome que eu dou para a pagina que vai listar os registros depois de excluidos
//id: é opcional, para quando preciso dizer para qual pagina pertence a listagem
function excluir_registro(pagina,label,id){
// Confirma a exclusão antes de executá-la.
               
               var confirma = confirm('Deseja realmente excluir este registro?');
				// Se realmente quiser excluir
				if (confirma == true) {
					// Inicia o Ajax, através da variável Ajax
					var ajax = ajaxInit()
					// Informamos o método e a página que será requisitada
					ajax.open('GET', pagina, true); 
					// bla
					ajax.onreadystatechange = function() {
						if ( ajax.readyState == 4 ) {
							if ( ajax.status == 200 ) {
							  
     	                       if ( label == 'noticias' ) {
							     //atualiza os produtos com o numero da paginação correta
								  getSections('process/list_noticias.php?pg='+id,'resultado');
							   }//label = noticias
		
							} // if-status->200
						} // if->readyState->4
					} // ajax->onreadystatechange
					// Envia a requisição
					ajax.send(null);
				} // if->confirma->true
				// Evita o reload da página
				return false;
} //end excluir_registro


/*-------- função para verificar se o formulário deve ou não prosseguir --------*/

//pagina: arquivo que processa o ajax.
//redirect: arquivo para onde quero que redirecione.
function verifica_redirect(pagina,redirect) { 
	var layer = document.getElementById(layer);
	var ajax = ajaxInit();
	ajax.open("GET",pagina, true);
	ajax.onreadystatechange = function() {
		if(ajax.readyState == 4 && ajax.status == 200) {
			var resultado = ajax.responseText;
			resultado=resultado.replace(/\+/g," ");
			resultado = unescape(resultado);
			if  ( resultado == 1 ) {
				parent.location = redirect;
			}
			else {
			   alert("Salve o formulario antes de prossequir!");
			}
		} 
	};
	ajax.send(null);
}
/*----------------------------------------------------------------------------------*/



