// -------------------------------------------------------------------------
//
// @file layout.js
// @auth: Andrea Raggi
// @date: 11/06/2008
// @descr:
// Libreria di autodimensionamento maschere web 
// @require:
//		- wizcommon.js
//		- draglib.js
//		- dragresize.js
// -------------------------------------------------------------------------

// -------------------------------------------------------------------------
// To Set In Main Webpage - init
// -------------------------------------------------------------------------
// Flag for SvilMode (true for show Window Control Popup, false for autoredim)

var layout_svilMode = true;
// SourceFile path (for save changes)
var layout_fileCorrente = '';
// Flag for Debug Message (true for show debug message in Window Control Popup)
var layout_abilitaDebug=true;


// Array with redim resolution factor (width & height)
var array_fattori_W = new Array();
var array_fattori_H = new Array();
// -------------------------------------------------------------------------
// To Set In Main Webpage - end
// -------------------------------------------------------------------------


// -------------------------------------------------------------------------
// Configuration - init
// -------------------------------------------------------------------------
// Url address of wincontrol file (for the Window Control Popup)
var winControlPath = "/dx/lib/wincontrol2.php";
// Url address of background grid image 
var imgBgGrid = "/dx/lib/imgs/bgGrid/DEDEDE/bgGridSmall9x9.gif";
// Number of pixel
var numPixBgGrid = 9;
// -------------------------------------------------------------------------
// Configuration - End
// -------------------------------------------------------------------------

// -------------------------------------------------------------------------
// Working Environment - init
// -------------------------------------------------------------------------
// Array for saving original position-dimension
var array_orig_div   = new Array();
var array_orig_frame = new Array();
var array_orig_gbox  = new Array();
var array_orig_input = new Array();
var array_orig_button = new Array();
var array_orig_label = new Array();
var array_orig_select = new Array();
var array_orig_textarea = new Array();


//other
var firstTime=true;
var dbg_win = null;
var in_resize = false;			
var forza_redim=false;
var mhResizeTimeout;
var richiamatoDaResize = false;
var obj_selected = null;
var old_border = null;
var dragresize = null;

var bloccaResizeGester=false;
// -------------------------------------------------------------------------
// Working Environment - end
// -------------------------------------------------------------------------

// -------------------------------------------------------------------------
// Program - init
// -------------------------------------------------------------------------

//Add Functions on window events Load , Unload, Resize.
//@see: wizcommon.js:addCompatibleXXXXListener functions
addCompatibleLoadListener(inizializzaLayout);
addCompatibleUnLoadListener(chiusaWinDbg);
addCompatibleResizeListener(gestisciOnResize);


/*
 * Function gestisciOnResize
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called on window resize event.
 * Set a timeout for do authomatic resize of page elements
 *
 */
function gestisciOnResize(){
	
	if (mhResizeTimeout) window.clearTimeout(mhResizeTimeout)
	if(bloccaResizeGester==true)return;
	mhResizeTimeout = 0
	mhResizeTimeout = window.setTimeout(doResizeStuff, 300)
}


/*
 * Function doResizeStuff
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called on window resize event, after timeout.
 * This function set a flag and call dynamicLayout()
 *
 */
function doResizeStuff(){
	richiamatoDaResize = true;
	
	mhResizeTimeout = 0
	if (mhResizeTimeout) window.clearTimeout(mhResizeTimeout)
	dynamicLayout();
}


/*
 * Function inizializzaLayout
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called on window load event.
 * This function start the program with or without Window Control Popup,
 * and autoresize of the elements
 *
 */
function inizializzaLayout(){
	//Test if svilmode is active and set a virtual grid like background image
	if(isSvilModeActive()==true){
		if(imgBgGrid){
			if(document.body){
				try{
					document.body.style.backgroundImage="url("+imgBgGrid+")";
				}
				catch(ex1){
					alert("Errore in assegnazione immagine url!");
				}
			}
		}
		//Open Window Control Popup
		apriDebugWin(1);
	}

	//If i'm not in the fraime content call to dynamicLayout
	//Warning: it's done for sync problem with IE6, on the document's and iframe's onload event
	if(imInAFrame()==false){
		if(thereIsAValidFrame()==true){
			layout_svilMode=false;
		}
		dynamicLayout();
	}

}

/*
 * Function doDynamicLayoutFromExternal
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called from external js function.
 * This function call the dynamicLayout for resize html element
 * Is called from external js function, for example createNewObject function
 *
 * Now only call dynamicLayout, but it can be used for other preliminar operation
 */
function doDynamicLayoutFromExternal(){
	dynamicLayout();
}


/*
 * Function apriDebugWin
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function open the Window Control Popup
 * 
 * @param: 
 * - n = [int] window's number
 */
function apriDebugWin(n){
	try{
		var wname = 'pop_' + n;
		var l = 100 * n + 300;
		var t = 100 * n + 200;
	
		var tmp_param = "";
		//Get Array of Resolution Factor
		array_fattori_W.sort(sortaArrayFattori);
		array_fattori_H.sort(sortaArrayFattori);
	
		//Append Array to Url's Query String
		if(array_fattori_W.length>0){
			tmp_param = "&countRes=" + array_fattori_W.length;
			for(var i = 0 ; i < array_fattori_W.length; i++){
				//First Element = Lowest Resolution 
				try{
					tmp_param = tmp_param + "&res" + i + "=" + array_fattori_W[i][0] + "x" + array_fattori_H[i][0];
				}
				catch(ex){
					//if(layout_svilMode)layout_svilMode=confirm("Attenzione Gli Array Fattori non sono stati correttamente settati!!! = " + ex);
				}
			}
		}
		//Append SourceFile to Url's Query String
		if(layout_fileCorrente!=""){
			tmp_param = tmp_param + "&basefile=" + layout_fileCorrente;
		}
		
		if(winControlPath!=""){
			//Open Window Control Popup
			dbg_win = window.open(checkAndCacheUrl(winControlPath) + tmp_param,
			wname,
			'statusbar=yes, menubar=no,resizable=yes,toolbar=no,width=400,height=300,left='+l+',top='+t+',status=0');
		}
	}
	catch(exIgn1){
		//alert("Errore in apertura finestra dbg = " + exIgn1);
		dbg_win = null;	
	}
	return false;	
}

/*
 * Function dynamicLayout
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function get all element's in the page and
 * resize it if the svilMode is false;
 * Otherwise enable dragResize for all element's, after setting className for 
 * resizable elements.
 * 
 */
