var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);
var is_minor = parseFloat(navigator.appVersion);
var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
var is_nav6up = (is_nav && (is_major >= 5));
var is_gecko = (agt.indexOf('gecko') != -1);
var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) && (document.all);
var is_ie6 = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.")!=-1) );
var is_ie7 = (is_ie && (agt.indexOf("msie 7.")!=-1) || (agt.indexOf("msie 8.")!=-1)); // or 8
var is_ie5up = is_ie6 || is_ie7;
var is_op = ((agt.indexOf("msie") == -1) && (document.all) && (agt.indexOf("opera") != -1)) && !is_ie;
var is_safari = (agt.indexOf("safari") != -1);

var fieldcolor = "#ffffff"; // checkfields
var invalidcolor = "#FFC0C0"; // checkfields
// ------------------------------------------
/*
function addEvent(o,n,f,l){ var a='addEventListener',h='on'+n,b='',s=''; if(o[a]&&!l) return o[a](n,f,false); o._c|=0; if(o[h]){ b='_f'+o._c++; o[b]=o[h] } s='_f'+o._c++; o[s]=f; o[h]=function(e){ e=e||window.event; var r=true; if(b) r=o[b](e)!=false&&r;   try {      r=o[s](e)!=false&&r;   } catch(ex) {     r = false;   }   return r }; aeOL[aeOL.length]={o:o,h:h} }
var aeOL=[]; addEvent(window,'unload',function() { for (var i=0;i<aeOL.length;i++) { with(aeOL[i]) { o[h]=null; for(var c=0;o['_f'+c];c++) o['_f'+c]=null }}});
*/
function addEvent(o,n,f) {
	Event.observe(o,n,f);
}
function selectall(para,filter) {
  frm = document.frmbase;
  for (var i=0;i<frm.elements.length;i++) {
    if ((!filter && frm.elements[i].id.substring(0,2) == "id") || (filter && frm.elements[i].id.indexOf(filter) != -1)) {
      if (!frm.elements[i].disabled) frm.elements[i].checked = para;
    }
  }
}
function nop() {
}
function ismail( oMail ) {
	return ereg(oMail,"^[A-Za-z0-9]+(([_\.\-]?[a-zA-Z0-9]+(_)?)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$");
}
function islogin(oValor,allowSpace) {
	return ereg(oValor,allowSpace?"^([ A-Za-z0-9_\-]){4,20}$":"^([A-Za-z0-9_\-]){4,20}$");
}
function hasmso(texto) {
return ereg(texto,"(class=\"MsoNormal\")|(style=\"mso\-)");
}
function isnumber( oValor , accept_commas ) {
if (!accept_commas)
  MyRegExp = new RegExp("^(\-)?([0-9]*)$");
else
  MyRegExp = new RegExp("^(\-)?([0-9]*)(([,\.])([0-9]{3}))*(([,\.]{1})([0-9]*))?$");
x = MyRegExp.test(oValor);
return x;
}
function parseajax(ajaxobj) {
  var texto=ajaxobj.responseText;
  texto=texto.replace(/\+/g," ");
  return unescape(texto);
}

