/*****************************************************************************
' File:    Internet10.js
' Purpose: This file is responsible para as funcoes utilizadas no site todo.
' Type: validacoes de forms e funcoes gerais
' ****************************************************************************
' Author                 Date                      Comments
' ------------------- ---------- ---------------------------------------------
' Domingos Loureiro    2002.11.19 Creation of the file.
' Rodrigo Catarino   2002.12.17 Alteração a todas as funções
' Rui Sousa			2003.04 Novas funções
' funcoes validacao
    getErro(sSpecialErro as int) 
	function isCodPostal(Frm as string ,CampoCP1 as string ,CampoCP2 as string, Required as bol)
	isName(Frm as string,Campo as string,CampoMsg as string ,Required as bol,Min as int)
	isPhoneNumber (Frm as string,Campo as string,CampoMsg as string,Required as bol)
	isFloatNumber(s as string)
	IsFloat(e)
	isPercent(s as string) 
	isDateFull(Frm,sData,Campo,CampoMsg,Required) DD/MM/AAAA o Campo é o focus
	isDate(Frm,sDD,sMM,sAAAA,CampoMsg,Required) DD MM AAAA
	isDateFullAMD(Frm,sData,Campo,CampoMsg,Required) AAAA/MM/DD o Campo é o focus
	isDateAMD(Frm,sAAAA,sMM,sDD,CampoMsg,Required) AAAA MM DD
	diasFevereiro(year)
	isNumber(Frmas as string ,Campo as string,CampoMsg as string,Required as bol,Min as int)
	isNumberCompare(Frm,Campo,CampoMsg,Required,Min,Max)
	isEmail(Frm as string,Campo as string,Required as bol)
	isNContribuinte (str as string) 
	isNIB (sNIB as string) 
	isValidSearch (s as string) 
	checkField(myString as string, msg as string)
	isEmpty(Frm as string,Campo as string,CampoMsg as string)
	isSelected(Frm as string,Campo as string,CampoMsg as string)
	isChecked(Frm,Campo,CampoMsg)
	GerarErro(Frm as string,Msg as string,Campo as string,TextoCampo as string)
	hide (strDiv as string)
    show (strDiv as string)
    isVisible (strDiv as string)
    isChar(str)
    divHideShowFromCombo(frm as string,combo as string,prefixo as string)
    divHideShowFromRadio(frm as string,radiogroup as string,prefixo as string)
	alertDelete (url)
	alertMove(frm, url)
	isDecimal(Frm,Campo,CampoMsg,nInt,nDec,Required)
	DateDiff(Frm,lateDD,lateMM,lateAA,earlDD,earlMM,earlAA,Op)
' ****************************************************************************
' OTHER FUNCTIONS
' ------------------- ---------- ---------------------------------------------
' Rui Sousa    2002.11.20 Creation of the file.
' Other funcoes          
	selectMove(origem, destino)
	windowClose ()
	selectAll(obj)
	abreJanelaPostFrm(url, frm)
	abreJanela(url)
	fPrint(url,frm)
	editContent (url)
	inicializa(div)
	refreshContenteWebEditPro(oName, oDiv)
	createPreview(oContentName)
	showGlossario (url)
	callTemplate(nomeForm,caminho)
	popUpPicture (imgPath as string)

' *****************************************************************************/

/* Nao funciona em browsers 5.0* e impedeo resto de funcionar.
String.prototype.trim = function(){
 var x=this;
  x=x.replace(/^\s*(.*)/, "$1");
  x=x.replace(/(.*?)\s*$/, "$1");
  return x;
}
*/
// **************************************************************
function trim(s) {
// **************************************************************
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}
//**************************************************************
function getErro(sSpecialErro){
// **************************************************************
switch (sSpecialErro) {  
 case 1 :  
  return "ção"
 case 2 :  
  return "ó"
 case 3 :  
  return "ão"
 case 4 :  
  return "º"
 case 5 :  
  return "é"
 case 6 :  
  return "á"
 case 7 :  
  return "í"
 case 8 :  
  return "ç"
 case 9 :  
  return "õe"
 case 10 :
  return "ú"
 case 11 :
  return "ê"
 case 12 :
  return "â"
 case 13 :
  return "€"
 case 14 :
  return "à"
 case 15 :
  return "ã"
 case 16 :
  return "ô"
 case 17 :
  return "É"
}
}

