function showMenu( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  vis.display = 'block';
}
function hideMenu( whichLayer )
{
  var elem, vis;
  if( document.getElementById ) // this is the way the standards work
    elem = document.getElementById( whichLayer );
  else if( document.all ) // this is the way old msie versions work
      elem = document.all[whichLayer];
  else if( document.layers ) // this is the way nn4 works
    elem = document.layers[whichLayer];
  vis = elem.style;
  // if the style.display value is blank we try to figure it out here
  vis.display = 'none';
}
function checkformRegisto(){
	if(!checkText('registo', 'email', 'Email')){
		return false;
	}
	if(!validateemail('registo', 'email')){
		return false;
	}
	if(!checkText('registo', 'idade', 'Idade')){
		return false;
	}
	if(!validateInteiro('registo', 'idade', 'Idade')){
		return false;
	}
	if(!checkText('registo', 'nome', 'Nome')){
		return false;
	}
	if(!validate_file_format('avatar','jpg','Avatar')){
		return false;
	}
	if(!checkText('registo', 'nickname', 'nickname')){
		return false;
	}
	if(!validate_file_format('imagemblog','jpg','Imagem do Blog')){
		return false;
	}
	if(!checkText('registo', 'password', 'password')){
		return false;
	}
	if(!checkText('registo', 'password_2', 'Repete password')){
		return false;
	}
	if(document.forms['registo'].elements['password'].value!=document.forms['registo'].elements['password_2'].value){
		alert("As passwords não combinam.");
		return false;
	}
	if(!checkText('registo', 'blog_titulo', 'Título do Blog')){
		return false;
	}
	if(!checkText('registo', 'blog_categoria', 'Tema do Blog')){
		return false;
	}
	if(!checkText('registo', 'blog_url', 'Url do Blog')){
		return false;
	}
	if(!checkText('registo', 'blog_descricao', 'Descrição do Blog')){
		return false;
	}
	if((document.forms['registo'].elements['notebook'][0].checked)&&(!checkText('registo', 'notebook2', 'Actualmente tem algum notebook?'))){
		return false;
	}
	if(!document.forms['registo'].elements['concorda'].checked){
		alert("É necessário concordar com os termos e condições da Asus para se poder inscrever.");
		document.forms['registo'].elements['concorda'].focus(); 
		return false;
	}
	return true;
}

function checkformAltRegisto(){
	if(!checkText('altregisto', 'NickName', 'Blogger')){
		return false;
	}
	if(!checkText('altregisto', 'FullName', 'Nome')){
		return false;
	}
	if(!checkText('altregisto', 'Age', 'Idade')){
		return false;
	}
	if(!validateInteiro('altregisto', 'Age', 'Idade')){
		return false;
	}
	if(!checkText('altregisto', 'titulo', 'Título do Blog')){
		return false;
	}
	if(!checkText('altregisto', 'descricao', 'Descrição do Blog')){
		return false;
	}
	if(!validate_file_format2('avatar','jpg','Avatar')){
		return false;
	}
	if(!validate_file_format2('imagemblog','jpg','Imagem do Blog')){
		return false;
	}
	if(!checkText('altregisto', 'url', 'Url do Blog')){
		return false;
	}
	return true;
}

function checkformVotar(){
	if(!checkText('votar', 'nome', 'Nome')){
		return false;
	}
	if(!checkText('votar', 'email', 'Email')){
		return false;
	}
	if(!validateemail('votar', 'email')){
		return false;
	}
	if(!checkText('votar', 'telefone', 'Telefone')){
		return false;
	}
	if(!checkText('votar', 'testemunho', 'Testemunho')){
		return false;
	}
	if(!document.forms['votar'].elements['concorda'].checked){
		alert("É necessário concordar com os termos e condições da Asus para se poder inscrever.");
		document.forms['votar'].elements['concorda'].focus(); 
		return false;
	}
	return true;
}

function checkText(the_form, campo, texto){
	var mychecked; 
	if (document.forms[the_form].elements[campo].value == '') { 
		alert('O campo "'+texto+'" é de preenchimento obrigatório'); 
		document.forms[the_form].elements[campo].focus(); 
		return false; 
	} 
	return true;
}
function validate_file_format(field_name, allowed_ext, texto){
    obj1=document.forms['registo'];
    var temp_field= 'obj1.'+field_name+'.value';
    field_value=eval(temp_field);
    if(field_value!=""){
        var file_ext= (field_value.substring((field_value.lastIndexOf('.')+1)).toLowerCase());
        ext=allowed_ext.split(',');
        var allow=0;
        for ( var i=0; i < ext.length; i++) {
                if(ext[i]==file_ext){
                    allow=1;
                }
        }
        if(!allow){
            alert('Formato inválido. Só são permitidos ficheiros ".jpg".');        
						document.forms['registo'].elements[field_name].focus(); 
            return false;
        }
    }else{
			alert('O campo "'+texto+'" é de preenchimento obrigatório'); 
	 		document.forms['registo'].elements[field_name].focus(); 
     return false;
    }
    return true;
}
function validate_file_format2(field_name, allowed_ext, texto){
    obj1=document.forms['altregisto'];
    var temp_field= 'obj1.'+field_name+'.value';
    field_value=eval(temp_field);
    if(field_value!=""){
        var file_ext= (field_value.substring((field_value.lastIndexOf('.')+1)).toLowerCase());
        ext=allowed_ext.split(',');
        var allow=0;
        for ( var i=0; i < ext.length; i++) {
                if(ext[i]==file_ext){
                    allow=1;
                }
        }
        if(!allow){
            alert('Formato inválido. Só são permitidos ficheiros ".jpg".');        
						document.forms['registo'].elements[field_name].focus(); 
            return false;
        }
    }
    return true;
}
function validateemail(form_id,email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) {
      alert('Endereço de email inválido');
	 		document.forms[form_id].elements[email].focus(); 
      return false;
   }
   return true;
}
function validateInteiro(the_form, campo, texto){
  var o = document.forms[the_form].elements[campo];
  if(!isInteger(o.value)){
    alert('O campo "'+texto+'" tem de ter um número inteiro');
 		document.forms[the_form].elements[campo].focus(); 
    return false;
	}
	return true;
}
function isInteger (s)
{
  var i;

  if (isEmpty(s))
  if (isInteger.arguments.length == 1) return 0;
  else return (isInteger.arguments[1] == true);

  for (i = 0; i < s.length; i++)
  {
     var c = s.charAt(i);

     if (!isDigit(c)) return false;
  }

  return true;
}

function isEmpty(s)
{
  return ((s == null) || (s.length == 0))
}

function isDigit (c)
{
  return ((c >= "0") && (c <= "9"))
}
function PopUrl(url, width, height)
{
     params  = 'width='+width;
     params += ', height='+height;
     params += ', fullscreen=no';
     params += ', directories=no';
     params += ', location=no';
     params += ', menubar=no';
     params += ', resizable=no';
     params += ', scrollbars=yes';
     params += ', status=no';
     params += ', toolbar=no';
     newwin=window.open(url,'eeepc', params);
     return false;
}
