// JavaScript Document
Array.prototype.sum = function(){
	for(var i=0,sum=0;i<this.length;sum+=this[i++]);
	return sum;
}
Array.prototype.max = function(){
	return Math.max.apply({},this)
}
Array.prototype.min = function(){
	return Math.min.apply({},this)
}
Function.prototype.bind = function (obj) {
	var fx = this;
	var args = [];
	for (var n=1;n<arguments.length;n++)
    args.push(arguments[n]);
	return function() {
		return fx.apply(obj,args);
	}
}

function popUp(url, w, h, option) {
	w = (w=='')?600:w;
	h = (h=='')?500:h;
	var wl = Math.round((screen.width-w)/2);
	var wt = Math.round((screen.height-h)/2);
	// toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes
	if (!option) option = 'status=no,scrollbars=yes,resizable=yes';
	option += ',width='+w+',height='+h+',top='+wt+',left='+wl+'';
	var windowName = '';
	window.open(url, windowName, option);
}
function winClose () {
	window.close();
}

function addListener(element, baseName, handler) {
	if (element.addEventListener)
		element.addEventListener(baseName, handler, false);
	else if (element.attachEvent)
		element.attachEvent('on' + baseName, handler);
}
function bindMethod(method, scope){
    return function() {
        method.apply(scope,arguments);
    }
}

function Elem (what) { // sostituito con la funzione di Prototype '$'
	if (typeof(what) == 'string')
		var elem = document.getElementById(what);
	else var elem = what;
	if (elem) return elem;
	else return false;
}
function elemValue (id) { // sostituito con la funzione di Prototype '$F'
	var elem = Elem(id);
	if (elem) return elem.value;
	else return false;
}

function elemClass (id) {
	var elem = Elem(id);
	if (elem) return elem.className;
	else return false;
}

function hideElement (what) {
	var elem = Elem(what);
	if (elem) {
		elem.className = elem.className.replace(/\s?shown/g,"")+((elem.className.indexOf("hidden")<0)?' hidden':'');
	}
}
function showElement (what) {
	var elem = Elem(what);
	if (elem) {
		elem.className = elem.className.replace(/\s?hidden/g,"")+((elem.className.indexOf("shown")<0)?' shown':'');
	}
}
function elemAddClass (what,newClass) {
	var elem = Elem(what);
	if ( elem && ( 'string' == typeof(newClass) ) ) {
		elem.className = elem.className + (elem.className.indexOf(newClass)<0) ? ((elem.className.length > 0) ? ' ' : '') + newClass : '';
	}
}
function elemClassReplace (what,oldClass,newClass,addAnyway) {
	var elem = Elem(what);
	addAnyway = (addAnyway) ? addAnyway : false;
	if ( elem && ( 'string' == typeof(oldClass) == typeof(newClass) ) ) {
		var isItThere = elem.className.indexOf(oldClass)>=0;
		if (isItThere || addAnyway)
			elem.className = ( (oldClass == '*') ? '' : elem.className.replace(new RegExp( oldClass, "g" ),"") ) + ((elem.className.indexOf(newClass)<0)?' '+newClass:'');
	}
}


function riempiSubCat () { riempi(Elem('formSearch-cat'),'formSearch-sub','cat'); }
function riempiProv () { riempi(Elem('formSearch-reg'),'formSearch-prov','reg'); }
function riempiSubCatIns () { riempi(Elem('form-ins-cat'),'form-ins-sub','cat'); }
function riempiProvIns () { riempi(Elem('form-ins-reg'),'form-ins-prov','reg'); }
function initSubCat () { if (elemValue('formSearch-cat')) riempiSubCat (); }
function initProv () { if (elemValue('formSearch-reg')) riempiProv (); }

function toggleSignIn() {
	var sel = Elem('form-reg-reg1').checked;
	if (sel) {
		hideElement('form-register-reg');
		showElement('form-register-log');
	}
	else {
		hideElement('form-register-log');
		showElement('form-register-reg');
	}
}
function showLogin() {
	hideElement('form-register-reg');
	showElement('form-register-log');
}
function showReg() {
	hideElement('form-register-log');
	showElement('form-register-reg');
}

function showContactBox() {
	hideElement('showContact');
	showElement('contact-box');
}
function hideContactBox() {
	hideElement('contact-box');
	showElement('showContact');
}
function sendContact() {
	Elem('contactForm').submit();
}
function getPage(page) {
	if (isNaN(page)) return;
	Elem('formSearch-page').value = parseInt(page,10);
	Elem('form-search').submit();
}

/*
function decimale (numero) {
	var result = parseFloat(numero.replace(",", "."));
	result = (isNaN(result))?0:result;
	return result;
}
function decimaleR (numero) {
	var result = (numero+'').replace(".", ",");
	result = (isNaN(numero))?0:result;
	return result;
}
*/

