function uHide() {
/*nasconde un numero arbitrario di elementi html impostando la proprietà display a 'none'*/  
  for (var z=0,len=uHide.arguments.length;z<len;z++) {
    ug(uHide.arguments[z]).style.display='none';
  }
}
function uShow() {
/*visualizza un numero arbitrario di elementi html impostando la proprietà display a 'block'*/  
  for (var z=0,len=uHide.arguments.length;z<len;z++) {
    ug(uHide.arguments[z]).style.display='block';
  }
}
  
function uIsUndefined(o) {
	if (typeof o=='undefined')
	  return true;
	return false;
}
function isUndefined(o) {
	return uIsUndefined(o);
}	

function uIsDefined(o) {
	if (typeof o=='undefined')
	  return false;
	return true;
}
function isDefined(o) {
	return uIsDefined(o);
}	
function uIsTrue(o) {
	if (isUndefined(o))
	  return false;
	else
	  if (o)
		  return true;
	return false;	
}	
function isTrue(o) {
	return uIsTrue(o);
}	

function uCLSep() { //column layout vertical separator
  return {width:10,layout:'form',bodyBorder:false,items:[{text:'ciao',style:'visibility:hidden'}]};
}  

function uRemoveCombo(oCombo) {
  oCombo.el.up('.x-form-item').remove(); 	
}

function uCombo(o) { //build a simple combo
  var store_target = new Ext.data.SimpleStore({
      fields: ['value', 'text'],
      data:o.data
  });
  var cfg={
    fieldLabel:'Targeting tag',width:120,store:store_target,editable:false,
    valueField:'value',displayField:'text',mode:'local',typeAhead:true,
    hiddenName:null,triggerAction:'all',
    lazyRender:true,listClass:'x-combo-list-small'
  };
  var target_combo=new Ext.form.ComboBox(Ext.apply(cfg,o));
  target_combo.setValue(1);
  return target_combo;
}  

function uSlider(o) {
  return Ext.apply({
    width:180,
    ctCls:'custom-slider',
    increment:10,
    minValue: 0,
    height:25,
    maxValue:100,
    value:50,
    xtype:'slider',
    plugins:new Ext.ux.SliderTip(o)
  },o);
}  

function uLabel(o) {
  return Ext.apply({fieldLabel:'label',xtype:'textfield',labelStyle:'text-align:left',style:'display:none'},o);
}


function uTL(o) { //column layout default builder
  var DEF={
    xtype:'tabpanel',
    activeTab: 0,
    defaults:{autoHeight:true,bodyStyle:'padding:10px'}, 
    deferredRender:true,
		layoutOnTabChange:true,
    style:'background:red',
  };
  o=Ext.apply(o,DEF);
  return o;
} 

function uCL(o) { //column layout default builder
  var DEF={
    layout:'column',
    bodyBorder:false
  };
  o=Ext.apply(o,DEF);
  return o;
}  


function uswf(o) {
  var background='#ffffff';
  if (typeof o.background!='undefined')
    background=o.background;
  var ss='';
	ss+='<div style="cursor:pointer">';
  ss+='<object width="'+o.width+'" height="'+o.height+'" align="" id="macromedia_flash_web_banner_example_1" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
  ss+='<param value="'+o.src+'" name="movie"/>'; 
  ss+='<param value="High" name="quality"/> <param value="ExactFit" name="scale"/> <param value="true" name="devicefont"/>';
  ss+='<param value="'+background+'" name="bgcolor"/>';
  ss+='<embed width="'+o.width+'" height="'+o.height+'" align="" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="macromedia_flash_web_banner_example_1" bgcolor="'+background+'" devicefont="true" scale="ExactFit" quality="High" src="'+o.src+'"/></object>';
	ss+='</div>';
  return ss;
}

function uGetPapa(p,sPapa) {
  var doDo=1;
  var count=0;
  while (doDo) {
    if (p.className==sPapa)
      return p;
    p=p.parentNode;
    if (!p)
      return;
    count++;
    if (count==30)
      return null;
  }  
}  

function ug(s) {
  return document.getElementById(s);
}  

function u_render_money(val) {
  if (!val)
    return "&euro; 0";
  var val=parseFloat(val);
  val=val.toFixed(2);
  var val = val.toString();
  var result='';
  var len = val.length;
  while (len > 3){
    result='.'+val.substr(len-3,3)+result;
    len-=3;
  }
  val=val.substr(0,len)+result;
  val=val.replace('..',',');

  return "&euro; "+val;
}

