/****************************** MAURI ************************************/
/*########################  GlobalProperties ############################*/
/****************************** MAURI ************************************/
//var ServerName = "dev";
var ServerName 		= "www.provinz.bz.it";
var FormatDate 		= "."
var Extension  		= new Array();
var SuperElements 	= new Array();

if (is_opera || is_nav6up) isIE = true;

if (isIE){
	document.write('<Script language="JavaScript" src="http://' + ServerName + '/_RedasServices/JS/monitors/MyIEControls.js" type="text/javascript"></Script>');
}
else if (is_nav4){
	document.write('<Script language="JavaScript" src="http://' + ServerName + '/_RedasServices/JS/monitors/MyNSControls.js" type="text/javascript"></Script>');
	document.write('<STYLE type="text/javascript">document.classes.fieldRequired.all.color = "red";</STYLE>')
}

document.write('<Script language="JavaScript" src="http://' + ServerName + '/_RedasServices/JS/monitors/MyLibrary.js" type="text/javascript"></Script>');
document.write('<Script language="JavaScript" src="http://' + ServerName + '/_RedasServices/JS/monitors/ErrorMsg.js" type="text/javascript"></Script>');

/******************************* MAURI *************************************/
/*				Classe per monitoraggio singolo Elemento della Form			 */
/******************************* MAURI *************************************/

function SingleEvent (e){
	this.Ie = isIE;
	this.Ns = is_nav4;
	this.NavEvent = e;
	this.event = this.initialize();
	this.value = this.getElementValue();
	this.X = this.getClientX();
	this.Y = this.getClientY();
	this.type = this.getEventType();
	this.MouseButton = this.getMouseButton();
	this.KeyBoardButton = this.getKeyBoardButton();
	return (this);
}

SingleEvent.prototype.initialize = initialize;
SingleEvent.prototype.getElementValue = getElementValue;
SingleEvent.prototype.getEventType = getEventType;
SingleEvent.prototype.getClientX = getClientX;
SingleEvent.prototype.getClientY = getClientY;
SingleEvent.prototype.getMouseButton = getMouseButton;
SingleEvent.prototype.getKeyBoardButton = getKeyBoardButton;
SingleEvent.prototype.isFormElement = isFormElement;
SingleEvent.prototype.getElementObj = getElementObj;
SingleEvent.prototype.getFormElementName = getFormElementName;
SingleEvent.prototype.hasElementProperty = hasElementProperty;
SingleEvent.prototype.getElementEvent = getElementEvent;
SingleEvent.prototype.getElementProperty = getElementProperty;
SingleEvent.prototype.isAltButtonPress = isAltButtonPress;
SingleEvent.prototype.isCtrlButtonPress = isCtrlButtonPress;
SingleEvent.prototype.isShiftButtonPress = isShiftButtonPress;
SingleEvent.prototype.stopEvent = stopEvent;
SingleEvent.prototype.isElementValidDate = isElementValidDate;
SingleEvent.prototype.isElementValidEmail = isElementValidEmail;
SingleEvent.prototype.isElementValidWWW = isElementValidWWW;
SingleEvent.prototype.areFieldsRequiredCompiled = areFieldsRequiredCompiled;
SingleEvent.prototype.isValidExtension    = isValidExtension;
SingleEvent.prototype.isMyValidFiscalcode = isMyValidFiscalcode;
SingleEvent.prototype.isMyValidPIva = isMyValidPIva;



//Metodi interni-esterni
function initialize(){
	if (this.Ie){
		if (is_nav6up){
			return this.NavEvent;
		}
		else{
			return window.event;
		}
	}
	else if (this.Ns){
		return this.NavEvent;
	}
}

function getEventType(){
	return this.event.type;
}

function getClientX(){
	if (this.Ie){
		return this.event.clientX;
	}
	else if (this.Ns){
		return this.event.x;
	}
}

function getClientY(){
	if (this.Ie){
		return this.event.clientY;
	}
	else if (this.Ns){
		return this.event.y;
	}
}

function getMouseButton(){
	if (this.Ie){
		return this.event.button;
	}
	else if (this.Ns){
		return isNaN(this.event.which) ? false : this.event.which;
	}
}

