//#IF b=gecko $import:Scripts/Gecko.IEExtension.js
/*
    //Array.indexOf(e)
    //Array.addRange(r)
    //Array.add(e)
    //Array.remove(e)
    //Array.replace(oldE,newE)
    //Array.contains(e)
    //Array.find(matchCallback)
    //Array.findAll(matchCallback)
    //Array.foreach(callback)
    //Array.trueForAny(matchValue)
    //Array.trueForAll(matchValue)
    //Array.match(matchCallback)
    //String.formatNumber(decimals,decimalChar,sepChar)
    //String.isNumber()
    //String.isDate()
    //String.splitEx()
    //String.splitPairs(pairSeparator,innerSeparator)
    //String.toDateEx(dateSeparator,timeSeparator,format)
    //String.toDate(format)
    //Object.isElement()
*/
if(typeof(isIE) == 'undefined') isIE = (navigator.userAgent.toLowerCase().indexOf("msie") != -1) ;
if(typeof(isMoz) == 'undefined')  isMoz = !isIE;
/********	DATE ********/
Date.prototype.shortDate=function (){
   var s = new String();
   s += this.getDate() + "/";                   //Get day
   s += (this.getMonth() + 1) + "/";            //Get month
   s += this.getYear();                         //Get year.
   return s;
}
/********	ARRAY ********/

Array.prototype.indexOf= function(e){
    for(var i=0; i<this.length;i++) {
        if(this[i]==e) return i;
    }
    return -1;
}

Array.prototype.indexOfInvariant = function(e){ //invariant case
    for(var i=0; i<this.length;i++) {
        if(this[i].toString().invariantCompare(e.toString())==0) return i;
    }
    return -1;
}
Array.prototype.addRange = function(r){
    for(var i=0; i<r.length;i++) {
        this.add(r[i]);
    }
}

Array.prototype.add = function(e){
    this[this.length]=e;
	return e
}

Array.prototype.remove = function(e){
    var i=this.indexOf(e);
    if (i!=-1) this.splice(i,1);
}

Array.prototype.replace = function(oldE,newE){
    var i=this.indexOf(oldE);
    if (i!=-1) this.splice(i,1,newE);
}

Array.prototype.contains = function(e){
	for(var i=0; i<this.length;i++) {if(this[i]==e) return true;}
    return false;
}

Array.prototype.find = function(matchCallback){
    if(typeof(matchCallback)=="function"){
        for(var i=0; i<this.length;i++) {
            if(matchCallback(this[i])) return this[i];
        }
    }
    return null;
}



Array.prototype.findAll  = function(matchCallback){
    var r=[];
    if(typeof(matchCallback)=="function"){
        for(var i=0; i<this.length;i++) {
            if(matchCallback(this[i])) r.add(this[i]);
        }
    }
    return r;
}

Array.prototype.foreach = function(callback){
    if(typeof(callback)=="function"){
        for(var i=0; i<this.length;i++) {
            callback(this[i]);
        }
    }
}

Array.prototype.trueForAny = function(matchValue){
    for(var i=0; i<this.length;i++) {
        if(this[i]==matchValue) return true;
    }
    return false;
}



Array.prototype.trueForAll = function(matchValue){
    for(var i=0; i<this.length;i++) {
        if(this[i]!=matchValue) return false;
    }
    return true;
}



Array.prototype.match = function(matchCallback){
    if(typeof(matchCallback)=="function"){
        for(var i=0; i<this.length;i++) {
           if(matchCallback(this[i])) return true;
        }
    }
    return false;
}



Array.prototype.toElements = function (){
    var a = []
    for(var i=0; i<this.length;i++) {
            var e= $(this[i]);
            if(!isNull(e)) a.add(e);
    }
    return  a;
}

Array.prototype.clear = function (){
	 this.length = 0;
}

Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0; i < iterable.length; i++)
      results.push(iterable[i]);
    return results;
  }
}