function dynamicLayout(){
	var tmp_richiamato = richiamatoDaResize;
	richiamatoDaResize = false;

	if(in_resize==true)return false;
	in_resize = true;

	if(array_fattori_W.length<=0)return;
	if(array_fattori_H.length<=0)return;
	
	
	if(isSvilModeActive()==true){
		//Test if Window Control Popup is alreadyLoaded
		//var tmp_esito = alertDbg("Inizio Debug!");
		//if(tmp_esito==false){
		//	in_resize = false;
		//	gestisciOnResize();
		//	return;
		//}
	}


	//Get Browser Dimension
	var browserWidth = getBrowserWidth();
	var browserHeight = getBrowserHeight();
	window._PERSBROWSERWIDTH = browserWidth;
	window._PERSBROWSERHEIGHT =browserHeight;
	
	var fattoreW = calcolaFattore(browserWidth,1280);  // calcolo fattore di proporzionalita
	var fattoreH = calcolaFattore(browserHeight,1024);  // calcolo fattore di proporzionalita
	
	//alertDbg("browser dim = " + browserWidth + " - " + browserHeight + " \n fattoreW = " + fattoreW + " - fattoreH = " + fattoreH);
	
	//Get All Elements
	var array_frame = document.getElementsByTagName("iframe");
	var array_div = document.getElementsByTagName("div");
	var array_gbox = document.getElementsByTagName("fieldset");
	var array_input = document.getElementsByTagName("input");
	var array_button = document.getElementsByTagName("button");
	var array_label = document.getElementsByTagName("label");
	var array_select = document.getElementsByTagName("select");
	var array_textarea	= document.getElementsByTagName("textarea");
	
	var i;
	    
	// Redim Fieldset
	for (i = 0; i < array_gbox.length; i++){		
		var obj = array_gbox.item(i);
		if(isValidObjLayout(obj)==true){
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_gbox, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}
	}

	// Redim Iframe
	for (i = 0; i < array_frame.length; i++){
		var obj = array_frame.item(i);
		if(isValidObjLayout(obj)==true){
			var tmp_usaCentratura = true;
			try{
				if(obj.getAttribute("wlaycentratura")){
					tmp_usaCentratura = obj.getAttribute("wlaycentratura");
				}
			}
			catch(exIgnored){}
			ridimensionaElemento(obj, array_orig_frame, browserWidth, browserHeight, fattoreW, fattoreH, tmp_usaCentratura);
		}
		
	}
	// Redim Div
	for (i = 0; i < array_div.length; i++){
		
		var obj = array_div.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}

			
			ridimensionaElemento(obj, array_orig_div, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}
	}

	// Redim Input
	for (i = 0; i < array_input.length; i++){
		var obj = array_input.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_input, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}		
	}
	// Redim Button
	for (i = 0; i < array_button.length; i++){
		var obj = array_button.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_button, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}		
	}
	
	// Redim Label
	for (i = 0; i < array_label.length; i++){
		var obj = array_label.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_label, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}		
	}

	// Redim Select
	for (i = 0; i < array_select.length; i++){
		var obj = array_select.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_select, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}		
	}

	// Redim textarea
	for (i = 0; i < array_textarea.length; i++){
		var obj = array_textarea.item(i);
		if(isValidObjLayout(obj)==true){
			
			if(isSvilModeActive()==true){
				if(firstTime==true){
					if(obj.className){
						if(containsString(""+obj.className, "drsElement drsMoveHandle")==false){
							obj.className += " drsElement drsMoveHandle";
						}
					}
					else obj.className = "drsElement drsMoveHandle";
				}
			}
			ridimensionaElemento(obj, array_orig_textarea, browserWidth, browserHeight, fattoreW, fattoreH, false);
		}		
	}

	// Create DragResize Object
	if(isSvilModeActive()==true){
		if(firstTime==true){
						
			dragresize = null;
			dragresize = new DragResize('dragresize',
 			{ minWidth: 5, minHeight: 5, minLeft: 0, minTop: 0, useResizeCorner: false});

			dragresize.isElement = function(elm){
				if (elm.id && startsWith(""+elm.id, "WLAY_")) return true;
			};
			dragresize.isHandle = function(elm){
				if (elm.id && startsWith(""+elm.id, "WLAY_")) return true;
			};
			dragresize.ondragfocus = function(obj) {
					clikkatoSuElementoObj(obj); 
					
					if(startsWith(""+obj.id, "WLAY_GBOX")){
						bloccaResizeGester=true;
					}
			};
			dragresize.ondragstart = function(obj,isResize) { };
			dragresize.ondragmove = function(obj,isResize) { };
			dragresize.ondragend = function(obj,isResize) {
										if(obj){
											terminatoDragSuObj(obj);
											if(isResize==true){	
													richiamaWlayEvtRes(obj); 
											}
											try{
												if(dbg_win)
													dbg_win.focus();
											}
											catch(exIgn){}

										}
										
										
									};
			dragresize.ondragblur = function(obj) { 
					if(startsWith(""+obj.id, "WLAY_GBOX")){
						bloccaResizeGester=false;
					}
			};
			dragresize.apply(document);	
		}
	}

	// Set Flag and exit
	firstTime=false;
	in_resize = false;	
} 


			
/*
 * Function ridimensionaElemento
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function change the position & dimension & font-size of an element
 * do it with specific factor 
 * 
 * @param: 
 * - obj = [Dom object] source element
 * - array_old_orig = [array] original position & dimension & font-size
 * - browserWidth = [int] current browserWidth
 * - browserHeight = [int] current browserHeight
 * - fattoreW = [float] width's factor
 * - fattoreH = [float] height's factor 
 * - usaCentratura = [boolean] flag for center element's width in the page (used for fieldset)
 * 
 */
