// ver. 1.1
//alert("Attenzione!\nCausa sviluppo nuove funzionalit�, si potrebbero temporaneamente riscontrare malfunzionamenti del gestionale")
Function.prototype.bind = function(object) {

  var __method = this;

  return function() {
    __method.apply(object, arguments);
  }
}
if (!Function.prototype.apply) {
  Function.prototype.apply = function(oScope, args) {
    var sarg = [];
    var rtrn, call;
    if (!oScope) oScope = window;
    if (!args) args = [];
    for (var i = 0; i < args.length; i++) {
      sarg[i] = "args["+i+"]";
    }     call = "oScope.__applyTemp__(" + sarg.join(",") + ");";
    oScope.__applyTemp__ = this;
    rtrn = eval(call);
    return rtrn;
  }
}

/*****************************/
/* Estensione metodi String  */
/*****************************/

String.prototype.trim = function(){
	var output = this.ltrim();
	output = output.rtrim();
	return output;
	}
String.prototype.ltrim = function(){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
	return"";
	}
	var v_length = this.length;
	var strTemp = "";

	var iTemp = 0;

	while(iTemp < v_length){
	if(this.charAt(iTemp) == w_space){
	}
	else{
	strTemp = this.substring(iTemp,v_length);
	break;
	}
	iTemp = iTemp + 1;
	}
	return strTemp;
}

String.prototype.rtrim = function(){
	var w_space = String.fromCharCode(32);
	var v_length = this.length;
	var strTemp = "";
	if(v_length < 0){
	return"";
	}
	var iTemp = v_length -1;

	while(iTemp > -1){
	if(this.charAt(iTemp) == w_space){
	}
	else{
	strTemp = this.substring(0,iTemp +1);
	break;
	}
	iTemp = iTemp-1;

	}
	return strTemp;

}

String.prototype.isNumeric = function(){
	var filter  = /^([0-9])+$/;
	if (!filter.test(this)) {
		return false;
		}else{
		return true;
	};
}
String.prototype.replaceAll = function(find,toStr){
	var strTmp = this.split(find).join(toStr)
	return strTmp;

}

String.prototype.formatGuid = function(){
	return this.replace("{","").replace("}","")

}
/*******************************************/

Date.prototype.toString = function(stringIn){

	var giorno = this.getDate();
	var mese = parseInt(this.getMonth())+1;
	var anno = this.getFullYear();

	if (giorno.toString().length==1) giorno = "0"+giorno.toString();
	if (mese.toString().length==1) mese = "0"+mese.toString();


	stringIn = stringIn.replace("dd",giorno);
	stringIn = stringIn.replace("MM",mese);
	stringIn = stringIn.replace("yyyy",anno);

	return stringIn;

};

Date.prototype.addDay =  function(n){
	var day=parseInt(this.getDate()+n);
	return new Date(this.getFullYear(),this.getMonth(),day);
}
Date.prototype.addMonth=function(n){
	var month=parseInt(this.getMonth()+n);
	return new Date(this.getFullYear(),month,this.getDate());
}
Date.prototype.addYear=function(n){
	var year=parseInt(this.getFullYear()+n);
	return new Date(year,this.getMonth(),this.getDate());
}
// TODO da verificare, ho cambiato object in String per problemi con tool-man
Number.prototype.convertToHTML = function(){
	return this.toString().replace(".",",");
};

Number.prototype.toInt = function(){
	return parseInt(this);
};

// n rappresente il n di cifre dopo la virgola
Number.prototype.toFloat = function(){

	return parseFloat(this);
};
String.prototype.convertToHTML = function(){
	return this.toString().replace(".",",");
};

String.prototype.toInt = function(){
	return parseInt(this);
};

// n rappresente il n di cifre dopo la virgola
String.prototype.toFloat = function(){

	return parseFloat(this);
};

/******************************/
/* Estensione metodi Document */
/******************************/