/********	STRING ********/
String.prototype.formatNumber = function (decimals,decimalChar,sepChar) {
	var s = this;
	
	s = s.replace(new RegExp("\\" + sepChar + "+","g"),"");
	if (decimalChar!=".")  s = s.replace(new RegExp("\\" + decimalChar + "+","g"),".");
	var parts = s.split (".");
	if (parts.length == 2)  {
		parts[1] = parts[1].substring(0,decimals);
	}
	return parts.join(".");
}

String.prototype.toURI= function(){
	try{
	return encodeURIComponent(this);
	}
	catch(e) {
	return this;
	}
}

String.prototype.fromURI= function(){
	if(this==null||this=='') return '';
	return decodeURIComponent(this);
}

String.prototype.surround = function (sBefore,sEnd){
	return sBefore + this + sEnd;
}
String.prototype.isNumber = function() {
	if(this==null||this=='') return false;
	var re=new RegExp("^[\\d\\.\\-]+$");
	return re.test(this);
}
String.prototype.invariantCompare = function (strCompare){
	if (this.toLower()>strCompare.toLower()) return 1;
	else if (this.toLower()<strCompare.toLower()) return -1;
	else return 0;
}

String.prototype.inArray = function (array){
	return array.contains(this)
}

String.prototype.isDate  = function () {   
    var patterns=[
        "^\\d{1,2}[\\s\\S]{1}\\d{1,2}[\\s\\S]{1}\\d{4}\\s*[\\d\\.\\:UTCGM\\s]*$",
        "^\\d{4}[\\s\\S]{1}\\d{1,2}[\\s\\S]{1}\\d{1,2}\\s*[\\d\\.\\:UTCGM\\s]*$"  
    ]
    var mFunc=new Function("pattern","var re=new RegExp (pattern);return re.test('"+ escape(this) +"');")
    return patterns.match(mFunc);
}

String.prototype.splitEx=function (){    
//split che accetta piĆ¹ caratteri di separazione
    if(arguments.length>0){
    var temp=[this];
         for(var i=0; i<arguments.length;i++) {                
            var separator=arguments[i];
            var innerTemp=[];
            for(var n=0; n<temp.length;n++) {
                innerTemp.addRange(temp[n].split(separator));
            }
            temp=innerTemp;
         }
    }
    return temp;
}

String.prototype.splitPairs=function(pairSeparator,innerSeparator){
    var pairs=this.split(pairSeparator);
    var result=new Object;
    for(var i=0; i<pairs.length;i++) {
        var kv=pairs[i].split(innerSeparator);
        if (kv.length!=0){
            var k= kv[0];
            var v= (kv.length>1)? kv[1] : null;
            result[k]=v;            
        }
    }        
    return  result;
}

String.prototype.toDateEx = function (dateSeparator,timeSeparator,format){
    var formatArray=format.splitEx(dateSeparator,timeSeparator," ");
    var valuesArray=this.splitEx(dateSeparator,timeSeparator," ");
    var formatFindPos=function(){
        for(var i=0;i<arguments.length;i++){
            var p = formatArray.indexOf(arguments[i]);
            if(p!=-1) return p
        }
        return  -1;
    }
    var positions = []
    positions["d"]=formatFindPos("d","dd");
    positions["M"]=formatFindPos("M","MM");
    positions["y"]=formatFindPos("y","yy","yyyy");
    positions["h"]=formatFindPos("h","hh");
    positions["m"]=formatFindPos("m","mm");
    positions["s"]=formatFindPos("s","ss");
    var valuesFind=function(token){
        var p=positions[token];
        if(p==-1) return  0;
        else return  valuesArray[p];
    }
    var d,m,y,h,M,s    
    d = valuesFind("d");
    M = valuesFind("M");
    y = valuesFind("y");
    h = valuesFind("h");
    m = valuesFind("m");    
    s = valuesFind("s");
    if([y,M,d].trueForAny(0)) return  null;
    else return  new Date(y,M-1,d,h,m,s);    
}