function u_render_percent(val) {
  if (!val)
    return "0,00%";
  var val=parseFloat(val);
  val=val.toFixed(2);
  var val = val.toString();
  val=val.replace('.',',');
  return val+'%';
}

function u_render_percent_int(val) {
  if (!val)
    return "0%";
  var val=parseInt(val);
  return val+'%';
}

function u_renderNumWithSeparator(val) {
  if (!val)
    return '0';
  var val = val.toString();
  var result='';
  var len = val.length;
  while (len > 3){
    result='.'+val.substr(len-3,3)+result;
    len-=3;
  }
  val=val.substr(0,len)+result;
	return val;
}	

function u_buildFormLabel(sLabel) {
  return {fieldLabel:sLabel,xtype:'textfield',labelStyle:'width:300px;font-weight:bold;text-align:left',style:'display:none'}   
}

function u_buildFormInfoLabel(sLabel,left,width) {
  return {fieldLabel:sLabel,xtype:'textfield',labelStyle:'width:'+width+'px;border:none;left:'+left+'px;font-weight:normal;font-size:10px;font-family:arial;text-align:right',style:'display:none'}   
}


function u_arrayJoin(a1,a2) {
  var a3=[];

  for (var z=0,zEnd=a1.length;z<zEnd;z++) {
    a3[a3.length]=a1[z];
  }
  for (var z=0,zEnd=a2.length;z<zEnd;z++) {
    a3[a3.length]=a2[z];
  }
  return a3;
}

function u_openTab(oTabParam) {
  
  var tabParams={
    id:'',
    closable:true,
    title:'',
    tabTip:'',
    callBack:'',
    url:'',
    params:{
      s:HR_SRC
    }
  }
  
  if (typeof oTabParam!='undefined') 
    oUDTab=oTabParam;
  
  for (var z in oUDTab) {
    if (z!='params') {
      if (oUDTab[z].needEval)
        tabParams[z]=eval(oUDTab[z].data);
      else  
        tabParams[z]=oUDTab[z];
    } else {
      for (var w in oUDTab.params) {
        if (oUDTab.params[w]==null) {  
          delete(tabParams.params[w])
        } else {    
          if (oUDTab.params[w].needEval)
            tabParams.params[w]=eval(oUDTab.params[w].data);
          else {
            tabParams.params[w]=oUDTab.params[w];
          }
        }    
      }
    }  
  }
  
  var t1=mainPanel.createTab(tabParams);
  t1.helpId='edit-list'; //force help id
  //but if there is any into the newTab config, use it instead
  if (typeof oUDTab!='undefined')
    if (oUDTab.helpId)
      t1.helpId=oUDTab.helpId;
  mainPanel.setActiveTab(t1); //auto enable first tab
  
}


function u_buildErrorMessage(sMsg,sName,sChars) {
  sName=sName.replace(/<img.*/,'');
  sMsg=sMsg.replace('%1',sName);
  sMsg=sMsg.replace('%2',sChars);

  return sMsg;  
}

function u_showErrorPopUp(msg,oOptions) {
  var oFn=null;
  if (typeof oOptions!='undefined') {
    if (typeof oOptions.fn!='undefined')
      oFn=oOptions.fn;
  }    
  Ext.Msg.show({
    title:'Error',
    msg:msg,
    icon:Ext.MessageBox.ERROR,buttons:Ext.MessageBox.OK,
    fn:oFn
  });  
}

function u_showOkPopUp(msg,oOptions) {
  var oFn=null;
  if (typeof oOptions!='undefined') {
    if (typeof oOptions.fn!='undefined')
      oFn=oOptions.fn;
  }    
  Ext.Msg.show({
    title:'Message',
    msg:msg,
    icon:Ext.MessageBox.INFO,buttons:Ext.MessageBox.OK,
    fn:oFn
  });  
}


function u_d(x,y) {

  if (typeof x=='undefined' && typeof y=='object')
    x={};
  
  if (typeof x=='object' && typeof y=='object') {
    for (var z in y) {
      if (typeof y[z]!='undefined')
        x[z]=y[z];
      /*
        if (typeof x[z]=='undefined' || x[z]==null)
          x[z]=y[z];
      */    
    }  
    return x;
  }
  if (typeof x !='undefined')
    return x
  return y;
}

