// JavaScript Document

// Validação do formulário da página de contactos
function valid_Email(email, msg){			
	email2='%'+email+'%';
	var re=/\%[a-z\-\_0-9]+(\.[a-z\-\_0-9]+)*@[a-z\-\_0-9]+(\.[a-z\-\_0-9]+)*\%/i;
	if (!email2.match(re)) {
		return false;
	}	
		
	var tam = email.length;
	var varA=0;
	var varB=0;
	for (i=0;i < tam;i++){
		caracter = email.substr(i,1);
		if(caracter == '@' && varA==0){
			varA = i;				
		}
		if(caracter == '.' && varA>0 && varB==0){
			varB = i;				
		}		
	}
	var str1 = email.substr(0,varA);
	var str2 = email.substr(varA+1,varB-(varA+1));
	var str3 = email.substr(varB+1,tam);
			
	if(str1.length < 2 || str2.length < 2 || str3.length < 2){
		return false;
	}

	return true;
	
	}		

function valid(){					
	if (document.forms.Contacto.nome.value.length < 3){
		alert('Introduza o seu  nome.');
		return false;				
	}
	if (!valid_Email(document.forms.Contacto.email.value,'O Email que indicou não é válido.') || document.forms.Contacto.email.value.length < 7) {
		alert('Introduza um e-mail válido.');
		return false;					
	}
	if (document.forms.Contacto.assunto.value.length < 5) {
		alert('Introduza a sua mensagem.');
		return false;
	}		
}


function validuk(){					
	if (document.forms.Contacto.nome.value.length < 3){
		alert('Insert your name.');
		return false;				
	}
	if (!valid_Email(document.forms.Contacto.email.value,'O Email que indicou não é válido.') || document.forms.Contacto.email.value.length < 7) {
		alert('Insert a valid e-mail.');
		return false;					
	}
	if (document.forms.Contacto.assunto.value.length < 5) {
		alert('Insert your message.');
		return false;
	}		
}

function checkFields_protected()
{

  var seleccao_marca = window.document.modelo.marca.selectedIndex;
  if ( (seleccao_marca == 0) || (seleccao_marca == 1) || (seleccao_marca == (window.document.modelo.marca.length-1)))
  {
    alert ("Seleccione uma marca");
  }
  else
  {
	  var selectedIndexMarca = window.document.modelo.marca.selectedIndex;

	  var url ="gmod.php?marca=" + window.document.modelo.marca.options[selectedIndexMarca].text + "&procurar=sim";
	  //alert(url);
	  window.location.replace(url);
	  //window.document.produtos.action = "produtos.php";
	}
  }
  
//função que verifica a marca escolhida
function swapOptions(the_array_name)
{
//  var numbers_select = window.document.produtos.modelo;
 
  var seleccao = window.document.produtos.marca.selectedIndex;
  // if utilizado para verificar se é necessária preencher a lista, só no caso de alguma marca estar eeleccionada
  if ( (seleccao == 0) || (seleccao == 1) || (seleccao == (window.document.produtos.marca.length-1)))
  {
  //alert("Não há marca seleccionada");
  window.document.produtos.modelo.length = 3;
  window.document.produtos.modelo.options[0].text = "2. Seleccione o modelo";
  window.document.produtos.modelo.options[1].text = "**************************";
  window.document.produtos.modelo.options[2].text = "**************************";
  }
  else{
  var re = new RegExp (' ', 'gi') ;
  var result = the_array_name.replace(re, '_');
    // transforma o valor escolhido na marca num array
  var the_array = eval(result);
  // define o tamanho da lista - mais 3 por da 1ª, 2ª e última linha
  window.document.produtos.modelo.length = the_array.length+3; 
  //chama função para preencher a lista 
  setOptionText(window.document.produtos.modelo, the_array);
  }
}

// função que preenche a lista segundo a opção escolhida
function setOptionText(the_select, the_array)
{
  the_select.options[0].text = "2. Seleccione o modelo";
  the_select.options[1].text = "**************************";
  for (loop = 0; loop < the_array.length; loop++)
  {
    the_select.options[loop+2].text = the_array[loop];
	i=loop;
  }
  the_select.options[the_array.length+2].text = "**************************";
  
}

//Verifica os se os campos dos produtos estão ou não seleccionados
function checkFields(num)
{
  var seleccao_industria = window.document.produtos.industria.selectedIndex;
  if ( (seleccao_industria == 0) || (seleccao_industria == 1) || (seleccao_industria == (window.document.produtos.industria.length-1)))
  {
    alert ("Seleccione a indústria");
  }
  else
  {
      var selectedIndexIndustria = window.document.produtos.industria.selectedIndex; 
	  if ((num == 1)&&(seleccao_industria != 2)){
	  var url ="index.php?topico=7&industria=" + window.document.produtos.industria.options[selectedIndexIndustria].text + "&opcao=" + selectedIndexIndustria + "&procurar=sim";
	  //alert(url);
	  }
	  if ((num == 1)&&(seleccao_industria == 2)){
	  var selectedIndexSub = window.document.produtos.subind.selectedIndex;
	  selectedIndexSub = selectedIndexSub + 7;
	  var url ="index.php?topico=7&industria=" + window.document.produtos.industria.options[selectedIndexIndustria].text + "&opcao=" + selectedIndexSub + "&procurar=sim";
	  }
	  
	  if (num == 3){
	  var url ="index.php?topico=7&industria=" + window.document.produtos.industria.options[selectedIndexIndustria].text + "&opcao=" + selectedIndexIndustria;
	  //alert(url);
	  }

	  window.location.replace(url);
	  //window.document.produtos.action = "produtos.php";
}  
}