/**
  * functions.js
  * this file contains all functions used in the site
  */
 
var oMenuAtivo=null, sSubMenuAtivo=null, iTempoMenu=null;
var c_IsNN4=false, c_IsOpera7=false, c_IsOpera5or6=false;
var c_IsMSIE=false, c_IsMac=false, c_IsMoz=false, c_IsSafari=false;
var c_UA=navigator.userAgent.toLowerCase();
var iframe_alvo, tempo_resize;

if(window.opera){
  c_IsOpera7=(c_UA.indexOf('opera 7')!=-1 || c_UA.indexOf('opera/7')!=-1);
  if(!c_IsOpera7) c_IsOpera5or6=(c_UA.indexOf('opera 5')!=-1 || c_UA.indexOf('opera/5')!=-1 || c_UA.indexOf('opera 6')!=-1 || c_UA.indexOf('opera/6')!=-1);
}
else if(document.all && c_UA.indexOf('msie')!=-1) c_IsMSIE=true;
else if(document.layers) c_IsNN4=true;

c_IsSafari=c_UA.indexOf('safari')!=-1;
c_IsMoz=(c_UA.indexOf('gecko')!=-1 && !c_IsSafari);
c_IsMac=c_UA.indexOf('mac')!=-1;

function xGetElementById(e){
  if(typeof(e)!='string') return e;
  if(document.getElementById) e=document.getElementById(e);
  else if(document.all) e=document.all[e];
  else e=null;
  return e;
}

function xGetContainerWith(node, tagName, className){
  while(node!=null){
    if (node.tagName!=null && node.tagName==tagName && xHasClassName(node, className)) return node;
    node = node.parentNode;
  }
  return node;
}

function xHasClassName(el, name){
  var i, list;

  list=el.className.split(" ");
  for(i=0; i<list.length; i++)
    if(list[i]==name) return true;
  return false;
}

function xAtivaMenu(oEle, sSubMenu){
    var sTagAtivo=(oMenuAtivo!=null)?oMenuAtivo.nodeName:null;
    var sIdAtivo=(oMenuAtivo!=null)?oMenuAtivo.id:null;
    var sTagEle=(oEle!=null)?oEle.nodeName:null;
    var sIdEle=(oEle!=null)?oEle.id:null;
  if(sTagAtivo==sTagEle && sIdAtivo==sIdEle){
    if(iTempoMenu!=null){
      clearTimeout(iTempoMenu);
      iTempoMenu=null;
    }
    return false;
  }
  else{
    if(iTempoMenu!=null) clearTimeout(iTempoMenu);
    xDesativaMenuExec();
    oMenuAtivo=oEle;
    if(sSubMenu){
      sSubMenuAtivo=sSubMenu;
      MM_showHideLayers(sSubMenu,'','show');
    }
    return true;
  }
}

function xDesativaMenu(){
  var iTempoAux=(c_IsSafari)?500:200;
  
  if(iTempoMenu!=null){
    clearTimeout(iTempoMenu);
    xDesativaMenuExec();}
  iTempoMenu=setTimeout('xDesativaMenuExec();', iTempoAux);
  return true;
}

function xDesativaMenuExec(){
  if(sSubMenuAtivo!=null) MM_showHideLayers(sSubMenuAtivo,'','hide');
  sSubMenuAtivo=null;
  oMenuAtivo=null;
  iTempoMenu=null;
}

function xTeste(evnt, oWindow){
  if(window.event) evnt=window.event;
	if(evnt.layerX<1 || evnt.layerX>700) alert(evnt.layerX);
	if(evnt.layery<1) alert(evnt.layerY);
}

function xMenuBlur_will(evnt, sTag, sTagId, oWindow){
  if(window.event) evnt=window.event;
  var tg=(!evnt.target)?evnt.srcElement:evnt.target;
  var reltg=(!evnt.target)?evnt.toElement:evnt.relatedTarget;
  if(typeof(tg)!='object') return false;
  if (reltg){
    while(reltg!=tg && reltg.nodeName!='HTML'){
      reltg=reltg.parentNode;}
    if(reltg==tg) return false;
  }
  else if(tg.nodeName!=sTag || tg.id!=sTagId) return false;
  return true;
}

function xMenuBlur(evnt, sTag, sTagId){
  if(window.event) evnt=window.event;
  var tg=(!evnt.target)?evnt.srcElement:evnt.target;
  var reltg=(!evnt.target)?evnt.toElement:evnt.relatedTarget;
  if(typeof(tg)!='object') return false;
  if (reltg){
    while(reltg!=tg && reltg.nodeName!='HTML'){
      reltg=reltg.parentNode;}
    if(reltg==tg) return false;
  }
  else if(tg.nodeName!=sTag || tg.id!=sTagId) return false;
  return true;
}

