/*    http://www.dwzone.it	Copyright © 2002 Revello Gianluigi, Inc. All rights reserved.				SOFTWARE LICENSE AGREEMENT:This is an agreement by and between Revello Gianluigi ("Licensor") and the End User ("Licensee"), who is being licensed to use the extension(s). Licensee acknowledges that this is only a limited nonexclusive license. Licensor is and remains the owner of all titles, rights, and interests in the Software.This License permits Licensee to install the Software on more than one computer system, as long as the Software will not be used on more than one computer system simultaneously, this also applies to computer systems of different operating systems (i.e. Macintosh or Windows) provided this provision is adhered to. Licensee will not make copies of the Software or allow copies of the Software to be made by others, unless authorized by this License Agreement. Licensee may make copies of the Software for backup purposes only. This Software is subject to a limited warranty. Licensor warrants to Licensee that the physical medium on which this Software is distributed is free from defects in materials and workmanship under normal use, the Software will perform according to its printed documentation, and to the best of Licensor's knowledge Licensee's use of this Software according to the printed documentation is not an infringement of any third party's intellectual property rights. To the extent permitted by law, THE ABOVE-STATED LIMITED WARRANTY REPLACES ALL OTHER WARRANTIES, EXPRESS OR IMPLIED, AND LICENSOR DISCLAIMS ALL IMPLIED WARRANTIES INCLUDING ANY IMPLIED WARRANTY OF TITLE, MERCHANTABILITY, NONINFRINGEMENT, OR OF FITNESS FOR A PARTICULAR PURPOSE. No agent of Licensor is authorized to make any other warranties or to modify this limited warranty. Licensee has specific legal rights pursuant to this warranty and, depending on Licensee's jurisdiction, may have additional rights. Notwithstanding the foregoing, LICENSOR IS NOT LIABLE TO LICENSEE FOR ANY DAMAGES, INCLUDING COMPENSATORY, SPECIAL, INCIDENTAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL DAMAGES, CONNECTED WITH OR RESULTING FROM THIS LICENSE AGREEMENT OR LICENSEE'S USE OF THIS SOFTWARE, whether based on lost revenue or otherwise, regardless of whether LICENSEE was advised of the possibility of such losses in advance. In no event shall LICENSOR's liability hereunder exceed the amount of license fees paid by Licensee, regardless of whether Licensee's claim is based on contract, tort, strict liability, product liability or otherwise. Licensee's jurisdiction may not allow such a limitation of damages, so this limitation may not apply.Licensee agrees to defend and indemnify Licensor and hold Licensor harmless from all claims, losses, damages, complaints, or expenses connected with or resulting from Licensee's business operations. Licensor has the right to terminate this License Agreement and Licensee's right to use this Software upon any material breach by Licensee.Licensee agrees to return to Licensor or to destroy all copies of the Software upon termination of the License.This License Agreement is the entire and exclusive agreement between Licensor and Licensee regarding this Software. This License Agreement replaces and supersedes all prior negotiations, dealings, and agreements between Licensor and Licensee regarding this Software. This License Agreement is valid without Licensor's signature. It becomes effective upon the earlier of Licensee's signature or Licensee's use of the Software.*/


function dwz_MaskedField(fieldName,mask,restrict,el){
	var v = el.value;
	var e = window.event.keyCode;
	if(e==8 || e==9 || e==0 || e==46){return};
	if(v.length<1){return};
	var len = v.length;
	if(restrict=="-1"){
		esci = false;
	}else{
		esci = true;
	};
	while(!esci){
		if(len<1){
			esci = true;
		};
		var regExp = dwz_CreateRegExt(mask.substr(0, len));
		var test = new RegExp('^' + regExp + '$');
		if (v.match(test)) { 
			break;
		};
		len -= 1;
		v = v.substring(0,len);
	};
	v = dwz_CompleteMask(v,mask);
	if(v!=el.value){
		el.value = v;
	}	;
};

function dwz_CompleteMask(txt,mask){
	exit = false;
	while(!exit){
		if(txt.length>=mask.length){
			break;
		};
		myChar = txt.length;
		switch(mask.charAt(myChar)){
		case "?":;
		case "W":;
		case "A":;
		case "0":;
			exit = true;
			break;
		default:;
			txt += mask.charAt(myChar);
			break;
		};
	};
	return txt;
};