document.getElementInformations = function(el){


	var lw = el.offsetWidth;
	var lh = el.offsetHeight;

	for (var lx=0,ly=0;el!=null;
	  lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);
	return {left:lx,top:ly,height:lh,width:lw}
}
document.getElementsByClassName = function(strClassName){

	oElm = this;
	strTagName = "*"


    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
    var arrReturnElements = new Array();
    strClassName = strClassName.replace(/\-/g, "\\-");
	strClassName = strClassName.replace("[", "\\[");
	strClassName = strClassName.replace("]", "\\]");

    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
    var oElement;
    for(var i=0; i<arrElements.length; i++){
        oElement = arrElements[i];

        if(oRegExp.test(oElement.className)){

            arrReturnElements.push(oElement);
        }
    }
    return (arrReturnElements)

}


document.forceMaxHeight = function(className){
	var div=document.getElementsByClassName(className);
	var valMax;
	valMax=0;
	for(var i=0;i<div.length;i++){
		var altezza=document.getElementInformations(div[i]).height;
		if(altezza>valMax) valMax=altezza;
	}
	for(var j=0;j<div.length;j++){
		div[j].style.height=valMax+'px';
	}

	}
document.hideElement =  function(idElement,ritardoMilSec){
	if (ritardoMilSec!=null) {
		var a = setTimeout("document.hideElement('"+idElement+"')",ritardoMilSec);
	}else{
		var objElement = document.getElementById(idElement);
		objElement.style.visibility="hidden"
	}

}
/*******************************************/
var _e3MouseCoords = function(){
	this.x = 0;
	this.y = 0;

};
var e3MouseCoords = new _e3MouseCoords();