String.prototype.toDate = function (format){
    return this.toDateEx("/",":",format);
}
String.prototype.toLower= function (){
    return this.toLowerCase();
}

String.prototype.toUpper= function (){
    return this.toUpperCase();
}

String.prototype.equals=function(s){
    return (this.toLower()==s.toString().toLower())
}


String.prototype.startsWith=function(s){
    return  (this.substring(0,s.length).toLower()==s.toLower());
}

String.prototype.endsWith=function(s){
    return  this.substr(this.length - s.length).toLower()==s.toLower();
}

String.prototype.Trim=function () {
return this.replace(/\s+$|^\s+/g,"");
}

String.prototype.RTrim=function () {
return this.replace(/^\s+/,"");
}

String.prototype.LTrim=function () {
return this.replace(/\s+$/,"");
}
   

String.prototype.truncate = function (maxlength,paddingchar){
	var p = paddingchar || "...";
	if(this.length<=maxlength) return  this;
	else return  this.substring(0,maxlength) + p;
}

Number.prototype.round = function (precision) {
	var number = this;
    if (precision == undefined) precision = 2;
    var sign = (number < 0) ? -1 : 1;
    var multiplier = Math.pow(10, precision);
    var result = Math.abs(number);
    result = Math.floor((result * multiplier) + .5000001) / multiplier;
    if (sign < 0) result = result * -1;
    return result;
}

Number.prototype.formatBytes = function (precision) {
	var giga_limit = 2 << 29;
	var mega_limit = 2 << 19;
	var kilo_limit = 2 << 9;
    if (precision == undefined) precision = 2;
	var number = this;
    if (number < 0)
        throw new Error("formatBytes(precision): Negative this not allowed in format_bytes.");
    var suffix = "";
    if (number > giga_limit) {
        number /= giga_limit;
        suffix = "Gb";
    }
    else if (this > mega_limit) {
        number /= mega_limit;
        suffix = "Mb";
    }
    else if (this > kilo_limit) {
        number /= kilo_limit;
        suffix = "Kb";
    }
    return number.round(precision).toLocaleString() + " " + suffix;
}

function extend (destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
}
Object.prototype.extend=function(base){return Object.extend(this,base)}
Object.extend=function(destination, source) {
    extend(destination, source);
    return  destination;
}

/********	SHORTCUTS ********/

var $ = getObj = function (e){ /*Shortcut per getElementById*/
    var obj= null;
    if(typeof(e)=="string"){
        obj = document.getElementById(e);
    }
    if(typeof(e)=="object"){
        obj=e
    }
    return  obj;
}
$.eval = function (s){
	eval("var obj=" + s);
	return obj;
}
function formatInt(d){
	var s = new Number(d).toLocaleString();
	if (navigator.systemLanguage=="it") {
		var p = s.indexOf(",");
		if(p != -1) s = s.substring(0,p);
	}
	return s;
} 

function DOMElement(e){
    this.base=getObj(e);
    extend(this,this.base);  
}

 //restituisce tutti gli elementi DOM di un oggetto
DOMElement.prototype.getElements=function(){
        var founds=[];
        this.private_getElementsOf(founds,this,null);
        return  founds;    
    }
//restituisce tutti gli elementi DOM di un oggetto con una data classe
DOMElement.prototype.getElementsByClassName=function(classname){
        var founds=[];    
                classname=classname.toLower();
                var matchFunction=function(e){        
                    if(typeof(e.className)!='undefined'){                    
                         return  (e.className.toLower()==classname);
                    }
                    return  false;
                }  // end matchFunction   
            this.private_getElementsOf(founds,this.base,matchFunction); 
        return  founds;    
    }  