//**************************************************************
function isCodPostal(Frm,CampoCP1,CampoCP2,Required){
// **************************************************************
	var valor1 = document.forms[Frm].elements[CampoCP1].value;
	var valor2 = document.forms[Frm].elements[CampoCP2].value;
	if (Required==1)
	{
		if ((!isInteger(valor1))||(valor1.length!=4)){
			GerarErro(Frm,'Por favor preencha correctamente o Código Postal. Este Campo tem que ter 4 algarismos.',CampoCP1,valor1);
			return(false);
		}
		if ((!isInteger(valor2))||(valor2.length!=3)){
			GerarErro(Frm,'Por favor preencha correctamente o Código Postal. Este Campo tem que ter 3 algarismos.',CampoCP2,valor2);
			return(false);
		}
	} else {
		if (!isInteger(valor1)){
			GerarErro(Frm,'Por favor preencha correctamente o Código Postal. Este Campo tem que ter 4 algarismos.',CampoCP1,'0000');
			return(false);
		}
		if (!isInteger(valor2)){
			GerarErro(Frm,'Por favor preencha correctamente o Código Postal. Este Campo tem que ter 3 algarismos.',CampoCP2,'000');
			return(false);
		}
	
	}
	return true
}
// **************************************************************
function isName(Frm,Campo,CampoMsg,Required,Min) {
// **************************************************************
  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWYXZabcdefghijklmnopqrstuvwyxzÁÉÍÓÚÀÈÌÒÙáéíóúàèìòùãõÃÕçÇâêîôûÂÊÎÔÛ-_.,0123456789@ ºª()";
  var allValid = true;
  var decPoints = 0;
  var allNum = "";
  var checkStr = document.forms[Frm].elements[Campo].value;
  for (i = 0;  i < checkStr.length;  i++) {
    ch = checkStr.charAt(i);
    for (j = 0;  j < checkOK.length;  j++) {
		if (ch == checkOK.charAt(j)) break;
    }
    if (j == checkOK.length) {
      allValid = false;
      break;
    }
    allNum += ch;
  }
  if (Required==1) {
	if (trim(checkStr).length ==0) {
	GerarErro(Frm,'Por favor preencha o campo: '+CampoMsg+'',Campo,checkStr);
	return(false);
	}
  } 
  if (!allValid || checkStr.length < Min) {
	GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'. Este Campo tem que ter '+Min+' caracteres.',Campo,checkStr);
	return(false);
	}
  return true;
}
// **************************************************************
function isPhoneNumber (Frm,Campo,CampoMsg,Required) {
// **************************************************************
var checkStr = document.forms[Frm].elements[Campo].value;	
var rePhoneNumber = /^([1-9]{2}\d{7}|00\d{12}|\+\d{12})/;
	if (Required==1) {
		if (checkStr.length==0) {
			GerarErro(Frm,'Por favor preencha Número de '+CampoMsg+'',Campo,checkStr);
			return(false);
		}
	}
	if (checkStr.length!=0) {
		if(!((checkStr.length > 8) && rePhoneNumber.test(checkStr)) ) {
			GerarErro(Frm,'Por favor preencha correctamente o campo Número de '+CampoMsg,Campo,checkStr);
			return(false);
		}
	
	}
  return true;

}
//******************************
//Não permite inserir para além de floats
//44 é a "," e 46 é o "."
function IsFloat(e) {
//******************************
		window.event
		var sValor=new String();
		
		if (navigator.appName=="Netscape")
			{
				if (e.which!=0 && e.which!=8 && e.which!=44 &&(e.which<48 || e.which>57))
					return false;
				else
					return e.which;
		}
		else
		{
			if (e.keyCode!=46 &&(e.keyCode<48 || e.keyCode>57))
				e.keyCode=null;
			return e.KeyCode;
		}
	}

