function elm(obj){
	return document.getElementById(obj);
}

function eliminar(path){
	if(confirm('Tem a certeza que deseja eliminar este registo?')){
		window.location=path
	}
}
function sonumeros(){
	var caract = new RegExp(/^[0-9]+$/i);
	var caract = caract.test(String.fromCharCode(event.keyCode));

	if(!caract){
		event.keyCode=0;
		return;
	}
}

function sair(){
	if(confirm('Tem a certeza que deseja sair?')){
		window.location='logout.php'
	}
}
function eliminar(path){
	if(confirm('Tem a certeza que pretende eliminar o registo?')){
		window.location=path
	}
}
function eliminarfoto(path){
  	if(confirm('Tem a certeza que pretende eliminar esta foto?')){
		window.location=path
	}
}

function openW(file,wW,wH,scr){
	
	var posX = (screen.width-wW) / 2; 
	var posY = (screen.height-wH) / 2; 
	
	if(!myWin){
		var myWin = window.open(file, 'varW', 'Height='+ wH +'px, Width='+ wW +'px,resizable=no,top='+posY+',left='+posX+',scrollbars='+scr)
		myWin.focus();	
	}
}

//Activa a caixa da Data da Opção activo ( disponivel em quase todos os formularios
function activo(){
	if(elm('chkactivo').checked==false){
		elm('calendario').style.visibility='hidden';
	}else{
		elm('calendario').style.visibility='visible';
	}
}

//Função para carregar o Google Maps
function load() {
  if (GBrowserIsCompatible()) {
  
	var Latinit = 40.35910267579199;
	var Loginit = -8.028945922851562;
	var Zoom = 7;
	
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GSmallMapControl());
	map.addControl(new GMapTypeControl());
	map.setCenter(new GLatLng(Latinit,Loginit), Zoom); 

	GEvent.addListener(map, "click", function(overlay, point) {
	  if (overlay) {
		map.removeOverlay(overlay);
	  } else {
		try { 
			map.removeOverlay(oldmarker); 
		} catch (e) { }; 
			var marker=new GMarker(point); 
			map.addOverlay(marker); 
			oldmarker=marker; 
			
			map.setCenter(new GLatLng(point.lat(),point.lng()), Zoom);
			
			document.getElementById("latitude").value = point.lat(); 
			document.getElementById("longitude").value = point.lng();
	  }
	});
  }
}

	
function htmlentities(str)
    {var keys=new Array(" ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "\"", "<", ">", "");
    var values=new Array("&nbsp;", "&iexcl;", "&cent;", "&pound;", "&curren;", "&yen;", "&brvbar;", "&sect;", "&uml;", "&copy;", "&ordf;", "&laquo;", "&not;", "&shy;", "&reg;", "&macr;", "&deg;", "&plusmn;", "&sup2;", "&sup3;", "&acute;", "&micro;", "&para;", "&middot;", "&cedil;", "&sup1;", "&ordm;", "&raquo;", "&frac14;", "&frac12;", "&frac34;", "&iquest;", "&Agrave;", "&Aacute;", "&Acirc;", "&Atilde;", "&Auml;", "&Aring;", "&AElig;", "&Ccedil;", "&Egrave;", "&Eacute;", "&Ecirc;", "&Euml;", "&Igrave;", "&Iacute;", "&Icirc;", "&Iuml;", "&ETH;", "&Ntilde;", "&Ograve;", "&Oacute;", "&Ocirc;", "&Otilde;", "&Ouml;", "&times;", "&Oslash;", "&Ugrave;", "&Uacute;", "&Ucirc;", "&Uuml;", "&Yacute;", "&THORN;", "&szlig;", "&agrave;", "&aacute;", "&acirc;", "&atilde;", "&auml;", "&aring;", "&aelig;", "&ccedil;", "&egrave;", "&eacute;", "&ecirc;", "&euml;", "&igrave;", "&iacute;", "&icirc;", "&iuml;", "&eth;", "&ntilde;", "&ograve;", "&oacute;", "&ocirc;", "&otilde;", "&ouml;", "&divide;", "&oslash;", "&ugrave;", "&uacute;", "&ucirc;", "&uuml;", "&yacute;", "&thorn;", "&yuml;", "&quot;", "&lt;", "&gt;", "");
    for(var i=0; i<keys.length; i++)
        {if(values[i]!="&nbsp;"){str=str.replace(keys[i], values[i]);}}
    return str;
}

function valError(textbox,msg){
	$('#'+textbox).addClass('textError');
	$('#erro' + textbox).remove();
	$('#'+textbox).focus();
	$('#'+textbox).select();
	return '<span id="erro' + textbox + '" class="valError"><img src="imagens/delete.png" title="' + msg + '"> ' + msg + '</span>';
	}
	
function limpaErro(textbox){
	$('#erro' + textbox).remove();
	$('#' + textbox).removeClass('textError');
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		
		alert("Verifique o formato da data")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Introduza um mês válido")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Introduza um dia válido")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Introduza um ano com 4 dígitos válido entre "+minYear+" e "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Introduza uma data válida")
		return false
	}
return true
}

function ValidateForm(){
	var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }
 
 
function CreateBookmarkLink() {
	title = "SNCGP - Sindicato Nacional do Corpo da Guarda Prisional";
	url = "http://www.sncgp.com";
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( url, title);
	} else if(window.opera && window.print) { // Opera Hotlist
		return true;
	}
}