//restituisce tutti gli elementi DOM di un oggetto con un dato attributo
DOMElement.prototype.getElementsByAttribute=function(attributeName,attributeValue){
        var founds=[];
                var matchFunction=function(e){					
                    var attrValue = e.getAttribute(attributeName,0);
                    if(attrValue!=null){
						if(isNull(attributeValue)) return true;
                        if(typeof(attributeValue)=="string")
                            return  (attrValue.equals(attributeValue));
                        else return  attrValue=attributeValue;
                    }
                    return  false;
                }  // end matchFunction
            this.private_getElementsOf(founds,this.base,matchFunction);
        return  founds;
    }
    
//Private 
DOMElement.prototype.private_getElementsOf=function (founds,current,matchFunction){
        if(current.nodeType==1){
            if(matchFunction==null) founds.add(current);
            else {
                if(matchFunction(current)) founds.add(current);
                }
            for(var i=0;i < current.childNodes.length;i++){
                this.private_getElementsOf(founds,current.childNodes[i],matchFunction);
            }
        }
    } 
    
window.getElementsByClassName=
document.getElementsByClassName= function(classname,parentElement){
    var DOM = new DOMElement(getObj(parentElement) || document.body);
    return  DOM.getElementsByClassName(classname);
}

window.getElementsByAttribute=
document.getElementsByAttribute= function(attributeName,attributeValue,parentElement){
    var DOM = new DOMElement(getObj(parentElement) || document.body);
    return  DOM.getElementsByAttribute(attributeName,attributeValue);
}

if(!window.attachEvent) { //attachEvent enabled for non IE
Window.prototype.attachEvent =Document.prototype.attachEvent = HTMLElement.prototype.attachEvent = function($name, $handler) {this.addEventListener($name.slice(2), $handler, false)}
Window.prototype.removeEvent =	Document.prototype.removeEvent = HTMLElement.prototype.removeEvent = function($name, $handler) {this.removeEventListener($name.slice(2), $handler, false)}
}

function isObject(obj){return obj instanceof Object}
function isArray(obj){return obj instanceof Array}
function isFunction(f){return  (typeof(f)=='function')}
function isString(obj){return (typeof(obj)=='string')}
function isNull(obj){ return (obj ==null)} // non funziona per undefined
function isNullString(obj){return (isNull(obj) || obj.toString() =="")}

if (!window.Element) var $E = Element = new Object();
else var $E = window.Element;