// **************************************************************
function isFloatNumber(s) {
// **************************************************************
	var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	var index = s.indexOf('.');
	if (index == -1 && s.length>13)
	{
	return(false);
	}
	if (index != -1 && s.substring(s,index+1,s.length).length>6)
		{
	return(false);
	}
	return reFloat.test(s);
}
// **************************************************************
function isPercent(s) {
// **************************************************************
	var reFloat = /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/
	if (!reFloat.test(s)||s>100){

	return(false);
	}
	return true;
}

// **************************************************************
function isNumber(Frm,Campo,CampoMsg,Required,Min){
// **************************************************************
	var checkStr = document.forms[Frm].elements[Campo].value;	
	var isValid = true;
	isValid = isInteger(checkStr)
	if (Required==1) {
		if (checkStr.length ==0) {
			GerarErro(Frm,'Por favor preencha o campo: '+CampoMsg+'',Campo,checkStr);
			return(false);
		}
	}
	if (checkStr.length!=0) {
		if (!isValid || checkStr.length < Min) {
			GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'. Este Campo tem que ter '+Min+' algarismos.',Campo,checkStr);
			return(false);
		}
	}
  return true;
}

// **************************************************************
function isNumberBI(Frm,Campo,CampoMsg,Required,Min){
// **************************************************************
	var checkStr = document.forms[Frm].elements[Campo].value;	
	var isValid = true;
	isValid = isInteger(checkStr)
	if (Required==1) {
		if (checkStr.length ==0) {
			GerarErro(Frm,'Por favor preencha o campo: '+CampoMsg+'',Campo,checkStr);
			return(false);
		}
	}
	if (checkStr.length!=0) {
		if (!isValid || checkStr.length < Min) {
			GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'.',Campo,checkStr);
			return(false);
		}
	}
  return true;
}

// **************************************************************
function isNumberCompare(Frm,Campo,CampoMsg,Required,Min,Max,decimais){
// **************************************************************
	var checkStr = document.forms[Frm].elements[Campo].value;	
	var isValid = true;
	if (decimais==0)
	{
		isValid = isInteger(checkStr);
		var minimo = Number(Min);
		var maximo = Number(Max);
		if (checkStr.length!=0) {
			if (!isValid || (checkStr < minimo) || (checkStr > maximo)) {
				GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'. Este Campo tem que ser entre '+Min+' e '+Max+'.',Campo,checkStr);
				return(false);
			}
		}	
	} else {
		var minimo = Math.min(Min,checkStr)
		var maximo = Math.max(checkStr,Max)
		if (checkStr!=Min && checkStr!=Max) {
			if (checkStr == minimo || checkStr == maximo) {
					GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'. Este Campo tem que ser entre '+Min+' e '+Max+'.',Campo,checkStr);
					return(false);
			}
		}	
	}
	if (Required==1) {
		if (checkStr.length ==0) {
			GerarErro(Frm,'Por favor preencha o campo: '+CampoMsg+'',Campo,checkStr);
			return(false);
		}
	}	
  return true;
}

// **************************************************************
function isEmail(Frm,Campo,Required) {
// **************************************************************
var checkStr = document.forms[Frm].elements[Campo].value;	
var posA = checkStr.indexOf ('@', 2)
var posP = checkStr.indexOf ('.', posA+3)
	if (Required ==1) {
		if (checkStr.length == 0) {
			GerarErro(Frm,'Por favor preencha o campo email',Campo,checkStr);
			return(false);
		}
	}
	if ((checkStr.length != 0) && ((posA == -1) || ( posP == -1))) {
			GerarErro(Frm,'Por favor preencha correctamente o campo email',Campo,checkStr);
			return(false);
	}	
			return true;
}