function dwz_CreateRegExt(testo){
	/*separatore*/;
	testo = testo.replace(/([-\/\[\]()\*\+\\])/g, '\\$1');
	/*Caratteri numerici*/;
	testo = testo.replace(/0/g, '\\d');
	/*Carattere yolly*/
	testo = testo.replace(/\?/g, '.');
	/*caratteri alfanumerici*/
	testo = testo.replace(/W/g, '[0-9\u0040-\u005A\u0061-\u007A\u0100-\u017E\u0180-\u0233\u0391-\u03CE\u0410-\u044F\u05D0-\u05EA\u0621-\u063A\u0641-\u064A\u0661-\u06D3\u06F1-\u06FE]');
	/*caratteri alfabetici*/
	testo = testo.replace(/A/g, '[\u0040-\u005A\u0061-\u007A\u0100-\u017E\u0180-\u0233\u0391-\u03CE\u0410-\u044F\u05D0-\u05EA\u0621-\u063A\u0641-\u064A\u0661-\u06D3\u06F1-\u06FE]');
	return testo;
};


function dwz_StartDependentCombo(combo,masterCombo,firstLineText){
	var detailCombo = dwz_GetE(combo);
	var detailValue = "";
	if(detailCombo.options.length>0){
		detailValue = detailCombo.options[detailCombo.selectedIndex].value;
	};
	dwz_ChangeDependentCombo(combo,masterCombo,firstLineText);
	for(x=0;x<detailCombo.options.length;x++){
		if(detailValue == detailCombo.options[x].value){
			detailCombo.selectedIndex=x;
			break;
		};
	};
};


function dwz_ChangeDependentCombo(combo,masterCombo,firstLineText){
	if(masterCombo.options.length<1){
		alert("WARNING!!\n\nThe MasterCombo is void\nCannot apply Dependent Combo behaviors");
		return;
	}else{
		var value = masterCombo.options[masterCombo.selectedIndex].value;
	};
	var detailCombo = dwz_GetE(combo);
	detailCombo.length = 0;
	if(value){
		newValue = eval("dwz_"+combo+"_ListValue");
		newText = eval("dwz_"+combo+"_ListText");
		var i = 0;
		if(firstLineText!=""){
			detailCombo.options[i] = new Option(firstLineText,"");
			i += 1;
		};
		if(newValue[value]){
			for(x=0;x<newValue[value].length;x++){
				detailCombo.options[i] = new Option(newText[value][x],newValue[value][x]);
				i += 1;
			};
		}
		detailCombo.selectedIndex=0;
		if(detailCombo.onchange){
			detailCombo.onchange();
		}
	}else{
		if(firstLineText!=""){
			detailCombo.options[0] = new Option(firstLineText,"");
			detailCombo.selectedIndex=0;
		};
	};
};

function dwz_SetDefaultValue(dwz_MasterCombo,dwz_DetailCombo,firstLine){
	if(dwz_GetE(dwz_MasterCombo).getAttribute("masterDefaultValue")){
		dwz_MasterValue = dwz_GetE(dwz_MasterCombo).getAttribute("masterDefaultValue");
		dwz_setComboValue(dwz_MasterCombo,dwz_MasterValue);
	};
	dwz_ChangeDependentCombo(dwz_DetailCombo,dwz_GetE(dwz_MasterCombo),firstLine);
	if(dwz_GetE(dwz_MasterCombo).getAttribute("detailDefaultValue")){
		dwz_DetailValue=dwz_GetE(dwz_MasterCombo).getAttribute("detailDefaultValue");
		dwz_setComboValue(dwz_DetailCombo,dwz_DetailValue);
	};
};

function dwz_setComboValue(comboName,value){
	combo = dwz_GetE(comboName);
	for(x=0;x<combo.options.length;x++){
		if(combo.options[x].value==value){
			combo.selectedIndex = x;
			break;
		};
	};
};