function ToggleHidden(obj,img,path) {
  	obj = $(obj);
  	if (img) img = $(img);
  	if (obj.style.display == "none") {
  		try {
  			new Effect.BlindDown(obj);
  		} catch (ee) {
  			obj.style.display = '';
  		}
  		if (img) img.src = path+"smcontract.gif";
  	} else {
  		try {
	  		new Effect.BlindUp(obj);
	  	} catch (ee) {
	  		obj.style.display = 'none';
	  	}
  		if (img) img.src = path+"smexpand.gif";
  	}
}
function str_replace(what,to,into,maxreplaces) {
antiloop = 0;
if (!maxreplaces) maxreplaces = 100;
while (into.indexOf(what)!=-1 && antiloop++<=maxreplaces) {
into = into.substring(0,into.indexOf(what)) + to + into.substring(into.indexOf(what) + what.length);
}
return into;
}
function cleanamps (texto) { // para W3C validator não amolar
  return str_replace("&amp;","&",texto,10);
}
function ereg(texto,mascara) {
	MyRegExp = new RegExp(mascara);
	return MyRegExp.test(texto);
}
function limpaString(S,modo){
  // Deixa so' os digitos no numero
  var Digitos = " 0123456789" + (modo?".,-":"");
  var temp = "";
  var digito = "";
  for (var i=0; i<S.length; i++)	{
    digito = S.charAt(i);
    if (Digitos.indexOf(digito) >= 0)	{
      temp=temp+digito;
    }
  }
  return temp;
}
function validaCGC(s) {
  var i;
  var s = limpaString(s);
  if (s.length < 14) return false;
  var c = s.substr(0,12);
  var dv = s.substr(12,2);
  var d1 = 0;
  for (i = 0; i < 12; i++) {
    d1 += c.charAt(11-i)*(2+(i % 8));
  }
  if (d1 == 0) return false;
    d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(0) != d1) {
    return false;
  }
  d1 *= 2;
  for (i = 0; i < 12; i++) {
    d1 += c.charAt(11-i)*(2+((i+1) % 8));
  }
  d1 = 11 - (d1 % 11);
  if (d1 > 9) d1 = 0;
  if (dv.charAt(1) != d1) {
    return false;
  }
  return true;
}
function validaCPF(cpf) {
  var i; 
  var s = cpf; 
  if (s.length < 11) return false;
  var c = s.substr(0,9); 
  var dv = s.substr(9,2); 
  var d1 = 0; 
  for (i = 0; i < 9; i++) { 
    d1 += c.charAt(i)*(10-i); 
  } 
  if (d1 == 0) {
    return false; 
  } 
  d1 = 11 - (d1 % 11); 
  if (d1 > 9) d1 = 0; 
  if (dv.charAt(0) != d1) { 
    return false; 
  } 
  d1 *= 2; 
  for (i = 0; i < 9; i++) { 
    d1 += c.charAt(i)*(11-i); 
  } 
  d1 = 11 - (d1 % 11); 
  if (d1 > 9) d1 = 0; 
  if (dv.charAt(1) != d1) { 
    return false; 
  } 
  return true; 
} 
function checkmailfield(campo,bgc) {
  if (!bgc || bgc=="") bgc = fieldcolor;
  if (!ismail(campo.value)) {
    campo.style.background= invalidcolor;
    return false;
  } else {
    campo.style.background= bgc;
    return true;
  }
}
function checknbrfield(campo,ponto,bgc) {
  if (!bgc || bgc=="") bgc = fieldcolor;
  if (!isnumber(campo.value,ponto)) {
    campo.style.background= invalidcolor;
    return false;
  } else {
    campo.style.background=bgc;
    return true;
  }
}
var lastlogin = "";
freelogin = true;
function checklogin(campo,bgc,imgField,allowSpace) {
  campo = $(campo);
  if (!bgc || bgc=="") bgc = fieldcolor;
  if ( islogin(campo.value,allowSpace) ) {
    campo.style.background=bgc;
    if (imgField && imgField != "") {
    	if (campo.value != lastlogin) {
    		if (freelogin) {
    			freelogin = false;
		    	new Ajax.Updater($(imgField),'/ajaxlogin.php?layout=2&login='+campo.value, {asynchronous:false, onComplete:resetChecklogin} );
    			lastlogin = campo.value;
    		} else {
    			setTimeout("function() { checklogin('"+campo.id+"','"+bgc+"','"+imgField+"','"+(allowSpace?"true":"false")+"')}",1000);
    		}
    	}
    } 
    return true;
  } else {
    campo.style.background= invalidcolor;
    return false;
  }
}
function resetChecklogin(data) {
  freelogin = true;
}
function checkdatetime(campo,canDate,canTime,bgc,mandatory) {
	if (!bgc || bgc=="") bgc = fieldcolor;
	if (canDate) {
		if (canTime)
			ok = ereg(campo.value,"^( )*(([0-9]{1,2})([^0-9])){4,5}([0-9]{2,4})( )*$"); // s is optional
		else
			ok = ereg(campo.value,"^( )*(([0-9]{1,2})([^0-9])){2}([0-9]{2,4})( )*$");
	} else {
		ok = ereg(campo.value,"^( )*(([0-9]{1,2})([^0-9])){1,2}([0-9]{2})( )*$"); // s is optional
	}
	ok = ok || (!mandatory && campo.value == '');
	if (ok) {
		campo.style.background=bgc;
		return true;
	} else {
    	campo.style.background= invalidcolor;
	    return false;
  	}    
}
function checkid(campo,accept_cpf,accept_cnpj,bgc) {  
  if (!bgc || bgc=="") bgc = fieldcolor;
  campo.value = limpaString(campo.value,false);
  if ( (accept_cpf && validaCPF(campo.value)) ||
       (accept_cnpj && validaCGC(campo.value))
      ) {
    campo.style.background=bgc;
    return true;
  } else {
    campo.style.background= invalidcolor;
    return false;
  }    
}
function tratajs(dados) {
  dados = " " + dados + " ";
  posa = dados.indexOf("<SCRIPT>");
  posb = dados.indexOf("</SCRIPT>");
  if (posa>0 && posb>0) {
    js = dados.substring(posa+8,posb);
    if (js != "") {
      eval(js);
      return true;
    }
  }
  return false;
}
function str_count(what,where) {
	var temp = where;
	localizados = 0;
	while (temp.indexOf(what) != -1 && localizados++<100) {
		temp = temp.substring(temp.indexOf(what) + what.length);
	}
	return localizados;
}
function addText(textarea_obj,text){
  sucess = true;
  try {
		if (document.selection) {
			textarea_obj.focus();
			sel = document.selection.createRange();
			sel.text = text;
			textarea_obj.focus();
		}
		else if (textarea_obj.selectionStart || textarea_obj.selectionStart == '0') {
			var startPos = textarea_obj.selectionStart;
			var endPos = textarea_obj.selectionEnd;
			var cursorPos = endPos;
			var scrollTop = textarea_obj.scrollTop;
			if (startPos != endPos) {
				textarea_obj.value = textarea_obj.value.substring(0, startPos) + text + textarea_obj.value.substring(endPos, textarea_obj.value.length);
				cursorPos = startPos + text.length;
			}
			else {
				textarea_obj.value = textarea_obj.value.substring(0, startPos)  + text + textarea_obj.value.substring(endPos, textarea_obj.value.length);
				cursorPos = startPos + text.length;
			}
			textarea_obj.focus();
			textarea_obj.selectionStart = cursorPos;
			textarea_obj.selectionEnd = cursorPos;
			textarea_obj.scrollTop = scrollTop;
		}
		else {
			textarea_obj.value += text;
		}
	} catch (ee) { // if textarea is not visible will cause error because of selection parameters
		sucess = false;
	}
	if (!sucess) { // so in that case we just add the text
	  try {
	    textarea_obj.value = textarea_obj.value + text;
	  } catch (ee) {
	  }
	}
}
function findPosX(obj) {
   	var curleft = 0;
   	if(obj.offsetParent) {
       	while(true) {
         		curleft += obj.offsetLeft;
         		if(!obj.offsetParent)
           		break;
         		obj = obj.offsetParent;
       	}
   	} else if(obj.x)
       	curleft += obj.x;
   	return curleft;
 	}
