

//----------------------------------------------------------------------------//
//ACORDEON
//----------------------------------------------------------------------------//

var Acordeon = {
	
	'active': function(categoria) {
		
		$('#divAcordeonProduto_' + categoria).toggle('slider');
		
		document.getElementById('acordeonResize_' + categoria).className = (document.getElementById('acordeonResize_' + categoria).className == "acordeonResize_close") ? "acordeonResize_open" : "acordeonResize_close";
		
		
	}
	
}


function fechaDiv(div)
{
	$('#'+div).toggle('slider');
	$.unblockUI();
}

function fechaDiv2(div)
{
	$('#'+div).hide('slider');
	$.unblockUI();
}


//verifica se é numero
function verificaNumero(obj)
{
	//verifica se não é numero
	if (isNaN(obj.value))
	{
		alert('Você deve digitar um número!');
		obj.value = "";
	}	
}


var Carrinho = {
	
		'AddProduto': function(id_produto,tipo)
		{
			var qtd = $('#'+id_produto+'_qtd_'+tipo).val();
			
			//verifica se não é numero
			if (isNaN(qtd))
				qtd = 1;
			
			$('#divDetalhes').hide('slow');
			
			$.unblockUI();
			
			if(id_produto)
			{
				$('#divCarrinho').load('load-carrinho.php?QTD='+qtd+'&ID='+id_produto+'&CMD=adicionar');
			}
			else
				$('#divCarrinho').load('load-carrinho.php');
				
			jQuery.blockUI({message: null, overlayCSS: { backgroundColor: '#6397BE' } });

		},
		
		
		'Atualiza':function(id_produto,qtd)
		{
			//verifica se não é numero
			if (isNaN(qtd))
				qtd = 1;
				
				
			$.ajax({
				type: "GET",
				url: "load-carrinho.php",
				data: "CMD=adicionar&ID="+id_produto+"&QTD="+qtd
			});
			
		},


		'RemoveProduto':function(id_produto)
		{
		
			$.ajax({
				type: "GET",
				url: "load-carrinho.php",
				data: "CMD=excluir&ID="+id_produto
			});
			
			$('#tr' + id_produto).remove();
			$('#linha' + id_produto).remove();
			
		},
		
		'Detalhe': function (id_produto)
		{
			$('#divDetalhes').load('load-detalhes.php?ID='+id_produto)
			jQuery.blockUI({message: null, overlayCSS: { backgroundColor: '#BBD0E1' } });
			Window._openDetalhe("divDetalhes");
		},
	
		'Valida': function()
		{
		
			if ( $('#nome').val() == "")
			{
				alert('Você precisa digitar o seu nome ! ');
				$('#nome').focus();
				return false;
			}
			
			if($('email').val() == "" || document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1)
			{
				alert('Você precisa digitar um endereço de e-mail válido ! ')
				$('#email').focus();
				return false;
			}
			
			if ( $('#telefone').val() == "")
			{
				alert('Você precisa digitar o seu telefone ! ');
				$('#telefone').focus();
				return false;
			}
				
			if ( $('#mensagem').val() == "")
			{
				alert('Você precisa digitar a sua mensagem ! ');
				$('#mensagem').focus();
				return false;
			}
		}
}


//função redimensiona

function getPageScroll()
{

	var yScroll;
	
	if (self.pageYOffset) 
	{
		yScroll = self.pageYOffset;
	} 
	else if(document.documentElement && document.documentElement.scrollTop)
	{ // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} 
	else if (document.body) 
	{// all other Explorers
		yScroll = document.body.scrollTop;
	}
	
	arrayPageScroll = new Array('',yScroll)
	
	return arrayPageScroll;
}
	
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 - 18;
	} 
	else 
	{
		pageWidth = xScroll;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}


