//indicizza gli elementi attivi della pagina web
function indexActiveDocumentElements(document,form,indexAttrName){
   if (indexAttrName==null){
     	indexAttrName = "customTabIndex";
   }
   idx=0;
   for (var i=0; i < document.all.length; i++){
       elem = document.all[i];
       if (isActiveElement(elem) && !isHiddenElement(elem)){
         idx++;
         elem[indexAttrName]=idx;
       }
   }
}

	
//il flag "isEnabledCheckContainerTag" abilita nella funzione skipTabOfReadonlyElement
//in esecuzione all'evento onload della pagina il controllo di eventuali tag di tipo "SPAN" o "DIV"
//non visibili contenenti campi editabili o pulsanti attivi
//il flag "isEnabledCheckContainerTag" deve essere impostato solo su quelle pagine che prevedono
//campi e/o pulsanti presenti all'internodi tag di tipo "SPAN" o "DIV"
//Es. rataContanteESuccessiva.bodyArea.jsp
isEnabledCheckContainerTag = false;

//ottiene la lista degli elementi attivi della pagina web
function getActiveDocumentElements(document,form){
   elements = new Array();
   for (var i=0; i < document.all.length; i++){
       elem = document.all[i];
       //02/10/2007-bolofel
       if (((elem.tagName=="INPUT" && elem.type!="hidden")
            || elem.tagName=="A" || elem.tagName=="SELECT" || elem.tagName=="TEXTAREA") && elem.style.visibility != "hidden"){
		  //13/02/2008-bolofel
          if (isEnabledCheckContainerTag){
             containerTag = getDocumentElementContainerTag(elem);
             if (containerTag==null){
            	elements[elements.length] = elem;
             }else{
				if (containerTag.style.display != "none")
					if(containerTag.style.visibility != "hidden")
						elements[elements.length] = elem;
             }
          }else{
            elements[elements.length] = elem;
          }
       }
   }
   return elements;
}

//span,div
function getDocumentElementContainerTag(element){
	container = element.parentElement;
	containerCount = 0;
	containerMaxCount = 10;
	while (container!=null){
		containerCount++;
		if (containerCount==containerMaxCount){
			return null;
		}
		if (container.tagName == 'DIV' || container.tagName == 'SPAN'){
			return container;
		}
		container = container.parentElement;
	}
	return null;
}

//ottiene il primo elemento attivo della pagina web
function getFirstActiveDocumentElement(elements){
   for (i=0;i<elements.length;i++){
       elem = elements[i];
       if (!elem.readOnly && !elem.disabled){
       	 return elem;
       }
   }
   return null;
}

// il flag "isFocusEnabledForCombobox" nella funzione getFirstActiveDocumentFielElement permette di impostare
// il focus al primo campo, anche se si tratta di una combo (select)
// il flag "isFocusEnabledForCombobox" deve essere impostato solo su quelle pagine in cui
// il primo campo a cui dare il focus č una combobox
//Es. individuazionePolizzaDaVariare.jsp
isFocusEnabledForCombobox = false;

//ottiene il primo campo attivo della pagina web
function getFirstActiveDocumentFieldElement(elements){
  target = null;
   for (i=0;i<elements.length;i++){
       elem = elements[i];
       
       // se il primo campo della pagina č un combobox
       // ed ho abilitato il flag "isFocusEnabledForCombobox"
       // andrņ a metterci il focus
       if(isFocusEnabledForCombobox){
       		if (!elem.readOnly && !elem.disabled && elem.type!="button"
    	   		&& elem.type!="submit" && elem.type!="hidden" && elem.tagName!="A"){
    	   		
    	   			if (elem.style.display!="none")
			       	   if (target==null){
			       	      target=elem;
		       	 	   }
	   	           if (isDocumentFieldElementInError(elem)){
	    	           	return elem;
	   	           }
	       }
      
       }else{
        
       		if (!elem.readOnly && !elem.disabled && elem.type!="button" 
       			&& elem.type!="submit" && elem.type!="hidden" && elem.tagName!="A" && elem.tagName!="SELECT"){
			       if (elem.style.display!="none")
			       	   if (target==null){
			       	      target=elem;
		       	 	   }
	   	           if (isDocumentFieldElementInError(elem)){
	    	           	return elem;
	   	           }
		       }
	       }
	   }
   return target;
}