function dwz_RestrictedTextArea(elName,el,size,field){
	if(el.value.length!=0){
		if(size!=""){
			if(el.value.length>parseInt(size)){
				el.value = el.value.substring(0,parseInt(size));
			};
		};
	};
	if(field!="" && dwz_GetE(field)){
		if(dwz_GetE(field).tagName && dwz_GetE(field).tagName.toUpperCase()=="DIV"){
			dwz_GetE(field).innerHTML=el.value.length;
		}else{
			dwz_GetE(field).value=el.value.length;
		};
	};
};

function dwz_GetE(n, d){var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}; if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++){x=d.forms[i][n];}; for(i=0;!x&&d.layers&&i<d.layers.length;i++){x=dwz_findObj(n,d.layers[i].document);}; if(!x && d.getElementById) x=d.getElementById(n); return x;};

function dwz_OnBlurField(fieldName,minValue,maxValue){
	var el = dwz_GetE(fieldName);
	if(el.value.length==0){return}
	var v = el.value;
	var nV = v;
	if(minValue!=""){
		if(parseFloat(v)<parseFloat(minValue)){
			nV = minValue;
		};
	};
	if(maxValue!=""){
		if(parseFloat(v)>parseFloat(maxValue)){
			nV = maxValue;
		};
	};
	if(nV!=el.value){
		el.value=nV;
	};
};

function dwz_NumericField(fieldName,allowNegative,allowFloat,minValue,maxValue,elc){
	var el = dwz_GetE(fieldName);
	if(el.value.length==0){return};
	var nV = "";
	var nVn = "";
	var v = el.value;
	var myChar;
	var negative = "";
	if(v.substring(0,1)=="-"){
		v = v.substring(1);
		if(allowNegative=="0"){
			negative = "";
		}else{
			negative = "-";
		};
	};
	tmp = v.split(".");
	for(x=0;x<tmp[0].length;x++){
		myChar = tmp[0].substring(x,x+1);
		if(!isNaN(myChar)){
			nV += myChar;
		};
	};
	virgola = "";
	if(tmp.length>1 && allowFloat=="-1"){
		virgola = ".";
		for(x=0;x<tmp[1].length;x++){
			myChar = tmp[1].substring(x,x+1);
			if(!isNaN(myChar)){
				nVn += myChar;
			};
		};
	};
	nV += virgola + nVn;
	nV = negative + nV;
	if(nV!=el.value){
		el.value=nV;
	};
};

function dwz_CreateItem(comboName,array,label,value){
	if(!eval("dwz_"+comboName+"_ListText[array]")){
		eval("dwz_"+comboName+"_ListText[array] = new Array()");
		eval("dwz_"+comboName+"_ListValue[array] = new Array()");
	};
	eval("var index = dwz_"+comboName+"_ListText[array].length");
	eval("dwz_"+comboName+"_ListText[array][index] = label");
	eval("dwz_"+comboName+"_ListValue[array][index] = value");
};


//*************************
//
//   Flashing form element
//
//*************************
var dwz_formcollect
var dwz_flashcollect = new Array()
function dwz_startFlashColor(listField,color,interval){
	dwz_flashcolor = color
	dwz_flashinterval = interval
	dwz_formcollect=document.getElementsByTagName? document.getElementsByTagName("FORM") : document.all? document.all.tags("FORM") : new Array()
	var campi = listField.split("|")
	for (i=0; i<dwz_formcollect.length; i++){
		for (e=0; e<dwz_formcollect[i].elements.length; e++){
			for(x=0;x<campi.length;x++){
				val = campi[x].split(";")
				if (dwz_formcollect[i].name.toLowerCase()==val[0].toLowerCase() && (dwz_formcollect[i].elements[e].name && dwz_formcollect[i].elements[e].name.toLowerCase()==val[1].toLowerCase() || dwz_formcollect[i].elements[e].id && dwz_formcollect[i].elements[e].id.toLowerCase()==val[1].toLowerCase())){
					index = dwz_flashcollect.length
					dwz_flashcollect[index] = new Object()
					dwz_flashcollect[index].field = dwz_formcollect[i].elements[e]
					dwz_flashcollect[index].color = val[2]
				}
			}
		}
	}
	if (dwz_flashcollect.length>0){
		setInterval("dwz_flashelements()",interval)
	}
}