function getKeyBoardButton(){
	if (this.Ie){
		return this.event.keyCode;
	}
	else if (this.Ns){
		return isNaN(this.event.which) ? this.event.which : false;
	}
}

function getElementValue(){
	if (this.Ie){
		if (is_nav6up){
			return this.event.target.value;
		}
		else{		
			return this.event.srcElement.value;
		}
	}
	else if (this.Ns){
		return this.event.target.value;
	}
}

//metodi esterni
function isFormElement(){
	if (this.Ie){
		return this.event.srcElement;
	}
	else if (this.Ns){
		return this.event.target;
	}
}

function getElementObj(){
	if (this.Ie){
		if (is_nav6up){
			return this.event.target;
		}
		else{
			return this.event.srcElement;
		}
	}
	else if (this.Ns){
		return this.event.target;
	}
}

function getFormElementName(){
	if (this.Ie){
		return this.event.srcElement.name;
	}
	else if (this.Ns){
		return this.target.name;
	}
}

function hasElementProperty( HTMLProperty ){
	if (this.Ie){
		if (is_nav6up){
			return this.event.target.getAttribute(HTMLProperty);
		}
		else{
			return this.event.srcElement.getAttribute(HTMLProperty);
		}
	}
	else if (this.Ns){
		return (eval('this.event.target.' + HTMLProperty + ' == "true"')) ? true : false;
	}
}

function getElementEvent(){
	if (this.Ie){
		if (is_nav6up){
			return this.event.target.getAttribute('Event');
		}
		else{
			return this.event.srcElement.getAttribute('Event');
		}
	}
	else if (this.Ns){
		return this.event.target.Event;
	}
}

function getElementProperty( PropertyName ){
	if (this.Ie){
		if (is_nav6up){
			return this.event.target.getAttribute(PropertyName);
		}
		else{
			return this.event.srcElement.getAttribute(PropertyName);
		}
	}
	else if (this.Ns){
		return eval('this.event.target.' + PropertyName + '');
	}
}

function isAltButtonPress(){
	if (this.Ie){
		return this.event.altKey;
	}
	else if (this.Ns){
		if (this.event.modifiers==3){
			return true;
		}
	}
}

function isCtrlButtonPress(){
	if (this.Ie){
		return this.event.ctrlKey;
	}
	else if (this.Ns){
		if (this.event.modifiers==2){
			return true;
		}
	}
}

function isShiftButtonPress(){
	if (this.Ie){
		return this.event.shiftKey;
	}else if (this.Ns){
		if (this.event.modifiers==4){
			return true;
		}
	}
}	

function stopEvent(){
	this.event.cancelBubble = true;
}

function isElementValidDate( Msg ){
	return isDate(this.value, FormatDate, Msg);
}

function isElementValidEmail( Msg ){
	return isMail(this.value, Msg);
}

function isElementValidWWW( Msg ){
	return isWWW(this.value, Msg);
}

function areFieldsRequiredCompiled ( MyForm, Msg ){
	return isValidForm( MyForm, Msg );
}

function isValidExtension ( Msg ){
var Excheck = true;
	for (i=0; i < Extension.length; i++){
		Excheck = isValidEstension( this.value, Extension[i] );
		if (!Excheck){alert(Msg); return Excheck}
	}
	return Excheck;
}

function isMyValidFiscalcode ( Msg ){
var Excheck = true;
	Excheck = isValidFiscalCode1( this.value );
	if (!Excheck){alert(Msg); return Excheck}
	return Excheck;
}

function isMyValidPIva ( Msg ){
var Excheck = true;
	Excheck = isPIva( this );
	if (!Excheck){alert(Msg); return Excheck}
	return Excheck;
}

/******************************* MAURI *************************************/
/*	Classe per compatibilità tra browser differenti per gesione degli attributi */
/******************************* MAURI *************************************/
function MauElement(name){
	this.name = name;
	this.attributes = new Array();
	return (this);
}
	MauElement.prototype.name 			= name;
	MauElement.prototype.addAttribute 	= addAttribute;
	MauElement.prototype.getAttribute 	= getAttribute;
	MauElement.prototype.attributes 	= attributes;
	