function ridimensionaElemento(obj, array_old_orig, browserWidth, browserHeight, fattoreW, fattoreH, usaCentratura){
		if( (obj.style.display=="none")||(obj.style.display=="hidden") )return ;
		if (obj.parentNode){
			var objP = obj.parentNode;
			if(objP.style){
				if( (objP.style.display=="none")||(objP.style.display=="hidden") )return ;
			}
			
		}
		
		//Get Current Object Properties
		var objTop = obj.offsetTop;
		var objLeft = obj.offsetLeft;
		var objWidth = obj.offsetWidth;
		var objHeight = obj.offsetHeight;
		//if(obj.id=="WLAY_GBOX2")cm_debuggaTxt("height = " + objHeight);
		
		var objFontSize = getFontSize(obj);
		
		//bugfix
		if(imInAFrame()==true){
			var tmp_top = obj.style.top;
			var tmp_left = obj.style.left;
			
			if((objTop==0)&&(objLeft==0)){
				if(window.parent){
					var seBlocca=false;
					try{
						seBlocca = window.parent.firstOfRidimensionaFrame(window);
					}
					catch(exIgn1){}
					if(seBlocca)return;
				}
				if((tmp_top!="0")&&(tmp_left!="0")){
					objTop = tmp_top.replace("px", "");
					objLeft = tmp_left.replace("px", "");
					objWidth = obj.style.width.replace("px","");
					objHeight = obj.style.height.replace("px","");
					return;
				}
			}
		}
		//fine bugfix

		//Search [& Save if not find] Original Object Properties
		var fnd_orig = false;		
		if(array_old_orig){
			var tmp_obj = foundElemento(obj.id, array_old_orig);
			if(tmp_obj){
				objTop = tmp_obj[0];
				objLeft = tmp_obj[1];
				objWidth = tmp_obj[2];
				objHeight = tmp_obj[3];
				objFontSize = tmp_obj[4];
				fnd_orig = true;
			}
		}		
		if(fnd_orig==false){
			var tmp_obj = new Array(objTop, objLeft, objWidth, objHeight, objFontSize);
			array_old_orig[obj.id] = tmp_obj;
		}
		
		objTop    = checkValoreNum(objTop);
		objLeft   = checkValoreNum(objLeft);
		objWidth  = checkValoreNum(objWidth);
		objHeight = checkValoreNum(objHeight);

		//Test if i'm in force_mode (When i test redim from Window Control Popup)
		if(forza_redim==false){
			//Test if i'm in svilMode
			if(layout_svilMode==true){
				//Set object's original properties
				obj.style.top =  objTop + "px";
				obj.style.left = objLeft + "px";
				obj.style.width = objWidth + "px";
				obj.style.height = objHeight + "px";
				obj.style.fontSize = objFontSize;
				
				var tmp_txt_dbg2 = "Trovato Orig 2 = " + fnd_orig + " \nObj = " + obj.id + " \nDim obj = pos(" + objTop + "," + objLeft + ") size(" + objWidth + "," +objHeight + ")";
				tmp_txt_dbg2 = tmp_txt_dbg2 + "\nNuovi Valori per elemento: pos(" + obj.style.top + "," + obj.style.left + ") size(" + obj.style.width + "," + obj.style.height + ")";
				tmp_txt_dbg2 = tmp_txt_dbg2 + "\n FontSize settato = " + tmp_fnt_int + "" + tmp_fnt_unit;
				//alertDbg(tmp_txt_dbg2);
				//Exit 
				return;
			}
		}
		//alertDbg("Elaboro elemento = " + obj.id);
	
		
		var new_t = objTop;
		var new_l = objLeft;
		var new_w = objWidth;
		var new_h = objHeight;
		
		var tmp_font_w = -1;
		var tmp_font_h = -1;

		array_fattori_W.sort(sortaArrayFattori);
		array_fattori_H.sort(sortaArrayFattori);

		//Set Width
		if(array_fattori_W.length>0){
			for(var idx = 0 ; idx < array_fattori_W.length; idx++){
				if(array_fattori_W[idx]){
					if(browserWidth<=array_fattori_W[idx][0]){
						tmp_font_w = array_fattori_W[idx][1];
						new_w = (objWidth * array_fattori_W[idx][1]);
						new_l = (objLeft * array_fattori_W[idx][1]);
						break;
					}
				}
			}
		}
		else{
			//If no factor in array, use passed factor
			new_w = (objWidth * fattoreW);
			new_l = (objLeft * fattoreW);
		}

		//Set Height
		if(array_fattori_H.length>0){
			for(var idx = 0 ; idx < array_fattori_H.length; idx++){
				//primo elemento risoluzione pił piccola
				if(array_fattori_H[idx]){
					if(browserHeight<=array_fattori_H[idx][0]){
						tmp_font_h = array_fattori_H[idx][1];
						new_h = (objHeight * array_fattori_H[idx][1]);
						new_t = (objTop * array_fattori_H[idx][1]);
						break;
					}
				}
			}
		}
		else{
			//If no factor in array, use passed factor
			new_h = (objHeight * fattoreH);
			new_t = (objTop * fattoreH);
		}

		var tmp_font_fact = -1;
		if( (tmp_font_w!=-1)&&(tmp_font_h!=-1) ){
				if(tmp_font_w<tmp_font_h)tmp_font_fact = tmp_font_w;
				else tmp_font_fact = tmp_font_h;			
		}
		else{
			if(tmp_font_w!=-1){
				tmp_font_fact = tmp_font_w;
			}
			else{
				tmp_font_fact = tmp_font_h;
			}
		}
		
		//Centering element's width if i'm not in a frame and flag is true
		var centerTop = new_t;
		var centerLeft = new_l;
		
		if(imInAFrame()==false){
			if(usaCentratura==true){
				//centerTop = (browserHeight/2) - (new_h/2);
				if(browserWidth>=new_w){				
					centerLeft = (browserWidth/2) - (new_w/2);
				}
			}
		}
		
		if(centerTop<=0)centerTop=0;
		if(centerLeft<=0)centerLeft=0;
		
		//Set new Properties
		var tmp_wlay_x = layout_getCompatibleAttribute(obj.getAttribute("wlayresX"), "true");
		var tmp_wlay_y = layout_getCompatibleAttribute(obj.getAttribute("wlayresY"), "true");
		var tmp_wlay_w = layout_getCompatibleAttribute(obj.getAttribute("wlayresW"), "true");
		var tmp_wlay_h = layout_getCompatibleAttribute(obj.getAttribute("wlayresH"), "true");
		
		
		if(tmp_wlay_x!="false")obj.style.left = centerLeft + "px";
		if(tmp_wlay_y!="false")obj.style.top =  centerTop + "px";
		if(tmp_wlay_w!="false")obj.style.width = new_w + "px";
		if(tmp_wlay_h!="false")obj.style.height = new_h + "px";

		var tmp_fnt_unit;
		var tmp_fnt_int;
		if(tmp_font_fact!=-1){
			var tmp_fnt_str = new String("" + objFontSize);
			tmp_fnt_unit = "px";
			if(containsString(tmp_fnt_str, "pt"))tmp_fnt_unit = "pt";
			if(containsString(tmp_fnt_str, "px"))tmp_fnt_unit = "px";
			
			
			tmp_fnt_str = tmp_fnt_str.replace("px","");
			tmp_fnt_str = tmp_fnt_str.replace("pt","");
			
			tmp_fnt_int = parseInt("" + tmp_fnt_str, 10);
			
			tmp_fnt_int = Math.round(tmp_fnt_int * tmp_font_fact);
			if(tmp_fnt_int<1)tmp_fnt_int = 1;
			
			tmp_fnt_int = tmp_fnt_int + 2;
			
			obj.style.fontSize = tmp_fnt_int + "" + tmp_fnt_unit;
		}
		
		
		//Debug
		var tmp_txt_dbg = "Trovato Orig = " + fnd_orig + " \nObj = " + obj.id + " \nDim obj = pos(" + objTop + "," + objLeft + ") size(" + objWidth + "," +objHeight + ")";
		tmp_txt_dbg = tmp_txt_dbg + "\nNuovi Valori per elemento: pos(" + obj.style.top + "," + obj.style.left + ") size(" + obj.style.width + "," + obj.style.height + ")";
		tmp_txt_dbg = tmp_txt_dbg + "\n FontSize settato = " + tmp_fnt_int + "" + tmp_fnt_unit;
		//alertDbg(tmp_txt_dbg);
		
		//if(obj.id=="WLAY_GBOX2")cm_debuggaTxt(tmp_txt_dbg);

		//Call an element's event for this redim.
		richiamaWlayEvtRes(obj);
}

