/*****************************************************************************
' File:    Internet.js
' Purpose: This file is responsible para as funcoes utilizadas no site todo.
' Type: validacoes de forms e funcoes gerais
' ****************************************************************************
' Author                 Date                      Comments
' ------------------- ---------- ---------------------------------------------
' Rui Sousa			2004.03 
' funcoes validacao    
	popUpPicture (imgPath as string)
	isName(Frm,Campo,CampoMsg,Required,Min)
	GerarErro(Frm as string,Msg as string,Campo as string,TextoCampo as string)
' *****************************************************************************/

// **************************************************************
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=no");
		window.focus();
		with (win.document) { 
			open();
			writeln("<HTML><HEAD><TITLE>Preview</TITLE>");
			write("<img style='border: 0px;' name='imagem' id='imagem'");
			write("onLoad='window.resizeTo(this.width+30, this.height+60)'");
			writeln("src='"+imgPath+"'>");
			writeln("<script> self.focus(); <\/script>");
		}
	} else {
		win = window.open(imgPath);
	}
}

function fPrint(url,frm)
		{
			var tar=frm.target;
			var act = frm.action;
			//window.open("","Imprimir",width='1',height='1',toolbar='+false+',locationbar='+false+',statusbar='+false+',scrollbars='+false+');
			window.open("","Catálogo","width=820,height=660,toolbar=false,locationbar=false,statusbar=false,scrollbars=no,scrollbars=yes");		
			window.document.forms[frm].target="Catálogo";
			//frm.destino.value="";			
			window.document.forms[frm].action=url;			
			window.document.forms[frm].submit();			
			window.document.forms[frm].action=act;
			window.document.forms[frm].target=tar;
			return false;
		}
		
function fPrint2(url,frm)
		{
			var tar=frm.target;
			var act = frm.action;
			//window.open("","Imprimir",width='1',height='1',toolbar='+false+',locationbar='+false+',statusbar='+false+',scrollbars='+false+');
			window.open("","Formulário","width=820,height=660,toolbar=false,locationbar=false,statusbar=false,scrollbars=no,scrollbars=yes");		
			window.document.forms[frm].target="Formulário";
			//frm.destino.value="";			
			window.document.forms[frm].action=url;			
			window.document.forms[frm].submit();			
			window.document.forms[frm].action=act;
			window.document.forms[frm].target=tar;
			return false;
		}		

// **************************************************************
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 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 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 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 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;

}


// **************************************************************
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 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 GerarErro(Frm,Msg,Campo,TextoCampo){
// **************************************************************
	alert (Msg);
    document.forms[Frm].elements[Campo].value = TextoCampo;
    document.forms[Frm].elements[Campo].focus();
}