/*                                                         */
/*                     SysGeral                            */
/*                                                         */
/*              Variaveis e funcões gerais                 */
/*                                                         */
/*          Desenvolvido por Dayan C.Galiazzi              */
/*             <dayan@creditaipu.com.br>                   */
/*                                                         */
/*              Variaveis de uso Geral                     */
/*                                                         */

var F_IE=document.getElementById&&!document.all;

/*                                                         */
/*                Funcoes de uso geral                     */
/*                                                         */
function createElm(n){ return document.createElement(n); };
function getElm(d){ return document.getElementById(d); };
function $(d){ return document.getElementById(d); };

/***********************************************************************/
/*          Nao permite que o texto seja selecionado                   */
/***********************************************************************/
function disableSelect(){
	document.onselectstart=function(){ return false; };
    if(window.sidebar) document.onmousedown=function(){ return false; };
};

/***********************************************************************/
/*          Habilita a seleção de texto                                */
/***********************************************************************/
function enableSelect(){
   	document.onselectstart=function(){ return true; };
   	if(window.sidebar) document.onmousedown=function(){ return true; };
};

/***********************************************************************/
/*           Paga as propriedades de tamanho da pagina                 */
/***********************************************************************/
function getPageSize(){
   	var pageWidth,pageHeight,windowWidth,windowHeight;
   	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;

   	this.pageWidth=pageWidth;
   	this.pageHeight=pageHeight;
   	this.windowWidth=windowWidth;
   	this.windowHeight=windowHeight;
};

/***********************************************************************/
/*           Deixa o objeto e seus filhos inselecionaveis              */
/***********************************************************************/
function setUnselectable(elm,c){	;
   	if(document.getElementsByTagName)
      	if(elm && typeof(elm.tagName)!='undefined')
         	if(elm.tagName!='INPUT' && elm.tagName!='TEXTAREA' && elm.tagName!='IFRAME'){
            	if(elm.hasChildNodes())
               		for(var i=0;i<elm.childNodes.length;i++)
                  		setUnselectable(elm.childNodes[i],c);
            		elm.unselectable=c?'on':'off';
         	}
};

/***********************************************************************/
/*         Retorna  todos os elementos com o determinado id            */
/***********************************************************************/
function getElementsById(sId){
   	var res=new Array();
	if(typeof(sId)!='string' || !sId) return res;

	if(document.evaluate){
		var xpathString = "//*[@id='" + sId.toString() + "']"
		var xpathResult = document.evaluate(xpathString, document, null, 0, null);
		while ((res[res.length] = xpathResult.iterateNext()));
		res.pop();
	}
	else if(document.all){
		for(var i=0,j=document.all[sId].length;i<j;i+=1)
			res[i]=document.all[sId][i];
	}
	else if(document.getElementsByTagName){
		var aEl=document.getElementsByTagName('*');
		for(var i=0,j=aEl.length;i<j;i+=1)
			if(aEl[i].id == sId )
				res.push(aEl[i]);
	};
	return res;
};

/***********************************************************************/
/*              Cria div de desabilitacao                              */
/***********************************************************************/
function DivDesableAll(opt){
	if(!opt) return;	
	if(!getElm("DivDesableAll")){
		var d=createElm("div");
		d.id="DivDesableAll";
    	d.style.visibility="hidden";
		d.style.position="absolute";
		d.style.backgroundColor="#A5A5A5";
		d.style.opacity=".35";
		d.style.filter="alpha(Opacity=35)";
		d.style.left=0;
		d.style.top=0;
    	document.body.appendChild(d);	
	}		
	
	var t=getElm("DivDesableAll");
	var a= new getPageSize();
	t.style.width=a.windowWidth;
    t.style.height=a.windowHeight;	
	if(opt.opacity){
		t.style.opacity="."+opt.opacity;
		t.style.filter="alpha(Opacity="+opt.opacity+")";
	} 
	if(opt.cor) t.style.backgroundColor=opt.cor;
	if(opt.zIndex) t.style.zIndex=opt.zIndex;
	if(opt.show) t.style.visibility="visible"; 
	else t.style.visibility="hidden";
};   

/***********************************************************************/
/*                   Interrompe evento                                 */
/***********************************************************************/
function stopEvent(e){
   if(!e) e=window.event;
   if(e.stopPropagation) e.stopPropagation();
   else e.cancelBubble=true;
};

/***********************************************************************/
/*                   Cancela evento                                    */
/***********************************************************************/
function cancelEvent(e){
   if(!e) e=window.event;
   if(e.preventDefault) e.preventDefault();
   else e.returnValue=false;
};

