var act = ''
/*--------------------------------------------------------------------------------------------------------------
          FUNÇÃO PARA ABRIR NOVAS JANELAS DE POPUP
---------------------------------------------------------------------------------------------------------------*/
function showPopup(opt){
	var opt;
	
	if (opt == 'Rec') {
		// Recrutamento
		strLocation = 'popRecrutamento.html'
		window.open(strLocation,'_blank','height=600, width=555, status=no, toolbar=no, menubar=no,location=no, resizable=no, top=100, left=300');
	}
	else if(opt == 'Lgin') {
		//  loja online
		strLocation = 'pop_login.php'
		window.open(strLocation,'_blank','height=280, width=340, status=no, toolbar=no, menubar=no,location=no, resizable=no, top=100, left=300');
	}
	else if(opt == '5Rz') {
		// 5 Razoes 
		strLocation = 'pop5razoes.html'
		window.open(strLocation, '_blank', 'height=540, width=325, status=no, toolbar=no, menubar=no, location=no, resizable=0, top=100, left=300', 'replace=true');
	}
	else if(opt == 'salesF') {
		// Sales folder
		strLocation = 'php/regMarcas.php?opt=salesF'
		window.open(strLocation, '_blank', 'height=200, width=240, status=no, toolbar=no, menubar=no, location=no, resizable=0, top=100, left=300', 'replace=true');
	}
	else if(opt == 'fichT') {
		// Fichas técnicas
		strLocation = 'php/regMarcas.php?opt="fichT"'
		window.open(strLocation, '_blank', 'height=200, width=240, status=no, toolbar=no, menubar=no, location=no, resizable=0, top=100, left=300', 'replace=true');
	}
		else if(opt.substr(0,3) == 'sf_') {
			var opt2 = opt.substr(3);
			strLocation = 'nossasMarcas_' + opt2 + '.html'
			window.open(strLocation, '_blank', 'height=920, width=680, status=no, toolbar=no, menubar=no, location=no, top=10, left=300, resizable=0, scrollbars=yes', 'replace=true');
	}
}
//-------------------------------------------------------------------------------------------------------------
function frompop2reg(){
	strLocation = "php/reg.php"
	window.opener.location.href=strLocation
	self.close()
}
//-------------------------------------------------------------------------------------------------------------
function fecharjanela(){
	window.close()
}
/*--------------------------------------------------------------------------------------------------------------
          FUNÇÕES PARA VISUALIZAR BALOONS
---------------------------------------------------------------------------------------------------------------*/
function showToolTip(e,text){
	if(document.all)e = event;
	
	var obj = document.getElementById('bubble_tooltip');
	var obj2 = document.getElementById('bubble_tooltip_content');
	obj2.innerHTML = text;
	obj.style.display = 'block';
	var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	if(navigator.userAgent.toLowerCase().indexOf('safari')>=0)st=0; 
	var leftPos = e.clientX - 100;
	if(leftPos<0)leftPos = 0;
	obj.style.left = leftPos + 'px';
	obj.style.top = e.clientY - obj.offsetHeight -1 + st + 'px';
}	

function hideToolTip()
{
	document.getElementById('bubble_tooltip').style.display = 'none';
	
}



/*--------------------------------------------------------------------------------------------------------------------
          VALIDAÇÃO DADOS DE LOGIN PARA SALESFOLDER
-----------------------------------------------------------------------------------------------------------------------*/

function form1_Validator(regForm)
{
  theForm = document.getElementById(regForm);
//------------------------------------------------------------------- NR CONTRIBUINTE
  if (theForm.nrcontrib.value == "")
  {
    alert("Por favor introduza um valor no campo \"N.\260 de Contribuinte\" .");
    theForm.nrcontrib.focus();
    return (false);
  }

  if (theForm.nrcontrib.value.length < 9)
  {
    alert("Por favor introduza 9 d\355gitos no campo  \"N.\260 de Contribuinte\" .");
    theForm.nrcontrib.focus();
    return (false);
  }

  if (theForm.nrcontrib.value.length > 9)
  {
    alert("Por favor introduza 9 d\355gitos no campo  \"N.\260 de Contribuinte\" .");
    theForm.nrcontrib.focus();
    return (false);
  }

  var result = soNumeros(theForm.nrcontrib.value);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"Contribuinte\".");
    theForm.nrcontrib.focus();
    return (false);
  }
