// JavaScript Document

function change_background(color_index){	//altera a cor de fundo
	
	var pallete_cores = new Array ("#5f82ac","#fc1871","#f96714","#c1e233","#908e8f","#ededed");

	var fundo = document.body;
	if (color_index >=0){
		fundo.style.backgroundColor = ""+ pallete_cores[color_index] +"";
	
		for(i=1; i<=3; i++){
			var fundo = document.getElementById("menu-top"+i);
			fundo.style.borderBottom ="4px solid "+  pallete_cores[color_index] +"";
			fundo.style.borderRight ="4px solid "+  pallete_cores[color_index] +"";
			
			var fundo1 = document.getElementById("menu-bottom"+i);
			fundo1.style.borderRight ="4px solid "+  pallete_cores[color_index] +"";
		}
	}
	return 1;
}

function change_site_width(){	//altera a largura do site consoante a resolução
	
	var doc = document.getElementById("base");

	if (screen.width >= 1024){
		doc.style.width = "1003px";
	}else{
		doc.style.width = "773px";
	}

	return 1;
}

function aceitaNums_telf(){
	//alert(event.keyCode)// pra saber kal a tecla ke se carrega
	if (event.keyCode != 43){ // tecla +
		if ((event.keyCode < 48) || (event.keyCode > 57)  )
			event.returnValue = false
	}
}

function validaNumber(obj, valor_min, valor_max){
	//check if an value is number and if is on the interval
   if (isNaN(obj.value)){
      alert ("Valores Não Numéricos!");
      return false;
   }

	if (valor_min != 0 && valor_max != 0)
	   // check age range
	   if (parseInt(obj.value) < valor_min || parseInt(obj.value) > valor_max)
	   {
		  alert ("Valores fora do limite estabelecido! min:"+valor_min+" max:"+valor_max);
		  return false;
	   }
	
	return true;
}

function getCalendarForMonth(month, year){
  	var dayOfWeekArray = new Array("D", "S", "T", "Q","Q", "S", "S")
  	var monthArray = new Array("Janeiro", "Fevereiro", 
                   "Março", "Abril", "Maio", "Junho", 
                   "Julho", "Agosto", "Setembro", 
                   "Outubro", "Novembro", "Dezembro")
  	var noDaysArray = new Array(31, 28, 31, 30, 31, 30, 
                    31, 31, 30, 31, 30, 31)
  	var firstOfMonth = new Date(month + "/1/" + year )
  	var day = - firstOfMonth.getDay()

	var currentTime = new Date()
	var today = currentTime.getDate()

	// determine number of days in month
	var noDays = noDaysArray[month-1]
	if (month == 2){
		if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0){
	  		noDays = 29
	  	}
	}

	//fabricate string for tables
	var strMonth = "<TABLE border=0 cellpadding=0 cellspacing=1 >"
	strMonth += "<TR><TD align='center'>"
	strMonth += "<FONT face='Arial' size=2>"  
	strMonth +=  monthArray[month-1] + " " + year 
	strMonth += "</FONT></TD><TR><TD>"
	strMonth += "<TABLE border=0 cellpadding=3 cellspacing=0>"
	strMonth += "</TR><TR bgcolor='#5f82ac' align='center'>"
  	
	for(var i = 0; i<=6; i++){
		strMonth += "<TD width=10><FONT face='arial' color='white' size=1>"
    	strMonth += dayOfWeekArray[i] + "</FONT></TD>"
	}
  
  	strMonth += "</TR><TR align='right'>"

  	//fabricate first week of month
  	for (i = 0; i <= 6; i++){
    	strMonth += "<TD width=10><FONT face='arial' size=1 color='#5f82ac'>"
    	day++
    	if(day > 0){
      		if (day == today)
				strMonth += "<span style='color:orange;'><b>"+day+"</b></span>"
			else
				strMonth += day
      	}else{
      		strMonth += " "
      	}
    	strMonth += "</FONT></TD>"
    }

  	//fabricate remaining weeks
  	for(var j=1; j < 6; j++){
    	strMonth += "</TR><TR align='right'>"
    	for (i = 0; i <= 6; i++){
      		strMonth += "<TD><FONT face='arial' size=1 color='#5f82ac'>"
      		day++
      		if(day <= noDays){
				if (day == today)
        			strMonth += "<span style='color:orange;'><b>"+day+"</b></span>"
				else
					strMonth += day
        	}else{
        		strMonth += " "
        	}
      		strMonth += "</FONT></TD>"
      	}
    	if (day >= noDays){
      		break;
      	}
    }
  	strMonth += "</TR></TABLE>"
  	strMonth += "</TD></TR></TABLE>"

  	return strMonth
  }

// JavaScript Document
function toggleVisibility(me){
	if (me.style.visibility=="hidden"){
		me.style.visibility="visible";
	}else{
		me.style.visibility="hidden";
	}
}

//Create a center popup
function createwindow(url, largura, altura,Scroll,nome){
	esq = (screen.availWidth - largura ) / 2 -30;
	cim  = (screen.availHeight - altura) / 2 - 10 ;	
	janela = window.open(url,nome,"toolbar=no,width="+ largura + ",height=" + altura + ",left=" + esq + ",top=" + cim + ",scrollbars="+ Scroll +",directories=no,status=no,resize=no,menubar=no")
} 

function createwindow_full(url){
	esq = screen.availWidth-5;
	cim  = screen.availHeight-20;		
	window.open(url,"displayWindow","toolbar=no,width="+ esq + ",height=" + cim + ",left=0, top=0, scrollbars=no,directories=no,status=no,resize=no,menubar=no");
}

function valid(){					

	if (document.forms.Contacto.nome.value.length < 3){
		alert('Por favor 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('Por favor introduza um e-mail válido!');
		return false;					
	}

	if (document.forms.Contacto.assunto.value.length < 5) {
		alert('Por favor introduza sua mensagem!');
		return false;
	}
}

function submit_newsletter(){

	aux=0;
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(f_subscrever.email_newsletter.value)){
		return true;            
	}

	if (aux==0){
		alert("Endereço de E-mail inválido!")
		f_subscrever.email_newsletter.focus();
    	return false;
	}

	return true;
}

//______________________________________________________________
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;
}

//___________________________________________________________________________