/***********************************************************************/
/*            Extrai a tag especificada de uma string                  */
/***********************************************************************/
function extraiTag(tag,txt){
    var ini=0;
    while (ini!=-1){
        ini=txt.indexOf('<'+tag,ini);
        if(ini>=0){
            ini=txt.indexOf('>',ini)+1;
            var fim=txt.indexOf('</'+tag+'>', ini);
            return txt.substring(ini,fim);
        }
    }
};

/***********************************************************************/
/*               coleta javascript de um obj html                      */
/***********************************************************************/
function ParseJs(obj){
   var Tags=obj.getElementsByTagName('SCRIPT');
   var code='';
   for(var n=0;n<Tags.length;n++){
      if(Tags[n].src){
         var head=getElm("head")[0];
         var o=createElm("script");
         o.setAttribute("type", "text/javascript");
         o.setAttribute("src",Tags[n].src);
      }
      else{
         if(navigator.userAgent.toLowerCase().indexOf('opera')>=0)
            code=code+Tags[n].text+'\n';
         else
            code=code+Tags[n].innerHTML;
      }
   }
   if(code) InstallScript(code);
};

/***********************************************************************/
/*               Executa comandos em javascript                        */
/***********************************************************************/
function InstallScript(script){
   if(!script) return;
   if(window.execScript) window.execScript(script)
   else window.setTimeout( script, 0 );
};

/***********************************************************************/
/*                   Insere Css                                        */
/***********************************************************************/
function evaluateCss(obj){
   var cssTags = obj.getElementsByTagName('STYLE');
   var head = document.getElementsByTagName('HEAD')[0];
   for(var no=0;no<cssTags.length;no++) head.appendChild(cssTags[no]);
};

/***********************************************************************/
/*              chama funcao com base na tecla precionada              */
/***********************************************************************/
function doOnKeyPress(event,key,f){
	if(event.keyCode==key){ 
		if(typeof f=="function") f();
		else eval(f); 
	}
};

/***********************************************************************/
/*              Troca foco para o 'id' componente		               */
/***********************************************************************/
function trocaFoco(id,e){
	if(e.keyCode==13){		
		if(getElm(id).select){
			getElm(id).select();
			if(!F_IE) getElm(id).focus();
		}
		else getElm(id).focus();
		return false;
	}
};

/***********************************************************************/
/*              Posicao top de um objeto fixo			               */
/***********************************************************************/
function getTopPos(o){
	var r=o.offsetTop;
	while((o=o.offsetParent)!=null)
		if(o.tagName!='HTML') r+=o.offsetTop;
	return r;
};

/***********************************************************************/
/*              Posicao left de um objeto fixo			               */
/***********************************************************************/
function getLeftPos(o){
   var r=o.offsetLeft;
   while((o=o.offsetParent)!=null)
      if(o.tagName!='HTML') r+=o.offsetLeft;
  return r;
};

/***********************************************************************/
/*              Valida datas no formato dd/mm/yyyy		               */
/***********************************************************************/
function validaData (e) {  
	var data=e.split('/');
	var dia=data[0];
   	var mes=data[1];
   	var ano=data[2];

	if(isNaN(dia)||isNaN(mes)||isNaN(ano)) return false;
	var d=new Date(ano,(mes-1),dia);
	var diaT=d.getDate();	
	var mesT=parseInt(d.getMonth())+1;

	if((dia!=diaT)||(mes!=mesT)) return false;
	return true;
};
function toDate(d){
	d=d.split('/');
	var data=new Date(d[2],(d[1]-1),d[0]);
	return data;
};

/*********************************************************************/
/*       Retorna todos os dados dos camos 					         */
/*********************************************************************/
function getDataToServer(obj,o){
	var id,i,type,r="";		
	if(!o.notSelect)
		for(i=0;i<obj.getElementsByTagName('select').length;i++){										
			r+="&"+obj.getElementsByTagName('select')[i].id;
			r+="="+obj.getElementsByTagName('select')[i].value;
		}						
	for(i=0;i<obj.getElementsByTagName('input').length;i++){
		type=obj.getElementsByTagName('input')[i].type;
		if(type=="button") continue;
		if(type=="text" && o.notText) continue;
		if(type=="radio" && o.notRadio) continue;
		if(type=="checkbox" && o.notCheckbox) continue;		
		r+="&"+obj.getElementsByTagName('input')[i].id;
		if(type=="checkbox") r+="="+obj.getElementsByTagName('input')[i].checked;
		else r+="="+url_encode(obj.getElementsByTagName('input')[i].value);
	}			
	return r;
}