function u_buildGridWindow(oO) {
  
  var config={
    buttons:[{text:'Close',handler:function(){tempWindow.close();if (oO.handler) oO.handler() } }],
    height:600,
    width:400,
    autoSaveAfterEdit:false
  }
  config=u_d(config,oO);
  
  var fields=oO.fields;
  var dupData=oO.arrayData;
  var W=config.width;
  var H=config.height;
  var title=oO.title;
  var clicksToEdit=(typeof oO.clicksToEdit!='undefined')?oO.clicksToEdit:0;
  
  var u_buildGridWindow_gridData=new GridData();
  var u_buildGridWindow_ge=new glo_editorGridPanel({gridData:u_buildGridWindow_gridData});
  u_buildGridWindow_gridData.setFields(fields); //init grid data
  var u_buildGridWindow_ds = new Ext.data.Store({ reader:u_buildGridWindow_gridData.getArrayReader() });
  u_buildGridWindow_ds.loadData(dupData);
  var u_buildGridWindow_cm = new Ext.grid.ColumnModel(u_buildGridWindow_gridData.getExtGridColumModel());
  
  var u_buildGridWindow_grid=u_buildGridWindow_ge.create({
    autoEl:false, //do not assign an el element to this grid since it's inside a window
    autoSaveAfterEdit:false,
    modifyButton:false,
    border:false,
    ds: u_buildGridWindow_ds,
    cm: u_buildGridWindow_cm,
    height:H-75,
    doTBar:false,
    clicksToEdit:config.clicksToEdit,
    //autoHeight:true,
    autoWidth:true,
    autoRender:false,
    addNewRecordsAlwaysOnBottom:true, //if true all records will be inserted on bottom no matter where the selected row is
    bbar:false
  });
  
  var tempWindow=new Ext.Window({
    gGrid:u_buildGridWindow_grid,
    title:title,
    modal:true,
    closable:true,
    width:W,
    height:H,
    minWidth:config.minWidth,
    minHeight:config.minHeight,
    resizable:config.resizable,
    border:true,
    plain:true,
    draggable: true,
    resizable: false,
    shadow:true,
    buttons:config.buttons,
    items:[u_buildGridWindow_grid],
    closeAction:'close',  //force auto-destroy with close prop. so it is always updated
    //buttons:[btn0,btn1]
  });
  tempWindow.on('close',
    function(){
      if (oO.onCloseHandler)
        oO.onCloseHandler();
    }  
  )
  tempWindow.show();
  return tempWindow;
}

function u_pasteEditableIcon(s) {
  return s+'<img class="headercolumn_editIcon" src="app/img/icon_edit.png">';
}

function u_objAppend(oDest,oSrc) {
//paste all found properties from oSrc (overwriting values)
//to oDest
  for (var z in oSrc) {
    oDest[z]=oSrc[z];
  }
  return oDest;
}

//define object
var _DEFINE = (function(){
	function toString(name, value){
		return	"const " + name + "=" + (
			/^(null|true|false|(\+|\-)?\d+(\.\d+)?)$/.test(value = String(value)) ? value : '"' + replace(value) + '"'
		)
	};
	var	define, replace;
	try {
		eval("const e=1");
		replace = function(value){
			var	replace = {"\x08":"b","\x0A":"\\n","\x0B":"v","\x0C":"f","\x0D":"\\r",'"':'"',"\\":"\\"};
			return	value.replace(/\x08|[\x0A-\x0D]|"|\\/g, function(value){return	"\\"+replace[value]});
		}
		define = function(name, value){
			var	script = document.createElement("script");
			script.type = "text/javascript";
			script.appendChild(document.createTextNode(toString(name, value)));
			document.documentElement.appendChild(script);
			document.documentElement.removeChild(script);
		}
	}catch(e){
		replace = function(value){
			var	replace = {"\x0A":"\\n", "\x0D":"\\r"};
			return	value.replace(/"/g, '""').replace(/\n|\r/g, function(value){return replace[value]});
		}
		define = this.execScript ?	function(name, value){
			execScript(toString(name, value), "VBScript");
		}:
		function(name, value){
			eval(toString(name, value).substring(6));
		}
	}
	return	define;
})(); 