Object.extend(Element,{
      preventDefault : function (){
        try{window.event.preventDefault();}
        catch(ex){}
      },
	  getElements:function (parent) {
	  	var e = $(parent);
		var founds = [];
		for(var i=0;i<e.childNodes.length;i++){
			var child = e.childNodes[i];
			if(child.nodeType==1) founds.add(child);
		}
		return founds;
	  },
	  getElementsByClassName:function (element,classname){
	  	  var e = $(element);
		  var founds = [];
		  for(var i=0;i<e.childNodes.length;i++){
		  	var child = e.childNodes[i];
			if(child.nodeType==1 && child.className && child.className.toLower()==classname.toLower()) {
				founds.add(child);
			}
		  }
		  return founds;
	  },
	  getChildElementsByClassName:function (element,classname){
	  	  var e = $(element);
		  var DOM = new DOMElement(e);
    	  return  DOM.getElementsByClassName(classname);
	  },
	  classover:function(element,normalClass,overClass) {
	  	var e = $(element);
		if(!overClass) overClass = normalClass + '_sel';
		e.className = normalClass;
		e.onmouseover=new Function("this.className='" + overClass + "'")
		e.onmouseout=new Function("this.className='" + normalClass + "'")
	  },
	  value:function (id){
	  	if(isNull($(id))) return;
		var isSetting = (arguments.length>1) ;
		if($(id).nodeName.invariantCompare("select")==0) {
			//select
			var options = $(id).options;			
			if(isSetting) {				
				for(var i=0;i<options.length;i++){
					if(options[i].value.invariantCompare(arguments[1])==0) {
						options[i].selected=true;
					}
				}
			}
			return options[$(id).selectedIndex].value;
		}
		else {
			//input, textarea
			if(isSetting) $(id).value=arguments[1];
			return $(id).value;
		}
	  },
      visible: function(element) {
        return getObj(element).style.display != 'none';
      }, //visible
      hideContextmenu:function (){ 
        //$E.hideContextmenu() disabilita per l'intero documento
        //$E.hideContextmenu('element' | element) disabilita per l'elemento
        var obj=(arguments.length==0)? document : $(arguments[0]);
        var action=function(e){
            try{window.event.preventDefault();}
            catch(x){};
            return  false;
            }
        obj.oncontextmenu=action;
      }, 
	  hover: function(e,outStyle,overStyle){
	  		this.style($(e),outStyle);
			$(e).onmouseover = new Function("$E.style(this,'" + overStyle + "')");
			$(e).onmouseout = new Function("$E.style(this,'" + outStyle + "')");
	  },
	  elementContains:function(thisElement,targetElement){
	  	if(isNull(thisElement) || isNull(targetElement)) return false;
		while (!isNull(thisElement)){
			if(thisElement==targetElement) return true;
			thisElement=thisElement.parentNode;
		}
		return false;	
	  },
	  style:function(e,strStyle){
	  	//applica style all'oggetto
		var getStylePropName = function (name){
			//trasforma un nome di stile tipo border-color in camelCase borderColor
			var ss = name.split("-");var StylePropName="";
			for(var i=0;i<ss.length;i++){
				var chunk = ss[i].toLower();
				if (i>0){var firstChar = chunk.charAt(0);var restChars = chunk.substr(1);chunk=firstChar.toUpper() + restChars.toLower();}
				StylePropName+=chunk;
			}
			return StylePropName;
		}//getStylePropName
		var stylePairs = strStyle.split(";");
		for(var i=0;i<stylePairs.length;i++){
			var stylePair = stylePairs[i].split(":");
			var styleName = getStylePropName(stylePair[0])
			var styleValue = (stylePair[1])?stylePair[1]:"";
			$(e).style[styleName]=styleValue;
		}
	  },//style
	  swap:function(){
	  	for(var i=0;i<arguments.length;i++){
			var a = arguments[i];
			if(this.visible(a)) {this.hide(a);return false}
			else {this.show(a);return true}
		}
	  },//swap	
	  swapVis:function(){
	  	for(var i=0;i<arguments.length;i++){
			var a = arguments[i];
			if($(a).style.visibility=="hidden") {this.vShow(a);return false}
			else {this.vHide(a);return true}
		}
	  },//swap	
	  vShow :function(e){
	  	this.style(e,"visibility:visible");
	  },
	  vHide :function(e){
	  	this.style(e,"visibility:hidden");
	  },
      show:function(){
        if(arguments.length>1){
            this.show(Array.from(arguments));
        }
        else if (arguments.length==1){
            var e = arguments[0];
            if(isArray(e)){
                for(var i=0;i<e.length;i++) { this.show(e[i])}
            }
            else{
				try {
                	getObj(e).style.display='';
				}
				catch(ex) {
					//alert(e);
				}
            }
        }
      },//show
      hide:function(){
        if(arguments.length>1){
            this.hide(Array.from(arguments));
        }
        else if (arguments.length==1){
            var e = arguments[0];
            if(isArray(e)){
                for(var i=0;i<e.length;i++) { this.hide(e[i])}
            }
            else{
				try{
                	getObj(e).style.display='none';
					}
				catch(ex){
					//alert(e);
				}
            }
        }
      }, //hide    
      border:function (element,style){
        getObj(element).style.border=style;
      },//border
	  pointer:function (element){
	  	try{$(element).style.cursor='pointer';}
		catch(ex){
			try{$(element).style.cursor='hand';}
			catch(ex2){}
		}
      },//pointer
      setWidth:function (element,value){
        $(element).style.width=this.pixelValueOf(value);
      },
      setHeight:function (element,value){
        $(element).style.height=this.pixelValueOf(value);
      },
      create:function (name){
        return  document.createElement(name);
      },
      createDiv:function (){
        return  this.create("div");
      },
      createTable:function (){
	  	var oTable=this.create("table");
		oTable.addRow = function (){
			var oTr = this.insertRow(-1);
			oTr.addCell = function (){
				var oTd = this.insertCell(-1);
				return oTd;
			}
			return oTr;
		}
        return  oTable;
      },
      addTr:function (table){
        return  table.insertRow(-1);
      },
      addTd:function (tr) {
        return  tr.insertCell(-1);
      },
	  addCells:function (tr,cellProperties,innerTextArray){
		for(var i=0;i<innerTextArray.length;i++){
			var c = tr.insertCell(-1);			
			extend(c,cellProperties);
			c.innerHTML=innerTextArray[i];
		}
	  }
    }
)