var Window =
{

	'_open': function(div,divW,divH) 
	{
	
		$('#'+div).show('slider');
		//$('#'+div).focus();
		//if (document.all)
		//var pos = evt.clientY;
		//else
		// var pos = window.pageYOffset;
		
		var pageSize = getPageSize();
		
		var objScroll = getPageScroll();
		
		//alert(objScroll[1]);
		
		var winW = pageSize[0];
		var winY = pageSize[1];
		
		//menos a largura da div
		var w = (winW - divW) / 2;
		 //menos a altura da div
		var y = (winY - divH) / 2;
		
		//var arr = getPageScroll();
		//alert(arr[1]);
/*
		if (objScroll[1] > 167)
			arr = (objScroll[1] - 167) + 'px';
		else
			arr = '20px';*/
			
		document.getElementById(div).style.left = w + 'px';
		//document.getElementById(div).style.top = y + 'px';
	/*	document.getElementById(div).style.top = arr;*/
		document.getElementById(div).style.top = (objScroll[1] + 10) + 'px';
	},

	'_openDetalhe': function(div) 
	{
	
		$('#'+div).show('slider');
		//$('#'+div).focus();
		//if (document.all)
		//var pos = evt.clientY;
		//else
		// var pos = window.pageYOffset;
		
		var pageSize = getPageSize();
		
		var objScroll = getPageScroll();
		
		//alert(objScroll[1]);
		
		var winW = pageSize[0];
		var winY = pageSize[1];
		
		//menos a largura da div
		var w = (winW - 640) / 2;
		 //menos a altura da div
		var y = (winY - 480) / 2;
		
		//var arr = getPageScroll();
		//alert(arr[1]);

		if (objScroll[1] > 167)
			arr = (objScroll[1] - 167) + 'px';
		else
			arr = '0px';
			
		document.getElementById(div).style.left = w + 'px';
		//document.getElementById(div).style.top = y + 'px';
	/*	document.getElementById(div).style.top = arr;*/
		document.getElementById(div).style.top = objScroll[1] + 'px';
	},
	
	'_openCarrinho': function(div) 
	{
	
		$('#'+div).show('slider');
		//$('#'+div).focus();
		//if (document.all)
		//var pos = evt.clientY;
		//else
		// var pos = window.pageYOffset;
		
		var pageSize = getPageSize();
		
		var objScroll = getPageScroll();
		
		//alert(objScroll[1]);
		
		var winW = pageSize[0];
		var winY = pageSize[1];
		
		//menos a largura da div
		var w = (winW - 610) / 2;
		 //menos a altura da div
		var y = (winY - 300) / 2;
		
		
		/*if (objScroll[1] > 167)
			arr = (objScroll[1] - 167) + 'px';
		else
			arr = '0px';*/
		document.getElementById(div).style.left = w + 'px';
		//document.getElementById(div).style.top = y + 'px';
		document.getElementById(div).style.top = objScroll[1] + 'px';
	}
}


//CENTRALIZADA serve para previem de noticia, artigo, etc...
function openWin(url, name, w, h, s) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+s+',resizable=no';
//	winprop = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars=yes';
	
	win = window.open(url, name, winprops);

	if(parseInt(navigator.appVersion) >= 4) {
		win.window.focus(); 
	}
}


//---------------------------------------------------------------------------------//
// COMMONS
//---------------------------------------------------------------------------------//