function dwz_flashelements(){
	for (f=0; f<dwz_flashcollect.length; f++){
		if (dwz_flashcollect[f].field.style.color==''){
			dwz_flashcollect[f].field.style.color = dwz_flashcollect[f].color
		}else{
			dwz_flashcollect[f].field.style.color = ""
		}
	}
}


//******************************
//
// Highlight form element
//
//*******************************
var dwz_ns6 = document.getElementById && !document.all
var dwz_previous = ''
var dwz_eventobj
var dwz_intended=/INPUT|TEXTAREA|SELECT|OPTION/
function dwz_checkel(which){
	if (which.style && dwz_intended.test(which.tagName)){
		if (dwz_ns6 && dwz_eventobj.nodeType==3){
			dwz_eventobj = dwz_eventobj.parentNode.parentNode
		}
		return true
	}else{
		return false
	}
}
function dwz_highlight(e,highlightcolor,fontColor){
	dwz_eventobj=dwz_ns6? e.target : event.srcElement
	if (dwz_previous!=''){
		if (dwz_checkel(dwz_previous)){
			dwz_previous.style.backgroundColor = ''
			dwz_previous.style.color = ''
		}
		dwz_previous = dwz_eventobj
		if (dwz_checkel(dwz_eventobj)){
			dwz_eventobj.style.backgroundColor = highlightcolor
			dwz_eventobj.style.color = fontColor
		}
	}else{
		if (dwz_checkel(dwz_eventobj)){
			dwz_eventobj.style.backgroundColor = highlightcolor
			dwz_eventobj.style.color = fontColor
		}
		dwz_previous = dwz_eventobj
	}
}

//*************************
//
//   Accept Terms Form Submission
//
//*************************
function dwz_AgreeSubmit(el,f){
	for(x=0;x<f.elements.length;x++){
		if(f.elements[x].type && f.elements[x].type.toLowerCase()=='submit'){
			f.elements[x].disabled=!el.checked
		}
	}
}

//*************************
//
//   Disable "Enter" key in Form
//
//*************************
function dwz_HandleEnter(e){
	field = dwz_ns6? e.target : event.srcElement
	if(field.tagName && field.tagName.toUpperCase()=='TEXTAREA'){
		return true
	}
	
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		var i;
		for (i = 0; i < field.form.elements.length; i++){
			if (field == field.form.elements[i]){
				break;
			}
		}
		while(1){
			i = (i + 1) % field.form.elements.length;
			if(!field.form.elements[i].disabled && field.form.elements[i].type && field.form.elements[i].type.toLowerCase()!="hidden"){
				field.form.elements[i].focus();
				break
			}
			if(i==0){
				break
			}
		}
		return false;
	}else{
		return true;
	}
}     


//*************************
//
//   Flashing links
//
//*************************
var dwzFlashLinks = new Array()
var dwzFlash = false
function dwzChangeLinkColor(){
	for (i=0; i< dwzFlashLinks.length; i++){
		if(!dwzFlash){
			dwzFlashLinks[i].links.style.color = dwzFlashLinks[i].textColor
			dwzFlashLinks[i].links.style.backgroundColor = dwzFlashLinks[i].bgColor
		}else{
			dwzFlashLinks[i].links.style.color = ''
			dwzFlashLinks[i].links.style.backgroundColor = ''
		}
	}
	if(dwzFlash){
		dwzFlash = false
	}else{
		dwzFlash = true
	}
}

function dwzStartFlashLink(interval){
	var links = document.getElementsByTagName("A")
	for(x=0;x<links.length;x++){
		par = ""
		if(links[x].getAttribute && links[x].getAttribute("dwzFlashLink")){
			par = links[x].getAttribute("dwzFlashLink")
		}else if(links[x].getAttributeNS && links[x].getAttributeNS("dwzFlashLink")){
			par = links[x].getAttributeNS("dwzFlashLink")
		}
		if(par!=""){
			val = par.split(";")
			i = dwzFlashLinks.length
			dwzFlashLinks[i] = new Object()
			dwzFlashLinks[i].links = links[x]
			dwzFlashLinks[i].bgColor = val[0]
			dwzFlashLinks[i].textColor = val[1]
		}
	}
	if(dwzFlashLinks.length>0){
		setInterval("dwzChangeLinkColor()", interval)
	}
}

