/**
 * Campo
 */
function Campo(percorso,maschera,tipo,titolo,obblig,invio,ricarica,father) {
	this.base = Modulo ;
	this.base(percorso) ;
	this.mask = maschera ;
	this.type = tipo ;
	this.title = titolo ;
	this.mandatory = obblig ;
	this.send = invio ;
	this.reload = ricarica ;
	this.father = father ;

	if(this.mandatory && (this.mask || this.father))
		pagina.addRequiredField(this) ;
}

Campo.prototype.clear = function() {
	this.modify() ;
	if(this.type=='collegato') {
		document.modulo[this.path].value = "" ;
		//document.modulo[this.path+"@view"].value = "" ;
	}
        if(this.type=='immagine') {
            document.getElementById(this.path+'@img').src = 'tr.gif' ;
        }
	/*if(this.type=='data') {
		document.modulo[this.path].value = "" ;
		document.modulo[this.path+'@text'].value = "" ;
		document.modulo[this.path+'@text'].className = "field_calendar" ;
	}*/
}

Campo.prototype.check = function() {
	var ret = true ;
	var i ;
	switch(this.type) {
		case "radio":
			var radio = document.modulo[this.path] ;
			ret = false ;
			for(i=0;!ret && i<radio.length;i++)
				ret = radio[i].checked ;
			break ;
		default:
			ret = document.modulo[this.path] && document.modulo[this.path].value ;
	}
	return ret ;
}

/** Handler dell'onChange */
Campo.prototype.modify = function(value) {
	if(this.type=='numero' && isNaN(document.modulo[this.path].value)) {
		alert('Il campo \''+this.title+'\' ammette solo valori numerici') ;
		document.modulo[this.path].value = "" ;
	} else {
		if(this.reload) {
			var field = document.modulo[this.path] ;
			var val = field ? field.value : null ;
			if(this.type=='tendina') {
				var sel = document.modulo[this.path] ;
				val = sel.options[sel.selectedIndex].value ;
			} else if(this.type='check')
				val = value ;
			pagina.call(this.path,'reload',val) ;
			
		} else {
			//propago la modifica sulla maschera solo in caso di modifica visibile
                        var modvis = document.getElementById(this.path+'_modificato') ;
			if(this.mask && modvis) {
				document.modulo[this.mask+'@modifica'].value = '*' ;
				document.modulo[this.path+'@modificato'].value = '*' ;
                                modvis.innerHTML = '*' ;
			}
			if(this.father)
				this.father.modify() ;
			if(this.type=='tendina') {
				pagina.selectClicked = true ;
				//copio il tip dalla option selezionata
				var sel = document.modulo[this.path] ;
				sel.title = sel.options[sel.selectedIndex].title ;
			}
		}
	}
}

/** Handler dell'onKeyPress */
Campo.prototype.enter = function(evt, oggetto, azione) {
	if(this.send) {
		var evt = (evt) ? evt : window.event ;
		if(evt.keyCode==13)
			pagina.call(this.path,'send','','','','','',this.mandatory) ;
	}
}

/** Handlers per il controllo da calendario (campi data) */
Campo.prototype.dateChange = function(time,text) {
      this.dateRefresh(time,text) ;
      this.modify() ;
}

Campo.prototype.dateRefresh = function(time,text) {
      document.modulo[this.path].value = time ;
      document.modulo[this.path+'@text'].value = text ;
}
/*********************************************************/


Campo.prototype.enbold = function(spanID,word) {
    if(word) {
      var words = word.split(" ") ;
      var i ;
      for(i=0;i<words.length;i++) {
            word = words[i] ;
            var txt = document.getElementById(spanID).innerHTML ;
            var newTXT = "" ;
            var index = 0,newIndex ;
            var re=new RegExp(word,"ig") ;
            while(re.exec(txt)!=null) {
                    newIndex = re.lastIndex ;
                    var chars = RegExp.lastMatch ;
                    newTXT += txt.substring(index,newIndex-chars.length) ;
                    newTXT += "<b>"+chars+"</b>" ;
                    index = newIndex ;
            }
            txt = newTXT+txt.substring(index) ;
            document.getElementById(spanID).innerHTML = txt ;
      }
    }
}