var _e3Utility = function(){
	this.startMouseMove = function (){
		var IE = document.all?true:false
		if (!IE) document.captureEvents(Event.MOUSEMOVE)
		document.onmousemove = e3Utility.getMouseXY;

	};
	this.getMouseXY = function(e) {
		var ie = document.all?true:false;
		if (ie) { // grab the x-y pos.s if browser is IE
			e3MouseCoords.x = event.clientX + document.documentElement.scrollLeft
			e3MouseCoords.y = event.clientY + document.documentElement.scrollTop;

		} else {  // grab the x-y pos.s if browser is NS
			e3MouseCoords.x = e.pageX
			e3MouseCoords.y = e.pageY
		}
		// catch possible negative values in NS4
		if (e3MouseCoords.x < 0){e3MouseCoords.x = 0}
		if (e3MouseCoords.y < 0){e3MouseCoords.y = 0}

		return true
	}

	this.testValueArray = function(array,value){
		var output = null;
		for (var i=0;i<array.length;i++){

			if (array[i].toString()==value.toString())
			{
					output = i;
					break;
			}

		};
		return output;
	};

	/* aggiunta, rimozione, sostituzione e verifica presenza classe dei fogli di stile in un elemento*/
	this.jscss = function(action,object,class1,class2){
		switch (action){
			case 'swap':
				object.className=!this.jscss('check',object,class1)?object.className.replace(c2,class1):
				object.className.replace(class1,c2);
			break;
			case 'add':
				if(!this.jscss('check',object,class1)){object.className+=object.className?' '+class1:class1;}
			break;
			case 'remove':
				var rep=object.className.match(' '+class1)?' '+class1:class1;
				object.className=object.className.replace(rep,'');
			break;
			case 'check':
				var className = object.className;

				if (className.length>0){
					var vericifa = new RegExp(eval("/"+class1+" /"));
					return vericifa.test(className+" ");
				}else{

					return false;
					}
			break;

			};
		}

	/* Ricava position reale */
	this.getStylePosition= function (el) {

		if(el.currentStyle)
			return el.currentStyle.position;
		if(document.defaultView)
			return document.defaultView.getComputedStyle(el, '').getPropertyValue("position");
		return false;
	}
	/* Aggiunta eventi*/

	this.addEvent  = function(obj, evType, fn)
			{
				if (obj.addEventListener){
				   obj.addEventListener(evType, fn, true);

				   return true;
				 } else if (obj.attachEvent){
				   var r = obj.attachEvent("on"+evType, fn);
				   return r;
				 } else {
				   return false;
				 }
			}
	/* Rimozione eventi*/
	this.removeEvent  = function(obj, evType, fn)
		{
			if ( obj.detachEvent ) {
				obj.detachEvent( 'on'+evType, obj[evType+fn] );
				obj[evType+fn] = null;
			} else
				obj.removeEventListener( evType, fn, false );
		}
	/* Cerca attributi all'interno dell'attributo class, per mantenere valido il documento XHTML */
	/*this.attribute = new _e3Attribute();*/


	this.findAll = function(){

		var value = this.attribute.findFromCss("popup","A");
		var elencoTag = this.attribute.resultSearch;

		for (var i=0;i<elencoTag.length;i++){
			elencoTag[i].onclick = this.Bind(this.popup, elencoTag[i]);

		}

		var value = this.attribute.findFromCss("visDiv","INPUT");

		var elencoTag = this.attribute.resultSearch;

		for (var i=0;i<elencoTag.length;i++){
			elencoTag[i].onclick = this.Bind(this.visDiv, elencoTag[i]);
			this.visDiv(elencoTag[i]);
		}

	}

	this.visDiv = function(obj){

		var visDiv = obj.getAttribute("visDiv");
		var visDivObj = document.getElementById(visDiv);
		if (visDivObj){
			if (obj.checked){
				visDivObj.style.display="block";
			}else{
				visDivObj.style.display="none";
			}
		}
	}

	this.Bind=function(fn, arg)
	{
  	return function () { return fn(arg);};
	}


	this.popup=function(obj){

		var valori = obj.getAttribute("popup").split(",");
		var name = valori[0];
		var width = valori[1];
		var height = valori[2];

		var popup = window.open(obj.href,"popup","WIDTH="+width+",HEIGHT="+height);
		popup.focus();

		return false;
		}
	// Restituisce il posizionamento assoulo di un elemento all'interno della pagina
	// !!!! deprecata
	this.getDim = function(el){
		var lw = el.offsetWidth;
		var lh = el.offsetHeight;

		for (var lx=0,ly=0;el!=null;
		  lx+=el.offsetLeft,ly+=el.offsetTop,el=el.offsetParent);


		return {x:lx,y:ly,w:lw,h:lh}
  	}

	this.tableAlign=function(){
		var tables=document.getElementsByClassName("[align]");

		var columnWidth = new Array();
		var rows=new Array();

		for(var i=0;i<tables.length;i++){
			var tmpRow = tables[i].getElementsByTagName('tr')
			for(var i2=0;i2<tmpRow.length;i2++){

				rows.push(tmpRow[i2]);
			}
		}

		for(var i=0;i<rows.length;i++){
			var colonne = rows[i].getElementsByTagName("TD");
			for (var i2=0;i2<colonne.length;i2++){

				var tmpWidth = document.getElementInformations(colonne[i2]).width;
				if (isNaN(columnWidth[i2])) columnWidth[i2] = 0

				if (tmpWidth > columnWidth[i2]){

					columnWidth[i2] = tmpWidth;
				}

			}

		}
		for (var i=0;i<columnWidth.length;i++){

			for(var i2=0;i2<rows.length;i2++){
				//rows[i2].getElementsByTagName('td')[i].style.backgroundColor = "#FF0";
				rows[i2].getElementsByTagName('td')[i].style.width=columnWidth[i];
			}
		}

	};
	this.selectAllText = function(fieldName) {


	  var tempval=eval("document."+fieldName);

	  tempval.focus();
	  tempval.select();
	}

	this.setHash=function(className){


			var allElm;
			if (className==null){
				allElm=document.getElementsByTagName("*");
			}else{
				allElm=document.getElementsByClassName(className);
			}

			for(var i=0;i<allElm.length;i++){
				if(!allElm[i].id){
					allElm[i].id='!hash'+this.idHash;
					this.idHash++;
					if (className!=null){
						allElm[i].getElementsByTagName('div')[0].id='!hash'+this.idHash;
						this.idHash++
					}
				}
			}



	}
	this.idHash = 1;
	this.attachNote=function(el,msOver,msOut,visClass,hidClass){

		var a=document.getElementsByClassName(el);

		for(var i=0;i<a.length;i++){
			var divObj = a[i].getElementsByTagName('div')[0];
			if (divObj){
				if (!divObj.id){
					divObj.id="!hash"+this.idHash;
					this.idHash++
				}
			}

			a[i].onmouseover=function(){
				var nChild=this.getElementsByTagName('div')[0];
				if(nChild){

					var ids=nChild.id;
					e3Utility.showHide(ids,true,msOver,0,visClass,hidClass);
				}
			}
			a[i].onmouseout=function(){
				var nChild=this.getElementsByTagName('div')[0];
				if(nChild){
					var ids=nChild.id;
					e3Utility.showHide(ids,false,msOut,1,visClass,hidClass);
				}
			}
		}
		};

	this.getCookie = function(nome)  {
	var ricerca = nome + "=";
	// quello che devo cercare nella stringa

	if (document.cookie.length > 0) {
	   //se esistono i cookies

	   inizio = document.cookie.indexOf(ricerca);
	   if (inizio != -1) {
	   	  inizio += ricerca.length;
		  // mi metto all'inizio del valore
		  fine = document.cookie.indexOf(";", inizio);
		  // trovo la fine del valore
		  fine = (fine==-1) ? document.cookie.length : fine;
		  //.. che potrebbe essere anche la fine della stringa

		  return unescape(document.cookie.substring(inizio, fine));
		  // missione compiuta!
		  }
		}
		return "";
		// se proprio non c'erano cookies

	}

	this.setCookie = function(name, value) {
		var todayDate = largeExpDate = new Date ();
		largeExpDate.setTime(todayDate.getTime() + 365 * 24 * 3600 * 1000);
		document.cookie = name+'='+escape(value)+'; expires=' + largeExpDate.toGMTString();
	};

	this.elToIdArray = function(ArrayObj){
		var idArray = new Array();
		for (var i=0;i<ArrayObj.length;i++){
			idArray.push(ArrayObj[i].id);
		};
		return idArray;
	};
	this.showHideTimeout;

	this.showHide=function(divId,visibility,ms,show,visClass,hidClass){
		var div=document.getElementById(divId);
		if(!visClass) visClass='visible';
		if(!hidClass) hidClass='';
		switch (show){
			case 0:
				if(!ms) var milliSec=1000;
				else var milliSec=ms;
				div.removeAttribute("setTimeout");
				this.showHideTimeout = setTimeout('e3Utility.showHide("'+divId+'",'+visibility+','+milliSec+',"","'+visClass+'","'+hidClass+'")',milliSec);
				return false;
			case 1:
				if(!ms) var milliSec=200;
				else var milliSec=ms;
				clearTimeout(this.showHideTimeout)
				div.setAttribute("setTimeout","stop");
				this.showHideTimeout = setTimeout('e3Utility.showHide("'+divId+'",'+visibility+','+milliSec+',"","'+visClass+'","'+hidClass+'")',milliSec);
				return false;
			default:
				var allElm=document.getElementsByTagName('DIV');
				for(var i=0;i<allElm.length;i++){

					if(allElm[i].getAttribute("setTimeout")!=null){
						allElm[i].className=hidClass;
					}
				}
				if(visibility){
					div.className=visClass;
				}
		}



	}

	/*********************************************************/
	/* Definisce il metodo scatenato all'onload della pagina */
	/*********************************************************/

	this.Main = function(source){
			this.addEvent(window,"load",source);
			try{
				if (e3Validate)	{
					this.addEvent(window,"load",function(){e3Validate.verifyForm();});

				}
			}catch(error){}
		}

	};



var e3Utility = new _e3Utility();

e3Utility.Main (
	function(){
				//e3Utility.findAll();
				//e3Utility.tableAlign();


				//document.forceMaxHeight("[height]");
		}
	)

// Apertuta pop-up dettaglio
function e3_dettaglio(url, name, w, h)
{
	window.open(url, name, 'scrollbars=yes,resizable=yes,width='+w+',height='+h+',status=no,location=no,toolbar=no');
	return false;
}