function name(){
	return this.name;
}

function addAttribute( Attr, Value ){
	var tempArray;
	var namedItem = document.createAttribute( Attr );
		namedItem.value = Value;
		tempArray = new Array(this.attributes.length+1);
		for (var i=0; i < this.attributes.length; i++)
			tempArray[i] = this.attributes[i];
		tempArray[this.attributes.length] = namedItem;
		this.attributes = tempArray;
		return this.attributes.length;
}

function getAttribute( Attr ){
	for ( var i = 0; i < this.attributes.length; i++ )
		if (this.attributes[i].name == Attr)
			return this.attributes[i];
}

function attributes(){
	return this.attributes;
}


/******************************* MAURI *************************************/
/*	Classe per monitoraggio eventi del form; Check a seconda delle proprietà */
/*	degli elementi del Form stesso:											 */
/******************************* MAURI *************************************/
function SuperMonitors( MyForm, lang ){
	this.Ie    = isIE;
	this.LowIe = is_ie5bet6;
	this.Ns    = is_nav4;
	this.Form  = MyForm;
	this.language = lang;
	//this.Elements = new Array();
	//TheForm = this.Form;
	return (this);
}
//Definisce i metodi della Classe SuperMonitors
	//Istanzio il gestore a Tutta la Form
	SuperMonitors.prototype.setControl = SuperMonitors_addElements;
	//Controllo eventi per singolo Elemento
	SuperMonitors.prototype.addHandlers = SuperMonitors_addHandlers;
	//Monitoraggio
	SuperMonitors.prototype.handler = SuperMonitors_MyHandler;
	//Settaggio lingua
	SuperMonitors.prototype.getLanguage = getLanguage;
	//Richiamo monitoraggio per gni evento
	SuperMonitors.prototype.getEvent = getEvent;
	//Settagio proprietà elemento
	SuperMonitors.prototype.setPropertyByElementName = setPropertyByElementName;
	//Settaggio Funzione Custom 
	SuperMonitors.prototype.CustomCtrlOnSubmit = CustomCtrlOnSubmit;
	//Settaggio estensioni del file in upload
	SuperMonitors.prototype.setExtensionFile = setExtensionFile;
	//Controllo se sono cambiati i valori del Form
	SuperMonitors.prototype.isElementFormChanged = isElementFormChanged;

/******************************* MAURI *************************************/
/*	Metodi interni Classe SuperMonitors								 	   */
/******************************* MAURI *************************************/

	
function SuperMonitors_addElements(){
	this.addHandlers(this.Form);
	this.setPropertyByElementName(this.Form.name,'sub','submit');
	for (var i=0; i < this.Form.length; i++){
		this.addHandlers(this.Form.elements[i]);
		if (this.Ie && this.Form.elements[i].getAttribute('required')){
			this.Form.elements[i].className = "required";
		}
		else if (this.Ns && this.Form.elements[i].required=="true"){
			eval('document.classes.requiredNS.'+this.Form.elements[i].name+'.background="yellow"');
			eval('document.classes.requiredNS.'+this.Form.elements[i].name+'.borderstyle="double"');
		}
	}	
}

function getLanguage(){
	l = this.language;
}

function setExtensionFile(){
	for( i=0; i < arguments.length; i++ ){
		Extension[i] = arguments[i];
	}
}

function isElementFormChanged(){
	return isFormChanged( this.Form )
}