function isDocumentFieldElementInError(element){
	error = element.nextSibling;
	siblingCount = 0;
	siblingCountMax = 10;
	while (error!=null){
		siblingCount++;
		if (siblingCount==siblingCountMax){
			return false;
		}
		if (error.tagName == 'IMG'){
			if (error.onclick){
			        handler = error.onclick.toString();
				if (handler.indexOf("alert")!=-1){
					return true;
				}
			}
		}
		error = error.nextSibling
	}
	return false;
}

//ottiene l'ultimo elemento attivo della pagina web
function getLastActiveDocumentElement(elements){
   for (i=elements.length-1;i>=0;i--){
       elem = elements[i];
       if (!elem.readOnly && !elem.disabled){
       	 return elem;
       }
   }
   return null;
}

//ottiene il precedente elemento attivo della pagina web
//rispetto ad un elemento dato
function getPreviousActiveDocumentElement(elements, element){
   if (elements==null){
   	  return null;
   }
   for (i=elements.length-1;i>=0;i--){
       curElem = elements[i];
       if (curElem==element){
           for (j=i-1;j>=0;j--){
              prevElem = elements[j];
		      if (!prevElem.readOnly && !prevElem.disabled){
                    return prevElem;
              }
           }
           return getLastActiveDocumentElement(elements);
       }
   }
}

//ottiene il successivo elemento attivo della pagina web
//rispetto ad un elemento dato
function getNextActiveDocumentElement(elements, element){
   if (elements==null){
   	  return null;
   }
   for (i=0;i<elements.length;i++){
       curElem = elements[i];
       if (curElem==element){
           for (j=i+1;j<elements.length;j++){
              nextElem = elements[j];
              if (!nextElem.readOnly && !nextElem.disabled){
                    return nextElem;
              }
           }
           return getFirstActiveDocumentElement(elements);
       }
   }
}


//yes IE, yes Firefox
isTabKeyClicked = false;
//yes IE, yes Firefox
isShiftTabKeyClicked = false;
//yes IE, no Firefox
isShiftKeyClicked = false;

//imposta l'evento onKeyPress degli elementi non readonly della pagina web solo per Firefox
//imposta l'evento onKeyUp degli elementi non readonly della pagina web solo per IE
//imposta l'evento onKeyDown degli elementi non readonly della pagina web solo per IE
//imposta l'evento onFocus degli elementi readonly della pagina web
function skipTabOfReadonlyElement(document,form){
   elements = getActiveDocumentElements(document,form);
   if (elements==null){
      return;
   }

   if (getFirstActiveDocumentFieldElement(elements)==null){
   	return;
   }
   
   for (var i=0; i < elements.length; i++){
		element = elements[i];
		if (element.readOnly){	
			element.onfocus = readonlyElementOnFocusHandler;

			if (isIE()){
			       element.onkeydown = activeElementOnKeyDownHandler;
			       element.onkeyup = activeElementOnKeyUpHandler;
			}else{
			       element.onkeypress = activeElementOnKeyPressHandler;
			}
		}else{
			if (element.onfocus){
				if (element.onfocus == readonlyElementOnFocusHandler){
					element.onfocus = null;
				}
			}
			if (isIE()){
			    element.onkeydown = activeElementOnKeyDownHandler;

			    if(element.tagName!='TEXTAREA')
					element.onkeyup = activeElementOnKeyUpHandler;
			}else{
				element.onkeypress = activeElementOnKeyPressHandler;
			}
		}
	}
}


function selectFirstActiveDocumentFieldElement(document,form) {
   var element = null;

   if (typeof selectCustomFirstActiveDocumentFieldElement!="undefined"){
	   element = selectCustomFirstActiveDocumentFieldElement(document,form);
	   if (element!=null){
		
			try{   
				element.focus();
			}catch(e){}

		   if (element.type!="button" && element.type!="submit" && element.select){
		   	 element.select();	      		 
		   }
		   return;
	   }
   }
   var elements = getActiveDocumentElements(document,form);
   if (elements==null){
      return;
   }
   element = getFirstActiveDocumentFieldElement(elements);
   if (element==null){
	return;
   }
   
	try{
		element.focus();
	}catch(e){}

   if (element.type!="button" && element.type!="submit" && element.select){
   	 element.select();	      		 
   }
}		 	      