function layout_getCompatibleAttribute(attrVal, defVal){
	if(!attrVal)return defVal;
	if(typeof(attrVal)=="undefined")return defVal;
	if(attrVal=="")return defVal;
	return attrVal;
}

/*
 * Function bloccaEditing
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function block or unblock the edit process.
 * Called from Window Control Popup, when want to test the redim
 * 
 * @param: 
 * - val = [boolean] true for block, false for unblock
 * 
 */
function bloccaEditing(val){
	if(dragresize!=null){
		dragresize.enabled=!val;
	}
	forza_redim = val;
}

/*
 * Function richiamaWlayEvtRes
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function check for an element's attribute called wlayevtres
 * If this is setted, try to call with eval!!!
 * 
 * @param: 
 * - obj = [Dom object] source element
 * 
 */
function richiamaWlayEvtRes(obj){
	if(obj){
		//If is setted, call to wlayevtres javascript code
		if(obj.getAttribute("wlayevtres")){
			try{
				eval(obj.getAttribute("wlayevtres"));
			}
			catch(exIgn){
				alertDbg("Errore on wlayevtres = " + exIgn);
			}
		}
	}
}


/*
 * Function recuperaDivLayout
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function go back to a valid object parent
 * Exit when find a valid id's prefix.
 * 
 * @param: 
 * - obj = [Dom object] source element
 * 
 */
function recuperaDivLayout(obj){
	if(obj==null)return null;
	
	var tmp_obj = obj;
	var tmp_flag=true;

	while(tmp_obj){
		if(tmp_obj.id){
			if(startsWith(""+tmp_obj.id, "WLAY_")==true){
				return tmp_obj;
			}
		}
		tmp_obj = tmp_obj.parentNode;
	}
	return null;
}

/*
 * Function terminatoDragSuObj
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * This function is called when end a Drag or Resize Operation, of an element.
 * 
 * @param: 
 * - obj = [Dom object] source element
 */
function terminatoDragSuObj(obj){
	if(dbg_win==null){
		if(isSvilModeActive()){
			if(layout_svilMode==true){
				apriDebugWin(1);
			}
		}
	}

	try{
		if(obj==null)return true;
		obj = recuperaDivLayout(obj);
		if(obj==null)return true;
		if(dbg_win!=null){


			var tmpWizObj = doWizardObjectFromObj(obj);
			/*
				trovare il modo di farlo solo per i nuovi obj!
				var objFontSize = getFontSize(obj);
				var tmp_arr = new Array(tmpWizObj.top, tmpWizObj.left, tmpWizObj.width, tmpWizObj.height, objFontSize);
				risettaArrayOrig(obj, tmp_arr);
			*/
			dbg_win.modificatoElemento(tmpWizObj);
		}

	}
	catch(ex){
		alertDbg("errore in doWizardObjectFromObj 2 = " + ex);
	}
}

/*
 * Function risettaArrayOrig
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called for change original properties of an object
 * 
 * @param: 
 * - obj = [Dom object] source element
 * - arr = [array] new properties array 
 */
function risettaArrayOrig(obj, arr){
	var tmp_name =("" + obj.nodeName).toUpperCase();		
	switch(tmp_name){
		case "DIV":
			array_orig_div[obj.id] = arr;
			break;
		case "INPUT":
			array_orig_input[obj.id] = arr;
			break;
		case "BUTTON":
			array_orig_button[obj.id] = arr;
			break;
		case "IFRAME":
			array_orig_frame[obj.id] = arr;
			break;
		case "SELECT":
			array_orig_select[obj.id] = arr;
			break;
		case "LABEL":
			array_orig_select[obj.id] = arr;
			break;
		case "FIELDSET":
			array_orig_gbox[obj.id] = arr;
			break;
		case "TEXTAREA":
			array_orig_textarea[obj.id] = arr;
			break;
	}	
}

/*
 * Function clikkatoSuElementoObj
 * @auth: Andrea Raggi
 * @date: 17/07/2008
 * @descr:
 * Called when an element is selected with mouse click
 * 
 * @param: 
 * - obj = [Dom object] source element
 */
function clikkatoSuElementoObj(obj){
	if(dbg_win==null){
		if(isSvilModeActive()){
			if(layout_svilMode==true){
				apriDebugWin(1);
			}
		}
	}

	try{
		if(obj==null)return true;
		obj = recuperaDivLayout(obj);
		if(obj==null)return true;

		//Set Old Border to Old Selected Object
		if(obj_selected!=null){
			if(old_border==""){
				//Reset Default Border to Old Selected Object
				obj_selected.style.borderColor = obj_selected.style.borderWidth = obj_selected.style.borderStyle = '';
			}
			else obj_selected.style.border = old_border;
		}
		//Save border for new Selected Object
		if(obj.style.border){
			old_border = obj.style.border;
		}
		else old_border = "";

		//Change Selected Object's Border and save Object
		obj.style.border = "1px dotted red";
		obj_selected = obj;
		
		if(dbg_win!=null){
			var tmpWizObj = doWizardObjectFromObj(obj);

			dbg_win.selezionatoElemento(tmpWizObj);
			
		}
	}
	catch(ex){
		alertDbg("errore = " + ex);
	}
}


