//strings
var txt_1="Insira nome !";
var txt_2="Insira username !";
var txt_3="Insira password !";

var txt_4="Introduza a password antiga !";
var txt_5="Passwords não coincidem !";
var txt_6="Introduza password !";

var txt_7="Preencha todos os campos!";
var txt_8="E-mail inválido, tente outra vez!";
var txt_9="Tem a certeza que deseja GUARDAR ?";

var txt_10="Tem a certeza que deseja APAGAR ?";
var txt_11="ATENÇÃO: Esta operação é IRREVERSSÍVEL. Deseja CONTINUAR ?";
var txt_12="Nome demasiado pequeno";

var txt_13="Comentários demasiado pequenos não são permitidos";
//fim Strings


///////////////////RELOAD AUTOMATICO
// Configure refresh interval (in seconds)
var refreshinterval=60*10

// Shall the coundown be displayed inside your status bar? Say "yes" or "no" below:
var displaycountdown="no"

// Do not edit the code below
var starttime
var nowtime
var reloadseconds=0
var secondssinceloaded=0

function starttime() {
	starttime=new Date()
	starttime=starttime.getTime()
    countdown()
}

function Set_Cookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	var expires_date = new Date( today.getTime() + (expires) );

	document.cookie = name + "=" + escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) &&
	( name != document.cookie.substring( 0, name.length ) ) )
	{
	return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain ) {
	if ( Get_Cookie( name ) ) document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( domain ) ? ";domain=" + domain : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// this deletes the cookie when called
function Extend_Expiration_Cookie( name, path, domain ,expires) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct
	expires time, the current script below will set
	it for x number of days, to make it for hours,
	delete * 24, for minutes, delete * 60 * 24
	*/
	var expires_date = new Date( today.getTime() + (expires) );

	if ( Get_Cookie( name ) )
	document.cookie = name + "=" + ( ( path ) ? ";path=" + path : "") +	( ( domain ) ? ";domain=" + domain : "" ) +	";expires=" + expires_date.toGMTString();
}

function countdown() {
	nowtime= new Date()
	nowtime=nowtime.getTime()
	secondssinceloaded=(nowtime-starttime)/1000
	reloadseconds=Math.round(refreshinterval-secondssinceloaded)
	if (refreshinterval>=secondssinceloaded) {
        var timer=setTimeout("countdown()",1000)
		if (displaycountdown=="yes") {
			window.status="Page refreshing in "+reloadseconds+ " seconds"
		}
    }
    else {
        clearTimeout(timer)
		window.location.reload(true)
    }
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
		if (oldonload) {
			oldonload();
		}
		func();
		}
	}
}
//addLoadEvent(function(){starttime();});

function setStyleByClass(t,c,p,v){
	var elements;
	if(t == '*') {
		// '*' not supported by IE/Win 5.5 and below
		elements = (ie) ? document.all : document.getElementsByTagName('*');
	} else {
		elements = document.getElementsByTagName(t);
	}
	for(var i = 0; i < elements.length; i++){
		var node = elements.item(i);
		for(var j = 0; j < node.attributes.length; j++) {
			if(node.attributes.item(j).nodeName == 'class') {
				if(node.attributes.item(j).nodeValue == c) {
					eval('node.style.' + p + " = '" +v + "'");
				}
			}
		}
	}
}

function getQueryVariable(query,variable) {
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  }
  return false;
}

function LoadPostVars(form){
	var postvars='';

	var inputs=form.getElementsByTagName('input');
	var inputsLen=inputs.length;

	var textareas=form.getElementsByTagName('textarea');
	var textareasLen=textareas.length;

	var selects=form.getElementsByTagName('select');
	var selectsLen=selects.length;

	//alert('INPUTS= '+inputsLen+' TEXTAREAS= '+textareasLen+' SELECTS= '+selectsLen);

	for ( i=0;i<inputsLen;i++ ) {
		if ( inputs[i].getAttribute('type') == 'text' ) {
			postvars=postvars+inputs[i].getAttribute('name')+'='+inputs[i].value+'&';
		}
		if ( inputs[i].getAttribute('type') == 'checkbox' ) {
			postvars=postvars+inputs[i].getAttribute('name')+'='+inputs[i].value+'&';
		}
		if ( inputs[i].getAttribute('type') == 'password' ) {
			postvars=postvars+inputs[i].getAttribute('name')+'='+inputs[i].value+'&';
		}
		if ( inputs[i].getAttribute('type') == 'radio' ) {
			postvars=postvars+inputs[i].getAttribute('name')+'='+inputs[i].value+'&';
		}
		if ( inputs[i].getAttribute('type') == 'hidden' ) {
			postvars=postvars+inputs[i].getAttribute('name')+'='+inputs[i].value+'&';
		}
	}
	for ( j=0;j<textareasLen;j++ ) {
		postvars=postvars+textareas[j].getAttribute('name')+'='+textareas[j].value+'&';
	}
	for ( t=0;t<selectsLen;t++ ) {
		postvars=postvars+selects[t].getAttribute('name')+'='+selects[t].value+'&';
	}

	//alert('POSTVARS_FINAL=='+postvars);

	return postvars;
}

function popitup(url,tipo,h,w)
{
	newwindow=window.open(url,tipo,'height='+h+',width='+w+'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1');
	if (window.focus) {newwindow.focus()}
	return false;
}

function refresh()
{
    window.location.reload(false);
}