//gestore dell'evento onFocus degli elementi readonly della pagina web
//recupera il precedente o il successivo elemento ativo della pagina web
//in base alla tabulazione impostata dall'utente
//successivo per la pressione del tasto TAB
//precedente per la pressione dei tasto SHIFT + TAB
function readonlyElementOnFocusHandler(evt){

   //IE
   if (!isTabKeyClicked){
   	return false;
   }

   //IE
   this.value+='';
   //IE
   this.blur();
   elements = getActiveDocumentElements(document,this.form);
   if (elements==null){
   	  return null;
   }
   otherElement = null;
   /*
   if (false && window.event){
	  //yes IE, no Firefox
   	  otherElement = getNextActiveDocumentElement(elements,this);
   }else{
	  //no IE, yes Firefox
   	  if (isShiftTabKeyClicked){
   	     otherElement = getPreviousActiveDocumentElement(elements,this);
   	  }else{
   	     otherElement = getNextActiveDocumentElement(elements,this);
   	  }
   }
   */
   if (isShiftTabKeyClicked){
     otherElement = getPreviousActiveDocumentElement(elements,this);
   }else{
     otherElement = getNextActiveDocumentElement(elements,this);
   }
   if (otherElement==null){
   	  return null;
   }
   try{
	   otherElement.focus();
	}catch (e){}
	
   if (isTextElement(otherElement)){
	 otherElement.select();
   }
}

//gestore dell'evento onKeyPress degli elementi non readonly della pagina web
//solo per Firefox
//verifica se e' stato premuto il tasto SHIFT ed il tasto TAB
function activeElementOnKeyPressHandler(evt){
	if (window.event!=null){
      //yes IE, no Firefox
	  return;
	}
     //no IE, yes Firefox
	isShiftTabKeyClicked = isShiftTabKey(evt);
}

//gestore dell'evento onKeyUp degli elementi non readonly della pagina web
//solo per IE
//verifica se e' stato premuto il tasto SHIFT e/o il tasto TAB
function activeElementOnKeyUpHandler(evt){
	if (window.event==null){
        //no IE, yes Firefox
	  return;
	}

   var customActiveElements = null;
   if (typeof customActiveDocumentFieldElements!="undefined"){
	  customActiveElements = customActiveDocumentFieldElements();
	}
   if (customActiveElements!=null && typeof customActiveElementOnKeyUpHandler!="undefined"){
	  for (var i=0; i < customActiveElements.length; i++){
        if (customActiveElements[i] == this){
	      	eval(customActiveElementOnKeyUpHandler(evt));
	      break;
        }
	  }
	}

	//window.status="BLANK UP";
    //31/01/2008-bolofel
    isTabKeyClicked = false;
    //yes IE, no Firefox
	if (!isShiftKey(evt)){
	   isShiftKeyClicked = false;
	   isShiftTabKeyClicked = false;
	   //window.status="";
	   return;
	}
	if (isTabKey(evt)){
		isShiftTabKeyClicked = isShiftKeyClicked;
		if (isShiftTabKeyClicked){
			//window.status="SHIFT+TAB UP";
		}
	}
}

//gestore dell'evento onKeyDown degli elementi non readonly della pagina web
//solo per IE
//verifica se e' stato premuto il tasto SHIFT
function activeElementOnKeyDownHandler(evt){
	if (window.event==null){
      //no IE, yes Firefox
	  return;
	}
	//31/01/2008-bolofel
	isTabKeyClicked = isTabKey(evt);
	//yes IE, no Firefox
    //window.status="BLANK DOWN";
    isShiftKeyClicked = isShiftKey(evt);
    if (isShiftKeyClicked){
    	//window.status="SHIFT DOWN";
    }
}

