var fonte_cartoes='Georgia';
var id_fonte_cartoes=0;
var cor_cartoes='#000000';
var id_cor_cartoes=0;
var formato_cartao=2;
var id_formato_cartao=0;
var cor_fundo='';
var copiar_campos_act=0;

function limpar(id){
	document.getElementById(id).value='';	
}

function ffam(){
	document.getElementById('descricao_familia').style.display='none';	
}

function foto_grande(id){
	document.getElementById('foto_grande').src="backoffice/img/"+id;
}

function calc_envelope(valor){
	
	switch (valor) {
		case 50:
		case '50':  document.getElementById('preco_envelopes').innerHTML='€ 260';
	            self.document.forms['correspondencia_e_cartoes'].valor.value=260;
					break;
		case 100:
		case '100':	document.getElementById('preco_envelopes').innerHTML='€ 380';
	            self.document.forms['correspondencia_e_cartoes'].valor.value=380;
					break;
	}		
}

function calc_cartao(valor){
	switch (valor) {
		case 50:
		case '50':	document.getElementById('preco_cartoes').innerHTML='€ 180';
	            self.document.forms['cartoes'].valor.value=180;
					break;
		case 100:
		case '100':	document.getElementById('preco_cartoes').innerHTML='€ 240';
	            self.document.forms['cartoes'].valor.value=240;
					break;
	}	
}

function cor_de_fundo(cor){
	document.getElementById('cart_tab').style.background=cor;
	
	cor_fundo=cor;
}

function muda_fonte(num){
		
	var um='Courier New';
	var dois='Arial';
	var tres='Georgia';
	var quatro='Brush Script MT';
	var cinco='Gill Sans MT';
	
	
	switch (formato_cartao) {
		case 1:var cod1='height:145px; vertical-align:center; text-align:center; font-size:14px; font-family:';
		         var cod2='; letter-spacing: 2px;color: '+cor_cartoes+';';
					break;
		case 2:var cod1='font-size:14px; font-family:';
		         var cod2='; text-align:center; height:125px; letter-spacing: 2px;color: '+cor_cartoes+';';	
					break;
	}	
	
	
	
	switch (num) {
		case '1':document.getElementById('cart_tab1').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab1').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab1').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab1').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab1').style.cssText=cod1+cinco+cod2; break;
	}
	
	//=====================
	
	var um='Courier New';
	var dois='Arial';
	var tres='Georgia';
	var quatro='Brush Script MT';
	var cinco='Gill Sans MT';
	
	
	switch (formato_cartao) {
		case 1:var cod1='width:50%; height:100px; vertical-align:top; text-aling:left; font-size:12px; font-family:';
		         var cod2='; color: '+cor_cartoes+';';
					break;
		case 2:var cod1='font-size:12px; font-family:';
		         var cod2='; text-align:center; height:45px;color: '+cor_cartoes+';';
					break;
	}	
	
	switch (num) {
		case '1':document.getElementById('cart_tab2').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab2').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab2').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab2').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab2').style.cssText=cod1+cinco+cod2; break;
	}	
	
	//=====================
	
	var um='Courier New';
	var dois='Arial';
	var tres='Georgia';
	var quatro='Brush Script MT';
	var cinco='Gill Sans MT';
	
	
	switch (formato_cartao) {
		case 1:var cod1='width:50%; height:100px; vertical-align:top; text-align:right; font-size:12px; font-family:';
		         var cod2='; color: '+cor_cartoes+';';
					break;
		case 2:var cod1='font-size:12px; font-family:';
		         var cod2='; text-align:center; height:45px; padding-bottom:30px;color: '+cor_cartoes+';';
					break;
	}	
	
	switch (num) {
		case '1':document.getElementById('cart_tab3').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab3').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab3').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab3').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab3').style.cssText=cod1+cinco+cod2; break;
	}	
  
  //------------------------
  
	switch (num) {
		case '1':fonte_cartoes=um; break;
		case '2':fonte_cartoes=dois; break;
		case '3':fonte_cartoes=tres; break;
		case '4':fonte_cartoes=quatro; break;
		case '5':fonte_cartoes=cinco; break;
	}
	
	id_fonte_cartoes=num;	  	
	
}