function foundElemento(id, tmp_arr){
	if(tmp_arr){
		for (key in tmp_arr){
		  	if(key==id)return tmp_arr[key];
		}
	}
	return null;
}

function foundElementoWithoutArray(id){
	var rit = null;
	if(array_orig_div){
		rit = foundElemento(id, array_orig_div);
	}
	if(rit==null){
		rit = foundElemento(id, array_orig_frame);
	}
	if(rit==null){
		rit = foundElemento(id, array_orig_gbox);
	}
	return rit;	
}

function foundElementoFromObj(obj){
	var tmp_name =("" + obj.nodeName).toUpperCase();		
	switch(tmp_name){
		case "DIV":
			return foundElemento(obj.id , array_orig_div);
			break;
		case "INPUT":
			return foundElemento(obj.id , array_orig_input);
			break;
		case "BUTTON":
			return foundElemento(obj.id , array_orig_button);
			break;
		case "IFRAME":
			return foundElemento(obj.id , array_orig_frame);
			break;
		case "SELECT":
			return foundElemento(obj.id , array_orig_select);
			break;
		case "LABEL":
			return foundElemento(obj.id , array_orig_select);
			break;
		case "FIELDSET":
			return foundElemento(obj.id , array_orig_gbox);
			break;
		case "TEXTAREA":
			return foundElemento(obj.id , array_orig_textarea);
			break;
	}	
	return null;	
}

/*
 *  Funzioni Comuni e di utilitą
 */



/*
 * Funzione per il debug 
 */
var tmp_debug = true;
function alertDbg(txt){
	try{
		if(layout_svilMode==true){
			if(dbg_win!=null){
				try{
					dbg_win.debuggaTxt(txt);
				}
				catch(ex){
					return false;
				}
			}
			else {
				if(isSvilModeActive()==true){
					apriDebugWin(1);
				}
				else layout_svilMode=confirm(txt);
			}
		}
	}
	catch(exIg1){
		if(tmp_debug)tmp_debug = confirm("Errore on alertDbg = " + exIg1);
	}
	return true;
}

function isValidObjLayout(obj){
	if(obj){
		if(obj.id){
			if(startsWith(""+obj.id, "WLAY_")==true)return true;
		}
	}
	return false;
}

function isSvilModeActive(){
	if(layout_svilMode==true){
		if(imInAFrame()==true){
			return true;
		}
		else{
			if(thereIsAValidFrame()==false){
				return true;
			}
		}
	}
	return false;
}


function sortaArrayFattori(a,b){
	//Compare "a" and "b" in some fashion, and return -1, 0, or 1
	if(a[0]==b[0])return 0;
	else{
		if(a[0]>b[0])return 1;
		else return -1;
	}
}
function thereIsAValidFrame(){
	var tmp_arr_frame = document.getElementsByTagName("iframe");
	for (i = 0; i < tmp_arr_frame.length; i++){
		var obj = tmp_arr_frame.item(i);
		if(isValidObjLayout(obj)==true){
			return true;
		}		
	}
	return false;	
}


/* -------------------------------------------------------------------------------
// calcola fattoreW di proporzionalitą rispetto alla risoluzione video impostata 
// ipotizzando che il layout sia stato definito in fase di sviluppo ad una
// risoluzione di 1280x1024
------------------------------------------------------------------------------- */
function calcolaFattore(risoluzione,divisore){
	var fattore;
	fattore = risoluzione/divisore;
	return fattore;
}

// -------------------------------------------------------------------------------
// Funzioni richiamate da Win Debug
// -------------------------------------------------------------------------------
function listParentForNewObject(){
	var rit = new Array();

	var tmp_arr  = document.getElementsByTagName("form");
	var tmp_arr_id_form = new Array();
	for (i = 0; i < tmp_arr.length; i++){		
		var obj = tmp_arr.item(i);
		tmp_arr_id_form[i] = obj.id;
	}
	rit[0] = tmp_arr_id_form;

	tmp_arr  = document.getElementsByTagName("fieldset");
	var tmp_arr_id_fset = new Array();
	for (i = 0; i < tmp_arr.length; i++){		
		var obj = tmp_arr.item(i);
		tmp_arr_id_fset[i] = obj.id;
	}
	rit[1] = tmp_arr_id_fset;

	tmp_arr  = document.getElementsByTagName("iframe");
	var tmp_arr_id_frame = new Array();
	for (i = 0; i < tmp_arr.length; i++){		
		var obj = tmp_arr.item(i);
		tmp_arr_id_frame[i] = obj.id;
	}
	rit[2] = tmp_arr_id_frame;

	return rit;
}



function listElementsByName(name){
	return document.getElementsByName(name);
}

function actionFromWizard(wizAction){
	var need_deselect = false;
	if(wizAction){
		switch(wizAction.idAction){
			case -1:
				//cambio risoluzione
				changeFakeDim(wizAction.objAction, wizAction.fakeAction);
				break;
			case 0:
				//drag resize assieme
				changeTopObj(wizAction.objAction, wizAction.fakeAction);
				changeLeftObj(wizAction.objAction, wizAction.fakeAction);
				changeWidthObj(wizAction.objAction, wizAction.fakeAction);
				changeHeightObj(wizAction.objAction, wizAction.fakeAction);
				need_deselect=true;
				break;
			case 1:
				//cambio top
				changeTopObj(wizAction.objAction, wizAction.fakeAction);
				need_deselect=true;
				break;
			case 2:
				//cambio left
				changeLeftObj(wizAction.objAction, wizAction.fakeAction);
				need_deselect=true;
				break;
			case 3:
				//cambio width
				changeWidthObj(wizAction.objAction, wizAction.fakeAction);
				need_deselect=true;
				break;
			case 4:
				//cambio height
				changeHeightObj(wizAction.objAction, wizAction.fakeAction);
				need_deselect=true;
				break;
			case 5:
				//cambio text
				changeTextObj(wizAction.objAction, wizAction.fakeAction);
				break;
			case 6:
				//cambio font size
				changeFontSize(wizAction.objAction, wizAction.fakeAction);
				break;
			case 7:
				//cambio font color
				changeFontColor(wizAction.objAction, wizAction.fakeAction);
				break;
			case 8:
				//cambio back color
				changeBackColor(wizAction.objAction, wizAction.fakeAction);
				break;
			case 9:
				//cambio alt/title
				changeTitle(wizAction.objAction, wizAction.fakeAction);
				break;
			case 10:
				//cambio checked 
				changeChecked(wizAction.objAction, wizAction.fakeAction);
				break;
			case 11:
				//cambio checked 
				changeName(wizAction.objAction, wizAction.fakeAction);
				break;
			case 99:
				//nuovo oggetto o elimina oggetto se gią presente!!!
				aggiungiEliminaObj(wizAction.objAction, wizAction.fakeAction);
				break;
		}
	}
		
	if(need_deselect){
		if(dragresize){
			try{
				dragresize.deselect(true);
			}
			catch(exIgnored1){}
			try{
				dragresize.select(document.getElementById(wizAction.objAction.id));
			}
			catch(exIgnored1){}
		}
	}
	
}

