//*****************************************************************************
// File:    /registo/_inc/JScript/functions.js
// Purpose: 
// Functions
// ----------------------------------------------------------------------------
// 
//*****************************************************************************
// Author   Date       Comments
// -------- ---------- --------------------------------------------------------
// ?        ?          Creation of the file.
// JGP/V    2005.02.17 Correction of a XML problem.
//*****************************************************************************

/********************************BEGIN - FUNÇÔES USADAS PARA CONVERSÂO MULTIBROWSER************************/

//função para sair
function fnSair()
{
	window.location.href = '/';
}

//função para reencaminhar para a página home.asp
function fnToRFX()
{
	window.location.href = '/RFx/home.asp';
}

//function to use XMLHTTP synchronous
function oHTTPAsync(sURL,sXML,oMSG,sMSGIN,oHANDLER){
// oHTTPAsync - JR/VRTL/2002

	if(oMSG){oMSG.innerHTML=sMSGIN;} 

	oHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	
	oHTTP.onreadystatechange = oHANDLER;
	
	oHTTP.open("POST", sURL, true);
	
	if(sXML){oHTTP.send(sXML);}else{oHTTP.send();}
	return;
}



//function to use XMLHTTP synchronous
//Parameters : sURL - url to open by POST or GET; sXML - xml send by oHTTP, rXML - xml of response
function oHTTPSync(sURL,sXML,rXML)
{
	// oHTTPSync - JR/VRTL/2002
	var s;
	var e;
	
	try
	{
		//6.0x
		oHTTP=new ActiveXObject("MSXML2.XMLHTTP");
	}
	catch(e)
	{
		//5.5x
		oHTTP=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	
	oHTTP.open("POST", sURL, false);	
	if(sXML){oHTTP.send(sXML);}else{oHTTP.send();}
	if(rXML){s=oHTTP.responseXML.xml;}else{s=oHTTP.responseText;}
	return(s);	
}

//function to use XMLHTTP asynchronous
function __oHTTPAsync()
{
	if(oHTTP.readyState == 4)
	{
		oHTTP.onreadystatechange=new function(){};
		var status = parseInt(oHTTP.status, 10);
		if((status < 200)||(status > 299)){alert("Problemas de comunicação com o servidor.");oHTTP=null;return false;}
		else if(oHTTP.getResponseHeader("Erro").length > 0){alert(oHTTP.getResponseHeader("MSG"));oHTTP=null;return false;}
		return true;
	}
	else
	{
		return false;
	}
}


/**********************BEGIN - FUNCTION MULTIBROWSER************************************/

//função para criar objectos xml e xmlhttp
function W3CNewObject(strObjectName)
{
    var obj;
 
    if (strObjectName == 'xmlhttp') 
    {                  
        if (typeof XMLHttpRequest == "undefined") XMLHttpRequest = function() 
        {
            try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch (e) { }
            try { return new ActiveXObject("Msxml2.XMLHTTP") } catch (e) { }
            try { return new ActiveXObject("Microsoft.XMLHTTP") } catch (e) { }
            throw new Error("This browser does not support XMLHttpRequest.");
        }
        else 
        {
            obj = new XMLHttpRequest();
        }              
    }
    else 
    {
        if (strObjectName == 'xml') 
        {
            try //Internet Explorer
            {
                obj = new ActiveXObject("MSXML2.DomDocument");
                obj.async = false;
            }
            catch (e) 
            {
                try //Internet Explorer
                {
                    obj = new ActiveXObject("Microsoft.XMLDOM");
                    obj.async = false;
                }
                catch (e) 
                {
                    try //Internet Explorer
                    {
                        obj = new ActiveXObject("Microsoft.DomDocument");
                        obj.async = false;
                    }
                    catch (e) 
                    {
                        try //Firefox, Mozilla, Opera, etc.
                        {
                            obj = document.implementation.createDocument("","",null);
                            obj.async = false;
                        }
                        catch (e) 
                        {
                            alert(e.message);
                        }
                    }
                }
            }
        }
    }
    
    return obj;
}

//função para efectuar load de ficheiros xml
function W3CXMLLoad(varobj, strXML)
{
	if(window.ActiveXObject)
	{//if internet explorer
		
		varobj.loadXML(strXML);
	}
	else
	{//if mozilla firefox and mac safari
	
		parser = new DOMParser();
		varobj = parser.parseFromString(strXML, "text/xml");
	}
	
	return varobj;
}

//função para listar os conteúdos
function W3CXMLtoString(objXML)
{
	if(window.navigator.userAgent.toLowerCase().indexOf('safari')!=-1)
	{//if mac safari
	
		var serializer = new XMLSerializer();
		var xml = serializer.serializeToString(objXML);
		return xml;
	}
	else
	{
		if(objXML.xml == null)
		{//if mozilla firefox
			
			var strObj = (new XMLSerializer()).serializeToString(objXML);
			return strObj;
		}
		else
		{//if internet explorer
		
			return objXML.xml;
		}
	}
}

/**********************END - FUNCTION MULTIBROWSER**************************/

/**********************BEGIN - FUNCTIONS TO WORK WITH ERRORS HANDLERS*******************/

//variable to control errors information
var errxml = false;


/******************************************************************************
'Function: errHandler()
'Purpose:  function to return xml element with information about errors
'Inputs:   oXML - xml element
'Returns:  true - if OK, false - if error found
'******************************************************************************
' Author   Date       Comments
' -------- ---------- ---------------------------------------------------------
' ?        ?          Creation of the function.
' JGP/V    2005.02.17 Validation of the documentElement.
******************************************************************************/
function errHandler(oXML)
{
	if(window.ActiveXObject)
	{//if internet explorer

		if(oXML.firstChild.text == '0' || oXML.documentElement.firstChild.text == '0')
		{
			oXML=null;
			return true;
		}
		else
		{
			errxml=oXML.xml;
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if (oXML.documentElement != null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					if(oXML.documentElement.childNodes(i).text !="t")
					{
						s = eval(oXML.documentElement.childNodes(i).attributes.item(0).text);
						s.innerHTML=oXML.documentElement.childNodes(i).text;
					}
					//se o atributo não for teste
					else
					{
						s = eval(oXML.documentElement.childNodes(i).attributes.item(0).text);
						s.innerHTML= '';
					}		
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes(i).attributes.item(0).text);
					s.innerHTML=oXML.childNodes(i).text;	
				}
			} 
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
	else
	{//if mozilla firefox and mac safari
	
		if(oXML.documentElement.firstChild.childNodes[0].nodeValue == '0')
		{	
			oXML=null;
			return true;
		}
		else
		{
			errxml = W3CXMLtoString(oXML);
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if(oXML.documentElement !=null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					if(oXML.documentElement.childNodes[i].childNodes[0].nodeValue !="t")
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML=oXML.documentElement.childNodes[i].childNodes[0].nodeValue;
					}
					//se o atributo não for teste
					else
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML = '';
					}		
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes[i].attributes[0].nodeValue);
					s.innerHTML=oXML.childNodes[i].childNodes[0].nodeValue;
				}
			}
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
}