function muda_cor(num){
	
	var um='#000000';
	var dois='#002726';
	var tres='#4A0001';
	var quatro='#001100';
	var cinco='#85713F';
		
	
	
	switch (formato_cartao) {
		case 1:var cod1='height:145px; vertical-align:center; text-align:center; font-size:14px; font-family:'+fonte_cartoes+'; letter-spacing: 2px;color:';
		         var cod2=';';
					break;
		case 2:var cod1='font-size:14px; font-family:'+fonte_cartoes+'; text-align:center; height:125px; letter-spacing: 2px;color: ';
		         var cod2=';';	
					break;
	}		
	
	switch (num) {
		case '1':document.getElementById('cart_tab1').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab1').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab1').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab1').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab1').style.cssText=cod1+cinco+cod2; break;
	}
	
	//=====================
	
	var um='#000000';
	var dois='#002726';
	var tres='#4A0001';
	var quatro='#001100';
	var cinco='#85713F';
			
	switch (formato_cartao) {
		case 1:var cod1='width:50%; height:100px; vertical-align:top; text-aling:left; font-size:12px; font-family:'+fonte_cartoes+'; color:';
		         var cod2=';';
					break;
		case 2:var cod1='font-size:12px; font-family:'+fonte_cartoes+'; text-align:center; height:45px;color: ';
		         var cod2=';';
					break;
	}		
	
	switch (num) {
		case '1':document.getElementById('cart_tab2').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab2').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab2').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab2').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab2').style.cssText=cod1+cinco+cod2; break;
	}	
	
	//=====================
	
	var um='#000000';
	var dois='#002726';
	var tres='#4A0001';
	var quatro='#001100';
	var cinco='#85713F';
			
	
	switch (formato_cartao) {
		case 1:var cod1='width:50%; height:100px; vertical-align:top; text-align:right; font-size:12px; font-family:'+fonte_cartoes+'; color:';
		         var cod2=';';
					break;
		case 2:var cod1='font-size:12px; font-family:'+fonte_cartoes+'; text-align:center; height:45px; padding-bottom:30px;color: ';
		         var cod2=';';
					break;
	}		
	
	switch (num) {
		case '1':document.getElementById('cart_tab3').style.cssText=cod1+um+cod2; break;
		case '2':document.getElementById('cart_tab3').style.cssText=cod1+dois+cod2; break;
		case '3':document.getElementById('cart_tab3').style.cssText=cod1+tres+cod2; break;
		case '4':document.getElementById('cart_tab3').style.cssText=cod1+quatro+cod2; break;
		case '5':document.getElementById('cart_tab3').style.cssText=cod1+cinco+cod2; break;
	}
	
  //------------------------
  
	switch (num) {
		case '1':cor_cartoes=um; break;
		case '2':cor_cartoes=dois; break;
		case '3':cor_cartoes=tres; break;
		case '4':cor_cartoes=quatro; break;
		case '5':cor_cartoes=cinco; break;
	}		
	
	id_cor_cartoes=num;
	
}


function muda_estrutura(num){
	um='<table id="cart_tab" border="0" cellpadding="0" cellspacing="0" align="center" style="width:366px; height:245px; background-color:#FFFFEB;"><tr><td id="cart_tab1" colspan="2" style="height:145px; vertical-align:center; text-align:center; font-size:14px; font-family:Georgia; letter-spacing: 2px;">FINE&CANDY STATIONERY</td></tr><tr><td id="cart_tab2" style="width:50%; height:100px; vertical-align:top; text-aling:left; font-size:12px; font-family:Georgia;">Loja / Store:<br/> Rua do Rosário, 129<br/>4050 - 523 Porto, Portugal</td><td id="cart_tab3" style="width:50%; height:100px; vertical-align:top; text-align:right; font-size:12px; font-family:Georgia;">T. 22 013 19 49</td></tr></table>';	
	dois='<table id="cart_tab" border="0" cellpadding="0" cellspacing="0" style="width:384px; height:245px; background-color:#FFFFEB;"><tr><td valign="center" id="cart_tab1" style="font-size:14px; font-family:Georgia; text-align:center; height:125px; letter-spacing: 2px; padding-top:20px;">FINE&CANDY STATIONERY</td></tr><tr><td id="cart_tab2" style="font-size:12px; font-family:Georgia; text-align:center; height:45px;">Loja / Store: Rua do Rosário, 129<br/>4050 - 523 Porto, Portugal</td></tr><tr><td id="cart_tab3" style="font-size:12px; font-family:Georgia; text-align:center; height:45px; padding-bottom:30px;">T. 22 013 19 49</td></tr></table>';


	switch (num) {
		case '1':document.getElementById('local_cartao').innerHTML=um; break;
		case '2':document.getElementById('local_cartao').innerHTML=dois; break;
	}	
	
  //------------------------
  
	switch (num) {
		case '1':formato_cartao=1; break;
		case '2':formato_cartao=2; break;

	}		
	
    if(id_fonte_cartoes!=0){ muda_fonte(id_fonte_cartoes); }
    if(id_cor_cartoes!=0){ muda_cor(id_cor_cartoes); }
    if(id_formato_cartao!=0){ muda_estrutura(id_formato_cartao); }
    if(cor_fundo!=''){ cor_de_fundo(cor_fundo); }
	
	id_formato_cartao=0;
	
}