//------------------------------------------------------------------- LOJA
  if (theForm.codloja.value == "NOSTORE")
  {
    alert("Por favor selecione uma loja para iniciar sess\343o");
    theForm.nrcontrib.focus();
    return (false);
  }

//-------------------------------------------------------------------- CLIENTE
  if (theForm.cliente.value == "")
  {
    alert("Por favor introduza um valor no campo \"N.\260 de Cliente\" .");
    theForm.cliente.focus();
    return (false);
  }

    var nrcli = pad(theForm.cliente.value, 5);
  result = soNumeros(nrcli);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"N.\260 de Cliente\".");
    theForm.cliente.focus();
    return (false);
  }
  theForm.cliente.value = nrcli;

  //------------------------------------------------------------------------- JA PASSOU
  //validar email(verificao de endereco electronico
        loja = theForm.codloja.value;
        nif = theForm.nrcontrib.value;
        ficha = theForm.cliente.value;
        document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='pagina' name='pagina' value='valida'>";
        document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='codloja' name='codloja' value='"+loja+"'>";
        document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='nrcontrib' name='nrcontrib' value=" + nif + ">";
        document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='nrinterno' name='nrinterno' value=" + ficha + ">";
        document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='aceito' name='aceito' value='Y'>";
		document.getElementById('regMarcas').innerHTML = document.getElementById('regMarcas').innerHTML + "<input type=hidden id='salesF' name='salesF' value='Y'>";
		theForm.submit();
        return (true); 
}
function soNumeros(field) {
  var checkOK = "0123456789.";
  var checkStr = field;
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  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 (ch == ",")
    {
      allNum += ".";
      decPoints++;
    }
    else if (ch != ".")
      allNum += ch;
  }
  
  if (decPoints > 1)  allValid = false;
  
  return allValid;
}
// fill number with zeros
function pad(number, length) {

    var str = '' + number;
    while (str.length < length) {
        str = '0' + str;
    }

    return str;

}
function rec_pass(){
	var f_usrn
	var f_email
	var f_nif
	var f_cli
	var f_loja
	if(document.forms.form1['f_usrn'].value) f_usrn = document.forms.form1['f_usrn'].value
	if(document.forms.form1['f_email'].value) f_email = document.forms.form1['f_email'].value
	if(document.forms.form1['f_nif'].value) f_nif = document.forms.form1['f_nif'].value
	if(document.forms.form1['f_cli'].value) f_cli = document.forms.form1['f_cli'].value
	if(document.forms.form1['f_loja'].value) f_loja = document.forms.form1['f_loja'].value
	if (f_usrn && f_email && f_nif && f_cli && f_loja != '000') {
		//strLocation = 'http://' + home + 'rec_password.php?X=1'
		strLocation = 'lojaonline_pwd.php?X=1'
		if(f_usrn) strLocation += '&USRN='+f_usrn
		if(f_email) strLocation += '&EMAIL='+f_email
		if(f_nif) strLocation += '&NIF='+f_nif
		if(f_cli) strLocation += '&CLI='+f_cli
		if(f_loja) strLocation += '&ST='+f_loja
 		window.open(strLocation, '_self', "fullscreen=yes, menubar=no, toolbar=no", false)

	} else {
		alert ('Todos os campos são de preenchimento obrigatório.')
		return;
	}
}

function goTo(dest) {
	switch(dest) {
		case 'home':
			//strLocation = 'http://' + home + 'index.php'
			strLocation = 'index.php'
		break;
	}
	if (strLocation) window.open(strLocation, '_self', "fullscreen=yes, menubar=no, toolbar=no", false)
	act = dest	
}
function goTo(dest) {
	switch(dest) {
		case 'home':
			//strLocation = 'http://' + home + 'index.php'
			strLocation = 'index.php'
		break;
	}
	if (strLocation) window.open(strLocation, '_self', "fullscreen=yes, menubar=no, toolbar=no", false)
	act = dest	
}
/*--------------------------------------------------------------------------------------------------------------------
          VALIDAÇÃO DOS FORMULARIOS DE REGISTO  E RECUPERAÇÃO PASSWD
-----------------------------------------------------------------------------------------------------------------------*/
function formX_Validator(theForm)