// **************************************************************
function isNContribuinte(Frm,Campo,Required) {
// **************************************************************
  	var checkStr = document.forms[Frm].elements[Campo].value;	
	if (Required ==1) {
		if (checkStr.length == 0) {
			GerarErro(Frm,'Por favor preencha o campo Número Contribuinte',Campo,checkStr);
			return(false);
		}
	}
	if ((checkStr.length<9) || (!isInteger(checkStr)) || (checkStr=="000000000") || (checkStr=="123456789")) {
		GerarErro(Frm,'Por favor preencha correctamente o campo Número de Contribuinte.',Campo,checkStr);
		return(false);  
	}
	produto=0;
	j=8;
	for (i=1; i<10; i++) {
			produto+=checkStr.substring(j,j+1)*i;
			j--;	
		}  
	resto=produto%11;
  	if(resto==0 || (resto==1 && checkStr.substring(8,9)==0)) {
		return true;
	} else { 
		GerarErro(Frm,'Por favor preencha correctamente o campo Número de Contribuinte',Campo,checkStr);
		return(false);  
	}
}	

// **************************************************************
function isNIB(sNIB) {
// **************************************************************
	if ((sNIB.length==0)||(sNIB.length<21)|| (!isInteger(sNIB)))
		return false;

	var cNIB= new Array();

	for (i=0; i<sNIB.length; ++i) { 
		cNIB[i]=sNIB.substring(i, i+1);
	}

	somadig=cNIB[0]*73 
	+ cNIB[1]*17
	+ cNIB[2]*89
	+ cNIB[3]*38
	+ cNIB[4]*62
	+ cNIB[5]*45
	+ cNIB[6]*53
	+ cNIB[7]*15
	+ cNIB[8]*50
	+ cNIB[9]*5
	+ cNIB[10]*49
	+ cNIB[11]*34
	+ cNIB[12]*81
	+ cNIB[13]*76
	+ cNIB[14]*27
	+ cNIB[15]*90
	+ cNIB[16]*9
	+ cNIB[17]*30
	+ cNIB[18]*3;

	resto=somadig%97;

	lCheckDigit=98 - resto;

	if (lCheckDigit != sNIB.substring(19,21))
		return false;
	else
		return true;
}


