function teclasAtalhos(e){
	if (window.event) {tecla = e.keyCode;}
	else if (e.which) {tecla = e.which;}
}

function validaCampos(form){
    var cont = 0;
    for(var i=0;i < document.forms[form].elements.length;i++){
        if(document.forms[form].elements[i].name){
            document.forms[form].elements[i].style.background = '';
            //if(document.forms[form].elements[i].value == ''){
            //        alert("O campo \" "+document.forms[form].elements[i].name+" \" é obrigatório,\ne será identificado pela cor amarela.");
            //        document.forms[form].elements[i].style.background = '#FFFFCC';
            //        document.forms[form].elements[i].focus();
            //        cont++;
            //        return;
            //}
        }
    }
    return cont;
}

function validaData(campo) {
  var expReg = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/(19|20)?\d{2}$/;
  //var aRet = true;
  
  if (campo.value != '') {
  	if ((campo) && (campo.value.match(expReg))) {
  		var dia = campo.value.substring(0, 2);
  		var mes = campo.value.substring(3, 5);
  		var ano = campo.value.substring(6, 10);
  		if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia > 30) {
  			alert('Data Invalida!\nData Informada: ' + campo.value);
  			campo.value = '';
  			campo.focus();
  		}
  		else {
  			if ((ano % 4) != 0 && mes == 2 && dia > 28) {
  				alert('Data Invalida!\nData Informada: ' + campo.value);
  				campo.value = '';
  				campo.focus();
  			}
  			else {
  				if ((ano % 4) == 0 && mes == 2 && dia > 29) {
  					alert('Data Invalida!\nData Informada: ' + campo.value);
  					campo.value = '';
  					campo.focus();
  				}
  			}
  		}
  	}
  	else {
  		alert('Data Invalida!\nData Informada: ' + campo.value);
  		campo.value = '';
  		campo.focus();
  	}
  }
}

function getFormulario(msg,form){
	
	if(!form) form = 0;
	
	if(!msg){
		var msgConfirm = confirm('Confirma a operação?');
	}else{
		var msgConfirm = true;
	}
	if (msgConfirm) {
              if (validaCampos(form) == 0) {
                      elementos = document.forms[form].elements;
                      nrElementos = elementos.length;

                      var s = 0;
                      var senha = new Array();
                      var parametros = '';
                      for (f = 0; f < nrElementos; f++) {
                              if (elementos[f].type == 'textarea' || elementos[f].type == 'password' || elementos[f].type == 'hidden' || elementos[f].type == 'text' || elementos[f].type == 'select-one' || elementos[f].checked || elementos[f].type == 'checkbox') {

                                      if (elementos[f].type == 'checkbox') {
									  	if (elementos[f].checked) {
									  		elementos[f].value = 1;
									  	}else {
									  		elementos[f].value = 0;
									  	}
									  }

                                      if (elementos[f].type == 'password' && elementos[f].title == 'senha') {
                                              senha[s] = elementos[f].value;
                                              s++;
                                      }

                                      if (senha.length == 2) {
                                              if (senha[0] != senha[1]) {
                                                      alert('Senhas nao conferem!');
                                                      return false;
                                              }
                                      }

                                      var elemento = elementos[f];
									  var value = elemento.value.toUpperCase();
                                      parametros += elemento.id + "=" +encodeURI(value)+ "&";
                              }
                      }
                      return parametros;
              }else{
                  return false;
              }
	}
}

function verificaStatusTela(){
    if(document.getElementById('status')){
        if(document.getElementById('status').value == 1){
            if(!confirm('Existem processos ativos nesta tela.\nDeseja realmente fechá-la?'))
                return false;
                    else
                        return true;
        }else{
            return true;
        }
    }else{
        return true;
    }
}