function printTarget (what) {
	//if (!(what = Elem(what))) return false;
	what.focus();
	what.print();
}
function sendForm (what) {
	if (!(what = Elem(what))) return false;
	what.submit();
}
function checkField () {
	if (undefined === this) return false;
	var val = this.value;
	var classe = this.className;
	if (classe.indexOf('numero') >= 0) {
		if (val.indexOf(',') >= 0) this.value = val = decimale(val);
		if (isNaN(val)) {
			fieldError (this,'il campo richiede un valore numerico');
		} else lowspotElement(this);
		if ((val+'').indexOf('.') >= 0) this.value = val = decimaleR(val);
	}
	if (classe.indexOf('percentuale') >= 0) {
		if (val.indexOf(',') >= 0) this.value = val = decimale(val);
		if (isNaN(val)) {
			fieldError (this,'il campo richiede un valore numerico');
		}
		else if (parseFloat(val) < 0 || parseFloat(val) > 100) {
			fieldError (this,'il campo richiede un valore percentuale compreso tra 0 e 100');
		} else {
			lowspotElement(this);
			this.value = val = decimaleR(val);
		}
	}
	if (classe.indexOf('range') >= 0) {
		str = classe.split(' ');
		if (str[0]) for (var i=0;i<str.length;i++) {
			var pattern = /^([0-9])+([a-z])+([0-9])+$/;
			if (str[i].search(pattern)>=0) {
				var terms = str[i].split('to');
				if ((parseFloat(val) < parseFloat(terms[0])) || (parseFloat(val) > parseFloat(terms[1]))) {
					fieldError (this,'il campo richiede un valore compreso tra '+terms[0]+' e '+terms[1]+' ');
					return;
				} else lowspotElement(this);
			} else lowspotElement(this);
		}
	}
	if (classe.indexOf('email') >= 0) {
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (val && !pattern.test(val)) {       
			fieldError (this,'il campo richiede un indirizzo email valido');
			return;
		}
		else lowspotElement(this);   
	}
}
function highlightElement (what) {
	if (typeof(what) == 'string')
		var elem = document.getElementById(what);
	else var elem = what;
	elem.className = elem.className+((elem.className.indexOf("highlight")<0)?' highlight':'');
}
function lowspotElement (what) {
	if (typeof(what) == 'string')
		var elem = document.getElementById(what);
	else var elem = what;
	elem.className = elem.className.replace(/\s?highlight/g,"");
}
function selectFieldContent () {
	//var elem = document.getElementById(id);
	if (undefined === this) return false;
	this.select();
}

function fieldError(what,text) {
	if (typeof(what) == 'string')
		var elem = document.getElementById(what);
	else var elem = what;
	if (elem[0]) {
		for (var i=0;i<elem.length;i++) {
			highlightElement(elem[i]);
			Elem(elem[i]).select();
		}
	}
	else {
		highlightElement(elem);
		elem.select();
	}
	alert (text || 'errore nella compilazione del campo');	
}

function checkPwd () {
	var chk = Elem('check-changepwd').checked;
	Elem('changepwd').disabled = (chk)?false:true;
}

function doLogin() {
	sendForm ('form-sideLogin');
}

function checkPwd () {
	var chk = Elem('check-changepwd').checked;
	Elem('changepwd').disabled = (chk)?false:true;
}

function getWinSize () {
	var myWidth = myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		myWidth = window.innerWidth;
		myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		myWidth = document.body.clientWidth;
		myHeight = document.body.clientHeight;
	}
	return Array( myWidth , myHeight );
}
function getScreenSize () {
	var myWidth = myHeight = 0;
	myWidth = screen.availWidth;
	myHeight = screen.availHeight;
	return (myWidth && myHeight) ? Array( myWidth , myHeight ) : false;
}
function setWinCentered () {
	var wsize = getWinSize ();
	var ssize = getScreenSize ();
	var x = Math.round((ssize[0] - wsize[0])/2);
	var y = Math.round((ssize[1] - wsize[1])/2);
	window.moveTo(x,y);
}
function fitWinHeight () {
	var padding = 20; // safety distance from screen borders
	var screenSize = getScreenSize ();
	var w = document.documentElement.clientWidth;
	var h = document.body.offsetHeight + 50;
	iWidth = (typeof( window.innerWidth ) == 'number')?window.innerWidth:document.body.clientWidth;
	iHeight = (typeof( window.innerHeight ) == 'number')?window.innerHeight:document.body.clientHeight;
	if (iWidth > (screenSize[0] - (padding*2))) iWidth = screenSize[0] - (padding*2);
	if (iHeight > (screenSize[1] - (padding*2))) iHeight = screenSize[1] - (padding*2);
	window.resizeBy(w - iWidth,h - iHeight);
}
function tidyUpWindow () {
	fitWinHeight ();
	setWinCentered ();
}
// Browser Window Size and Position
// copyright Stephen Chapman, 3rd Jan 2005, 8th Dec 2005
// you may copy these functions but please keep the copyright notice as well
function pageWidth() {
	return window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ?       document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
}
function pageHeight() {
	return  window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ?  document.documentElement.clientHeight : document.body != null? document.body.clientHeight : null;
}
function posLeft() {
	return typeof window.pageXOffset != 'undefined' ? window.pageXOffset :document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft ? document.body.scrollLeft : 0;
}
function posTop() {
	return typeof window.pageYOffset != 'undefined' ?  window.pageYOffset : document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop ? document.body.scrollTop : 0;
}
function posRight() { return posLeft()+pageWidth(); }
function posBottom() { return posTop()+pageHeight(); }

//addListener (window,'load',initPage);