/*
Funcoes para o ecomerce
Hamilton Teixeira
*/

function ValidarBusca(campo){
	if (campo.value.length=="") {window.alert ("Desculpe! \r Nenhum resultado encontrado \r Você pode: \r Tentar outra palavra ou grafia \r ou navegue por departamentos.");campo.focus();return (false);
	} else {
	if (campo.value.length<="3"){window.alert ("Utilize no mínimo 4 letras, para que a busca possa ser efetuada.");campo.select();campo.focus();return (false);
	} else {
	
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzƒŠŒŽšœžŸÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ0123456789-.,; /\t\r\n\f";
	var checkStr = campo.value;
	var allValid = true;
	for (i = 0;  i < checkStr.length;  i++)
	  {
	    ch = checkStr.charAt(i);
	    for (j = 0;  j < checkOK.length;  j++)
	      if (ch == checkOK.charAt(j))
	        break;
	    if (j == checkOK.length)
	    {
	      allValid = false;
	      break;
	    }
	  }
	if (!allValid){window.alert("Para efetuar a procura são aceitos apenas letras (com ou sem acentos) ou números. Obrigado");campo.select();campo.focus();return (false);
	} else {
	
	return (true);
		}}}
	}

function openWin_disclaimer(theURL)
	         {
				 myWin = open(theURL,"Pagina","toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,help:0,resizable=no,width=603,height=300,top=140,left=210");
			}
	

function selectDrop(campo,url,tipo)
	{
	var select = campo
	if (select.options[select.selectedIndex].value != "")
		{
		self.location = url + "/" + select.options[select.selectedIndex].value + "/" + "buscaautomatica"
		}
	}	
	

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}	

function checavazio(campo, tipo)
{
var texto = "";
if (tipo == 's') {texto = "Selecione .:";} else {{texto = "Preencha .:";}};
if (campo.value.length==""){window.alert (texto);campo.focus();return (false);}
}


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function logout()
{
return window.confirm('Deseja efetuar a saida do sistema?');
}

function check_login(check_senha){
		if (check_senha.nome.value=="seu e-mail"){window.alert ("Preencha seu e-mail");check_senha.nome.focus();return (false);
		} else {
		if (check_senha.nome.value.length==""){window.alert ("Preencha seu e-mail");check_senha.nome.focus();return (false);
		} else {
		if (check_senha.senha.value.length==""){window.alert ("Preencha sua senha");check_senha.senha.focus();return (false);
		} else {
		
		return (true)}}}
	}


VerifiqueTAB=true;
function Mostra(quem, tammax)
{
if ( (quem.value.length == tammax) && (VerifiqueTAB) )
{ 
var i=0,j=0, indice=-1;
for (i=0; i<document.forms.length; i++)
{ 
for (j=0; j<document.forms[i].elements.length; j++)
{ 
if (document.forms[i].elements[j].name == quem.name)
{
	indice=i;
	break;
}
} 
if (indice != -1) break; 
} 
for (i=0; i<=document.forms[indice].elements.length; i++) { 
if (document.forms[indice].elements[i].name == quem.name) { 
while ( (document.forms[indice].elements[(i+1)].type == "hidden") &&
	(i < document.forms[indice].elements.length) ) 
{ 
	i++;
} 
document.forms[indice].elements[(i+1)].focus();
VerifiqueTAB=false;
break;
} 
} 
} 
} 
function PararTAB(quem)
{
VerifiqueTAB=false;
} 
function ChecarTAB()
{
VerifiqueTAB=true;
} 

function val_numero(S) {
var Digitos = "0123456789";
var digito = "";
for (var i=0; i<S.length; i++) {
digito = S.charAt(i);
if (Digitos.indexOf(digito)<0) {
return(false);
}					
}
return(true);
}

function validarcep(form)
{
if ((val_numero(form.txtCEP1.value) == false) || (form.txtCEP1.value.length < 5)) 
{
alert("Digite um valor para o CEP");
form.txtCEP1.focus();
return(false);
}

if ((val_numero(form.txtCEP2.value) == false) || (form.txtCEP2.value.length < 3)) 
{
alert("Digite um valor para o CEP");
form.txtCEP2.focus();
return(false);
}	
}