function xWidth(iframeWindow){
  if(c_IsMac && c_IsMoz) return iframeWindow.document.body.scrollWidth;
  if(c_IsMac && !c_IsMoz) return iframeWindow.document.documentElement.offsetWidth;
  if(typeof(iframeWindow.document)) return iframeWindow.document.body.scrollWidth;
  if(iframeWindow.document){
    if(iframeWindow.document.height) return iframeWindow.document.body.scrollWidth;
  }
  else if(document.all){
    if(iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat')
      return iframeWindow.document.documentElement.scrollWidth;
    else return iframeWindow.document.body.scrollWidth;
  }
}

function xHeight(iframeWindow){
	if(iframeWindow.name=='bosta') {
		alert(iframeWindow.name);
		alert(iframeWindow.document.body.scrollHeight);
		alert(iframeWindow.document.documentElement.offsetHeight);
		alert(iframeWindow.document.documentElement.scrollHeight);
	}
  if(c_IsMac && c_IsMoz) return iframeWindow.document.body.scrollHeight;
  if(c_IsMac && !c_IsMoz) return iframeWindow.document.documentElement.offsetHeight;
  if(typeof(iframeWindow.document)) return iframeWindow.document.body.scrollHeight;
  if(iframeWindow.document){
    if(iframeWindow.document.height) return iframeWindow.document.body.scrollHeight;
  }
  else if(document.all){
    if(iframeWindow.document.compatMode && iframeWindow.document.compatMode != 'BackCompat')
      return iframeWindow.document.documentElement.scrollHeight;
    else return iframeWindow.document.body.scrollHeight;
  }
}

function adjustIFrameSize(){
  if(arguments.length<1) return;
  var iframeWindow=arguments[0];
  var iframeElement=xGetElementById(iframeWindow.name);
  var iAlt=xHeight(iframeWindow);
  iframeElement.style.height=iAlt+'px';

  if(c_IsMoz) iframeElement.style.height=xHeight(iframeWindow)+2+'px';
  
  if(arguments.length==2) parent.adjustIFrameSize(arguments[1]);
}

function xLoadUrl(sFrame, sUrl){
  var iframeElement=xGetElementById(sFrame);
  iframeElement.src=sUrl;
}

function iframe_resize(sIframe, hfinal, wfinal){
  iframe_alvo=xGetElementById(sIframe);
  hfinal=parseInt(hfinal);
  wfinal=parseInt(wfinal);
  if(isNaN(hfinal)) hfinal=0;
  if(isNaN(wfinal)) wfinal=0;
  if(tempo_resize!=null){
    clearInterval(tempo_resize);
    tempo_resize=null;
  }
  tempo_resize=setInterval("exec_iframe_resize("+hfinal+", "+wfinal+")",1);
}

function exec_iframe_resize(hfinal, wfinal){
  AltTotal=(hfinal==0)?parseInt(iframe_alvo.style.height):hfinal;
  if(isNaN(AltTotal)) AltTotal=0;
  CompTotal=(wfinal==0)?parseInt(iframe_alvo.style.width):wfinal;
  if(isNaN(CompTotal)) CompTotal=0;
  AltNovo=isNaN(parseInt(iframe_alvo.style.height))?0:parseInt(iframe_alvo.style.height);
  CompNovo=isNaN(parseInt(iframe_alvo.style.width))?0:parseInt(iframe_alvo.style.width);
  AltOffSet=Math.round((AltTotal-AltNovo)*.5);
  CompOffSet=Math.round((CompTotal-CompNovo)*.5);
  AltTam=AltNovo+AltOffSet;
  CompTam=CompNovo+CompOffSet;
  if(hfinal!=0) iframe_alvo.style.height=(AltOffSet==0)?AltTotal+'px':AltTam+'px';
  if(wfinal!=0) iframe_alvo.style.width=(CompOffSet==0)?CompTotal+'px':CompTam+'px';
  if((AltOffSet==0) && (CompOffSet==0)){
    clearInterval(tempo_resize);
    tempo_resize=null;
  }
}

function MM_reloadPage(init) {  //reloads the window if Nav4 resized
	if (init==true) with (navigator) {
		if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
			document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; 
		}
	}
	else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.0
	var p,i,x;
	if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
		d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_showHideLayers() { //v3.0
	var i,p,v,obj,args=MM_showHideLayers.arguments;
	for (i=0; i<(args.length-2); i+=3) {
		if ((obj=MM_findObj(args[i]))!=null) {
			v=args[i+2];
			if (obj.style) {
				obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v;
			}
			obj.visibility=v;
		}
	}
}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


// used in onLoad if set popup in home
function openPopUp(id, height, width) {
	onloadPopUp = window.open("/popup.php?popup_id="+id,"Confira","toolbar=no,location=no,scrollbars=no,width="+width+",height="+height+",left=25,top=25");
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
	window.open(theURL,winName,features);
}

function submitForm(formName, formMethod, formAction, formTarget, formVars){
	var objFrm=document.createElement('form');
	
	objFrm.id=formName;
	objFrm.name=formName;
	
	objFrm.method=formMethod;
	objFrm.action=formAction;
	objFrm.target=formTarget;
	
	var param, i, j;
	var HTMLVars='';
	var params=formVars.split('&');
	
	for(i=0; i<params.length; i++){
		param=params[i].split('=');
		if(param.length>=2) {
			HTMLVars+="<input type='hidden' name='"+param[0]+"' value='"+param[1]
			for(j=2; j<param.length; j++) HTMLVars+="="+param[j];
			HTMLVars+="'>\n";
		}
	}
	
	objFrm.innerHTML=HTMLVars;
	
	document.body.appendChild(objFrm);
	
	objFrm.submit();
}