location.getNameWithoutExtension = function (){
	var n = this.getName ();
	var lastDot = n.lastIndexOf(".");
	return n.substring(0 , lastDot);
}

location.getExtension = function (){
	var n = this.getName ();
	var lastDot = n.lastIndexOf(".") + 1;
	if(lastDot!=0)
	return n.substring(lastDot);
}

location.extractName = function (uri){
	var lastSlash = uri.lastIndexOf("/")	+ 1
	var hasPath = (uri.indexOf("?")!=-1);
	uri = uri.substring(lastSlash)
	if (hasPath) 
		uri = uri.substring(0,uri.indexOf("?"));
	return uri;
}

location.getName = function (){
	return this.extractName(this.href);
}
location.directoryName = function (){
	var cDir = this.currentDirectory();
	if(cDir.endsWith("/")) {
		cDir = cDir.substring(0, cDir.lastIndexOf("/"))
	}
	cDir = cDir.substr(cDir.lastIndexOf("/") + 1)
	return cDir
}

location.currentDirectory = function (){
	h = this.href;
	var lastSlash = h.lastIndexOf("/")	+ 1
	return h.substring(0,lastSlash);
}

location.query = location.search;

location.request = function () {
	//alert(location.search.length);
	if(location.search.length!=0) {		
		q = location.search.substr(1);
		return q.splitPairs("&","=");
	}
	return new Object();
}
location.refresh = function (){
	window.location=this;
}
var $L = window.location;
var Cookies = new Object();

Cookies.get = function (name,defaultValue) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) {
			var ckValue=c.substring(nameEQ.length,c.length);
			if(ckValue!='')	return ckValue;
			}
	}
	return defaultValue;
}

Cookies.set = function (name,value) {
	var expires = "";
	var days = arguments[2]; // 3° param expire
	var path = (arguments[3])?arguments[3]:"/"; // 4° param path
	if (days) {
		if(!isNaN(new Number(days))) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			expires = "; expires="+date.toGMTString();
		}
	}
	var setValue = name+"="+value+expires+"; path=" + path;
	document.cookie = setValue;
}

Cookies.remove =  function (name){
	Cookies.set(name,"",-1);
}

var $C = Cookies;

var KeyWatch = new Object();
KeyWatch.keyPress = function (e,action){
		try{
			evt = (window.event && window.event.keyCode)? window.event : e;
  			var keyPressed = evt.which || evt.keyCode;
			var sender = evt.target || evt.srcElement;
			for(var i=2;i<arguments.length;i++){
				if(keyPressed==arguments[i] && isFunction(action)) {
					action(sender,keyPressed);
					window.status = "keypress" + action;
					}
				
			}
		}
		catch (ex) {alert(ex)}
	}
KeyWatch.keyReturn = function (e,action){

		this.keyPress(e,action,13);
	}
var $K = KeyWatch;