//function to handle errors to use steps of regwizard
function errHandlerRegWizard(oXML)
{
	if(window.ActiveXObject)
	{//if internet explorer
		
		if(oXML.firstChild.text == '0')
		{
			oXML=null;
			return true;
		}
		else
		{
			errxml=oXML.xml;
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if (oXML.documentElement != null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					s = eval(oXML.documentElement.childNodes(i).attributes.item(0).text);
					s.innerHTML=oXML.documentElement.childNodes(i).text;
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes(i).attributes.item(0).text);
					s.innerHTML=oXML.childNodes(i).text;	
				}
			} 
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
	else
	{//if mozilla firefox and mac safari
	
		//alert("Primeira validação : " + oXML.documentElement.firstChild.value);
		if(oXML.documentElement.firstChild.value == '0')
		{	
			oXML=null;
			return true;
		}
		else
		{
			errxml = W3CXMLtoString(oXML);
			//alert(errxml);
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if(oXML.documentElement !=null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					if(oXML.documentElement.childNodes[i].childNodes[0].nodeValue != "t")
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML=oXML.documentElement.childNodes[i].childNodes[0].nodeValue;		
					}
					else
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML="";
					}
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes[i].attributes[0].nodeValue);
					s.innerHTML=oXML.childNodes[i].childNodes[0].nodeValue;
				}
			}
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
}