{
	if (act != '') return (false);
//------------------------------------------------------------------- NR CONTRIBUINTE
  if (theForm.nif.value == "")
  {
    alert("Por favor introduza um valor no campo \"N.\260 de Contribuinte\" .");
    theForm.nif.focus();
    return (false);
  }

  if (theForm.nif.value.length < 9)
  {
    alert("Por favor introduza 9 d\355gitos no campo  \"N.\260 de Contribuinte\" .");
    theForm.nif.focus();
    return (false);
  }

  if (theForm.nif.value.length > 9)
  {
    alert("Por favor introduza 9 d\355gitos no campo  \"N.\260 de Contribuinte\" .");
    theForm.nif.focus();
    return (false);
  }

  var result = soNumeros(theForm.nif.value);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"Contribuinte\".");
    theForm.nif.focus();
    return (false);
  }

  //--------------------------------------------------------------------  NOME
  if (theForm.nome.value == "")
  {
    alert("Por favor introduza um valor no campo \"Nome\".");
    theForm.nome.focus();
    return (false);
  }

  if (theForm.nome.value.length > 50)
  {
    alert("Por favor introduza no maximo 50 caracteres no campo \"Nome\"");
    theForm.nome.focus();
    return (false);
  }

  
//-------------------------------------------------------------------------- EMAIL
  if (theForm.email.value != ""){

        parte1 = theForm.email.value.indexOf("@");
        parte2 = theForm.email.value.lastIndexOf(".");
        parte3 = theForm.email.value.length;
        if (!(parte1 >= 3 && parte2 >= 6 && parte3 >= 9)) {
    	 	alert("Por favor introduza apenas letras, n\372meros, \"@\" e \".\" no campo \"Email\".");
            theForm.email.focus();
            return false;
        }
	}

  //-------------------------------------------------------------------- TELEFONE
  if (theForm.telefone.value == "")
  {
    alert("Por favor introduza um valor no campo \"Telefone\".");
    theForm.telefone.focus();
    return (false);
  }

  if (theForm.telefone.value.length < 9)
  {
    alert("Por favor introduza 9 digitos no campo \"Telefone\".");
    theForm.telefone.focus();
    return (false);
  }

  if (theForm.telefone.value.length > 9)
  {
    alert("Por favor introduza 9 digitos no campo \"Telefone\".");
    theForm.telefone.focus();
    return (false);
  }

  var result = soNumeros(theForm.telefone.value);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"Telefone\".");
    theForm.telefone.focus();
    return (false);
  }
  
  //-------------------------------------------------------------------- CODIGO POSTAL
  if (theForm.cpostal.value == "" || theForm.cpostal.value == "0000")
  {
    alert("Por favor introduza um valor no campo \"Código Postal\" .");
    theForm.cpostal.focus();
    return (false);
  }

  var cpost1 = pad(theForm.cpostal.value, 4);
  result = soNumeros(cpost1);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"Código Postal\".");
    theForm.cpostal.focus();
    return (false);
  }
  theForm.cpostal.value = cpost1;
  
   if (theForm.cpostal2.value == "" || theForm.cpostal2.value == "000")
  {
    alert("Por favor introduza um valor no campo \"Código Postal\" .");
    theForm.cpostal2.focus();
    return (false);
  }

  var cpost2 = pad(theForm.cpostal2.value, 3);
  result = soNumeros(cpost2);
  if (!result)
  {
    alert("Por favor introduza apenas n\372meros no campo \"Código Postal\".");
    theForm.cpostal2.focus();
    return (false);
  }
  theForm.cpostal2.value = cpost2;
  
    //-------------------------------------------------------------------- CAE
  if (theForm.activ.value == "")
  {
    alert("Por favor introduza um valor no campo \"Atividade\".");
    theForm.telefone.focus();
    return (false);
  }

	if (theForm.activ.value.length < 5)
	{
	alert("Por favor introduza 5 digitos no campo \"Atividade\".");
	theForm.activ.focus();
	return (false);
	}

	if (theForm.activ.value.length > 5)
	{
	alert("Por favor introduza 5 digitos no campo \"Atividade\".");
	theForm.activ.focus();
	return (false);
	}

	var result = soNumeros(theForm.activ.value);
	if (!result)
	{
	alert("Por favor introduza apenas n\372meros no campo \"Atividade\".");
	theForm.activ.focus();
	return (false);
	}
 
  return (true);
}
function changeMenu(mnu) {
	var mnus = new Array('A1', 'A2', 'A3', 'A4', 'A5', 'A6');
	for (i=0; i < mnus.length; i++) {
		if(mnu != mnus[i]) document.getElementById(mnus[i]).style.display = 'none';
	}
	var style1 = document.getElementById(mnu).style;
	if(style1.display == 'none' ) style1.display = 'block';
	else style1.display = 'none';
}
