// JavaScript Document

//INICIA AJAX
function ajaxInit() {
	var req;
	
	try {
			req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				try {
				req = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(ex) {
				try {
				req = new XMLHttpRequest();
			} catch(exc) {
				alert("Esse browser não tem recursos para uso do Ajax");
				req = null;
			}
		}
	}
		
	return req;
}

//MUDA PRODUTO
function mudaConteudo(id,g) {
	ajax = ajaxInit();
	if(ajax) {
		noCache = new Date().getTime();
		var param = 'id='+id+'&nc='+noCache+'';
		
		//document.getElementById('content_conteudo_produtos').innerHTML = '<p id="loading">Aguarde...<br />Carregando produto</p>';
		document.getElementById('container_conteudo_produtos').style.bgColor = '#ffffff';
		
		ajax.open('POST','../includes/pegaConteudo.php', true);
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(param);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var retorno = ajax.responseText;
					//alert(retorno);
					if (retorno != "") {
						/*array = retorno.split('||');
						for (i=0;i<array.length;i++) {
							dados = array[i].split('==');
							if (dados[0] == 'cor') {
								if (dados[1] == '') vCor = '#CDEBF5';
								else vCor = dados[1];
								document.getElementById('container_conteudo_produtos').style.background = ''+ vCor +'';
								Nifty("div#container_conteudo_produtos","big fixed-height");
							}
							
							if (dados[0] == 'conteudo') {
								document.getElementById('content_conteudo_produtos').innerHTML = dados[1];
							}*/
							
							//alert(dados[1]);
							//alert(retorno);
							document.getElementById('box_conteudo').innerHTML = retorno;
							Nifty("div#container_conteudo_produtos","big fixed-height");
							Nifty("a#zoom","medium fixed-height");
							if (g != '') {
								Nifty("p#link_gabarito","fixed-height transparent");
								document.getElementById("repos").style.height = "370px";
							}
						//}
					} 
				} else {
					alert(ajax.statusText);
				}
			}
		}
	}
}


//specify speed of scroll (greater=faster)
var speed=6

function movedown(div){
	var crossobj = document.getElementById(div);
	var contentheight = crossobj.offsetHeight;
	
	//alert((parseInt(crossobj.style.top)));
	//alert(contentheight*(-1)+250);
	
	if (window.moveupvar) clearTimeout(moveupvar)
	if (parseInt(crossobj.style.top)>=(contentheight*(-1)+370))
		crossobj.style.top=parseInt(crossobj.style.top)-speed+"px"
	movedownvar=setTimeout("movedown('"+div+"')",15)
}

function moveup(div){
	var crossobj = document.getElementById(div);
	if (window.movedownvar) clearTimeout(movedownvar)
	if (parseInt(crossobj.style.top)<=0)
		crossobj.style.top=parseInt(crossobj.style.top)+speed+"px"
	moveupvar=setTimeout("moveup('"+div+"')",15)
}

function stopscroll(){
	if (window.moveupvar) clearTimeout(moveupvar)
	if (window.movedownvar) clearTimeout(movedownvar)
}

function movetop(div){
	var crossobj = document.getElementById(div);
	stopscroll()
	crossobj.style.top=0+"px"
}

//FUNÇÃO PARA OVER DO MENU
over = function() {
	var sfEls = document.getElementById("menu").getElementsByTagName("LI");
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onmouseover=function() {
			this.className+=" over";
		}
		sfEls[i].onmouseout=function() {
			this.className=this.className.replace(new RegExp(" over\\b"), "");
		}
	}
}
if (window.attachEvent) window.attachEvent("onload", over);

//CHECA CAMPO DE CADASTRO DE NEWSLETTER - ONFOCUS
function checaNin(campo) {
	if (campo == 'usuario') {
		if (document.getElementById(campo).value == 'Usuário')	document.getElementById(campo).value = '';
	}
	
	if (campo == 'senha') {
		if (document.getElementById(campo).value == 'Senha') document.getElementById(campo).value = '';
	}
}

//CHECA CAMPO DE CADASTRO DE NEWSLETTER - ONBLUR
function checaNout(campo) {
	if (campo == 'usuario') {
		if (document.getElementById(campo).value == '')	document.getElementById(campo).value = 'Usuário';
	}
	
	if (campo == 'senha') {
		if (document.getElementById(campo).value == '') document.getElementById(campo).value = 'Senha';
	}
}

//LOGIN
function login() {
	usuario = document.getElementById('usuario').value;
	senha = document.getElementById('senha').value;
	ajax = ajaxInit();
	if(ajax) {
		noCache = new Date().getTime();
		var param = 'nc='+noCache+'&usuario='+usuario+'&senha='+senha+'';
		//document.getElementById(n).innerHTML = 'Aguarde, seu cadastro está sendo efetuado...';

		ajax.open('POST','../includes/login_user.php', true);
		ajax.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		ajax.send(param);
		ajax.onreadystatechange = function() {
			if(ajax.readyState == 4) {
				if(ajax.status == 200) {
					var retorno = ajax.responseText;
					//alert(retorno);
					if (retorno != "") {
						if (retorno == '0')
							alert('Usuário ou senha incorretos.');
						else
							window.location='../tabelas/';
					} 
				} else {
					alert(ajax.statusText);
				}
			}
		}
	}
}

function acao(tipo,id) {
	switch (tipo) {
		case 'atualiza':
			document.getElementById('form').submit();
			break;
		case 'exclui':
			window.location='carrinho.php?excId='+id;
			break;
	}	
}

function checaItens(theform) {
	var theform = document.getElementById(theform);
	var sfEls = theform.getElementsByTagName("INPUT");
	var erro = 0;
	
	for (var i=0; i<sfEls.length; i++) {
		if (sfEls[i].type != 'hidden') {
			if (!sfEls[i].value || sfEls[i].value == '0') erro++;
		}
	}
	
	if (erro > 0) {
		alert('Informe uma quantidade válida para cada item!');	
	} else if (erro == 0) { 
		window.location='carrinho.php?fecha=true';
	}

}

function checaEnvio(form) {
	
	if (!form.nome_empresa.value) {
		alert('Preencha corretamente o campo "Nome da Empresa"');
		form.nome_empresa.focus();
		return false;
	}
	
	if (!form.responsavel.value) {
		alert('Preencha corretamente o campo "Responsável"');
		form.responsavel.focus();
		return false;
	}
	
	if (!form.email.value) {
		alert('Preencha corretamente o campo "E-mail"');
		form.email.focus();
		return false;
	}
	
	if (!form.cidade.value) {
		alert('Preencha corretamente o campo "Cidade"');
		form.cidade.focus();
		return false;
	}
	
	if (!form.estado.value) {
		alert('Preencha corretamente o campo "Estado"');
		form.estado.focus();
		return false;
	}
	
	if (!form.telefone.value) {
		alert('Preencha corretamente o campo "Telefone de Contato"');
		form.telefone.focus();
		return false;
	}
	
	return true;
}
