var ie=(navigator.appName == 'Microsoft Internet Explorer')?true:false;
var ns=(navigator.appName == 'Netscape')?true:false;
var DOMsupport=(!(document.implementation && document.implementation.hasFeature))?false:true;

/*
* Objeto Ajax
*/
function getHTTPObject() {
    var req;
    // Mozilla, Firefox, Safari, e Netscape
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch(e) {
            req = false;
        }
        return req;
    }

    // Internet Explorer
    if (window.ActiveXObject) {
        try {
            req = new ActiveXObject("Msxml3.XMLHTTP");
        } catch(e) {
            try {
                req = new ActiveXObject("Msxml2.XMLHTTP");
            } catch(e) {
                try {
                    req = new ActiveXObject("Microsoft.XMLHTTP");
                } catch(e) {
                    req = false;
                }
            }
        }
        return req;
    }
    alert("Objeto XMLHTTP não e suportado pelo navegador.");
}
var xmlhttp = getHTTPObject();


function keepAliveAspxSession()
{
	var result = '', tmp = '', method = 'GET', urlAjax = '';
	
	//urlAjax += '/treinaweb'; // comentar em ambiente de produção
	urlAjax += '/keepAliveAspxSession.aspx'; // em produção
	
	if (typeof xmlhttp != "undefined") {
		xmlhttp.open(method, urlAjax, true);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4)
			{
				if (xmlhttp.status == 404) 
		            alert("Status HTTP Response: Keep Alive Aspx Session - " + xmlhttp.status + "\nError: " + xmlhttp.statusText);
		        else if (xmlhttp.status != 200) 
		            alert("Status HTTP Response: Keep Alive Aspx Session - " + xmlhttp.status + "\nError: " + xmlhttp.statusText);
		        else {
					result = xmlhttp.responseText;
				}
			}
		}
	    xmlhttp.send(null);
		setTimeout('keepAliveAspxSession()', 150000);//chama de 5 em 5min pra manter a sessão atual viva
	}else 
		alert("Keep Alive Aspx Session - Object xmlhttp nao foi encontrado.");
}
////////////////////////////////////////////////////////////////////
function WorkLayers()
{
	var args = WorkLayers.arguments, i = 0, obj = '';
	for(var i=0; i<args.length; i+=2)
	{
		obj = document.getElementById(args[i]);
		if(obj != null) {
			obj.style.display = args[i+1];
		}
	}
}

////////////////////////////////////////////////////////////////////
// Verifica se existem letras na variavel
function nochars(strval){
	var retval = true;
	for (var i = 0; i < strval.length; i++){
		if (strval.substring(i,i+1) < "0" || strval.substring(i,i+1) > "9"){
			retval=false;
			break;
		}
	}
	return retval;
}
function isdate(obj)
{
	var mes, dia, ano;
	var retval = false;
	ano=obj.substring(6,10);
	
	if (obj != "")
	{
	   // se é diferente de 00/00/0000
	   if (obj.length != 10 || obj.substring(2,3)!="/" || obj.substring(5,6)!="/") 
	   	  retval = false;
	   else {
	      mes = obj.substring(3,5);
	      if (nochars(mes) && parseInt(mes,10) > 0 && parseInt(mes,10) <= 12)
		  {
	         mes = parseInt(mes,10);
	         if (nochars(ano) && parseInt(ano,10) >= 0000) 
			 {
	            if ((parseInt(ano,10) % 4) == 0) bissexto = 1;
	            else bissexto = 0;
				
	            dia = obj.substring(0,2);
	            if (nochars(dia))
				{
	               dia = parseInt(dia,10);
	               if (((mes==1 || mes==3 || mes==5 || mes==7 || mes==8 || mes==10 || mes==12) && (dia >= 1 && dia <= 31)) ||
	                  ((mes==4 || mes==6 || mes==9 || mes==11) && (dia >= 1 && dia <= 30)) ||
	                  (mes==2 && dia >= 1 && dia <= (28 + bissexto))) 
				   {
	                  retval=true;
	               }//if 6
	            }//if 5
	         }//if 4
	      }//if 3
	   }//else vazio
	}else //if 1
	     retval = false;

	return retval;
}
function somenteNumeros(tecla) {
	var Code;
	
	Code = (ie)?tecla.keyCode:tecla.which;
	
	if(Code < 48 || Code > 57) {
		if(ie) tecla.returnValue = false;
		else {
			// se for diferente do TAB - Firefox
			if(Code != 0) tecla.preventDefault();
		}
	}
}
function setarNotaNulo(tecla, idField)
{
	var Code;
	var field = document.getElementById(idField);
	
	Code = (ie)?tecla.keyCode:tecla.which;
	if(Code == 32) {
		field.value = '';
		notas[idField] = field.value;
		notasNulas[idField] = 1;
	}
}
function FormataData(Campo, teclapres){
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace("/", "");
	vr = vr.replace("/", "");
	tam = vr.length + 1;
	if (tecla != 9 && tecla != 8 && tecla != 46)
	{
		if (tam > 2 && tam < 5) Campo.value = vr.substr(0, 2) + '/' + vr.substr(2, tam);
		if (tam >= 5 && tam <=10) Campo.value = vr.substr(0,2) + '/' + vr.substr(2,2) + '/' + vr.substr(4,4);
	}
}
function FormataHora(Campo,teclapres){ // hh:mm
	var tecla = teclapres.keyCode;
	var vr = new String(Campo.value);
	vr = vr.replace( ":", "" );
	tam = vr.length + 1;
	
	if ( tecla != 9 && tecla != 8 && tecla != 46 ){
		if ( tam > 2 && tam < 5 ){
			Campo.value = vr.substr( 0, tam - 2  ) + ':' + vr.substr( tam - 2, tam );
		}
	}
}

//////////////////////////////////////////////////////////////////
// Marca todos os checkboxes com nome passado em "field", no formulário "tf"
function Checkall(tf, field, obj) {
 var Cont = 0;
   
   if (tf[field].length) {
      for (var i=0; i < tf[field].length; i++) {
         tf[field][i].checked = obj.checked;
      }
   }else
      tf[field].checked = obj.checked;
   
   if (tf[obj.name].length) {
	   for (var i=0; i < tf[obj.name].length; i++) {
	   	tf[obj.name][i].checked = obj.checked;
	   }
   }else 
   	 tf[obj.name].checked = obj.checked;
   
   return;
}