//Cambia le dimensioni fittizie della pagina
function changeFakeDim(args, fakeAction){
	if(fakeAction==true)return;

	var tmp_arr = args.split("x");
	if(imInAFrame()==true){
		window.parent.fake_browserWidth = parseInt("" + tmp_arr[0], 10);
		window.parent.fake_browserHeight = parseInt("" + tmp_arr[1], 10);
		
		window.parent.gestisciOnResize();
	}
	else{
		if(thereIsAValidFrame()==false){
			fake_browserWidth = parseInt("" + tmp_arr[0], 10);
			fake_browserHeight = parseInt("" + tmp_arr[1], 10);
			gestisciOnResize();			
		}
	}
}


function changeTopObj(wizObj, fakeAction){
	
	var obj = document.getElementById(""+ wizObj.id);

	if(fakeAction==false){
		obj.style.top = fixMozPosition(parseInt(""+wizObj.top,10)) + "px";		
	}
	
	var tmp_obj = foundElementoFromObj(obj);
	if(tmp_obj){
		var tmp_arr = new Array(fixMozPosition(parseInt(""+wizObj.top,10)), tmp_obj[1], tmp_obj[2], tmp_obj[3], tmp_obj[4]);
		risettaArrayOrig(obj, tmp_arr);
	}
}

function changeLeftObj(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		obj.style.left = fixMozPosition(parseInt(""+wizObj.left,10)) + "px";
	}
	var tmp_obj = foundElementoFromObj(obj);
	if(tmp_obj){
		var tmp_arr = new Array(tmp_obj[0], fixMozPosition(parseInt(""+wizObj.left,10)), tmp_obj[2], tmp_obj[3], tmp_obj[4]);
		risettaArrayOrig(obj, tmp_arr);
	}
}
function changeWidthObj(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		obj.style.width = parseInt(""+wizObj.width,10) + "px";
	}

	var tmp_obj = foundElementoFromObj(obj);
	if(tmp_obj){
		var tmp_arr = new Array(tmp_obj[0], tmp_obj[1], parseInt(""+wizObj.width,10), tmp_obj[3], tmp_obj[4]);
		risettaArrayOrig(obj, tmp_arr);
	}
}
function changeHeightObj(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		obj.style.height = parseInt(""+wizObj.height,10) + "px";
	}

	var tmp_obj = foundElementoFromObj(obj);
	if(tmp_obj){
		var tmp_arr = new Array(tmp_obj[0], tmp_obj[1], tmp_obj[2], parseInt(""+wizObj.height,10), tmp_obj[4]);
		risettaArrayOrig(obj, tmp_arr);
	}
}
function changeTextObj(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(obj.value){
		if(fakeAction==false){
			obj.value=""+wizObj.label;
		}		
	}
	else{
		if(fakeAction==false){
			settaCaptionNew(obj,""+wizObj.label);
		}
	}
}
function changeTitle(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		settaTitle(obj,""+wizObj.title);
	}
}
function changeChecked(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		settaChecked(obj,""+wizObj.checked);
	}
}
function changeName(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(fakeAction==false){
		settaName(obj,""+wizObj.name);
	}
}

function changeFontSize(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(obj){
		if(fakeAction==false){
			settaFontSize(obj, wizObj.fontSize);
		}
	}
	var tmp_obj = foundElementoFromObj(obj);
	if(tmp_obj){
		var tmp_arr = new Array(tmp_obj[0], tmp_obj[1], tmp_obj[2], tmp_obj[3], wizObj.fontSize);
		risettaArrayOrig(obj, tmp_arr);
	}
}
function changeFontColor(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(obj){
		if(fakeAction==false){
			settaFontColor(obj, wizObj.fontColor);
		}
	}	
}

function changeBackColor(wizObj, fakeAction){
	var obj = document.getElementById(""+ wizObj.id);
	if(obj){
		if(fakeAction==false){
			settaBackgroundColor(obj, wizObj.backColor);
		}
	}		
}