//function to handler erros for reg_wizard3.asp
function errHandlerRegWizard3(oXML)
{
	if(window.ActiveXObject)
	{//if internet explorer
	
		if(oXML.documentElement.firstChild.text == '0')
		{
			oXML=null;
			return true;
		}
		else
		{
			errxml=oXML.xml;
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if (oXML.documentElement != null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					if(oXML.documentElement.childNodes(i).text != "t")
					{
						s = eval(oXML.documentElement.childNodes(i).attributes.item(0).text);
						s.innerHTML=oXML.documentElement.childNodes(i).text;
					}
					else
					{
						s = eval(oXML.documentElement.childNodes(i).attributes.item(0).text);
						s.innerHTML="";
					}
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes(i).attributes.item(0).text);
					s.innerHTML=oXML.childNodes(i).text;	
				}
			} 
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
	else
	{//if mozilla firefox and mac safari
	
		if(oXML.documentElement.childNodes[0].childNodes[0].nodeValue == '0')
		{	
			oXML=null;
			return true;
		}
		else
		{
			errxml = W3CXMLtoString(oXML);
			var s;
			
			// JGP/V 2005.02.17 - If has documentElement use it.
			if(oXML.documentElement !=null)
			{
				for (var i=1; i<oXML.documentElement.childNodes.length; i++)
				{
					if(oXML.documentElement.childNodes[i].childNodes[0].nodeValue != "t")
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML=oXML.documentElement.childNodes[i].childNodes[0].nodeValue;		
					}
					else
					{
						s = eval(oXML.documentElement.childNodes[i].attributes[0].nodeValue);
						s.innerHTML="";
					}
				}
			}
			else
			{
				for (var i=1; i<oXML.childNodes.length; i++)
				{
					s = eval(oXML.childNodes[i].attributes[0].nodeValue);
					s.innerHTML=oXML.childNodes[i].childNodes[0].nodeValue;
				}
			}
			 // JGP/V 2005.02.17 - End
		}
		oXML=null;
		return false;
	}
}

//função para tratar de limpar erros
function errUnHandler()
{
	if(errxml)
	{
		if(window.ActiveXObject)
		{//if internet explorer
		
			oerrXML=new ActiveXObject("MSXML2.DOMDocument");
			oerrXML.loadXML(errxml);
			
			var s;
			
			for(var i=1;i<oerrXML.documentElement.childNodes.length;i++)
			{
				s=eval(oerrXML.documentElement.childNodes(i).attributes.item(0).text);
				s.innerHTML='';
			}
			
			oerrXML=null;
			return false;
		}
		else
		{//if mozilla firefox and mac safari
		
			var oerrXMLFF = W3CNewObject("xml");
			oerrXMLFF = W3CXMLLoad(oerrXMLFF,errxml);
			
			var s;
			
			for(var i=1;i<oerrXMLFF.documentElement.childNodes.length;i++)
			{
				s=eval(oerrXMLFF.documentElement.childNodes[i].attributes.item(0).value);
				s.innerHTML='';
			}
			
			delete(oerrXMLFF);
			return false;
		} 	
	}
}



//função que efectua a limpeza de dados de formulário
function formlimpar(oForm)
{
	for(var i=0;i<oForm.elements.length;i++)
	{
		if(oForm.elements(i).tagName=='SELECT')
		{
			oForm.elements(i).options(0).selected=true;	
		}
		else
		{
			oForm.elements(i).value='';	
		}
	}	
}

