//Funções para a Mothers Chocolate
////////////////////////////////////////////////////////////////////////////////////////////

//Valida Formulário Busca

function ValidaFormBusca (form)
{
    
	if(form.busca.value.length<=1)
	{
		alert("O campo Busca deve estar preenchido corretamente.");
		form.busca.focus();
		return false;
	}
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////

//valida Formulário Newsletter
function ValidaFormNewsletter(form)
{
	
	if(form.nome.value.length<=1 || form.nome.value=="Nome:")
  {
	   alert("O campo Nome deve estar preenchido corretamente.");
	   form.nome.focus();
	   return false;
   }
	
	var str_email = form.email.value ;
    if (( str_email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1 ) || ( str_email == 'E-mail:' ))
	{
    	alert("O campo E-mail deve ser preenchido corretamente");
    	form.email.focus();
    	return false;
	}
	

}

/////////////////////////////////////////////////////////////////////////////////////////////////////

//valida formulário Contato
function ValidaForm_Contato(form)
{
	if(form.nome.value.length<=1)
	{
		alert("O campo Nome deve estar preenchido corretamente.");
		form.nome.focus();
		return false;
	}
	
	var str_email = form.email.value ;
    if (( str_email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1 ) || ( str_email == 'Email:' ))
	{
    	alert("O campo E-mail deve ser preenchido corretamente");
    	form.email.focus();
    	return false;
	}
	
	if(form.telefone.value.length<=1)
	{
		alert("O campo Telefone deve estar preenchido corretamente.");
		form.telefone.focus();
		return false;
	}
	
	if(form.cidade.value.length<=1)
	{
		alert("O campo Cidade deve estar preenchido corretamente.");
		form.cidade.focus();
		return false;
	}
	
	if(form.estado.value.length<=1)
	{
		alert("O campo Estado deve estar selecionado corretamente.");
		form.estado.focus();
		return false;
	}
	
	if(form.mensagem.value.length<=1)
	{
		alert("O campo Mensagem deve estar preenchido corretamente.");
		form.mensagem.focus();
		return false;
	}
	
	if(form.cod.value.length<=1)
	{
		alert("O campo Código de Segurança deve estar preenchido corretamente.");
		form.cod.focus();
		return false;
	}
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////

//função para o Menu lateral (mostrar e ocultar)
function onOff(id){
	visivel = document.getElementById(id).style.display;
	if(visivel == "" || visivel == "none"){
		document.getElementById(id).style.display = "block";
	}
	else{
		document.getElementById(id).style.display = "none";
	}
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

//função para o (mostrar e ocultar acima)
function onOffEsconde(id) { 
	esconder = (document.getElementById(id).style.display == 'block');
	/*if ( visivel == "" || visivel == "none" ) {
		document.getElementById(id).style.display = "block"
	} else {
		document.getElementById(id).style.display = "none";
	}*/
	
	divs = document.getElementsByTagName('DIV');
	for(d=0; d<divs.length; d++){
		_div = divs[d];

		if (_div.className == 'submenu'){
			id_link = 'titulo_'+(_div.id);
			_link = document.getElementById(id_link);

			if (_div.id != id){
				_div.style.display = 'none';
	
			}
			else{
				_div.style.display = esconder ? 'none' : 'block';
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////

// Função de Máscara de Campos
function FormatMask(objForm, strField, sMask, evtKeyPress) 
{
  
  var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;

  if(window.event) { // Internet Explorer
    nTecla = evtKeyPress.keyCode; }
  else if(evtKeyPress.which) { // Nestcape
    nTecla = evtKeyPress.which;
  }

  sValue = objForm[strField].value;

  // Limpa todos os caracteres de formatação que
  // já estiverem no campo.
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( "-", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( ".", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( "/", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( "(", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( ")", "" );
  sValue = sValue.toString().replace( " ", "" );
  sValue = sValue.toString().replace( " ", "" );
  fldLen = sValue.length;
  mskLen = sMask.length;

  i = 0;
  nCount = 0;
  sCod = "";
  mskLen = fldLen;

  if (nTecla != 8) { // backspace
    if (sMask.charAt(i-1) == "9") { // apenas números...
      return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
    else { // qualquer caracter...


  while (i <= mskLen) {
     bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/"))
     bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

     if (bolMask) {
       sCod += sMask.charAt(i);
       mskLen++; }
     else {
       sCod += sValue.charAt(nCount);
       nCount++;
     }

     i++;
   }

   objForm[strField].value = sCod;

      return true;
    } }
  else {
    return true;
  }

}

////////////////////////////////////////////////////////////////////////////////////////////////

//Função de popup
function MM_openBrWindow(theURL,winName,features) 
{
	var myWin =  window.open(theURL,winName,features);
	myWin.focus();
}

/////////////////////////////////////////////////////////////////////////////////////////////////

//Função para o Menu PopUp

function fnImagemPopUp( titulo, largura , altura , imagem , alt )
{
	
		var janela ;
		janela = window.open("","popFoto","width="+largura+",height="+altura+",scrollbars=no,toolbar=no,location=no,status=no,menubar=no,resizable=no,left=300,top=300'");
		janela.document.write('<html><head><title>' + titulo + '</title></head>');
		janela.document.write('<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">');
		janela.document.write('<a href="javascript:window.close();"><img src="'+ imagem + '" alt="' + alt + '" border="0" /></a> ');
		janela.document.write('</body></html>');

}


/////////////////////////////////////////////////////////////////////////////////////////////////

//Função Adiciona a Favoritos
function fnAdicionaFavoritos()
{
	str_titulo = "Mother's Chocolate";
	str_url = "http://www.mothers.com.br";
	//FireFox
	if (window.sidebar)
	{
   		window.sidebar.addPanel(str_titulo, str_url,"");
	}
	//IE
	else if( window.external )
	{
  		window.external.AddFavorite( str_url, str_titulo);
	}
	//Opera
	else if(window.opera && window.print)
	{
   		return true;
	}
}


/////////////////////////////////////////////////////////////////////////////////////////////////

//Função Validação do Indique Amigo
function ValidaForm_IndiqueAmigo(form)
{	

	if (form.nome.value=="")
	{
		alert("O campo Seu nome deve ser preenchido corretamente.");
		form.nome.focus();
		return false;
	}
	
	
	var str_email = form.email.value ;
    if (( str_email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1 ) || ( str_email == 'Email:' ))
	{
    	alert("O campo Seu E-mail deve ser preenchido corretamente.");
    	form.email.focus();
    	return false;
	}
	
	var emails_indicados = form.emailamigo.value.split(',');
	
	for(e=0; e<emails_indicados.length; e++){
		var str_email = emails_indicados[e].replace(' ', '');
	    if (( str_email.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1 ) || ( str_email == 'Email:' ))
		{
	    	alert("O campo E-mail do Destinatário deve ser preenchido corretamente. \n"+str_email);
	    	form.emailamigo.focus();
	    	return false;
		}
	}
	
	
}

////////////////////////////////////////////////////////////////////////////////////////////////////////

//Função para o flash

function fnFlash(str_url, int_largura, int_altura)
{
	document.writeln('<object type="application/x-shockwave-flash" data="' + str_url + '" width="' + int_largura + '" height="' + int_altura + '">');
	document.writeln('<param name="movie" value="' + str_url + '">');
	document.writeln('<param name="quality" value="high">');
	document.writeln('<param name="wmode" value="transparent">');
	document.writeln('<embed wmode="transparent" src="' + str_url + '" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="' + int_largura + '" height="' + int_altura + '"></embed></object>');
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

function verificaEmail(newsletter,tipo){
	document.getElementById('acao').value = tipo;
	if(newsletter.email.value == ""){
		alert("Atenção!\nO campo EMAIL deve ser preenchido!");
		newsletter.email.focus();
		return false;
	}
	if(newsletter.email.value.search(/^\w+((-\w+)|(\.\w+))*\@\w+((\.|-)\w+)*\.\w+$/) == -1){
		alert("Atenção!\nVocê não preencheu o campo E-MAIL corretamente!");
		newsletter.email.focus();
		return false;
	}
	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////

/* Função das imagens trocarem de lugar */
function trocaFoto(campo){
    var foto = document.getElementById('foto_principal').src;
	
	//Pega o Caminho Novo da Imagem
	var caminho_novo = document.getElementById(campo).src;
	//Caso Seja Thumbail , pega o caminho da imagem maior
	caminho_novo = caminho_novo.replace("/thumbnail","");
	
    document.getElementById('foto_principal').src = document.getElementById(campo).src;
    document.getElementById(campo).src = foto;
	
	document.getElementById('link_foto_principal').href = caminho_novo;
	
}

function trocarConteudoDaDiv ( str_origem, str_destino ) {
	var str_conteudo_origem ;
	var str_conteudo_destino ;
	// a imagem menor
	str_conteudo_origem = byId( str_origem ).innerHTML ;
	// a imagem maior
	str_conteudo_destino = byId( str_destino ).innerHTML ;
	// substitui o tamanho da menor
	str_conteudo_origem = str_conteudo_origem.replace( 'width="75"', 'width="120"' ) ;
	// substitui o tamanho da maior
	str_conteudo_destino = str_conteudo_destino.replace( 'width="120"', 'width="75"' ) ;
	// atribui a imagem menor na div maior
	byId( str_destino ).innerHTML = str_conteudo_origem ;
	// atribui a imagem maior na div menor
	byId( str_origem ).innerHTML = str_conteudo_destino ;
}

function byId ( campo ) {
	return document.getElementById( campo ) ;
}