function get_radio_value(id){
	try {
		var options = document.getElementsByName(id);
		for(var i=0; i<options.length; i++){
			if(options[i].checked)
				return options[i].value;
		}
	}catch(e){
		alert("ERRO: get_radio_value: "+id+"/"+e);
	}
	return null;
}

function get_checkbox_values(id){
	var values		= [];
	try {
		var collection = document.getElementsByName(id);
		for(var i=0; i<collection.length; i++){
			if(collection[i].checked)
				values.push(collection[i].value);
		}
	}catch(e){
		alert("ERRO: get_checkbox_values: "+id+"/"+e);
	}
	return values.join(",");
}

function array_search(obj,value){
	try {
		for(var i=0; i<obj.length; i++){
			if(obj[i]==value)
				return true;
		}
	}catch(e){
		alert("ERRO: array_search: "+obj+"/"+value+"/"+e);
	}
	return false;
}

function set_checkbox_values(id,values){
	try {
		var options = document.getElementsByName(id);
		for(var i=0; i<options.length; i++)
			options[i].checked = array_search(values,options[i].value); // stupid ie ...
	}catch(e){
		alert("ERRO: set_checkbox_values: "+id+"/"+values+"/"+e);
	}
}

function set_opacity(id,opacity){
	var element = document.getElementById(id);
	if (navigator.userAgent.indexOf("MSIE")!=-1){
		var normalized = Math.round(opacity*100);
		element.style.filter = "alpha(opacity=" + normalized + ")";
	}else{
		element.style.opacity = opacity;
	}
}

function reset_all_option(id){
	try {
		var collection = document.getElementsByName(id);
		for(var i=0; i<collection.length; i++){
			if(collection[i].value=="#")
				collection[i].checked=false;
		}
	}catch(e){
		alert("ERRO: reset_all_option: "+id+"/"+e);
	}
}

function reset_other_option(id){
	try {
		var collection = document.getElementsByName(id);
		for(var i=0; i<collection.length; i++)
			collection[i].checked = collection[i].value=="#";
	}catch(e){
		alert("ERRO: reset_other_option: "+id+"/"+e);
	}
}

function ReverseDisplay(d) {
	if(document.getElementById(d).style.display == "none") { 
		document.getElementById(d).style.display = "block";
		document.getElementById(d + "img").src = "http://static.vericia.com/img/BulletPreto2.gif";
	}else{ 
		document.getElementById(d).style.display = "none";
		document.getElementById(d + "img").src = "http://static.vericia.com/img/BulletPreto.gif";
	}
}

function get_elements_by_class_name(obj,name){
   if(obj.getElementsByClassName)
	  return obj.getElementsByClassName(name);
   var rtn = [];
	try {
		var col = obj.childNodes;
		var len = col.length;
		var cur;
		for(var i=0; i<len; i++){
		  cur = col[i];
		  if(!cur.className)
			 continue;
		  if(cur.className==name)
			 rtn.push(cur);
		}
	}catch(e){
		alert("ERRO: get_elements_by_class_name: "+obj+"/"+name+"/"+e);
	}
   return rtn;
}

function find_pos(id) {
	  var curleft = curtop = 0;
	  var obj = document.getElementById(id);
	  if(!obj)
		 return [0,0];
	  
	  if(obj.offsetParent){
		 do{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		 }while(obj = obj.offsetParent);
	  }
   return [curleft,curtop];
}

function screen_size() {
	try {
		if(typeof(window.innerWidth)=='number')
			return [window.innerWidth,window.innerHeight];
		else if(document.documentElement && (document.documentElement.clientWidth||document.documentElement.clientHeight))
			return [document.documentElement.clientWidth,document.documentElement.clientHeight];
		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		  return [document.body.clientWidth,document.body.clientHeight];
	}catch(e){
		alert("ERRO: screen_size: "+e);
	}
	return [0,0];
}

function scroll_pos(){
	try {
		if(typeof(window.pageYOffset) != 'undefined')
			return [window.pageXOffset,window.pageYOffset];
		else if(typeof document.documentElement.scrollTop != 'undefined' && document.documentElement.scrollTop > 0)
			return [document.documentElement.scrollLeft,	document.documentElement.scrollTop];
		else if(typeof document.body.scrollTop != 'undefined')
			return [document.body.scrollLeft,document.body.scrollTop];
	}catch(e){
		alert("ERRO: scroll_pos: "+e);
	}
	return [0,0];
}

function reverse(s){
	var nstr = [];
	try {
		for(var i=s.length-1; i>=0; i--)
			nstr.push(s[i]);
	}catch(e){
		alert("ERRO: reverse: "+s+"/"+e);
	}
	return nstr.join("");
}

function formata_milhar(n){
	var ar	= [];
	try {
		var rest = n.length%3;
		var div	= Math.floor(n.length/3);
		if(rest>0){
			ar.push(n.substring(0,rest));
			n = n.substring(rest);
		}
		for(var i=0; i<div; i++){
			ar.push(n.substring(0,3));
			n = n.substring(3);
		}
	}catch(e){
		alert("ERRO: formata_milhar: "+n+"/"+e);
	}
	return ar.join(".");
}

function formata_moeda(n){
	var cents = /\.\d{2}$/.exec(n);
	cents = cents ? cents[0].replace(".",",") : ".00";
	return formata_milhar(n.replace(/\.\d{1,2}$/,""))+cents;
}

function select_options(id,b){
	var obj = document.getElementById(id);
	if(!obj)
		return;
	try {
		var options = obj.options;
		for(var i=0; i<options.length; i++)
			options[i].selected = b;
	}catch(e){
		alert("ERRO: select_options: "+id+"/"+b+"/"+e);
	}
}

function in_array(ar,val){
	try {
		for(var i=0; i<ar.length; i++){
			if(ar[i]==val)
				return true;
		}
	}catch(e){
		alert("ERRO: in_array: "+ar+"/"+val+"/"+e);
	}
	return false;
}

function find_array_pos(ar,val){
	try {
		for(var i=0; i<ar.length; i++){
			if(ar[i]==val)
				return i;
		}
	}catch(e){
		alert("ERRO: find_array_pos: "+ar+"/"+val+"/"+e);
	}
	return -1;
}
