// check not to reinclude myself
if(typeof xForms!='function'){

	// ========== BEGIN: IMPORTED FUNCTION ==========

	// skip white spaces, text and comment nodes
	// until next TYPE 1 node is found (container)
	// @n node to start from
	// @d direction (0=up 1=down)
	if(typeof skipWS!='function'){
		function skipWS(o,d){
			if(o&&o.nodeType){
				if(d){
					while(o.nodeType!==1&&o.nextSibling){
						o=o.nextSibling;
					}
				}else{
					while(o.nodeType!==1&&o.previousSibling){
						o=o.previousSibling;
					}
				}
			}
			return o;
		}
	}

	// =========== END: IMPORTED FUNCTION ===========

	// =========== BEGIN: INLINE EXECUTION ==========

	// avoid click events on submit buttons
	// until the page has been fully loaded
	document.onclick=
		function(e){
			e=e||window.event;
			var t=e.target||e.srcElement;
			if(t && t.type&&t.type=='submit'){
				if(!window.Loaded){
					if(e.preventDefault){
						e.preventDefault();
					}else{
						e.returnValue=false;
					}
				}
			}
		};

	// ============ END: INLINE EXECUTION ===========

	// ========== BEGIN: CLASS DECLARATION ==========


	var xForms=new function(doc){
		// save scope
		var o=this;

		// document binding
		o.doc=doc||document;
		// in use counter
		o.count=0;

		// initialize document forms helpers
		o.init=function(){
			// only init if in use count === 0
			window.Loaded=true;
			if(o.count===0){
				o.attach(o.doc);
			}
		};

		// shutdown document forms helpers
		o.shut=function(){
			// only shut if in use count == 1
			window.Loaded=false;
			if(o.count==1){
				o.detach(o.doc);
			}
		};

		// attach behavior to document
		o.attach=function(d){
			if(d&&d.nodeType&&d.nodeType==9){
				fixLabels(d);
				fixBorders(d);
				//fixBooleans(d);
				fixTextareas(d);
				var f,i,j,k,os,fs=d.forms;
				// loop all forms
				for(i=0;fs.length>i;i++){
					fs[i].onsubmit=o.handleForm;
					// loop all forms elements
					for(j=0,f=fs[i].elements;f.length>j;j++){
						if(typeof f[j].type!='undefined'){
							// for select elements restore
							// defaultSelected on reload (Geko)
							if(f[j].type.indexOf('select')===0){
								// loop all options elements
								for(os=f[j].options,k=0;os.length>k;k++){
									if(os[k].selected||os[k].defaultSelected){
										f[j].selectedIndex=k;
										os[k].selected=true;
										os[k].defaultSelected=true;
									}
								}
								// add defaultLength property to select elements
								f[j].defaultLength=k;
							}else if(f[j].type=='hidden'){
								// patch hidden defaultValue add a defaultString property
								if(typeof f[j].defaultString=='undefined')f[j].defaultString=f[j].value;
							}else if(f[j].type=='password'){
								// accessibility feature, [10.4.1.2]: form control default text invalid (null).
								f[j].onfocus=function(){if(this.value.match(/\[|\]/)){this.value='';this.select();}};
								f[j].onkeypress=capsDetect;
							}else if(f[j].type=='text'){
								// accessibility feature, [10.4.1.2]: form control default text invalid (null).
								f[j].onfocus=function(){if(this.value.match(/\[|\]/)){this.value='';this.select();}};
							}
						}
					}
				}
				o.count++;
			}
			// debug lines, remove...
			else{
				alert('XForms init() Error: '+arguments.callee.caller);
			}
		};
	
		// detach behavior from document
		o.detach=function(d){
			if(d&&d.nodeType&&d.nodeType==9){
				var i,j,f,os,fs=d.forms;
				for(i=0;fs.length>i;i++){
					fs[i].onsubmit=null;
					for(j=0,f=fs[i].elements;f.length>j;j++){
						if(typeof f[j].type!='undefined'){
							if(f[j].type.indexOf('select')===0){
								// restore defaultSelected on reload (Geko)
								for(os=f[j].options,k=0;os.length>k;k++){
									if(os[k].defaultSelected){
										f[j].selectedIndex=k;
									}
								}
								// remove defaultLength property from select elements
								f[j].defaultLength=undefined;
							}else if(f[j].type=='hidden'){
								// patch hidden defaultValue add a defaultString property
								if(typeof f[j].defaultString=='undefined'){f[j].defaultString=f[j].value;}
							}else if(f[j].type=='password'){
								// accessibility feature, remove onfocus events
								f[j].onfocus=null;
							}else if(f[j].type=='text'){
								// accessibility feature, remove onfocus event
								f[j].onfocus=null;
							}
						}
					}
				}
				o.count--;
			}
			// debug lines, remove...
			else{
				alert('XForms shut() Error: '+arguments.callee.caller);
			}
		};

		// element or elements values in the same form changed ?
		o.hasChanged=function(el){
			if(typeof el!='object')return false;
			return el.nodeName!='FORM'?o.hasElementChanged(el):hasFormChanged(el);
		};

		// some form elements values changed ?
		o.hasFormChanged=function(f){
			if(f.nodeName!='FORM'){return false;}
			for(var e=0;f.length>e;e++){
				if(o.hasElementChanged(f[e])){
					return true;
				}
			}
			return false;
		};

		// element value changed ?
		o.hasElementChanged=function(el){
			var changed=false;
			switch(el.type){
				case 'text':
				case 'file':
				case 'password':
				case 'textarea':
					if(typeof el.defaultValue!='undefined'){
						changed=el.defaultValue!=el.value;
					}
					break;
				case 'hidden':
					if(typeof el.defaultString!='undefined'){
						changed=el.defaultString!=el.value;
					}
					break;
				case 'radio':
				case 'checkbox':
					if(typeof el.defaultChecked!='undefined'){
						changed=el.defaultChecked!=el.checked;
					}
					break;
				case 'select-one':
				case 'select-multiple':
					var i,os=el.options;
					if(el.defaultLength!=os.length){
						changed=true;
					}else{
						for(i=0;os.length>i;i++){
							if(os[i].defaultSelected!=os[i].selected){
								changed=true;
							}
						}
					}
					break;
				case 'image':
				case 'button':
					// some security check for these elements ???
					// like not having inline event attributes....
					break;
				default:
					break;
			}
			return changed;
		};

		// form submit event
		/*
		o.handleForm=function(e){
			var el=e.target||e.srcElement;
			if(el.nodeName=='FORM'&&!o.hasFormChanged(el)){
				alert('[WARNING]: Default fields values in the form were not changed.\n\nNo form submission is necessary, the database is already up to date\nor the data in some field still contains default filler text (ex. [username]).');
				e.preventDefault();
			}else {
				var err=false;
				if(el.nodeName=='FORM'&&typeof el.required=='object'&&el.required.value.length>0){
					// a required hidden field may exists
					var fields=el.required.value.split(',');
					for(var i=0;i<fields.length;i++){
						if(IsEmpty(el[fields[i]])){
							alert('[WARNING]: The "'+fields[i]+'" field is required and cannot be left empty.');
							el[fields[i]].focus();
							el[fields[i]].select();
							e.preventDefault();
							break;
						}
						if(IsBlank(el[fields[i]])){
							alert('[WARNING]: The "'+fields[i]+'" field only contains spaces or blank characters.');
							el[fields[i]].focus();
							el[fields[i]].select();
							e.preventDefault();
							break;
						}
						if(el[fields[i]].value!=el[fields[i]].value.match(/[!#$%&()*+,\-.\/:;<=>?@\w]+/)){
							alert('[WARNING]: The "'+fields[i]+'" field contains invalid characters.\n\nAllowed characters are:\n - digits:\x20from 0 to 9\x20(decimal numbers),\n - letters:\x20from a to z\x20(lower and upper case),\n - symbols:\x20space\x20(" "), and the following characters\x20!\x20\"\x20#\x20$\x20%\x20&\x20\'\x20(\x20)\x20*\x20+\x20,\x20\-\x20.\x20\/\x20:\x20;\x20<\x20=\x20>\x20?\x20@\x20.');
							el[fields[i]].focus();
							el[fields[i]].select();
							e.preventDefault();
							break;
						}
					}
				}
			}
			return true;
		};
*/
		// window onload/onunload events may be overwritten by the onload/onunload attributes of the body tag
		// so, first try event registration if available, then fall back to inline onload/onunload events

		if (window.Loaded) {
			// we missed it...
			o.init();
		} else if (window.addEvent) {
			// our choice of event manager
			addEvent(window, 'load', o.init, false);
		} else if (window.addEventListener) {
			// for MOZ, FF, Opera, Konqueror Safari
			window.addEventListener('load', o.init, false);
		} else if (window.attachEvent) {
			// for IE (and clones)
			window.attachEvent('onload', o.init);
		} else {
			// for all browsers...
			var oldonload = window.onload;
			if (typeof window.onload != 'function') {
				window.onload = o.init;
			} else {
				window.onload = function() {
					if (oldonload) {
						oldonload();
					}
					o.init();
				}
			}
		}

		if (window.addEvent) {
			// our choice of event manager
			addEvent(window, 'unload', o.shut, false);
		} else if (window.addEventListener) {
			// for MOZ, FF, Opera, Konqueror Safari
			window.addEventListener('unload', o.shut, false);
		} else if (window.attachEvent) {
			// for IE (and clones)
			window.attachEvent('onunload', o.shut);
		} else {
			// for all browsers...
			var oldonunload = window.onunload;
			if (typeof window.onunload != 'function') {
				window.onunload = o.shut;
			} else {
				window.onunload = function() {
					if (oldonunload) {
						oldonunload();
					}
					o.shut();
				}
			}
		}

		return o;
	}

	// =========== END: CLASS DECLARATION ===========

	// ========== BEGIN: STYLE UTILITIES ============

	// fix label width in forms
	function fixLabels(d){
		var i,j,k,l,max=0,f=d.forms;
		for(j=0;f.length>j;j++){
			l=f[j].getElementsByTagName('label');
			for(k=0;k<l.length;k++){
				max=l[k].offsetWidth>max?l[k].offsetWidth:max;
			}
			for(k=0;k<l.length;k++){
				l[k].parentNode.style.overflow='auto';
				l[k].style.width=max+'px';
				l[k].style.cssFloat='left';
			}
		}
	}

	// fix checkboxes and radiobuttons borders
	function fixBorders(d){
		var j,t,e=d.getElementsByTagName('input');
		for(j=0;j<e.length;j++){
			t=e[j].getAttribute('type');
			if(t=='checkbox'||t=='radio'){
				e[j].style.border='none';
			}
		}
	}

   var imgpath = 'http://vinirizzi.awbinformatica.local/lib/nwforms/img/';
   
	// fix checkboxes and radiobuttons
	// to match with the interface look
	function fixBooleans(d){
		var i,j,l,t,e=d.getElementsByTagName('input');
		for(j=0;j<e.length;j++){
			t=e[j].getAttribute('type');
			if(t=='checkbox'||t=='radio'){
				i=d.createElement('img');
				i.id=t+'Image'+j;
				i.src=imgpath+t+(e[j].checked?'1':'0')+'.gif';
				i.style.cssText='vertical-align: middle;';
				i.onclick =
					function(e){
						return handleBooleans.call(this.nextSibling,e);
					};
				if(e[j].parentNode.nodeName.toLowerCase()=='label'){
					l=e[j].parentNode;
				}else if(skipWS(e[j].previousSibling,0).nodeName.toLowerCase()=='label'){
					l=skipWS(e[j].previousSibling,0);
				}
				if(l){
					l.onclick =
						function(e){
							var o=this.getElementsByTagName('input')[0]||skipWS(this.nextSibling.nextSibling,1);
							if(!o||o.nodeName.toLowerCase()!='input'){
								if(this['for']){
									o=this.form.getElementById(this['for']);
								}
							}
							if(o){
								handleBooleans.call(o,e);
							}
						};
					i.style.height=l.offsetHeight+'px';
				}
				e[j].parentNode.insertBefore(i,e[j]);
				e[j].style.display='none';
			}
		}
	}

	// handle click on boolean elements
	// radio buttons and checkboxes
	function handleBooleans(e){
		var j,o=this,f=this.form;
		if(f&&o.type=='radio'){
			// if element is in a group, load unchecked
			// images for all the elements in the group
			var c=f.getElementsByTagName('input');
			for(j=0;j<c.length;j++){
				if(c[j].name==o.name){
					c[j].checked='';
					c[j].value='0';
					c[j].previousSibling.src=imgpath+c[j].type+'0.gif';
				}
			}
			// set checked element and active image
			o.previousSibling.src=imgpath+o.type+'1.gif';
			o.checked=true;
		}else if(f){
			var c=e.target||e.srcElement;
			if(typeof c.htmlFor!='undefined'&&c.htmlFor.length>0){
				// a label bound to the element with for="id" attribute
				// toggle checked image for element, checked automatically
				this.previousSibling.src=imgpath+o.type+(o.checked?'0':'1')+'.gif';
			}else if(c.nodeName.toLowerCase()=='img'||c.nodeName.toLowerCase()=='input'){
				this.previousSibling.src=imgpath+o.type+(o.checked?'0':'1')+'.gif';
				o.checked=o.checked?false:true;
				if(e.stopPropagation){
					e.preventDefault();
					e.stopPropagation();
				}else{
					e.returnValue=false;
					e.cancelBubble=true;
				}
			}else{
				// toggle checked image and checked value for element
				this.previousSibling.src=imgpath+o.type+(o.checked?'0':'1')+'.gif';
				o.checked=o.checked?false:true;
			}
		}
	}

	function fixTextareas(d){
		var i,j,t,e=d.getElementsByTagName('textarea');
		for(j=0;j<e.length;j++){
			// fix text areas
		}
	}

	// =========== END: STYLE UTILITIES =============

	// ========== BEGIN: FORMS UTILITIES ============

	// add options to select box
	function addOption(s,t,v){
		var o=getDocument(s).createElement('option');
		o.text=t;
		o.value=v;
		s.options.add(o);
	}

	// remove option from select box
	function removeOption(s){
		for(var i=s.options.length-1;i>=0;i--){
			if(s.options[i].selected){
				s.remove(i);
			}
		}
	}

	// form element or string as argument
	// true if the string only contains whitespaces
	function IsBlank(el){
		if(el===null)return true;
		if(typeof el=='object'&&typeof el.value=='string'){
			return el.value.replace(/[\s]/g,'')=='';
		}else if(typeof el=='string'){
			return el.replace(/[\s]/g,'')=='';
		}
		return false;
	}

	// form element or string as arguments
	// true if any character is found in the string
	function IsEmpty(el){
		if(el===null)return true;
		if(typeof el=='object'&&typeof el.value=='string'){
			if(el.value.length===0){return true;}
		}else if(typeof el=='string'){
			if(el.length===0){return true;}
		}
		return false;
	}

	// form element or string as argument
	// true if string not blank and not empty
	function IsValid(el){
		return !IsBlank(el)&&!IsEmpty(el);
	}

	// detect activated CAPS LOCK
	function capsDetect(e){
		var theShift=e.shiftKey||(e.modifiers&&(e.modifiers&SHIFT_MASK));
		var theKey=e.which?e.which:(e.keyCode?e.keyCode:(e.charCode?e.charCode:0));
		if((theKey>64&&theKey<91&&!theShift)||(theKey>96&&theKey<123&&theShift)){
			alert('Disattiva il tasto CAPS LOCK nei campi password !!!');
//			alert('Deactivate the CAPS LOCK key in password fields !!!');
		}
	}

	// =========== END: FORMS UTILITIES =============

// end of check not to reinclude myself
}