function copiar_campos(){

	switch (copiar_campos_act) {
		case 0:copiar_campos_act=1; break;
		case 1:copiar_campos_act=0; break;
 }
	
	
self.document.dados_facturacao.nome_end_env.value=  self.document.dados_facturacao.nome_end_fac.value
self.document.dados_facturacao.morada_end_env.value=  self.document.dados_facturacao.morada_end_fac.value
self.document.dados_facturacao.cod_postal_end_env.value=  self.document.dados_facturacao.cod_postal_end_fac.value
self.document.dados_facturacao.cidade_end_env.value=  self.document.dados_facturacao.cidade_end_fac.value
self.document.dados_facturacao.pais_end_env.value=  self.document.dados_facturacao.pais_end_fac.value
self.document.dados_facturacao.telemovel_end_env.value=  self.document.dados_facturacao.telemovel_end_fac.value
self.document.dados_facturacao.telefone_end_env.value=  self.document.dados_facturacao.telefone_end_fac.value
self.document.dados_facturacao.fax_end_env.value=  self.document.dados_facturacao.fax_end_fac.value
self.document.dados_facturacao.email_end_env.value=  self.document.dados_facturacao.email_end_fac.value
	
	switch (copiar_campos_act) {
		case 1:
					self.document.dados_facturacao.nome_end_env.readOnly=true;
					self.document.dados_facturacao.morada_end_env.readOnly=true;
					self.document.dados_facturacao.cod_postal_end_env.readOnly=true;
					self.document.dados_facturacao.cidade_end_env.readOnly=true;
					self.document.dados_facturacao.pais_end_env.readOnly=true;
					self.document.dados_facturacao.telemovel_end_env.readOnly=true;
					self.document.dados_facturacao.telefone_end_env.readOnly=true;
					self.document.dados_facturacao.fax_end_env.readOnly=true;
					self.document.dados_facturacao.email_end_env.readOnly=true;		
		 break;
		case 0:
					self.document.dados_facturacao.nome_end_env.readOnly=false;
					self.document.dados_facturacao.morada_end_env.readOnly=false;
					self.document.dados_facturacao.cod_postal_end_env.readOnly=false;
					self.document.dados_facturacao.cidade_end_env.readOnly=false;
					self.document.dados_facturacao.pais_end_env.readOnly=false;
					self.document.dados_facturacao.telemovel_end_env.readOnly=false;
					self.document.dados_facturacao.telefone_end_env.readOnly=false;
					self.document.dados_facturacao.fax_end_env.readOnly=false;
					self.document.dados_facturacao.email_end_env.readOnly=false;		
		break;
 }

}

function actualiza_campo(nome){
	
	if(copiar_campos_act==1){
	
		switch (nome) {
			case 'nome_end_fac': var campo='nome_end_env'; break;
			case 'morada_end_fac': var campo='morada_end_env'; break;
			case 'cod_postal_end_fac': var campo='cod_postal_end_env'; break;
			case 'cidade_end_fac': var campo='cidade_end_env'; break;
			case 'pais_end_fac': var campo='pais_end_env'; break;
			case 'telemovel_end_fac': var campo='telemovel_end_env'; break;
			case 'telefone_end_fac': var campo='telefone_end_env'; break;
			case 'fax_end_fac': var campo='fax_end_env'; break;
			case 'email_end_fac': var campo='email_end_env'; break;
	  }		
		self.document.dados_facturacao[campo].value=self.document.dados_facturacao[nome].value
  }
	
}