var Common = {
	
	'loadCidade': function(uf)
	{
		//alert(uf);
		$('#divCidade').load('load-cidade.php?uf=' + uf);	
	},
	
	'loadBairro': function(id_cidade) 
	{
		//alert(id_cidade);
		$('#divBairro').load('load-bairro.php?id_cidade=' + id_cidade);	
	},
	
	// ID = id do que se quer escluir
	// URL = pagina que sera feita a exclusao
	//MSG = se quiser definir mensagem especial para exclusao
	'Deleta': function(URL,MSG)
	{
		if (MSG == "" || MSG == null)
			MSG = "Tem certeza que deseja excluir ?";
			
	   if(confirm(MSG))
	   document.location = URL;
	},
	
	//Função para formatar moeda R$
	'formataMoeda': function(campo,event)
	{
		//Pega o código da tecla
		function getKey(e)
		{
		if (e == null) 
		{ // ie
			keycode = event.keyCode;
		} 
		else 
		{ // mozilla
			keycode = e.which;
		}
		//key = fromCharCode(keycode).toLowerCase();
		
		return keycode;
		}
		
		
		
		//Bloqueia caracteres que não sejam números
		function teclas(campo,event)
		{
			if(((event.keyCode < 96) || (event.keyCode > 105)) && ((event.keyCode < 48) || (event.keyCode > 57)) )
			{
			   campo.value = campo.value.replace(String.fromCharCode(event.keyCode).toLowerCase(),"");
			}
		}

	   //para evitar caracteres alfas.
	   teclas(campo,event);
	   str = campo.value;
	
	   while(str.search(",") != -1)
		   str = str.replace(",","");
	   i = 0;
	
	   while(i< str.length){
		   if(str.substr(i,1) == ".")
			  str = str.replace(".","");
			  i++;
	   }
	
	   part1 = str.substr(0,str.length - 2);
	   while(part1.search(" ") != -1)
		   part1 = part1.replace(" ","");
	
		   part2 = str.substr(str.length - 2,2);
		   res = "";
		   i = part1.length;
		   sob = i % 3;
		   if((sob != 0) && (i > 2))
			  res = part1.substr(0,sob) + ".";
		   else
			  res = part1.substr(0,sob);
		   j = 1;
		   part1 = part1.substr(sob);
		   i = 0;
		   while(i < part1.length){
			  if(j == 3){
				 if(i + 1 == part1.length)
					res = res + part1.substr(i-2,3);
				 else res = res + part1.substr(i-2,3) + ".";
			  }
			  i++;
			  j = j<3?j+1:1;
		   }
		   campo.value = res + "," + part2;
	}

}
//----------------------------------------------------------------------------------//
// Contato
//-----------------------------------------------------------------------------------//

//valida contato
var Contato = {
	
	'Valida': function()
	{
		
		if ( $('#nome').val() == "")
		{
			alert('Você precisa digitar o seu nome ! ');
			$('#nome').focus();
			return false;
		}
		
		if($('email').val() == "" || document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) 
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('#email').focus();
			return false;
		}
		
		if ( $('#telefone').val() == "")
		{
			alert('Você precisa digitar o seu telefone ! ');
			$('#telefone').focus();
			return false;
		}
		
		if ( $('#assunto').val() == "")
		{
			alert('Você precisa digitar o assunto ! ');
			$('#assunto').focus();
			return false;
		}

		if ( $('#mensagem').val() == "")
		{
			alert('Você precisa digitar a sua mensagem ! ');
			$('#mensagem').focus();
			return false;
		}
	}
}

//----------------------------------------------------------------------------------//
// Revendedor
//-----------------------------------------------------------------------------------//

