//paramIN: s -> string
//paramOut: boolean
//descricao: Valida se a string é um numero inteiro
function validarStringNumeroInteiro(s){   
	var i;
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

//paramIN: s -> string, N -> int
//paramOut: boolean
//descricao: Valida se a string é um numero de N algarismos
function validarStringNAlgarismos(s, N){
	if(s.length < N)
		return false;
	else
		return validarStringNumeroInteiro(s);
}


//paramIN: endereco -> string
//paramOut: boolean
//descricao: Valida se a string é um endereço de email valido
function validarEnderecoEmail(endereco){
	var emailRegxp = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/;
	if (emailRegxp.test(endereco) != true) return false;
	return true;
}


//paramIN: altura em pixeis para o Ifram
//paramOUT: void
//summary: faz o resize da iframe onde o documento actual está carregado
//			se o valor newHeight passar em branco o valor é calculado conforme a altura do body
function resizeIframe(newHeight) {

	// Must launched on the body onload event handler for IE
	// Use document.documentElement if you are in Compat mode

	i = parent.document.getElementById(window.name)
	
	if (newHeight == undefined){
		newHeight = document.body.scrollHeight
	}
	i.style.height = newHeight + "px";
	
	//alert('height = ' + i.style.height);
	
}

/**
* Abre uma janela simples centrada
*
* @param string $url
* @param int $largura
* @param int $altura
* @author jose.almeida@t-bsolutions.com
*/

function abrirJanelaCentrada(url, largura, altura){
    var larguraEcra = window.screen.width;
    var alturaEcra = window.screen.height;
    var posicaoX = (larguraEcra / 2) - (largura / 2);
    var posicaoY = (alturaEcra / 2) - (altura / 2);
	window.open(url,"","left="+posicaoX+",top="+posicaoY+",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width="+largura+", height="+altura);
}

/************************************************
DESCRIPTION: Validates that a string is not all
  blank (whitespace) characters.

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.
*************************************************/
function validateNotEmpty( strValue ) {

   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}



/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/
function trimAll( strValue ) {

 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

//função usada para o formulário de candidatura
function esconderPathCV()
{
	el = document.getElementById("pathCVescondido");

	if(el!=null){
		al = el.filters || el.style.MozOpacity || el.style.khtmlOpacity;
		if(al)
		{
			falso=document.getElementById('divCaixaFalso');
			falso.style.display = "block";
			falso=document.getElementById('divBotaoFalso');
			falso.style.display = "block";
			if (el.style.MozOpacity)
			{
				el.style.MozOpacity = 0;
			}
			else if (el.filters)
			{
				el.filters.alpha.opacity = 0;
			}
			else if (el.style.khtmlOpacity)
			{
				el.style.khtmlOpacity = 0;
			}
		}
		
		escondido=document.getElementById('divEscondido');
		el.style.position = "relative";
		el.style.zIndex = 2;
		escondido.style.position = "absolute";
		var posicaoTop=escondido.offsetTop;

		escondido.style.position = "static";
		
		falso=document.getElementById('divCaixaFalso');
		falso.style.position = "absolute";
		falso.style.zIndex = 3;
		falso.style.top=posicaoTop;



 		falso=document.getElementById('divBotaoFalso');
		falso.style.position = "absolute";
		falso.style.top=posicaoTop;
		falso.style.zIndex = 0;

		
	}
}

/**
* Selecciona um elemento de inserção de data
*
* Função chamada por um campo de inserção de data,
* que apresenta o formato da data (dd-mm-aaaa) 
*
* @param object $elemento
* @author jose.almeida@t-bsolutions.com
*/

function onFocusCampoData(elemento){
	if(!validarData(elemento.value)){
		elemento.value="dd-mm-aaaa";
		elemento.select();
	}
}

/**
* Valida um elemento de inserção de data
*
* Função chamada por um campo de inserção de data,
* que valida o formato da data (dd-mm-aaaa) 
*
* @param object $elemento
* @author jose.almeida@t-bsolutions.com
*/

function onBlurCampoData(elemento){
	if(!validarData(elemento.value)){
		elemento.value="aaaa-mm-dd";
	}
}


/**
* Valida se a string é uma data válida no formato aaaa-mm-dd
*
* @param string data
* @return boolean
* @author jose.almeida@t-bsolutions.com
*/

function validarData(data)
{	
	expRegular="^[0-9]{4}-[0-9]{2}-[0-9]{2}$";
	return data.match(expRegular);
}

/**
* Valida se a string é um email válido
*
* @param string email
* @return boolean
* @author T
*/
function validarEmail(email)
{
	email = email.toLowerCase();
	expRegular = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$";
	return email.match(expRegular);
}

/**
* Chamada ao HttpRequest
*
* @param string $url
* @author joao.brites@t-bsolutions.com
*/

var req;

function loadXMLDoc(url) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        req.onreadystatechange = processReqChange;
        req.open("GET", url, true);
        req.send(null);
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send();
        }
    }
}

/**
* tratamento de recepção da resposta
*
* 
* @author joao.brites@t-bsolutions.com
*/

function processReqChange() 
{
    // only if req shows "complete"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            // ...processing statements go here...
      response  = req.responseXML.documentElement;

      method    = response.getElementsByTagName('method')[0].firstChild.data;

      result    = response.getElementsByTagName('result')[0].firstChild.data;

      eval(method + '(\'\', result)');
        } else {
            alert("There was a problem retrieving the XML data:\n" + req.statusText);
        }
    }
}