function setPropertyByElementName( MyElement, MyProperty, MyEvent ){
	if ( MyElement != this.Form.name ){
		if (this.Ie){
			for (i=0; i < this.Form.length; i++){
				/*
				Mauri: Modificato ticket nr. 42728 e nr. 39099 e 39100
				if (this.Form.elements[i].name == MyElement &&
					this.Form.elements[i].type != "hidden"){*/
				if (this.Form.elements[i].name == MyElement){
					if (this.LowIe || is_gecko){
						this.Form.elements[i].setAttribute( 'CustomAttribute', MyProperty );
					}
					else{
						this.Form.elements[i].setAttribute( MyProperty, 'true' );						
						var EleObj = new MauElement(MyElement);
						var idxObj = -1;
						for(var jj=0;jj < SuperElements.length; jj++)
							if (SuperElements[jj].name == MyElement)
								idxObj = jj;
						if (idxObj==-1){
							for( Attr in this.Form.elements[i].attributes);
								EleObj.addAttribute(Attr, this.Form.elements[i].getAttribute(Attr));							
							EleObj.addAttribute(MyProperty, "true");		
							var TempArray = new Array(SuperElements.length+1)
							for(var mm=0;mm<SuperElements.length;mm++)
								TempArray[mm] = SuperElements[mm];
							TempArray[SuperElements.length] = EleObj;
							SuperElements = TempArray;
						}else
							SuperElements[idxObj].addAttribute(MyProperty, "true");
					}
					if (arguments.length > 2){
						this.Form.elements[i].setAttribute('Event', MyEvent);
					}
				}
			}
		}
		else if (this.Ns){
			eval('this.Form.elements[arguments[0]].'+arguments[1]+'="true"');
			if (arguments.length > 2){
				this.Form.elements[arguments[0]].Event=arguments[2];
			}
		}
	}
	else{
		if (this.Ie){
			this.Form.setAttribute(MyProperty,'true');
			if (arguments.length > 2){
				this.Form.setAttribute('Event', MyEvent);
			}
		}
		else if (this.Ns){
			eval('this.Form.'+arguments[1]+'="true"');
			if (arguments.length > 2){
				this.Form.Event=arguments[2];
			}
		}
	}
}

function CustomCtrlOnSubmit( MyCustomFunction ){
	if (this.Ie){
		this.Form.setAttribute('CustomFunction','true');
		this.Form.setAttribute('CustomFunctionName', MyCustomFunction);
	}
	else if (this.Ns){
		eval('this.Form.CustomFunction="true"');
		eval('this.Form.CustomFunctionName="' + arguments[0] + '"');
	}
}

function getEvent(){
	this.getLanguage();
	return this.handler;
}

function SuperMonitors_addHandlers(Element){

	//Element.onabort = handler;
	Element.onchange = this.getEvent();
	//Element.ondblclick = handler;
	//Element.onfocus = handler;
	//Element.onkeypress = this.getEvent();
	//Element.onload = handler;
	//Element.onmouseup = handler;
	//Element.onmouseout = handler;
	//Element.onreset = handler;
	//Element.onselect = handler;
	//Element.onunload = handler;
	Element.onblur = this.getEvent();
	Element.onclick = this.getEvent();
	//Element.onerror = handler;
	//Element.onkeydown = this.getEvent();
	//Element.onkeyup = this.getEvent();
	//Element.onmousedown = handler;
	//Element.onmouseover = handler;
	//Element.onmove = handler;
	//Element.onresize = handler;
	Element.onsubmit = this.getEvent();
}