//valida Revendedor
var Revendedor = {
	
	'Valida': function()
	{
		
		if ( $('#razao_social').val() == "")
		{
			alert('Você precisa digitar a Razão social ! ');
			$('#razao_social').focus();
			return false;
		}
		
		if ( $('#nome_fantasia').val() == "")
		{
			alert('Você precisa digitar o nome Fantasia ! ');
			$('#nome_fantasia').focus();
			return false;
		}
		
		if ( $('#cnpj').val() == "")
		{
			alert('Você precisa digitar o CNPJ ! ');
			$('#cnpj').focus();
			return false;
		}

		if ( $('#ie').val() == "")
		{
			alert('Você precisa digitar o numero da Inscrição Estadual ! ');
			$('#ie').focus();
			return false;
		}

		
		if ( $('#telefone1').val() == "")
		{
			alert('Você precisa digitar o seu telefone1 ! ');
			$('#telefone1').focus();
			return false;
		}
		
		if ( $('#uf').val() == "")
		{
			alert('Você precisa escolher o estado ! ');
			$('#uf').focus();
			return false;
		}
		
		if ( $('#id_cidade').val() == 0)
		{
			alert('Você precisa escolher a cidade ! ');
			$('#id_cidade').focus();
			return false;
		}
		
		if ( $('#bairro').val() == "")
		{
			alert('Você precisa digitar o bairro ! ');
			$('#bairro').focus();
			return false;
		}
		
		if ( $('#endereco').val() == "")
		{
			alert('Você precisa digitar o endereço ! ');
			$('#endereco').focus();
			return false;
		}
		
		if ( $('#cep').val() == "")
		{
			alert('Você precisa digitar o CEP ! ');
			$('#cep').focus();
			return false;
		}
		
		if($('email').val() == "" || document.getElementById('email').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) 
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('#email').focus();
			return false;
		}
		
		if ( $('#responsavel').val() == "")
		{
			alert('Você precisa digitar o Responsável ! ');
			$('#responsavel').focus();
			return false;
		}

		if ( $('#cpf').val() == "")
		{
			alert('Você precisa digitar o CPF ! ');
			$('#cpf').focus();
			return false;
		}
		
		if ( $('#rg').val() == "")
		{
			alert('Você precisa digitar o RG ! ');
			$('#rg').focus();
			return false;
		}

	}
}



var Enviador = {
	
	'Valida': function()
	{
		if ( $('#matricula').val() == "")
		{
			alert('Você precisa digitar a matrícula! ');
			$('#matricula').focus();
			return false;
		}
		
		if ( $('#nome_usuario').val() == "")
		{
			alert('Você precisa o nome do usuário! ');
			$('#nome_usuario').focus();
			return false;
		}
	}
	
}
//----------------------------------------------------------------------------------//
// Acesso
//-----------------------------------------------------------------------------------//

//valida Acesso
var Acesso = {
	
	'Valida': function()
	{
				
		if ( $('#tipo_acesso').val() == 0)
		{
			alert('Você precisa escolher o tipo de acesso ! ');
			$('#tipo_acesso').focus();
			return false;
		}
		
		if($('emailAcesso').val() == "" || document.getElementById('emailAcesso').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) 
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('#emailAcesso').focus();
			return false;
		}
		
		if ( $('#pass').val() == "")
		{
			alert('Você precisa digitar a senha ! ');
			$('#pass').focus();
			return false;
		}
	},
	
	
	'Valida2': function()
	{
				
		if ( $('#tipo_acesso2').val() == 0)
		{
			alert('Você precisa escolher o tipo de acesso ! ');
			$('#tipo_acesso2').focus();
			return false;
		}
		
		if($('emailAcesso2').val() == "" || document.getElementById('emailAcesso2').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) 
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('#emailAcesso2').focus();
			return false;
		}
		
		if ( $('#pass2').val() == "")
		{
			alert('Você precisa digitar a senha ! ');
			$('#pass2').focus();
			return false;
		}
	},
	
	'Valida3': function()
	{
				
		if ( $('#tipo_acesso3').val() == 0)
		{
			alert('Você precisa escolher o tipo de acesso ! ');
			$('#tipo_acesso3').focus();
			return false;
		}
		
		if($('emailAcesso3').val() == "" || document.getElementById('emailAcesso3').value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1) 
		{
			alert('Você precisa digitar um endereço de e-mail válido ! ')
			$('#emailAcesso3').focus();
			return false;
		}
		
	},
	
	
	'novaTentativa': function (tipo,tentativa)
	{
		$('#divAcesso').load('load-acesso.php?TIPO='+tipo+'&TENTATIVA='+tentativa);
		jQuery.blockUI({message: null, overlayCSS: { backgroundColor: '#BBD0E1' } });
		Window._open("divAcesso",400,400);
	}

}