function aggiungiEliminaObj(wizObj, fakeAction){
	if(fakeAction==true)return;
	
	var obj = document.getElementById(""+ wizObj.id);
	if(obj){
		var par = obj.parentNode;
		par.removeChild(obj);
	}
	else{
		//creo oggetto 
		alertDbg("wizObj.type = " + wizObj.type);
		switch(parseInt(""+wizObj.type,10)){
			case 0:
				if(!(startsWith(""+wizObj.id,"WLAY_ST_"))){
					wizObj.id = "WLAY_ST_" + wizObj.id;
				}

				alertDbg("creo elemento static!");
				obj = document.createElement("DIV");
				obj.innerHTML = "" + wizObj.id;
				obj.style.border = "1px solid black";
				
				break;
			case 1:
				if(!(startsWith(""+wizObj.id,"WLAY_TXT_"))){
					wizObj.id = "WLAY_TXT_" + wizObj.id;
				}

				alertDbg("creo elemento textbox!");
				obj = document.createElement("INPUT");
				obj.type = "text";
				break;
			case 2:
				if(!(startsWith(""+wizObj.id,"WLAY_BT_"))){
					wizObj.id = "WLAY_BT_" + wizObj.id;
				}

				alertDbg("creo elemento bottone!");
				obj = document.createElement("INPUT");
				obj.type = "button";
				obj.value = wizObj.id;
				obj.alt = wizObj.id;
				obj.title = wizObj.id;
				break;
			case 3:
				if(!(startsWith(""+wizObj.id,"WLAY_SEL_"))){
					wizObj.id = "WLAY_SEL_" + wizObj.id;
				}

				alertDbg("creo elemento select!");
				obj = document.createElement("DIV");
				obj2 = document.createElement("SELECT");
				obj.appendChild(obj2);

				break;
			case 4:
				if(!(startsWith(""+wizObj.id,"WLAY_CHK_"))){
					wizObj.id = "WLAY_CHK_" + wizObj.id;
				}

				alertDbg("creo elemento check!");
				obj = document.createElement("DIV");
				obj2 = document.createElement("INPUT");
				obj2.type = "checkbox";
				obj3 = document.createTextNode(""+wizObj.id);
				obj.appendChild(obj2);
				obj.appendChild(obj3);
				break;
			case 5:
				if(!(startsWith(""+wizObj.id,"WLAY_OPT_"))){
					wizObj.id = "WLAY_OPT_" + wizObj.id;
				}
				alertDbg("creo elemento radio!");
				obj = document.createElement("DIV");
				obj2 = document.createElement("INPUT");
				obj2.type = "radio";
				obj3 = document.createTextNode(""+wizObj.id);
				obj.appendChild(obj2);
				obj.appendChild(obj3);
				break;
			case 6:
				if(!(startsWith(""+wizObj.id,"WLAY_HR_"))){
					wizObj.id = "WLAY_HR_" + wizObj.id;
				}

				alertDbg("creo elemento hr!");
				obj = document.createElement("DIV");
				obj2 = document.createElement("HR");
				obj.appendChild(obj2);
				break;
			case 7:
				if(!(startsWith(""+wizObj.id,"WLAY_"))){
					wizObj.id = "WLAY_" + wizObj.id;
				}

				alertDbg("creo elemento container!");
				obj = document.createElement("DIV");
				obj.innerHTML = "&nbsp;X&nbsp;";
				
			default:
				alertDbg("elemento nn riconosciuto!");
				break;
		}
		if(obj){
			obj.id = wizObj.id;
			alertDbg("id = " + obj.id);
			obj.style.top = parseInt(""+wizObj.top,10) + "px";
			obj.style.left = parseInt(""+wizObj.left,10) + "px";
			obj.style.width = parseInt(""+wizObj.width,10) + "px";
			obj.style.height = parseInt(""+wizObj.height,10) + "px";
			obj.style.position = "absolute";
			obj.style.fontSize = "12px";
	
			if(obj.parentId=="BODY"){
				document.body.appendChild(obj);
			}
			else{
				var par_obj = document.getElementById(obj.parentId);
				if(par_obj){
					par_obj.appendChild(obj);
				}
				else{
					document.body.appendChild(obj);
				}
								
			}

			if(parseInt(""+wizObj.type,10)==7){
				if(document.getElementById(wizObj.id)){
					document.getElementById(wizObj.id).style.border = "1px solid black";
					document.getElementById(wizObj.id).style.borderColor = "black";
					document.getElementById(wizObj.id).style.borderWidth = "1px";
					document.getElementById(wizObj.id).style.borderStyle = "solid";
				}
			}
			
			firstTime=true;
			dynamicLayout();

			blinka(wizObj.id);
		}
		else alertDbg("obj is null!");
	}
}




var mBlkTimeout = null;
var mBlkId = null;
var mBlkCount = 0;
var mBlkBorder = "";
function blinka(tmpId){
	mBlkId = tmpId;
	mBlkCount = 0;
	if (mBlkTimeout) window.clearTimeout(mBlkTimeout);
	mBlkTimeout = 0;
	mBlkTimeout = window.setTimeout(blinka2, 100);
}
function blinka2(){
	if (mBlkTimeout) window.clearTimeout(mBlkTimeout)
	mBlkTimeout = 0;

	//alertDbg("blinka richiamato oldBorder = " + mBlkBorder);
	if(document.getElementById("" + mBlkId)){
		var tmp_obj = document.getElementById("" + mBlkId);
		if(mBlkCount==0){
			mBlkBorder = "" + tmp_obj.style.border;
			//alertDbg("Salvo oldBorder = " + mBlkBorder);
		}
		if(isPari(mBlkCount)){
			tmp_obj.style.border = "2px solid yellow";
		}
		else{
			if(mBlkBorder==""){
				tmp_obj.style.borderColor = tmp_obj.style.borderWidth = tmp_obj.style.borderStyle = '';
			}
			else {
				//alertDbg("setto old border = " + mBlkBorder);
				tmp_obj.style.border = "" + mBlkBorder;
			}
		}
	}
	mBlkCount++;
	if(mBlkCount>=6){
		if(mBlkBorder==""){
			tmp_obj.style.borderColor = tmp_obj.style.borderWidth = tmp_obj.style.borderStyle = '';
		}
		else {
			//alertDbg("setto old border 2 = " + mBlkBorder);
			tmp_obj.style.border = mBlkBorder;
		}
		return;
	}
	else{
		mBlkTimeout = window.setTimeout(blinka2, 300)
	}
}
function fixDoctypeDim(id, val){
	var rit = val;
	if(isDoctypeStandardMode(document)){
		var obj = document.getElementById(""+id);
		if(obj){
			var tmp_name =("" + obj.nodeName).toUpperCase();
			if(tmp_name=="INPUT"){
				var tmp_type =("" + obj.type).toUpperCase();
				if(tmp_type=="TEXT"){
					rit = rit - 6;
				}
			}
		}
	}
	return rit;
}

function isPari(numero){
	return (numero %2 == 1 ?  true : false);
}