function montaJanela(pagina,base,titulo,largura,altura,top,left){
    
    if(!verificaStatusTela()){return;}

	document.getElementById("divContainer").innerHTML = '';
	var divCorpo	=	document.createElement('div');
	var divTitulo	=	document.createElement('div');
	var divBase		=	document.createElement('div');
	
	divCorpo.className 	= 'divCorpo';
	divCorpo.id		 	= 'divCorpo';
	divTitulo.className = 'divTitulo';
	divBase.className 	= 'divBase';
	divBase.id 			= 'divBase';
	
	var labelTitulo		=	document.createElement('label');
	var labelBtFechar	=	document.createElement('label');
	
	labelTitulo.className	=	'labelTitulo';
	labelBtFechar.className	=	'labelBtFechar';
	
	labelTitulo.innerHTML 	= titulo;
	labelBtFechar.innerHTML = 'Fechar';
	
	divTitulo.appendChild(labelBtFechar);
	divTitulo.appendChild(labelTitulo);
	
	if(isNaN(largura) || isNaN(altura)){
		divCorpo.style.width = '99%';
		divCorpo.style.height= '99%';
	}else{
		divCorpo.style.width = largura+'px';
		divCorpo.style.height= altura+'px';
	}
	
	if(isNaN(top) || isNaN(left)){
		document.getElementById(base).appendChild(divCorpo);
		
		labelBtFechar.onclick = function(){
            if(!verificaStatusTela()){return;}
			document.getElementById(base).removeChild(this.parentNode.parentNode);
		}
		
	}else{
		top = top+'px';
		left = left+'px';
		
		labelBtFechar.onclick = function(){
            if(!verificaStatusTela()){return;}
			document.body.removeChild(this.parentNode.parentNode);
		}
		
	}
	
	divCorpo.appendChild(divTitulo);
	divCorpo.appendChild(divBase);
	
	carregaPagina(pagina, 'divBase');
}

function include(arquivo){
  var novo = document.createElement("script");
  novo.setAttribute('type', 'text/javascript');
  novo.setAttribute('src', arquivo);
  document.getElementsByTagName('head')[0].appendChild(novo);
}


function addNewItem(divEsconder,divExibir){
    var divEsconder = document.getElementById(divEsconder);
    var divExibir = document.getElementById(divExibir);
    divEsconder.style.display = 'none';
    divExibir.style.display = 'block';
}

function tabelaZebrada(tabela){
	var cor1 = '#FFFFFF';
	var cor2 = '#D3D3D3';
	
	if(document.getElementById(tabela))
		var tabela = document.getElementById(tabela);
		else
			return
			
	var nrTabela = tabela.rows.length;
	var corAux = cor1;
	
	var t = document.createElement('tr');
	
	for(x = 0; x <= nrTabela - 1; x++){
		
		tabela.rows[x].style.cursor = 'pointer';
		
		tabela.rows[x].onmouseover = function(){
			this.style.background = '#FBE973';
		}
		
		if (corAux == cor1) {
			if (tabela.rows[x].style.display != 'none') {
				tabela.rows[x].style.background = cor2;
				corAux = cor2;
				tabela.rows[x].onmouseout = function(){
					this.style.background = cor2;
				}
			}
		}else{
			if (tabela.rows[x].style.display != 'none') {
				tabela.rows[x].style.background = cor1;
				corAux = cor1;
				
				tabela.rows[x].onmouseout = function(){
					this.style.background = cor1;
				}
			}
		}	
	}
}

function getPageSize(){

   var xScroll, yScroll;

   if (window.innerHeight && window.scrollMaxY) {
       xScroll = document.body.scrollWidth;
       yScroll = window.innerHeight + window.scrollMaxY;
   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
       xScroll = document.body.scrollWidth;
       yScroll = document.body.scrollHeight;
   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
       xScroll = document.body.offsetWidth;
       yScroll = document.body.offsetHeight;
   }

   var windowWidth, windowHeight;
   if (self.innerHeight) {    // all except Explorer
       windowWidth = self.innerWidth;
       windowHeight = self.innerHeight;
   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
       windowWidth = document.documentElement.clientWidth;
       windowHeight = document.documentElement.clientHeight;
   } else if (document.body) { // other Explorers
       windowWidth = document.body.clientWidth;
       windowHeight = document.body.clientHeight;
   }

   // for small pages with total height less then height of the viewport
   if(yScroll < windowHeight){
       pageHeight = windowHeight;
   } else {
       pageHeight = yScroll;
   }

   // for small pages with total width less then width of the viewport
   if(xScroll < windowWidth){
       pageWidth = windowWidth;
   } else {
       pageWidth = xScroll;
   }

   return arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)

}