function SuperMonitors_MyHandler(e){

	var MyEvent = new SingleEvent(e);
	//alert("Evento: " + MyEvent.type);
	//alert('Evento in proprietà: ' + MyEvent.getElementEvent());
	//Se combaciano gli eventi di controllo
	
	if (MyEvent.type == MyEvent.getElementEvent()){
	
		//alert("Evento: " + MyEvent.type);
	
		//if (MyEvent.getElementValue()!=''){ //Comment Date 16.01.2006 Field controll don't work fine if element value is null
			if (MyEvent.hasElementProperty('date')){
				return MyEvent.isElementValidDate( ErrorMsg['date_'+l] );
			}
			if (MyEvent.hasElementProperty('email')){
				return MyEvent.isElementValidEmail( ErrorMsg['email_'+l] );
			}
			if (MyEvent.hasElementProperty('upload')){
				return MyEvent.isValidExtension( ErrorMsg['extension_'+l] );
			}
			if (MyEvent.hasElementProperty('fiscalcode')){
				return MyEvent.isMyValidFiscalcode( ErrorMsg['fiscalcode_'+l] );
			}
			if (MyEvent.hasElementProperty('piva')){
				return MyEvent.isMyValidPIva( ErrorMsg['piva_'+l] );
			}
			if (MyEvent.hasElementProperty('currency')){
				MyEvent.value = GetEuroCurrency(MyEvent.value);
			}
			if (MyEvent.hasElementProperty('formateuro')){
				FormatEuro(MyEvent.getElementObj());
			}
			if (MyEvent.hasElementProperty('www')){
				return MyEvent.isElementValidWWW( ErrorMsg['www_'+l] );
			}		
			if (MyEvent.hasElementProperty('uniquecheckbox')){
				setOnlyThisCheckBox(this.form, MyEvent.getElementObj());
			}				
			if (MyEvent.hasElementProperty('radiobox')){
				DisabledHiddenRadioButton(MyEvent.getElementObj());
			}			
			
			
			if (MyEvent.getElementObj().attributes!=null){
				if ((is_gecko || is_ie5bet6) && MyEvent.getElementObj().getAttribute("CustomAttribute") != null)
					return eval( MyEvent.getElementObj().getAttribute("CustomAttribute") );
				else{
					var isFind = false;
					for(ElementAttribute in MyEvent.getElementObj().attributes)
						if (ElementAttribute.substring(0,6).toLowerCase() == 'custom' && ElementAttribute.indexOf("(") > 0){
							isFind = true;
							return eval(ElementAttribute);
						}
					if (!isFind){
						for(var jj = 0; jj < SuperElements.length; jj++)
							if (MyEvent.getElementObj().name==SuperElements[jj].name)
								for(var jjj=0; jjj < SuperElements[jj].attributes.length;jjj++)
									if(SuperElements[jj].attributes[jjj].name.substring(0,6).toLowerCase() == 'custom' && SuperElements[jj].attributes[jjj].name.indexOf("(") > 0)
										return eval(SuperElements[jj].attributes[jjj].name);
					}
				}
			}
				
		//}

		//Se è elemento Form stesso...
		if (MyEvent.hasElementProperty('sub')){
			if ( MyEvent.hasElementProperty('CustomFunction') ){
				if(!eval(MyEvent.getElementProperty('CustomFunctionName')))
					return false;
			}
			for (var i=0; i < MyEvent.getElementObj().length; i++){
				var check = true;
				var MyFormElement = MyEvent.getElementObj().elements[i];
				
				if (hasProperty( MyFormElement, 'required' ) || hasProperty( MyFormElement, 'currency' )){
					check = isValidForm( MyEvent.getElementObj(), ErrorMsg['required_'+l] );					
					if (!check){ return check }
					
					if (hasProperty( MyFormElement, 'date' )){
						check = isDate(MyFormElement.value, FormatDate, ErrorMsg['date_'+l]);
						if (!check){ return check }
					}
					
					if (hasProperty( MyFormElement, 'email' )){
						check = isMail(MyFormElement.value, ErrorMsg['email_'+l]);
						if (!check){ return check }
					}

					if (hasProperty( MyFormElement, 'fiscalcode' )){
						check = isValidFiscalCode1( MyFormElement.value );
						if (!check){ alert(ErrorMsg['fiscalcode_'+l]); return check }
					}	
					
					if (hasProperty( MyFormElement, 'piva' )){
						check = isPIva( MyFormElement );
						if (!check){ alert(ErrorMsg['piva_'+l]); return check }
					}	
								
					if (hasProperty( MyFormElement, 'currency' )){
						(isNaN(GetEuroCurrency(MyFormElement.value))) ? alert(ErrorMsg['number_'+l]) : MyFormElement.value = GetEuroCurrency(MyFormElement.value);
						check = (!isNaN(GetEuroCurrency(MyFormElement.value)));
						if (!check){ return check }
					}

					if (hasProperty( MyFormElement, 'upload' )){
						for (i=0; i < Extension.length; i++){
							check = isValidEstension( MyFormElement.value, Extension[i] );
							if (!check){ alert(ErrorMsg['extension_'+l]); return check }
						}
					}
				}
			}
		}
	}
		
	MyEvent.stopEvent();

}