// **************************************************************
function checkField(myString, msg) {
// **************************************************************
   var newString = myString.match(/((")|(')|({)|(})|(\\)|(\/)|(\[)|(]))/g);
   if (!newString) return true;
	  {
    alert(msg);
	return(false);
	}
}
// **************************************************************
function isValidSearch (str, msg) {
// **************************************************************
	if (str!="" && str.length>3 )
		return checkField(str);
	else {
	return(false);
	}
}
// **************************************************************
//check vazio
function isEmpty(Frm,Campo,CampoMsg) { 
// **************************************************************
  var checkStr = document.forms[Frm].elements[Campo].value;
//checkStr = checkStr.trim();
  if (checkStr.length == 0){
	if(CampoMsg != "") {
		GerarErro(Frm,'O campo '+CampoMsg+' é obrigatório.',Campo,checkStr);
	}
	return(false);
  } 
  return (true);
}
// **************************************************************
function isSelected(Frm,Campo,CampoMsg) { 
// **************************************************************
  var checkStr = document.forms[Frm].elements[Campo].selectedIndex;
  if (checkStr == 0 || checkStr == '' ){
	GerarErro(Frm,'Por favor preencha o campo: '+CampoMsg+'',Campo,'');
	return(false);
  } 
  return (true);
}
// **************************************************************
function isChecked(Frm,Campo,CampoMsg) { 
// **************************************************************
  for(var i=0;i<document.forms[Frm].elements[Campo].length;i++){		
	if (document.forms[Frm].elements[Campo][i].checked){			
	return (true);
	}
  }
	alert ('Por favor preencha o campo: '+CampoMsg+'');
	return(false);
}
// **************************************************************
function GerarErro(Frm,Msg,Campo,TextoCampo){
// **************************************************************
	alert (Msg);
    document.forms[Frm].elements[Campo].value = TextoCampo;
    document.forms[Frm].elements[Campo].focus();
}

// **************************************************************
 function hide (strDiv) {
// **************************************************************
     if (document.all)
       document.all[strDiv].style.display = 'none';
     else if (document.getElementById)
       document.getElementById(strDiv).style.display = 'none';
   }

// **************************************************************
 function show (strDiv) {
// **************************************************************
	 if (document.all)
       document.all[strDiv].style.display = 'block';
     else if (document.getElementById)
       document.getElementById(strDiv).style.display = 'block';
   }
// **************************************************************
 function isVisible (strDiv) {
// **************************************************************
     if (document.all)
       if (document.all[strDiv].style.display == 'block'){ 
		return(true);
	   } else {
	   return(false);
	   }
     else if (document.getElementById)
      if (document.getElementById(strDiv).style.display == 'block'){
		 return(true);
		 } else {
		return(false);
		 }
   }
// **************************************************************
function Janele(fich,nome,w,h,scrol){
// **************************************************************
			LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
			TopPosition = (screen.height) ? (screen.height-h)/4 : 0;
	
	var topp=parseInt(TopPosition);
	
			janele=window.open(fich,nome,'width='+w+',height='+h+',scrollbars='+scrol+',top='+topp+',left='+LeftPosition+'');
			setTimeout('janele.focus()',50);
}
// **************************************************************
function isInteger(Str){
// **************************************************************
	for(var i=0;i<Str.length;i++){
	   var ch=Str.substring(i, i+1);
	   if ( ch<"0" || "9"<ch ) return(false);
	}
	return(true);
}
// **************************************************************
function isMatricula(Frm,Campo,Required){
// **************************************************************
var checkStr = document.forms[Frm].elements[Campo].value;
	if (Required==1) {
		if ((checkStr.length==0)||(checkStr.length<8)){
			GerarErro(Frm,'A matrícula é obrigatória. Este Campo tem que ter 8 caracteres',Campo,checkStr);
			return(false);		
		}
	} 
ch=checkStr.substring(2,3);
ch2=checkStr.substring(5,6);
ch3=checkStr.substring(6,8);
ch4=checkStr.substring(0,2);
ch5=checkStr.substring(3,5);
	if (((!isChar(ch4))&&(!isInteger(ch4)))||(!isInteger(ch5))||(ch!="-") || (ch2!="-") || ((!isChar(ch3))&&(!isInteger(ch3)))) {
		GerarErro(Frm,'O campo Matrícula está incorrecto',Campo,checkStr);
		return(false);	
	} 
		return (true);
}	
// **************************************************************
function isChar(str) {   
// **************************************************************
	for (var i = 0; i < str.length; i++){      
		var ch = str.substring(i, i + 1);      
		if (((ch < "a" || "z" < ch) && (ch < "A" || "Z" < ch)) && ch != ' ' && ch != "ç"  && ch != "ã" && ch != "â" && ch != "ä" && ch != "á" && ch != "à" && ch != "&" && ch != "," && ch != "õ" && ch != "ó" && ch != "ö" && ch != "ô" && ch != "é" && ch != "è" && ch != "ê" && ch != "í" && ch != "ì" && ch != "ô" && ch != "ú" && ch != "ù") {
			return (false);         
		}      
	} 
	return (true);   
}
// **************************************************************
function divHideShowFromCombo(frm,combo,prefixo) {
// **************************************************************
	if (document.forms[frm].elements[combo].selectedIndex==0) {
		for(var i=1;i<document.forms[frm].elements[combo].options.length;i++){
			//alert('hide'+prefixo+i);
			try{hide(prefixo+i);} catch(err){};
		}
	} else {
		for(var i=1;i<document.forms[frm].elements[combo].options.length;i++){		
			//alert('hide'+prefixo+i);
			try{hide(prefixo+i);} catch(err){};
		}
		try{show(prefixo+document.forms[frm].elements[combo].selectedIndex);} catch(err){};
		//alert('show'+prefixo+i)
	}
}
// **************************************************************
function divHideShowFromRadio(frm,radiogroup,prefixo) {
// **************************************************************
	for(var i=0;i<document.forms[frm].elements[radiogroup].length;i++){		
		//alert('hide '+prefixo+i)
		try{hide(prefixo+i);}catch(err){};
		if (document.forms[frm].elements[radiogroup][i].checked){			
			//alert('show '+prefixo+i)
			try{show(prefixo+i);}catch(err){};
		}
	}
}
// **************************************************************
function isDateFull(Frm,sData,Campo,CampoMsg,Required){
// **************************************************************
	 //var checkStr = sData;
	 var checkStr = document.forms[Frm].elements[Campo].value;
	 d = checkStr.substring(0, 2)// day
     c = checkStr.substring(2, 3)// '-'
     b = checkStr.substring(3, 5)// month
     e = checkStr.substring(5, 6)// '-'
     f = checkStr.substring(6, 10)// year
	 if (Required == 1 && checkStr.length == 2) {
			alert('Por favor preencha a data: '+CampoMsg+'');
			document.forms[Frm].elements[Campo].focus();
			return(false);
	 }
	 if (checkStr.length != 2)
	 {
		 if ((checkStr.length != 10) || (!(c == '-')) || (!(e == '-')))
		 {
		  alert ('Por favor preencha a data '+CampoMsg+' correctamente.');
		  document.forms[Frm].elements[Campo].focus();
		  return (false);
		 }

		 if ((b<1 || b>12) || (d<1 || d>31) || (f<1900 || f>9999) || ((b==4 || b==6 || b==9 || b==11) && d==31) || (!isInteger(f)))
		 {
		  alert('Por favor preencha correctamente o campo data '+CampoMsg);
		  document.forms[Frm].elements[Campo].focus();
		  return (false);
		 }	
		 if (b==2 && (!(d<=diasFevereiro(f))))
		 {
			  tempDiasFevereiro = diasFevereiro(f) +1;
			  alert ("Dia inválido. O valor do dia, para o mês e ano escolhido, tem de ser inferior a "+ tempDiasFevereiro + ".");
			  document.forms[Frm].elements[Campo].focus();
			  return (false);
		 }
	 }

  return (true);
}
// **************************************************************
function isDate(Frm,sDD,sMM,sAAAA,CampoMsg,Required) {
// **************************************************************
	var sData = document.forms[Frm].elements[sDD].value+'/'+document.forms[Frm].elements[sMM].value+'/'+document.forms[Frm].elements[sAAAA].value;	
	return isDateFull(Frm,sData,sAAAA,CampoMsg,Required);
}
// **************************************************************
function diasFevereiro(year) {   
// **************************************************************
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}


// **************************************************************
function alertDelete (url){
// **************************************************************
if (confirm("Tem a certeza que pretende apagar?")) {
	window.location=url;
 }
}

// **************************************************************
function alertMove(frm, url) {
// **************************************************************
	if (confirm("Tem a certeza que pretende mover?")) {
		document.forms[frm].action=url;
		document.forms[frm].submit();
		return true;
	}
	return false;
}

/**************************************************************
'
' OTHER FUNCTIONS
'
'**************************************************************/

//**************************************************************
function showGlossario (url) {
//**************************************************************
var w;
w=window.open(url,"glossario","toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,width=400,height=400");
w.focus();
}

//**************************************************************
function selectMove(origem, destino) {
//**************************************************************
 var obj = origem;
 var selectedIdex = obj.options.selectedIndex;
 if (selectedIdex>=0) {
 	if (obj.options[selectedIdex].selected = true) {
 		 var oOption = document.createElement("OPTION");
		 oOption.text=obj.options[selectedIdex].text;
		 oOption.value=obj.options[selectedIdex].value;
		 destino.add(oOption);
		 origem.remove(obj.options.selectedIndex);
		 }
 	}
}

//**************************************************************
function windowClose () {
//**************************************************************
 self.close();
}

//**************************************************************
function selectAll(obj) {
//**************************************************************
 //var newState = document.frmHomepage.intCampanhaId;
 var tamanho = obj.length;
 for (var n = 0; n < tamanho; n++) {
  obj.options[n].selected=true; 
 }
}

// **************************************************************
function abreJanelaPostFrm(url, frm) {
// **************************************************************
	var tar=frm.target;
	var act = frm.action;
	var x = window.open(url,"Campanhas");
	frm.target="Campanhas";
	frm.action=url;
	frm.submit();
	x.focus();
	frm.action=act;
	frm.target=tar;	
}	

// **************************************************************
function abreJanela(url) {
// **************************************************************
	var x = window.open(url,"janela");
	x.focus();
}	

function fPrint(url,frm)
		{
	var tar=frm.target;
	var act = frm.action;
	var x = window.open("","Imprimir","width=640,height=480,toolbar=false,locationbar=false,statusbar=false,scrollbars=no,scrollbars=yes");
	frm.target = "Imprimir";
	frm.action = url;
	frm.submit();
	x.focus();
	frm.action=act;
	frm.target=tar;	
		}

// **************************************************************
function editContent (url) {
// **************************************************************
var w = window.open(url,"editorwithstyle");
}
   
// **************************************************************
function refreshContenteWebEditPro(oName, oDiv) {
// **************************************************************
oDiv.innerHTML = oName.value;
}

// **************************************************************
function createPreview(oContentName) {
// **************************************************************
	var win = window.open("", "Preview", "width=800,height=500,resizable,scrollbars,status,titlebar"); 
	with (win.document) { 
		write("<HTML><HEAD><TITLE>Preview Page</TITLE>");
		write("<LINK rel='stylesheet' type='text/css' href='/css/10/internet101.css'>\n");
		write("<meta http-equiv=Content-Type content='text/html;'>\n");
		write("</HEAD><BODY><form>\n");
		write(oContentName.value);
		write("<BR><BR><BR><center>");
		write("<input type='button' value='Close' onClick='javascript:self.close();'>");
		write("</center></form></BODY></HTML>");
		close();
	}
}

// **************************************************************
function callTemplate(nomeForm,caminho) {
// **************************************************************
	nomeForm.action=caminho;
	nomeForm.submit();
	return true;
}

// **************************************************************
// frm - nome do formulário
// Campo - nome do campo a validar
// CampoMsg - mensagem a acrescentar no alerta de erro
// nInt - nº máximo de algarismos à esquerda da vírgula
// nDec - nº máximo de algarismos à direita da vírgula
// Required - 1 se o campo é obrigatório
// Nesta função, a vírgula é obrigatória.
function isDecimal(Frm,Campo,CampoMsg,nInt,nDec,Required) {
// **************************************************************
var checkStr = document.forms[Frm].elements[Campo].value;
isInteger
var posA = checkStr.indexOf (',',1)
var ex = ""

	for (i = 0; i < nInt; i++) {
		ex=ex+"#";
	}
	ex=ex+",";
	for (i = 0; i < nDec; i++) {
		ex=ex+"#";
	}

	if (Required ==1) {
		if (checkStr.length == 0) {
			GerarErro(Frm,'O campo '+CampoMsg+' é obrigatório',Campo,checkStr);
			return false;
		}
	}

	if (((checkStr.length != 0) && (posA == -1))||(!isInteger(checkStr.substring(0,posA)))||(posA>nInt)||(!isInteger(checkStr.substring(posA+1)))||(checkStr.length-posA-1>nDec)||(posA==checkStr.length-1)) {
		GerarErro(Frm,'Por favor preencha correctamente o campo '+CampoMsg+'. Este campo tem de ter o formato '+ex+'.',Campo,checkStr);
		return false;
	}
		return true;
}
// **************************************************************
function popUpPicture (imgPath) {
// **************************************************************
var fExtension = imgPath.substring(imgPath.lastIndexOf(".")+1);
var win;
if (fExtension == "jpg" || fExtension == "jpeg" || fExtension == "gif" || fExtension == "png") {
	win = window.open("", "Preview", "width=80,height=50,resizable,scrollbars=no,status=no,titlebar");
    window.focus();
	with (win.document) { 
		open();
		writeln("<HTML><HEAD><TITLE>Preview</TITLE>");
		writeln("<LINK rel='stylesheet' type='text/css' href='/css/10/internet101.css'>");
		writeln("<meta http-equiv=Content-Type content='text/html;'>");
		writeln("</HEAD><BODY>");
		writeln("<script>function img(){")
		writeln("document.write('<span class=TC>Largura='+document.images[0].width+'<\/span><br>');"); 
		writeln("document.write('<span class=TC>Altura='+document.images[0].height+'<\/span>');");
		writeln("}<\/script>");
		write("<img style='border: 0px;' name='imagem' id='imagem'");
		write("onLoad='window.resizeTo(this.width + 40, this.height + 100)' ");
		writeln("src='"+imgPath+"'>");
		writeln("<script> self.focus(); img(); <\/script></BODY></HTML>");
		close();
	}
} else {
	win = window.open(imgPath);
}
}

// **************************************************************
function DateDiff(Frm,lateDD,lateMM,lateAA,earlDD,earlMM,earlAA,Op){
//retorna a diferença entre duas datas em Op(Anos,Dias,Minutos)
// **************************************************************
if ((lateDD == "") && (lateMM == "") & (lateAA == ""))  {
	var laterdate = new Date();
	} else {
	var laterdate = new Date(document.forms[Frm].elements[lateAA].value,document.forms[Frm].elements[lateMM].value-1,document.forms[Frm].elements[lateDD].value);     // Data mais antiga
	}
//alert (laterdate);
var earlierdate = new Date(document.forms[Frm].elements[earlAA].value,document.forms[Frm].elements[earlMM].value-1,document.forms[Frm].elements[earlDD].value);  // Data mais recente
//alert (earlierdate);
var difference = laterdate.getTime() - earlierdate.getTime();
var AnosDifference = Math.floor(difference/1000/60/60/24/365);
var daysDifference = Math.floor(difference/1000/60/60/24);
difference -= daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference -= hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference -= minutesDifference*1000*60
	switch (Op) {  
	 case "AA" :  
	  return AnosDifference
	 case "DD" :  
	  return daysDifference
	}
}

function checkFieldArea(myString,Campo)
{
   var newString = myString.match(/((")|(')|(<)|(>)|({)|(})|(\\)|(\/)|(\[)|(]))/g);
   if (!newString) return true;
   else {
      alert("Introduziu pelo menos um dos seguintes caracteres inv"+getErro(6)+"lidos no campo "+Campo+": \" ' { } \\ \/ \[ ] \< >");
	  return false;
      }
}
//******************************
//Não permite inserir para além de inteiros
function IsDigit(e) {
//******************************
	if (navigator.appName=="Netscape")
	{
		if (e.which!=0 && e.which!=8 &&(e.which<48 || e.which>57))
			return false;
		else
			return e.which;
	}
	else
	{
	if (e.keyCode<48 || e.keyCode>57)
		e.keyCode=null;
	return e.KeyCode;
	}
}
