/*Riempe il comboBox
Parametri : idtag  e arrayOpzioni e valori 
text = testo primo item "Select .."
nascondi = true  (se l'array è vuoto il combo non viene visualizzato) */ 
function riempiCombo(idtag, arrayOpzioni , arrayValori, text  , nascondi){
if ( (arrayOpzioni.length == 0) && (nascondi) ) {
	idtag.style.display = "none";
}else{
	idtag.style.display = "block";
}
while (idtag.options.length > 0){
	idtag.options[(idtag.options.length - 1)] = null;
}
idtag.options[0]= new Option( text , '');
for (var i=1; i < (arrayOpzioni.length + 1); i++) {
	idtag.options[i]= new Option ( arrayOpzioni[(i-1)] , i);
	idtag.options[i].value = arrayValori [(i-1)];
}
}

function changeText(idtag, elem, text) {
 var idtag = document.getElementById (idtag);
 var myText = document.createElement(elem);
 idtag.innerHTML = text;
 if (idtag.hasChildNodes())
 {
	idtag.replaceChild (myText, idtag.firstChild);
 } else {
	idtag.appendChild (myText);
 }
}

/************ SHOW TOOLTIP ********/
function showTooltip(srcObj,message,distanzaX,distanzaY) {
	var pos;
	pos=findPos(srcObj);
	tooltip.style.left=pos[0] + srcObj.offsetWidth + distanzaX;
	tooltip.style.top=pos[1] + distanzaY;
	tooltip.style.display="block";
	tooltiptext.innerHTML=message;
}
//non tengo conto del div in cui sto ma prendo la posizione del mouse
function showTooltip2(message,distanzaX,distanzaY,e) {
	var pos;
	pos=PosizioneCursore(e);
	tooltip.style.left=pos[0]+ distanzaX;
	tooltip.style.top=pos[1]+ distanzaY;
	tooltip.style.display="block";
	tooltiptext.innerHTML=message;
}

function hideTooltip() {
	tooltip.style.display="none";	
}
	
function findPos(obj) {

	var curleft = curtop = 0;
		if (obj.offsetParent) {
			curleft = obj.offsetLeft;
			curtop = obj.offsetTop;
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}
		return [curleft,curtop];
}


function PosizioneCursore(e){
	var asse_x = (e.pageX||e.clientX||0);
	var asse_y = (e.pageY||e.clientY||0);
	if (typeof e.pageX=="undefined"){
		if (document.documentElement&&	(document.documentElement.scrollTop||document.documentElement.scrollLeft) ){
			asse_x+=document.documentElement.scrollLeft;
			asse_y+=document.documentElement.scrollTop;
		}else if (document.body&&(document.body.scrollTop||document.body.scrollHeight)){
			asse_x+=document.body.scrollLeft;
			asse_y+=document.body.scrollTop;
		}
	}
	return [asse_x , asse_y];
}



/********************************/

// Firefox worked fine. Internet Explorer shows scrollbar because of frameborder
//passare min_height = 0 se non si vuole un'altezza minima
function resizeFrame(f , min_height) {
   if (at_checkbrowser('FIREFOX','') ){
		new_height = f.contentWindow.document.body.parentNode.clientHeight ;
   } else if (at_checkbrowser('SAFARI','') ){
		new_height = f.contentWindow.document.body.offsetHeight ;
   } else {
		new_height = f.contentWindow.document.body.scrollHeight ;
   }
   if (new_height < min_height)
   new_height = min_height;
   
   new_height += 80;
   f.style.height = new_height + "px";
}


//Visualizza/Nasconde tutti gli elementi con nome  = name
//display_mode = "none" || display
function visualHide_ElementsByName(name , display_mode ){
	v = document.getElementsByName(name);
	for (i=0; i< v.length ; i++){
		v[i].style.display = display_mode;	
	}
}

//Visualizza/Nasconde tutti gli elementi con nome  = name
//display_mode = "none" || display
function visualHide_ElementsByTagName( tag, name , display_mode ){
	v = getElementsByName_iefix( tag , name);
	for (i=0; i< v.length ; i++){
		v[i].style.display = display_mode;	
	}
}


function getElementsByName_iefix(tag, name) {
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }
     return arr;
}


function visualHide_ElementById (id , display_mode){
// Nasconde/esplode il div con id= id
//se passo display_mode ha priorità sennò controllo se è visibile o non e faccio l'azione contraria
	if (display_mode == 'y'){
		display = "block";
	}else if (display_mode == 'n') {
		display = "none";
	}else{
		if (document.getElementById(id).style.display == "none"){ display="block"; }
		else {	display="none";}
	}
	document.getElementById(id).style.display = display;
	return display; //ritorno per fare il controllo 
}

// Funzione per selezionare solo alcuni checkbox passando il nome della form e il nome chcekbox = "nome[]"
function check_box_select(formname , checkbox_name , checked ) {
 
  for (var i = 0; i < formname.elements.length; i++) {
   if((formname.elements[i].type == 'checkbox') && (formname.elements[i].name == checkbox_name)) {
     formname.elements[i].checked = checked;
	 } 
   }
}

//Controllo se è stato selezionato almeno un checkbox "name[]" 
//true se è stato selezionato
//alert (message) e torna false se non è stato selezionato nessun elemento
function checkbox_checker(nome , message)
{
    var a_checkbox = document.getElementsByName(nome);
	for (counter = 0; counter < a_checkbox.length; counter++)
	{
		// If a checkbox has been selected it will return true
		// (If not it will return false)
		if ( a_checkbox[counter].checked)
		{ 
			return true; 
		}
	}
	if(message!='')
	alert(message);
		
	return false;
}


// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