function Valida_cartoes_2( formulario ) {
  if (formulario.nome.value!='') {
    return true
  } else {
	  alert('Necessário preencher todos os campos obrigatórios');
    return false
  }
} 

function Valida_dados_facturacao(formulario){
  if (formulario.nome_end_fac.value!=''
      && formulario.morada_end_fac.value!=''
      && formulario.cod_postal_end_fac.value!=''
      && formulario.cidade_end_fac.value!=''
      && formulario.pais_end_fac.value!=''
      && formulario.nome_end_env.value!=''
      && formulario.morada_end_env.value!=''
      && formulario.cod_postal_end_env.value!=''
      && formulario.cidade_end_env.value!=''
      && formulario.pais_end_env.value!=''
      && (formulario.telefone_end_fac.value!='' || formulario.telemovel_end_fac.value!='' || formulario.email_end_fac.value!='')) {
    return true
  } else {
	  alert('Necessário preencher todos os campos obrigatórios e um contacto');
    return false
  }
}

function div_login(){

	  if(document.getElementById('div_do_login').style.display=='block'){
		  document.getElementById('div_do_login').style.display='none';
    }else{

			if(window.innerHeight != null){
				altura=window.innerHeight;
			}
			  
			if(document.body.clientHeight != null){
				altura=document.body.clientHeight;
			}	    
			
			if(document.getElementById('home').clientHeight!=null){
				altura=document.getElementById('home').clientHeight;
      }
			
			document.getElementById('div_do_login').style.height=altura;
	    document.getElementById('div_do_login').style.display='block';
    }
}

function funcao_submit_newsletter(formulario){
 if (formulario.email.value.indexOf('@', 1) == -1 || formulario.email.value.length < 5 ) {
  alert("Deve indicar um email com formato válido: Entidade@dominio");
  return false
 }else{
	return true
 }
}

function regista_email(estado){
	 
    var xmlHttp;
    try{
	     //firefox, opera 8.0+,safari
	     xmlHttp=new XMLHttpRequest();
    } catch(e){
	     // internet explorer
	     try{
		     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e){
			 try{
				 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			 } catch (e) {
				 alert("Your browser does not support AJAX!");
				 return false;
		     }
	     }
	 }
	 
	 
	 xmlHttp.onreadystatechange=function(){
		 if(xmlHttp.readyState==4){
			 //alert(xmlHttp.responseText);
			 if(xmlHttp.responseText==1){alert("E-mail registado com sucesso."); self.document.newsletter.email.value=""}
			 if(xmlHttp.responseText==2){alert("Erro ao registar o e-mail, tente novamente ou contacte o administrador do site.");}
			 if(xmlHttp.responseText==3){alert("E-mail apagado com sucesso."); self.document.newsletter.email.value=""}
			 if(xmlHttp.responseText==4){alert("Erro ao apagar o e-mail, tente novamente ou contacte o administrador do site.");}
	     }
	 }
	 
	 url='grava_newsletter.php?email='+document.newsletter.email.value
	 url+='&data='+document.newsletter.data.value
	 url+='&estado='+estado
	 
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}