function __MUDAMES(oSelectMes,oSelectDia){
	var arrDias=new Array('31','28','31','30','31','30','31','31','30','31','30','31');
	var intMaxDia=oSelectDia.options.length-arrDias[oSelectMes.selectedIndex];
	for(var i=intMaxDia;i>0;i--){
		oSelectDia.options.remove(oSelectDia.options.length-1);
	}
	for(var i=-intMaxDia;i>0;i--){
		var oOption = document.createElement("OPTION");
		oSelectDia.options.add(oOption);
		oOption.innerText = oSelectDia.options.length;
		oOption.Value = oSelectDia.options.length;
	}
}


//adiciona e retira linha 29 aos anos bissextos
function validaano(ano,mes,dia){
	if(parseInt(ano)/4==Math.round(parseInt(ano)/4)){
		if(parseInt(ano)/100!=Math.round(parseInt(ano)/100)){
			if(mes=='02'){
				if(dia.options.length!=29){
				 var oOption = document.createElement("OPTION");
							dia.options.add(oOption);
							oOption.innerText = '29';
							oOption.Value = '29';
				 return true;
				}
			}
		}		
	}else{
		if(dia.options.length==29){
			dia.options.remove(dia.options.length-1)
		}
	}
}	

//função para criar datas
function createData(elem)
{
	var data;
	var dia = eval(elem + '_DIA');
	sdia=dia.value + '/';
	var mes = eval(elem + '_MES');
	smes=mes.value + '/'; 
	var ano = eval(elem + '_ANO');
	sano=ano.value;

	if (ano.value==null)
		return '';
	
	if (ano.value=='')
		sano = '2002'
	
	data = sdia + smes + sano;

	return(data);
}

//fonte do help
FontFace="Verdana,8"

//function to show help menu with information
function fnHelp(TopicText)
{
	if(window.ActiveXObject)
	{//if internet explorer
	
		helper.TextPopup(TopicText, FontFace, 10,10,-1,-1);
	}
	else
	{
		if(window.navigator.userAgent.toLowerCase().indexOf('firefox')!=-1)
		{//if mozilla firefox
		
			createTextPopupEmail(this,'validacao',TopicText,200,10,310,600);
		}
		else
		{//if mac safari
		
			createTextPopupEmail(this,'validacao',TopicText,200,10,450,650);
		}
	}
}

//função para criar a popup para a validação de email em firefox e mac safari
function createTextPopupEmail(winParent, strTitle, TopicText, intWidth, intHeight, intTop, intLeft)
{
	// Declaration of the local variables.
	var objDialogParameters = new Object();
	var result;
	
	if(isNaN(intWidth) || intWidth <=0) intWidth = 10;
	if(isNaN(intHeight) || intHeight<=0) intHeight = 10;
	
	objDialogParameters.winParent = winParent;
	objDialogParameters.title = strTitle;
	
	if(window.navigator.userAgent.toLowerCase().indexOf('firefox')!=-1)
	{//if mozilla firefox
	
		result = winParent.open("", objDialogParameters, "width="+intWidth+"px, height="+intHeight+"px,top="+intTop+"px, left="+intLeft+"px, help=yes, menubar=no, scrollbars=no, statusbar=no, directories=no, toolbar=no");
		result.document.writeln(TopicText);
	}
	else
	{//if mac safari
	
		var result;
		result = window.open("",objDialogParameters, "width="+intWidth+"px, height="+intHeight+"px,top="+intTop+"px, left="+intLeft+"px, help=yes, menubar=no, scrollbars=no, statusbar=no, directories=no, toolbar=no");
		result.document.open();
		result.document.writeln("<html><head><title>strTitle</title></head><body>Introduza o endereço de E-mail que especificou no momento de registo.</body></html>");
		result.document.close();
	}
	
	return result;
}


//function to clean erros
function errorClean(elem)
{
	if(window.ActiveXObject)
	{//if internet explorer
	
		elem.innerHTML='';	
	}
	else
	{//if mozilla firefox and mac safari
	
		document.getElementById(elem).innerHTML = '';
	}
}	
