//DA FARE:
//-------
//1. Meccanismo invio in finestre separate
//2. Impostazione dell'azione di ritorno alla finestra genitore
//3. Impostazioni di chiusura automatica
//4. Implementazione e utilizzo del valore di ritorno
//5. Salvataggio, inserimento e cancellazione
//6. Azioni vincolate
//7. Controllo delle modifiche

/** handler per la chiusura di una finestra */
function onClose() {
	//chiude tutte le finestre figlie
	closeWindows() ;
}

/** Chiude la finestra */
function closeWindow() {
	if(window.opener && window.opener.name && window.opener.name.substgring(0,3)=='wm_')
		window.opener.removeWindow(window.name) ;
	window.close() ;
}

/** Chiude tutte le finestre figlie */
function closeWindows() {
	var wins = getWindows() ;
	var i ;
	for(i=0;i<wins.length;i++)
		window.open('',wins[i]).close() ;
}

/** Restituisce l'array delle finestre figlie */
function getWindows() {
	var ret = new Array() ;
	var w = document.modulo.wm_windows.value  ;
	if(w) {
		var wins = w.split(",") ;
		var i ;
		for(i=0;i<wins.length;i++)
			ret.push(wins[i]) ;
	}
	return ret ;
}

/** Aggiunge una finestra figlia (che evidentemente e' stata aperta) */
function addWindow(win) {
	var wins = getWindows() ;
	if(!wins.contains(win)) {
		wins.push(win) ;
		document.modulo.wm_windows.value = wins.join(",") ;
	}
}

/** Rimuove una finestra figlia (che evidentemente e' stata chiusa) */
function removeWindow(win) {
	var wins = getWindows() ;
	wins.remove(win) ;
	document.modulo.wm_windows.value = wins.join(",") ;
}

function impostaFinestra(finestra,proprieta) {
	if(finestra==null) {
		modulo.wm_window.value = "" ;
		modulo.wm_properties.value = "" ;
	} else {
		modulo.wm_window.value = finestra ;
		if(!proprieta)//proprieta di default
			proprieta = "menubar=no,toolbar=no,width=640,height=480,scrollbars=yes" ;
		if(proprieta.substr(0,10)=="fullscreen")//fullscreen meno fastidioso
			proprieta = "width=1024,height=768,scrollbars=yes" ;
		modulo.wm_properties.value = proprieta ;
	}
}

//invia l'azione alla pagina con un dato valore
function invio(azione,valore,conferma,controllo,redir) {

	var modulo = document.modulo ;
	
	if(!azione) azione = "" ;//modulo.wm_action.value ;
	//if(modified) alert("La pagina corrente e' stata modificata") ;
	modulo.target = "" ;
	//modulo.wm_close_windows.value="" ;
	modulo.wm_redir.value = redir ? redir : "" ;
	if(modulo.wm_window.value) {
		var win = window.open("",modulo.wm_window.value,modulo.wm_properties.value) ;
		win.focus() ;
		win.creator = self ;
		modulo.target = modulo.wm_window.value ;
	}
	modulo.wm_action.value = azione ;
	modulo.wm_value.value = valore ;
	modulo.submit() ;
	modulo.wm_window.value = "" ;
	modulo.target=self ;//??
	
	//gestione provvisoria del modulo genitore
	/*if(finestra=="_parent") {
		var m = window.opener.document.modulo ;
		m.wm_action.value=azione ;
		m.wm_value.value=valore ;
		m.wm_window.value = "" ;//importante (per poter inizializzare una nuova finestra con la stessa pagina)!!!!
		//m.wm_close_windows.value = self.name ;//dice di chiudere se stessa dopo il post
		m.target="" ;
		window.opener.showWait() ;
		m.submit() ;
	} else {
		if(finestra) {
			var win = window.open("",finestra,proprieta) ;
			win.focus() ;
			win.creator = self ;
			modulo.wm_window.value = self.name ;
			modulo.target=finestra ;
		}

		if(!conferma || confirm(conferma)) {
			if(!controllo || checkRequired(azione)) {
				if(!modulo.target)
					setTimeout("showWait()",500) ;
				modulo.wm_action.value = azione ;
				modulo.wm_value.value = valore ;
				modulo.submit() ;
				modulo.target=self ;//??
			}
		}
	}*/

}

function checkRequired(action) {
	var ret = true ;
	var obj = action.substring(0,action.lastIndexOf('/')) ;
	var i ;
	for(i=0;i<required.length;i++) {
		var r = required[i] ;
		if(required[i].substring(0,obj.length)==obj && document.modulo[r] && !document.modulo[r].value) {
			ret = false ;
			alert('I campi in rosso sono obbligatori! ('+required[i].substring(obj.length+1)+')') ;
			break ;
		}
	}
	return ret ;
}

function changePage(obj, nr) {
    var pag = eval("document.modulo['"+obj+"@pagina']") ;
    if(pag)
		pag.value=nr ;
    invia() ;
}

