﻿// JScript File


/* esconder o link no status bar do browser - p/ guardar estatistica */
function ss()
{
    window.status='';
    return true;
}

function showids(ids){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		showid(ids[i]);
	}		  
}
function hideids(ids){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hideid(ids[i]);
	}		  
}
function hideid(id) {
	//safe function to hide an element with a specified 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 showid(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}

function abreJanela(pPag,posTop,posLeft,alt,larg)	
{
	//winmap = window.open( pPag, 'winmap', 'directories=0; status=0; menubar=0;toolbar=0; resize=1; scrollbars=1; height='+alt+',width='+larg+',top='+posTop+',left='+posLeft+'');
    winmap=window.open(pPag,'winmap','toolbar=no,location=no,directories=no,status=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+larg+',height='+alt+',top='+posTop+',left='+posLeft+'');

	if (parseInt(navigator.appVersion) >= 4)
		{ winmap.window.focus(); }
}

// Obter lingua
function getLang()
{
	// Lingua
	if (window.location.href.indexOf("lang=en") != -1)
		return "en"; 
	else
		return "pt";
}

function limpaCamposPesqAvancada()
{
	var queryStr="?tipoPesq=a&lang=" + getLang();
    go(window.location.href,queryStr);		
}

function jumpPesqSimples()
{
    var txtP=document.getElementById("pesquisaAvancada1_txtPesq").value;
	var queryStr="?tipoPesq=s" + "&lang=" + getLang();
    go(window.location.href,queryStr);			
}

function jumpPesqAvancada()
{
	var txtP=document.getElementById("pesquisaSimples1_txtPesq").value;
    var queryStr="?tipoPesq=a" + "&lang=" + getLang();
	go(window.location.href,queryStr);
}

function go(href,queryStr)
{
	var i=href.indexOf('?');
	if (i == -1)
		window.location.href = href + queryStr;			
	else
		window.location.href = href.substring(0,i) + queryStr;
}

function validaParamsS(txtP,lang)
{
	// Sem params
	if (txtP=="")
	{
		if (lang=="pt")
			alert("Por favor refine a sua pesquisa !");
		else
			alert("Please refine your search !");
		return false;
	}
	
	// texto de pesquisa com menos de 2 caracteres
	if (txtP!="" && txtP.length < 2)
	{
		if (lang=="pt")
			alert("O texto a pesquisar não deverá ter menos de 2 caracteres !");
		else
			alert("The text to be searched should not have less then 2 caracteres !");
		return false;
	}
	
	return true;
}

	
function validaParamsA(fam,tit,loc,prov,txtP,tipoMeio,txtM,lang)
{
	// Sem params
	if ((fam=="0" && tit=="0" && prov=="0" && loc=="0" && txtP=="" && tipoMeio=="0" && txtM=="") || (tipoMeio!="0" && txtM==""))
    {
		if (lang=="pt")
			alert("Por favor refine a sua pesquisa !");
		else
			alert("Please refine your search !");
        document.getElementById("pesquisaAvancada1_txtPesq").focus();
		return false;
	}
	
	// texto de pesquisa com menos de 2 caracteres
	if (txtP!="" && txtP.length < 2)
	{
		if (lang=="pt")
			alert("O texto a pesquisar não deverá ter menos de 2 caracteres !");
		else
			alert("The text to be searched should not have less then 2 caracteres !");
        document.getElementById("pesquisaAvancada1_txtPesq").focus();
		return false;
	}
	
	// meio preenchido sem tipo seleccionado
	if (txtM!="" && tipoMeio=="0")
	{
		if (lang=="pt")
			alert("Por favor seleccione o tipo de contacto !");
		else
			alert("Please select the contact type !");
		document.getElementById("pesquisaAvancada1_ddlMeios").focus();
		return false;
	}
	
	// tipo e meio preenchidos - validar tipos de meio
	if (txtM!="" && tipoMeio!="0")
	{
		// telefone, telefax, moveis, telex
		if (tipoMeio=="1" || tipoMeio=="2" || tipoMeio=="3" || tipoMeio=="4")
		{
			if (!validaNum(txtM))
			{
				if (lang=="pt")
					alert("Apenas números !");
				else
					alert("Only numbers !");
				document.getElementById("pesquisaAvancada1_txtMeio").focus()
				return false;
			}
		}
		
		// email
		if (tipoMeio=="5")
		{
			/*
			if (!validaEmail(txtM))
			{
				if (lang=="pt")
					alert("Email Inválido !");
				else
					alert("Invalid Email !");
				document.getElementById("pesquisaAvancada1_txtMeio").focus()
				return false;
			}
			*/
		}
		// url
		if (tipoMeio=="6")
		{
			/*
			if (!validaURL(txtM))
			{
				if (lang=="pt")
					alert("URL Inválido !");
				else
					alert("Invalid URL !");
				document.getElementById("pesquisaAvancada1_txtMeio").focus()
				return false;
			}
			*/
		}					
	}
	
	return true;
}

function pesquisa()
{   

	var fam,tit,prov,loc,txtP,tipoMeio,txtM,queryStr,lang;
	lang=getLang();

	if (document.getElementById("pesquisaAvancada1_ddlFamilias") == null )  
	{
		// Pesquisa Simples
		txtP=document.getElementById("pesquisaSimples1_txtPesq");
		
		txtP.value = txtP.value.replace("<"," ")
		txtP.value = txtP.value.replace(">"," ")
        		
    if (validaParamsS(txtP.value,lang))
		{
		    queryStr="?txtPesq=" + txtP.value + "&tipoPesq=s" + "&procura=s" + "&lang=" + lang;
        go("resultados_pesquisa.aspx",queryStr);
		}
        else txtP.focus();	 
	}		
	else  
	{
		// Pesquisa Avançada	
		fam=document.getElementById("pesquisaAvancada1_ddlFamilias").value;
		tit=document.getElementById("pesquisaAvancada1_ddlTitulos").value;
		prov=document.getElementById("pesquisaAvancada1_ddlProvincias").value;
		loc=document.getElementById("pesquisaAvancada1_ddlLocalidades").value;
		txtP=document.getElementById("pesquisaAvancada1_txtPesq").value;
		tipoMeio=document.getElementById("pesquisaAvancada1_ddlMeios").value;
		txtM=removeEspacos(document.getElementById("pesquisaAvancada1_txtMeio").value);
	
		if (validaParamsA(fam,tit,loc,prov,txtP,tipoMeio,txtM,lang))
		{
            queryStr="?tipoPesq=a" + "&procura=s" + "&lang=" + lang;
            if (fam!="0") queryStr += "&fam=" + fam;
            if (tit!="0") queryStr += "&tit=" + tit;
            if (prov!="0") queryStr += "&prov=" + prov;
            if (loc!="0") queryStr += "&loc=" + loc;
            if (txtP!="") queryStr += "&txtPesq=" + txtP;
            if (tipoMeio!="0") queryStr += "&tipoMeio=" + tipoMeio + "&txtMeio=" + txtM;          
			go("resultados_pesquisa.aspx",queryStr);
		}
		
	}
}


//Validar email
function validaEmail(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1)
		return false

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		return false
	
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		return false

		if (str.indexOf(at,(lat+1))!=-1)
		return false
	
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
		return false

		if (str.indexOf(dot,(lat+2))==-1)
		return false
	
		if (str.indexOf(" ")!=-1)
		return false
		
		return true					
}

function validaNum(str)
{
	var v = "0123456789";
	for (var i=0; i < str.length; i++) 
		if (v.indexOf(str.charAt(i),0) == -1)
			return false
	return true
}

function removeEspacos(str)
{
	var result="";
	for (var i=0; i < str.length; i++)
	{ 
		x=str.charAt(i);
		if (x!=" ")
			result+=x;
	}
	return result
}


/* Slidding Menus */

var intMenuTop=0;
var heightTopBanner=150;
var minHeightMenu=400;
var scrollTop=0;
var flagDivLeft1=true;
var flagDivLeft2=true;
var flagDivRight1=true;
var flagDivRight2=true;


function crtlSlide(divName,flagNo,slide)
{
    if ((divName=='divLeft') && (flagNo==1) && (slide==false))
        flagDivLeft1=false;
    if ((divName=='divLeft') && (flagNo==1) && (slide==true))
        flagDivLeft1=true;

    if ((divName=='divLeft') && (flagNo==2) && (slide==false))
        flagDivLeft2=false;
    if ((divName=='divLeft') && (flagNo==2) && (slide==true))
        flagDivLeft2=true;

    if ((divName=='divRight') && (flagNo==1)&& (slide==false))
        flagDivRight1=false;
    if ((divName=='divRight') && (flagNo==1)&& (slide==true))
        flagDivRight1=true;
    
    if ((divName=='divRight') && (flagNo==2)&& (slide==false))
        flagDivRight2=false;
    if ((divName=='divRight') && (flagNo==2)&& (slide==true))
        flagDivRight2=true;    
}

function SlideMenuDiv(divName) // nova versão
{   
    var scrollX, scrollY,menu,availHeightForMenu,availHeight;   
    menu=document.getElementById(divName);
    availHeight=windowSize()[1];

    availHeightForMenu=availHeight-heightTopBanner;
    if ( availHeightForMenu>minHeightMenu )
    {
        scrollY = getScrollXY()[1];
     			
	    if (scrollY<=heightTopBanner)
        {
		    scrollTop = 0;
	    }
	    else
        {
		    scrollTop = scrollY - heightTopBanner;
	    }

	    if (scrollTop != intMenuTop) 
        {
		    if (intMenuTop>scrollTop)
			    intMenuTop = intMenuTop - parseInt(Math.abs((scrollTop - intMenuTop)/6)) - 1; 
		    else
			    intMenuTop = intMenuTop + parseInt(Math.abs((scrollTop - intMenuTop)/6)) + 1;
    	      
            if (menu.id == 'divLeft')
            {
                if((flagDivLeft1==true) && (flagDivLeft2==true))
                {
                    menu.style.top = intMenuTop;
                }
                else menu.style.top=0;
            }
            
            if (menu.id == 'divRight')
            {
                if((flagDivRight1==true) && (flagDivRight2==true))
                {
                    menu.style.top = intMenuTop;
                }
                else menu.style.top=0;
            }
	    }
        
    }
    else menu.style.top=0;

    setTimeout("SlideMenuDiv('" + menu.id + "')",50);
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function windowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [ myWidth, myHeight ]; 
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
}

function MyScroll(fName)
{   
	var x=document.forms[fName].scrollLeft.value;
	var y=document.forms[fName].scrollTop.value;
  window.scrollTo(x, y);		
}
	
function GetCoords(fName)
{
	var scrollX, scrollY;
	if (document.all)
	{
		if (!document.documentElement.scrollLeft)
			scrollX = document.body.scrollLeft;
		else
			scrollX = document.documentElement.scrollLeft;
		if (!document.documentElement.scrollTop)
			scrollY = document.body.scrollTop;
		else
			scrollY = document.documentElement.scrollTop;
	}
	else
	{
		scrollX = window.pageXOffset;
		scrollY = window.pageYOffset;
	}
		
	document.forms[fName].scrollLeft.value = scrollX;
	document.forms[fName].scrollTop.value = scrollY;
}

function MyLoad(fName)
{
  if (fName!="")
		MyScroll(fName);
	SlideMenuDiv('divLeft');
  SlideMenuDiv('divRight');
}

function pesqOnEnter(event) 
{
  if (window.event && window.event.keyCode == 13) //IE
    	pesquisa();
  else if (event && event.which == 13)
 		pesquisa();
 	else
		return true;
	return false;
}
