var idtempo;
var CWOPENWIN_OBJ = null; // oggetto ottenuto da openwinc
var CWFORM_OBJ = null; 
var CWMENU = null;

var ns4=document.layers?1:0
var ie4=document.all&&navigator.userAgent.indexOf("Opera")==-1
var ns6=document.getElementById&&!document.all?1:0

function fix(name, value) {
    var obj=null;
    if(parent.document.getElementById(name)!=null) { 	
		obj=parent.document.getElementById(name);
	} else {
		if(opener) obj=opener.document.getElementById(name);
	}
	if(obj!=null) {
	  switch(obj.type) {
		case 'text':
	    case 'textarea':
	    case 'select-one':
	    case 'hidden':
			obj.value=value;
			//alert(name);							
	    break;
		case 'select-multiple':
			/*
			var s_value = value.split(",");
			for(i=0;i<s_value.length;i++)  {
				for(indice=0;indice<obj.length;indice++) {
					if(obj[indice].value==s_value[i]) 
						obj[indice].selected=true;
				}
			}
			*/
			for(indice=0;indice<obj.length;indice++) {
				if(obj[indice].value==value) obj[indice].selected=true;
				}
		break;
		
		case 'radio':
		case 'checkbox':
			/*
			obj_multi = parent.document.getElementsByName(name); 			
			var s_value = value.split(",");
			for(i=0;i<s_value.length;i++)  {
				for(indice=0;indice<obj_multi.length;indice++) {
					if(obj_multi[indice].value == s_value[i]) obj_multi[indice].checked = true;
				}
			}
			*/
			obj_multi = parent.document.getElementsByName(name); 
			for(indice=0;indice<obj_multi.length;indice++) {
				
				if(obj_multi[indice].value == value) obj_multi[indice].checked = true;
				}			
		break;		
	  	default:
			alert(obj.types);
		break;
	  }
	} else alert("fix: Non trovato '" + name + "' = " + value);
}
function update(auto, index) {
	if(index==null) index=0;	// index e il numero 
	clearTimeout(idtempo);
	index=index+0;
	var obj = cw_DOM('diviframe'+index);
	if(auto=='auto') {
		if (obj.style.display=='block') obj.style.display='none';
		else obj.style.display='block';		
	}
	if(auto==1 && obj.style.display=='none') { obj.style.display='block'; }
	if (obj.style.display=='block') {
		idtempo=setTimeout("if( ns4==1 || ns6==1) window.stop(); cw_DOM('CWFORM').submit();", 500);
		// CWFORM è un oggetto assegnato da cw_DOM
		} 
    }
    
function expand(id) {
 obj = document.getElementById(id);
 if ( obj.style.display=='none') obj.style.display='block';
 else obj.style.display='none';
}

function maximizeWin() {
  if (window.screen) {
    var aw = screen.availWidth;
    var ah = screen.availHeight;
    parent.window.moveTo(0, 0);
    parent.window.resizeTo(aw, ah);
  }
}

function centerWin(w,h) {
  if (window.screen) {
    LeftPosition = (screen.availWidth) ? (screen.availWidth-w)/2 : 0;
    TopPosition = (screen.availHeight) ? (screen.availHeight-h)/2 : 0;
    parent.window.moveTo(LeftPosition, TopPosition);
    parent.window.resizeTo(w, h);
  }
}

function openwinc(mypage,myname,w,h,scroll) {
  var win = null;
  LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
  TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
  settings ='height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable'
  win = window.open(mypage,myname,settings);
  return win;
}

function openwin(mypage,myname,l,t,w,h,scroll) {
  var win = null;
  settings ='height='+h+',width='+w+',top='+t+',left='+l;
  if(scroll == '-1') {settings = settings + ',fullscreen';}
  else if(scroll == '0') {settings = settings + ',scrollbars=0, menubar=0, location=0, resizable';}
  else if(scroll == '1') {settings = settings + ',scrollbars=1, menubar=0, location=0, resizable';}
  else {settings = settings+',scrollbars=1,menubar=1,location=1,resizable';};
  win = window.open(mypage,myname,settings);
  return win;
}

function togliva(campo, iva) {
	if(cw_DOM(campo).value>0 ) {
		cw_DOM(campo).value = Math.round(100 * document.getElementById(campo).value / (1+iva/100)) / 100; 		
		}
}
function nrSelected(obj) {
	counter=0;
	for(var i=0;i<obj.options.length;i++)
	     if(obj.options[i].selected) counter++
    return counter;    
}

function Convert2String(campo) {
	var string="";
	var obj=document.getElementById(campo);
	for(i=0;i<obj.length;i++) {
		if(obj[i].selected==true) string+=obj[i].value+" ";
		}
    return string;    
}
function convalida_new_mod(campo, test, alarm) {
	var obj=document.getElementById(campo);
	var value=null;
	if(obj!=null)	switch(obj.type) {
		case 'text':
	    case 'textarea':
	    case 'select-one':
	    case 'hidden':
		case 'file':
			value=obj.value;	
	    break;
		case 'select-multiple':
			value=Convert2String(campo);
		break;		
	}
	if(value==null) alert("convalida_new_mod.\nerrore rif. " + campo);
	if(value!=null && value==test) {
		if(alarm!="") alert(alarm);
		return true;
	}
	return false;
}
function cw_DOM(name) {
	var obj=null;
	if(parent.document.getElementById(name)!=null) 	
		obj=parent.document.getElementById(name);
    else if(parent.document.getElementById(name)==null) {
		if(opener) obj=opener.document.getElementById(name);
	}
	return obj;
}
function cw_openwin_form() {
	return openwinc("about:blank", "CWOPENWIN", 280, 100, 0);
}