function fieldChange(campo,reloading) {
	modified = true ;
	if(reloading) {
		if(reloading=='si')
			invia(campo+"/reload") ;
		else
			invia("/"+reloading) ;
	}
}

//document.captureEvents(Event.KEYUP) ;

var invier = "" ;

function inviaTesto(e,azione,valore) {
	if(!e)
		e = window.event ;
	if(e.keyCode==13) {
		//alert(this) ;
		invia(azione,valore) ;//basterebbe un reload???
	}
}

function showLayer(layer,message) {
	var tip = document.getElementById(layer) ;
	tip.style.visibility = 'visible' ;
}

function hideLayer(layer) {
	var tip = document.getElementById(layer) ;
	tip.style.visibility = 'hidden' ;
}

function showWait() {
	var sels = document.getElementsByTagName("SELECT") ;
	var i ;
	for(i=0;i<sels.length;i++)
		sels.item(i).style.visibility = 'hidden' ;
	var wait = document.getElementById('page_wait_layer') ;
	if(wait) {
		//alert('W='+wait.style.width) ;
		//alert('H='+wait.style.height) ;
		//wait.style.left = ((getWidth()-parseInt(wait.style.width))/2)+"px" ;
		//wait.style.top = ((getHeight()-parseInt(wait.style.height))/2)+"px" ;
		wait.style.visibility='visible' ;
	}
}

function showXML() {
	var modulo = document.modulo ;
	modulo.wm_xflag.value='xml:' ;
	modulo.wm_action.value='x' ;
	var win = window.open("","xml") ;
	win.focus() ;
	win.creator = self ;
	modulo.target="xml" ;
	//modulo.wm_window.value = "" ;//self.name ;
	modulo.submit() ;
	modulo.wm_xflag.value='' ;
	modulo.target = null ;
}

function decode(s) {
	var enc = new String(s) ;
	return unescape(enc.replace(/\+/g," ")) ;
}

//funzione fittizia da sostituire con quella vera del sito IMSS
function restringi() {}


function erroreMenu() {
	alert("Sezione non disponibile") ;
}

Array.prototype.contains = function(item) {
	var ret = false ;
	var i ;
	for(i=0;i<this.length;i++) {
		if(this[i]==item) {
			ret = true ;
			break ;
		}
	}
	return ret ;
}

Array.prototype.remove = function(item) {
	var found = -1 ;
	for(i=0;found<0 && i<this.length;i++) {
		if(this[i]==item)
			found = i ;
	}
	if(found>=0)
		this.splice(found,1) ;
}

//compatibilita' IE vari
if (typeof Array.prototype.push == "undefined") {
	Array.prototype.push = function(item) {
		this[this.length] = item ;
	}
}
if (typeof Array.prototype.add == "undefined") {
	Array.prototype.add = function(index,item) {
		var i ;
		for(i=this.length-1;i>=index;i--)
		    this[i+1] = this[i] ;
		this[index] = item ;
	}
}
if (typeof Array.prototype.indexOf == "undefined") {
	Array.prototype.indexOf = function(item) {
	    var ret = -1 ;
	    var i ;
	    for(i=0;ret<0 && i<this.length;i++)
		if(this[i]==item)
			ret = i ;
	    return ret ;
	}
}
if (typeof Function.prototype.call == "undefined") {
	Function.prototype.call = function(obj, param) {
		obj.base = this;
		return obj.base(param);
	}  
}

function getWidth() {
	var ret = 0 ;
	if (self.innerHeight) {
		// all except Explorer
		ret = self.innerWidth;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		ret = document.documentElement.clientWidth;
	} else if (document.body) {
		// other Explorers
		ret = document.body.clientWidth;
	}
	return ret ;
}

function getHeight() {
	var ret = 0 ;
	if (self.innerHeight) {
		// all except Explorer
		ret = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		// Explorer 6 Strict Mode
		ret = document.documentElement.clientHeight;
	} else if (document.body) {
		// other Explorers 
		ret = document.body.clientHeight;
	}
	return ret ;
}

function rollOver(img) {
	if(img) {
		var prefix = img.src.substring(0,img.src.length-5) ;
		img.src = prefix+'2.gif' ;
	}
}

function rollOut(img) {
	if(img) {
		var prefix = img.src.substring(0,img.src.length-5) ;
		img.src = prefix+'1.gif' ;
	}
}

var objects = new Object() ;

function toggleElement(elname) {
    var el = document.getElementById(elname+'.collapser') ;
    var val = document.modulo[elname+'@collassato'] ;
    var d = el.style.display ;
    if(d=='none') {
        val.value = 'no' ;
        el.style.display = '' ;
    } else {
        val.value = 'si' ;
        el.style.display = 'none' ;
    }
}

/*@cc_on
(function(f){
 window.setTimeout=f(window.setTimeout);
 window.setInterval=f(window.setInterval);
})
(function(f){
return function(c,t){
  var a=[].slice.call(arguments,2);
  if(typeof c!= "function")
    c = new Function(c) ;
  return f(function(){
    c.apply(this,a)},t)
  }
});
@*/
 