function replaceIt(sString, sReplaceThis, sWithThis){
	if (sReplaceThis != "" && sReplaceThis != sWithThis) {
	var counter = 0;
	var start = 0;
	var before = "";
	var after = "";
	while (counter<sString.length) {
	start = sString.indexOf(sReplaceThis, counter);
	if (start == -1) {
	break;
	} else {
	before = sString.substr(0, start);
	after = sString.substr(start + sReplaceThis.length, sString.length);
	sString = before + sWithThis + after;
	counter = before.length + sWithThis.length;
	}
	}
	}
	return sString;
}

function VerificaCompeLink(){
if(document.getElementById('tipo0'))
	if(document.getElementById('tipo0').checked){
		document.getElementById('comp01').style.display='none';
		document.getElementById('link01').style.display='';
		document.getElementById('link02').style.display='';
	}else{
		document.getElementById('comp01').style.display='';
		document.getElementById('link01').style.display='none';
		document.getElementById('link02').style.display='none';
	}
}
/**
 *
 * @access public
 * @return void
 **/
function hideById(id){
	hideDivById(id);
}
function hideDivById(id) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}

function isCheckBoxCheckedById(id){
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).checked)
			return true;
		else
			return false;
	}
	else {
		if (document.layers) { // Netscape 4
			if(document.id.checked)
				return true;
			else
				return false;
		}
		else { // IE 4
			if(document.all.id.checked)
				return true;
			else
				return false;
		}
	}
}
function isVisibleById(id){
	if (document.getElementById) { // DOM3 = IE5, NS6
		if(document.getElementById(id).style.display == 'none')
			return false;
		else
			return true;
	}
	else {
		if (document.layers) { // Netscape 4
			if(document.id.display == 'none')
				return false;
			else
				return true;
		}
		else { // IE 4
			if(document.all.id.style.display == 'none')
				return false;
			else
				return true;
		}
	}
}

function showById(id){
	showDivById(id);
}
function showDivById(id) {

	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = '';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = '';
		}
		else { // IE 4
			document.all.id.style.display = '';
		}
	}
}

function setFocusById(id){
    if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).focus();
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.focus;
		}
		else { // IE 4
			document.all.id.focus;
		}
	}
}

function checkemail(text){
	var str=text;
	var testresults;
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(str))
		testresults=true;
	else{
		alert(txt_8);
		testresults=false;
	}
	return (testresults);
}
function ValidaNewGalForm(form){
	if(!form)
		var form = document.editgalform;

	if(form.nome.value=='')return false;
	return true;
}

function ValidaFotoForm(form){
	if(!form)
		var form = document.fotoform;
	if(form.nome && form.nome.value==''){
		alert('Nome de foto Inválido');
		return false;
	}
	if(form.imagem && form.imagem.value==''){
		alert('Imagem Inválida, verifique o caminho.');
		return false;
	}
	return true;
}

function ValidarGuestbookForm(){
	var form=document.guestbookform;
	if(form.nome.value.length<=2){
		alert(txt_12);
		return false;
	}
	if(form.comentario.value.length<=10){
		alert(txt_13);
		return false;
	}
	return checkemail(form.email.value);
}

function Confirma_Guardar(){
	var submitOK=confirm(txt_9);
	if(submitOK)
		return true;
	else
		return false;
}
function Confirma_Apagar(){

	if(!confirm(txt_10))
		return false;

	var submitOK=confirm(txt_11);
	if(submitOK)
		return true;
	else
		return false;
}
// /////////////////////////////////////////////
// ///////////////////////////////////////////////
// Gerir Utilizadores
function BotaoAlterar(){
	if (document.alterarpass.pass0.value == ''){
		alert(txt_4);
		return false;
	}
	if (document.alterarpass.pass1.value!='' && document.alterarpass.pass2.value!=''){
		if(document.alterarpass.pass1.value == document.alterarpass.pass2.value){
			return true;
		}else{
			alert(txt_5);
			return false;
			}
	}else{
		alert(txt_6);
		return false;
		}
}
function validar_editar_utilizador(){
	if(!checkemail(document.formuseredit.email.value))
		return false;
	if(document.formuseredit.nome.value!='' && document.formuseredit.username.value!='')
		return true;
	else{
		alert(txt_7);
		return false;
		}
}
function validar_novo_utilizador(){
	if(document.formuseredit.nome.value==''){
		alert(txt_1);
		return false;
	}
	if(!checkemail(document.formuseredit.email.value))
		return false;
	if(document.formuseredit.username.value==''){
		alert(txt_2);
		return false;
	}
	if(document.formuseredit.pass1.value=='' || document.formuseredit.pass2.value==''){
		alert(txt_3);
		return false;
	}
	if(document.formuseredit.pass1.value != document.formuseredit.pass2.value){
		alert(txt_5);
		return false;
	}
	return true;
}
// /////////////////////////////////////////////
// ///////////////////////////////////////////////
// Gerir Utilizadores


var keyvalue="";
if( document.captureEvents ) {
    if( Event.KEYUP ) {
        document.captureEvents( Event.KEYUP );
    }
}
document.onkeyup = alertkey;

function cleanKeyValue(){
    keyvalue='';
}
function alertkey(e) {
	if(keyvalue.length>11)
		return;
    if( !e ) {
        if( window.event ) {
            e = window.event;
        } else {
            return;
        }
    }
    if( typeof( e.which ) == 'number' ) {
        e = e.which;
    } else if( typeof( e.keyCode ) == 'number'  ) {
        e = e.keyCode;
    } else if( typeof( e.charCode ) == 'number'  ) {
        e = e.charCode;
    } else {
        return;
    }
		if(e!=116)
			keyvalue=keyvalue+ e ;
		if(keyvalue==716983847982 && !useradmin){
			//parent.location='./pagina/admin/';
            window.scroll(0,0);
            showDivById('fundo-transparente-login-admin');
          	//document.body.style.height = '100px';
            setFocusById('login');

		}
}