function registo_utilizador(){
	
	   if(!document.getElementById('reg_1_name').value
	      || !document.getElementById('reg_2_name').value
	      || !document.getElementById('reg_email').value
	      || !document.getElementById('reg_username').value
	      || !document.getElementById('reg_pass').value
	      || !document.getElementById('reg_conf_pass').value){ alert('Necessário preencher todos os campos!'); return; }

	   if(document.getElementById('reg_pass').value!=document.getElementById('reg_conf_pass').value){ alert('Dados incorrectos na palavra-passe'); return; }   
	      
    var xmlHttp;
    try{
	     //firefox, opera 8.0+,safari
	     xmlHttp=new XMLHttpRequest();
    } catch(e){
	     // internet explorer
	     try{
		     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e){
			 try{
				 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			 } catch (e) {
				 alert("Your browser does not support AJAX!");
				 return false;
		     }
	     }
	 }
	 
	 
	 xmlHttp.onreadystatechange=function(){
		 if(xmlHttp.readyState==4){
			 //alert(xmlHttp.responseText);
			 if(xmlHttp.responseText==1){
				 alert("Conta registada com sucesso.\nEntre na sua conta em 'Minha conta'"); 
                 document.getElementById('reg_1_name').value='';
                 document.getElementById('reg_2_name').value='';
                 document.getElementById('reg_email').value='';
                 document.getElementById('reg_username').value='';
                 document.getElementById('reg_pass').value='';
                 document.getElementById('reg_conf_pass').value='';
                 document.getElementById('reg_news').checked=false;	
			 }
			 if(xmlHttp.responseText==2){alert("Erro ao registar conta, tente novamente ou contacte o administrador do site.");}
	     }
	 }
	 
	 url='conteudo/ajax_registo_utilizador.php?';
	 url+='reg_1_name='+document.getElementById('reg_1_name').value;
	 url+='&reg_2_name='+document.getElementById('reg_2_name').value;
	 url+='&reg_email='+document.getElementById('reg_email').value;
	 url+='&reg_username='+document.getElementById('reg_username').value;
	 url+='&reg_pass='+document.getElementById('reg_pass').value;
	 url+='&reg_conf_pass='+document.getElementById('reg_conf_pass').value;	
	 
	 if(document.getElementById('reg_news').checked==true){ url+='&reg_news='+document.getElementById('reg_news').value; }
	  
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);
}


function entrar_conta_verifica(formulario){   
	
 if (!formulario.username.value || !formulario.password.value) {
    alert("Necessário preencher todos os e-mails");
    return false;
 }else{
    return true;
 }	    
 
}

function gravar_dados_utilizador(formulario){
  if (formulario.nome_end_fac.value!=''
      && formulario.morada_end_fac.value!=''
      && formulario.cod_postal_end_fac.value!=''
      && formulario.cidade_end_fac.value!=''
      && formulario.pais_end_fac.value!=''
      && formulario.nome_end_env.value!=''
      && formulario.morada_end_env.value!=''
      && formulario.cod_postal_end_env.value!=''
      && formulario.cidade_end_env.value!=''
      && formulario.pais_end_env.value!='') {
    return true
  } else {
	  alert('Necessário preencher todos os campos obrigatórios');
    return false
  }	
}

function add_carrinho(url, id,id_mod){	
	self.location='index.php?'+url+'&add_pro='+id+'&add_promod='+id_mod;
}

function altera_preco(campo,valor, id, id_modelo){
  document.getElementById('valor_'+id+'_'+id_modelo).innerHTML='€ '+(campo.value*valor);
}

function elimina_produto(id,id_mod, url){
	self.location='?'+url+'&rem_pro='+id+'&rem_pro_mod='+id_mod;
}

function mudar_fundo(fundo){
	document.getElementById("home").style.backgroundImage="url(layout/"+fundo+")";
	
    var xmlHttp;
    try{
	     //firefox, opera 8.0+,safari
	     xmlHttp=new XMLHttpRequest();
    } catch(e){
	     // internet explorer
	     try{
		     xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		 } catch (e){
			 try{
				 xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			 } catch (e) {
				 alert("Your browser does not support AJAX!");
				 return false;
		     }
	     }
	 }
	 
	 
	 xmlHttp.onreadystatechange=function(){
		 if(xmlHttp.readyState==4){
			 //alert(xmlHttp.responseText);
	     }
	 }
	 
	 url='ajax_grava_fundo.php?fundo='+fundo;
	 
	 xmlHttp.open("GET",url,true);
	 xmlHttp.send(null);	
}

function def_tipo_pag(id){
	switch(id){
	case 1:
	  self.document.conclusao_encomenda.tipo_pagamento.value='paypal';
	  self.document.conclusao_encomenda.submit();
	  break;
	case 2:
	  self.document.conclusao_encomenda.tipo_pagamento.value='trans';
	  self.document.conclusao_encomenda.submit();
	  break;
	}
}