function ltrim(texto) { return texto.replace(/^[ ]+/, ''); }

function rtrim(texto) { return texto.replace(/[ ]+$/, ''); }

function trim(texto) { return ltrim(rtrim(texto)); }



function pega(cepv)
{
cpev = trim(cepv)
cepv = cepv.split("-");
document.ceps.txtCEP1.value = String(cepv[0]).substring(1,6)
document.ceps.txtCEP2.value = cepv[1]
}	



function check_news(check_n){
		if (check_n.nome_news.value==""){window.alert ("Preencha seu nome");check_n.nome_news.focus();return (false);
		} else {
		if (check_n.email_news.value==""){window.alert ("Preencha seu e-mail");check_n.email_news.focus();return (false);
		} else {
		if (isValidEmail(check_n.email_news.value)==false){window.alert ("Preencha seu e-mail corretamente");check_n.email_news.focus();return (false);
		} else {
		
		return (true)}}}
	}
	
function isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

function cleannews(){
		form_news.nome.value="";
		form_news.email.value="";
		
	}


/*

Slide Show

*/

var activeImage = false;
	var imageGalleryLeftPos = false;
	var imageGalleryWidth = false;
	var imageGalleryObj = false;
	var maxGalleryXPos = false;
	var slideSpeed = 0;
	var imageGalleryCaptions = new Array();
	function startSlide(e)
	{
		if(document.all)e = event;
		var id = this.id;
		this.getElementsByTagName('IMG')[0].src = 'images/' + this.id + '_over.jpg';	
		if(this.id=='arrow_right'){
			slideSpeedMultiply = Math.floor((e.clientX - this.offsetLeft) / 5);
			slideSpeed = -1*slideSpeedMultiply;
			slideSpeed = Math.max(-10,slideSpeed);
		}else{			
			slideSpeedMultiply = 10 - Math.floor((e.clientX - this.offsetLeft) / 5);
			slideSpeed = 1*slideSpeedMultiply;
			slideSpeed = Math.min(10,slideSpeed);
			if(slideSpeed<0)slideSpeed=10;
		}
	}
	
	function releaseSlide()
	{
		var id = this.id;
		this.getElementsByTagName('IMG')[0].src = 'images/' + this.id + '.jpg';
		slideSpeed=0;
	}
		
	function gallerySlide()
	{
		if(slideSpeed!=0){
			var leftPos = imageGalleryObj.offsetLeft;
			leftPos = leftPos/1 + slideSpeed;
			if(leftPos>maxGalleryXPos){
				leftPos = maxGalleryXPos;
				slideSpeed = 0;
				
			}
			if(leftPos<minGalleryXPos){
				leftPos = minGalleryXPos;
				slideSpeed=0;
			}
			
			imageGalleryObj.style.left = leftPos + 'px';
		}
		setTimeout('gallerySlide()',20);
		
	}
	
	function showImage()
	{
		if(activeImage){
			activeImage.style.filter = 'alpha(opacity=50)';	
			activeImage.style.opacity = 0.5;
		}	
		this.style.filter = 'alpha(opacity=100)';
		this.style.opacity = 1;	
		activeImage = this;	
	}
	
	function initSlideShow()
	{
		document.getElementById('arrow_left').onmousedown = startSlide;
		document.getElementById('arrow_left').onclick = releaseSlide;
		document.getElementById('arrow_right').onmousedown = startSlide;
		document.getElementById('arrow_right').onclick = releaseSlide;
		
		imageGalleryObj = document.getElementById('theImages');
		imageGalleryLeftPos = imageGalleryObj.offsetLeft;
		imageGalleryWidth = document.getElementById('galleryContainer').offsetWidth - 80;
		maxGalleryXPos = imageGalleryObj.offsetLeft; 
		minGalleryXPos = imageGalleryWidth - document.getElementById('slideEnd').offsetLeft;
		var slideshowImages = imageGalleryObj.getElementsByTagName('IMG');
		for(var no=0;no<slideshowImages.length;no++){
			slideshowImages[no].onmouseover = showImage;
		}
		
		
		gallerySlide();
	}
	

	window.onload = initSlideShow;