function findPosY (obj) {
   	var curtop = 0;
   	if(obj.offsetParent) {
       	while(true) {
         		curtop += obj.offsetTop;
         		if(!obj.offsetParent)
           		break;
         		obj = obj.offsetParent;
      		}
   	} else if(obj.y)
       	curtop += obj.y;
   	return curtop;
}

menuMarkOpacity = 1; // change to make the dropdown transparent
menuSmooth = false; // change to true to make use of scriptaculous blind function
menuHideTime = 500; // ms
menuMarkhover = new Array(); // hover menu system
menuHoverID = 0; // which menu am I over?
// hoverbtn => onmouseover="hovermenu([i],true)" onmouseout="hovermenu([i],false,true)"
// hoverdiv => id="divm[i]" onmouseover="hovermenu([i],true)" onmouseout="hovermenu([i],false,true)"
// NOTE: display:none at the divs must be in the STYLE not in the CLASS
function hovermenu(id,mode,timed,auto) {
	// mode = true/false 		Wants to show or hide
	// timed = true/fase		This was a timed event (wait a while to perform)
	// auto = true/fase			Triggered by the script and not an event
	
	var action = "";
	if (!menuMarkhover[id])
		menuMarkhover[id] = "0"; // default not shown
	var visible = menuMarkhover[id] == "1";
	
	if (mode) menuHoverID = id;
	
	if (mode && !visible) { // want to show, and its hidden
		action = "S"; // show
	}
	if (!mode && visible) { // want to hide, but its still shown
		if (!auto) { // not automatic
			menuHoverID = 0; // Im off any menu
			if (timed) setTimeout("hovermenu("+id+",false,false,true)",menuHideTime); 
			else action = "H";
		} else if (id != menuHoverID) // timed hide and I am no longer at this menu
			action = "H";
	}
	if (mode && menuMarkOpacity < 1) {
		Element.setOpacity($('divm'+id), menuMarkOpacity);
	}
	if (action != "") {
		if (!visible && action == "S") {
			if (menuSmooth) {
				new Effect.BlindDown('divm'+id);
			} else {
				$('divm'+id).style.display = '';
			}
			menuMarkhover[id] = "1";
		} else if (visible) {		
			$('divm'+id).style.display = 'none';
			menuMarkhover[id] = "0";
		}
	}
	
}
function explode(separator,items) {
	var output = new Array();
	var size = items.length;
	var outputpos = 0;
	var buffer = '';
	for (var c=0;c<size;c++) {
		if (items.charAt(c) == separator) {
			output[outputpos] = buffer;
			outputpos++;
			buffer = '';
		} else
			buffer += items.charAt(c);
	}
	if (buffer != '') {
		output[outputpos] = buffer;
	}
	return output;
}
function activateFlash() { // Microsoft IE Eolas patent issue
	E=document.getElementsByTagName('object');
	for(i=0;i<E.length;i++){
		P=E[i].parentNode;
		H=P.innerHTML;
		P.removeChild(E[i]);
		P.innerHTML=H;
	}
}
//if (is_ie) Event.observe(window,'load',function(){activateFlash()});