function settaCaptionNew(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	
	switch(tmp_name){
		case "DIV":
			//studio composizione id
			var tmp_id = "" + obj.id;
			if(startsWith(tmp_id, "WLAY_ST")){
				 obj.innerHTML = txt;
				 return true;
			}
			if(startsWith(tmp_id, "WLAY_OPT")){
				 	var old_txt = stripHtmlTag(obj.innerHTML);
 					var new_txt = (""+obj.innerHTML).replace(""+old_txt,""+txt);
				 	obj.innerHTML = new_txt
   					return true;
			}
			if(startsWith(tmp_id, "WLAY_CHK_")){
				 	var old_txt = stripHtmlTag(obj.innerHTML);
 					var new_txt = (""+obj.innerHTML).replace(""+old_txt,""+txt);
				 	obj.innerHTML = new_txt
   					return true;
			}
			if(startsWith(tmp_id, "WLAY_IMG_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
   						if(children[i].nodeType==1){
   							//trovato oggetto immagine 
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.src = txt;
   						return true;
   					}
				}
				return false;
			}
			break;
		case "INPUT":
			if(startsWith(tmp_id, "WLAY_BT")){
				obj.value = txt;
				return true;
			}
			if(startsWith(tmp_id, "WLAY_TXT")){
				obj.value = txt;
				return true;
			}
			break;
		case "TEXTAREA":
			if(startsWith(tmp_id, "WLAY_TXTAR")){
				obj.value = txt;
				return true;
			}
			break;
	}
	return true;	
}



function settaTitle(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	switch(tmp_name){
		case "DIV":
			if(startsWith(tmp_id, "WLAY_ST_")){
				return false;
			}
			if(startsWith(tmp_id, "WLAY_OPT_")){
				return false;
			}
			if(startsWith(tmp_id, "WLAY_SEL_")){
				//TODO
				return false;
			}
			if(startsWith(tmp_id, "WLAY_CHK_")){
				return false;
			}			
			if(startsWith(tmp_id, "WLAY_HR_")){
				return false;
			}			
			if(startsWith(tmp_id, "WLAY_IMG_")){

				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
   						if(children[i].nodeType==1){
   							//trovato oggetto immagine 
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.title = txt;
   						fnd.alt = txt;
   						return true;	
   					}
				}
			}	
		 	break;
		case "INPUT":
			if(startsWith(tmp_id, "WLAY_BT")){
				obj.title = txt;
				obj.alt = txt;
				return true;	
			}
			if(startsWith(tmp_id, "WLAY_TXT")){
				return false;
			}
			break;
		case "TEXTAREA":
			if(startsWith(tmp_id, "WLAY_TXTAR")){
				return false;
			}
			break;
	}
	return false;
}




function settaChecked(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	switch(tmp_name){
		case "DIV":
			if(startsWith(tmp_id, "WLAY_ST_")){
				return false;
			}
			if(startsWith(tmp_id, "WLAY_OPT_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						if(txt=="true")
   							fnd.checked = true;
   						else 
   							fnd.checked = false;
   						return true;
   					}
				}
				return false;
			}
			if(startsWith(tmp_id, "WLAY_SEL_")){
				//TODO
				return false;
			}
			if(startsWith(tmp_id, "WLAY_CHK_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						if(txt=="true")
   							fnd.checked = true;
   						else 
   							fnd.checked = false;
   						return true;
   					}
				}
				return false;
			}
			
			if(startsWith(tmp_id, "WLAY_HR_")){
				return false;
			}			
			if(startsWith(tmp_id, "WLAY_IMG_")){
				return false;
			}	
		 	break;
		case "INPUT":
			if(startsWith(tmp_id, "WLAY_BT")){
				return false;
			}
			if(startsWith(tmp_id, "WLAY_TXT")){
				return false;
			}
			break;
		case "TEXTAREA":
			if(startsWith(tmp_id, "WLAY_TXTAR")){
				return false;
			}
			break;
	}
	return false;
}

function settaName(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	switch(tmp_name){
		case "DIV":
			if(startsWith(tmp_id, "WLAY_ST_")){
				return false;
			}
			if(startsWith(tmp_id, "WLAY_OPT_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
  						fnd.name = txt;
   						return true;
   					}
				}
			}
			if(startsWith(tmp_id, "WLAY_SEL_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
  						fnd.name = txt;
   						return true;
   					}
				}
			}
			if(startsWith(tmp_id, "WLAY_CHK_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
  						fnd.name = txt;
   						return true;
   					}
				}
			}
			
			if(startsWith(tmp_id, "WLAY_HR_")){
				return false;
			}			
			if(startsWith(tmp_id, "WLAY_IMG_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.name = txt;
   						return true;
   					}
				}
			}
			return false;
		 	break;
		case "INPUT":
			if(startsWith(tmp_id, "WLAY_BT")){
				//ritorno sempre obj.name alla fine
				obj.name = txt;
				return true;
			}
			if(startsWith(tmp_id, "WLAY_TXT")){
				//ritorno sempre obj.name alla fine
				obj.name = txt;
				return true;
			}
			break;
		case "TEXTAREA":
			if(startsWith(tmp_id, "WLAY_TXTAR")){
				//ritorno sempre obj.name alla fine
				obj.name = txt;
				return true;
			}
			break;
	}
	return false;	
}	


function settaFontSize(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	if(tmp_name=="DIV"){
			if(startsWith(tmp_id, "WLAY_SEL_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.style.fontSize = txt;
   						return true;
   					}
				}
				
			}
	}
	obj.style.fontSize = txt;
}

function settaFontColor(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	if(tmp_name=="DIV"){
			if(startsWith(tmp_id, "WLAY_SEL_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.style.color = txt;
   						return true;
   					}
				}
				
			}
	}
	obj.style.color = txt;
}


function settaBackgroundColor(obj, txt){
	var tmp_name =("" + obj.nodeName).toUpperCase();	
	var tmp_id = "" + obj.id;

	if(tmp_name=="DIV"){
			if(startsWith(tmp_id, "WLAY_SEL_")){
				if (obj.hasChildNodes()){
   					var children = obj.childNodes;
   					var fnd = null;
   					for (var i = 0; i < children.length; i++) {
	   					if(children[i].nodeType!=3){
   							//trovato oggetto checkbox
   							fnd = children[i];
   							break;
   						}
   					}
   					if(fnd!=null){
   						fnd.style.backgroundColor = txt;
   						return true;
   					}
				}
				
			}
	}

	obj.style.backgroundColor = txt;
}


function esisteId(tmpId){
	if(document.getElementById(tmpId))return true;
	return false;
}

function chiusaWinDbg(){
	try{
		if(dbg_win){
			dbg_win.close();
		}
	}
	catch(ex){}
	dbg_win = null;
	
}

function checkAndCacheUrl(tmp_url){
	var rit = ""+tmp_url;
	rit +=((rit.indexOf("?")!=-1)?"&":"?")+"tmphdata="+(new Date()).valueOf();
	return rit;
}
// -------------------------------------------------------------------------
// Program - end
// -------------------------------------------------------------------------



