// arry dei campi della form
var formFields = new Array();

// ****************************************************************************
// aggiunge un campo alla lista degli obbligatori
function waform_setMandatoryField(fieldName)
	{
	return setMandatoryField(fieldName);
	}
function setMandatoryField(fieldName)
	{
	for (var i = 0; i< formFields.length; i++)
		{
		if (formFields[i].name == fieldName)
			{
			formFields[i].mandatory = true;
			break;
			}
		}
	}

// ****************************************************************************
// toglie un campo dalla lista degli obbligatori
function waform_unsetMandatoryField(fieldName)
	{
	return unsetMandatoryField(fieldName);
	}
function unsetMandatoryField(fieldName)
	{
	for (var i = 0; i< formFields.length; i++)
		{
		if (formFields[i].name == fieldName)
			{
			formFields[i].mandatory = false;
			break;
			}
		}
	}

// ****************************************************************************
// modifica l'atributo di obbligarieta' di una label (cambia la classe)
function waform_setMandatoryLabel(fieldName)
	{
	return setMandatoryLabel(fieldName);
	}
function setMandatoryLabel(fieldName)
	{
	var span = document.getElementById(fieldName);
	if (span)
		span.className = 'waform_mdtlbl';
	var hlpLink = document.getElementById('hlplink_' + fieldName);
	if (hlpLink)
		hlpLink.className = 'waform_mdtlbl';
	}
	
// ****************************************************************************
// modifica l'atributo di obbligarieta' di una label (cambia la classe)
function waform_unsetMandatoryLabel(fieldName)
	{
	return unsetMandatoryLabel(fieldName);
	}
function unsetMandatoryLabel(fieldName)
	{
	var span = document.getElementById(fieldName);
	if (span)
		span.className = 'waform_nomdtlbl';
	var hlpLink = document.getElementById('hlplink_' + fieldName);
	if (hlpLink)
		hlpLink.className = 'waform_nomdtlbl';
	}
	
// ****************************************************************************
// modifica l'atributo di editabilita' di una label (cambia la classe)
// NON USARE!!! fa casino!
function waform_setReadonlyLabel(fieldName)
	{
	return setReadonlyLabel(fieldName);
	}
function setReadonlyLabel(fieldName)
	{
	var span = document.getElementById(fieldName);
	if (span)
		span.className = 'waform_lbl';
	var hlpLink = document.getElementById('hlplink_' + fieldName);
	if (hlpLink)
		hlpLink.className = 'waform_lbl';

	}

// ****************************************************************************
function waform_dammiIndiceCampo(fieldName)
	{
	return DammiIndiceCampo(fieldName);
	}
function DammiIndiceCampo(fieldName)
	{
	var i = 0;
	for (i=0; i < formFields.length; i++)
		{
		if (formFields[i].name == fieldName)
			break;
		}
	if (i >= formFields.length)
		{
		alert('Campo ' + fieldName + ' non trovato');
		return -1;
		}
	return i;
	}
	
// ****************************************************************************
// abilita/disabilita un singolo elemento di un campo
function waform_enableElem(elem, flagEnable)
	{
	elem.disabled = !flagEnable;
	if (flagEnable)
		{
		if (elem.className.indexOf("_disabled") != -1)
			elem.className = elem.className.substr(0, elem.className.length - ("_disabled").length);
		}
	else
		{
		if (elem.className.indexOf("_disabled") == -1)
			elem.className += "_disabled";
		}
	}
	
// ****************************************************************************
// restituisce un controllo di tipo input button o button
function waform_getButtonElem(form, buttonName)
	{
	var b = form.elements[buttonName];
	if (!b)
		// pugnuetta di safari coi button....
		b = form.children(buttonName);		
	return b;
		
	}
	
// ****************************************************************************
// abilita/disabilita un campo
// (fieldIndex e' l'indice di un eventuale array di radio; se vale -1, allora
// tutti i radio con quel nome vengono disabilitati)
function waform_innerEnableField(fieldName, fieldIndex, flagEnable)
	{
	var i = waform_dammiIndiceCampo(fieldName);
	if (i < 0)
		return false;

	var theForm = document.forms[formFields[i].formName];
	switch(formFields[i].type)
		{
		case	'button':
			waform_enableElem(waform_getButtonElem(theForm, formFields[i].name), flagEnable);
			break;
						
		case	'checkbox':
		case	'select':
		case	'text':
		case	'integer':
		case	'mailaddress':
		case	'textarea':
			waform_enableElem(theForm.elements[formFields[i].name], flagEnable);
			break;
			
		case	'multiselect':
		case	'listbox':
			waform_enableElem(theForm.elements[formFields[i].name + '[]'], flagEnable);
			break;

		case	'datetime':
		case	'time':
			waform_enableElem(theForm.elements['hour' + formFields[i].name], flagEnable);
			waform_enableElem(theForm.elements['min' + formFields[i].name], flagEnable);
			if (theForm.elements['sec' + formFields[i].name])
				waform_enableElem(theForm.elements['sec' + formFields[i].name], flagEnable);
		if(formFields[i].type == 'time')
			break;
		case	'date':
			waform_enableElem(theForm.elements['day' + formFields[i].name], flagEnable);
			waform_enableElem(theForm.elements['month' + formFields[i].name], flagEnable);
			waform_enableElem(theForm.elements['year' + formFields[i].name], flagEnable);
			b = waform_getButtonElem(theForm, 'waform_monthcal_' + formFields[i].name);
			if (b)
				{
				waform_enableElem(b, flagEnable);
				b = waform_getButtonElem(theForm, 'waform_yearcal_' + formFields[i].name);
				waform_enableElem(b, flagEnable);
				}
			break;

		case	'currency':
			waform_enableElem(theForm.elements['int_' + formFields[i].name], flagEnable);
			if (theForm.elements['dec_' + formFields[i].name])
				waform_enableElem(theForm.elements['dec_' + formFields[i].name], flagEnable);
			break;

		case	'radio':
			if (fieldIndex != undefined && fieldIndex != 'undefined')
				{
				if (fieldIndex > -1)
					{
					waform_enableElem(theForm.elements[formFields[i].name][fieldIndex], flagEnable);
					break;
					}
				}
			for (var c = 0; c < theForm.elements[formFields[i].name].length; c++)
				waform_enableElem(theForm.elements[formFields[i].name][c], flagEnable);
			break;

		case	'upload':
			waform_enableElem(theForm.elements[formFields[i].name], flagEnable);
			b = waform_getButtonElem(theForm, 'waform_showfile_' + formFields[i].name);
			if (b)
				{
				waform_enableElem(b, flagEnable);
				waform_enableElem(theForm.elements['waform_delfile_' + formFields[i].name], flagEnable);
				}
			break;

		
		default:
			return alert( "Il tipo del campo " + formFields[i].name + " non e' stato riconosciuto");
		}
	return true;
	}

// ****************************************************************************
// disabilita un campo
// (fieldIndex e' l'indice di un eventuale array di radio; se vale -1, allora
// tutti i radio con quel nome vengono disabilitati)
function waform_disableField(fieldName, fieldIndex)
	{
	return disableField(fieldName, fieldIndex);
	}
function disableField(fieldName, fieldIndex)
	{
	return waform_innerEnableField(fieldName, fieldIndex, false);
	}
	
	
// ****************************************************************************
// abilita un campo
// (fieldIndex e' l'indice di un eventuale array di radio; se vale -1, allora
// tutti i radio con quel nome vengono abilitati)
function waform_enableField(fieldName, fieldIndex)
	{
	return enableField(fieldName, fieldIndex);
	}
function enableField(fieldName, fieldIndex)
	{
	return waform_innerEnableField(fieldName, fieldIndex, true);
	}

// ****************************************************************************
// verifica che un singolo campo obbligatorio di una form sia valorizzato
// formField contiene l'array delle informazioni applicative relative al campo
function waform_checkMandatoryField(formField)
	{
	return checkMandatoryField(formField);
	}
function checkMandatoryField(formField)
	{
	if (!formField.mandatory)
		return "";

	var errMsg 	= '';
	var ctrls	= document.forms[formField.formName].elements;

	switch(formField.type)
		{
		case	'select':
		case	'text':
		case	'integer':
		case	'mailaddress':
		case	'textarea':
		case	'upload':
			if (Trim(ctrls[formField.name].value) == '')
				errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
			break;
			
		case	'multiselect':
		case	'listbox':
			fld = ctrls[formField.name + '[]'];
			var cntr = 0;
			for (cntr = 0; cntr < fld.options.length; cntr++)
				{
				if (fld.options[cntr].selected == true)
					break;
				}
			if (cntr == fld.options.length)
				errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
			break;

			
		case	'datetime':
		case	'time':
			ora = ctrls['hour' + formField.name];
			min = ctrls['min' + formField.name];
			if (ora.options[ora.selectedIndex].value == '' ||
				min.options[min.selectedIndex].value == '')
				errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
			sec = ctrls['sec' + formField.name];
			if (sec)
				{
				if (sec.options[sec.selectedIndex].value == '')
					errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
				}
			if(formField.type == 'time')
				break;
		case	'date':
			gio = ctrls['day' + formField.name];
			mes = ctrls['month' + formField.name];
			ann = ctrls['year' + formField.name];
			if (gio.options[gio.selectedIndex].value == '' ||
				mes.options[mes.selectedIndex].value == '' ||
				ann.options[ann.selectedIndex].value == '')
				errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
			break;

		case	'currency':
			if (ctrls['dec_' + formField.name])
				{
				if (ctrls['int_' + formField.name].value == '')
					errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
				}
			else
				{
				if (ctrls['int_' + formField.name].value  == '')
					errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
				}		
			break;

		case	'checkbox':
			if (!ctrls[formField.name].checked)
				errMsg = "Il campo " + formField.label + " e' obbligatorio e non e' stato valorizzato.\n";
			break;
			
		default:
			errMsg = "Il tipo del campo " + formField.label + " non e' stato riconosciuto.\n";
		}

	return errMsg;
	}



// ****************************************************************************
// verifica che gli elementi obbligatori di una form siano valorizzati
function waform_checkMandatoryFields(theForm)
	{
	return checkMandatoryFields(theForm);
	}
function checkMandatoryFields(theForm)
	{
	var errMsg = '';
	for (var i=0; i < formFields.length; i++)
		{
		if (formFields[i].formName == theForm.name && formFields[i].mandatory == true)
			errMsg += waform_checkMandatoryField(formFields[i]);
		}

	if (errMsg != '')
		return alert(errMsg);
		
	return true;

	}

// ****************************************************************************
// verifica che gli elementi di una form siano valorizzati correttamente
function waform_formalCheck(theForm)
	{
	return formalCheck(theForm);
	}
function formalCheck(theForm)
	{
	var errMsg = '';
	for (var i=0; i < formFields.length; i++)
		{
		if (formFields[i].formName == theForm.name)
			{
			switch(formFields[i].type)
				{
				case	'currency':
					if (!waform_checkInteger(theForm.elements['int_' + formFields[i].name]) || 
						!waform_checkInteger(theForm.elements['dec_' + formFields[i].name]))
						errMsg += "Il campo " + formFields[i].label + " non e' stato valorizzato correttamente.\n";
					break;
				case	'integer':
					if (!waform_checkInteger(theForm.elements[formFields[i].name]))
						errMsg += "Il campo " + formFields[i].label + " non e' stato valorizzato correttamente.\n";
					break;
				case	'mailaddress':
					if (!waform_checkMailAddress(theForm.elements[formFields[i].name]))
						errMsg += "Il campo " + formFields[i].label + " non e' stato valorizzato correttamente.\n";
					break;
				case	'date':
				case	'datetime':
					gio = theForm.elements['day' + formFields[i].name];
					mes = theForm.elements['month' + formFields[i].name];
					ann = theForm.elements['year' + formFields[i].name];
					if (gio.options[gio.selectedIndex].value != '' &&
						mes.options[mes.selectedIndex].value != '' &&
						ann.options[ann.selectedIndex].value != '')
						{
						mydate = new Date(ann.options[ann.selectedIndex].value,
												mes.options[mes.selectedIndex].value - 1,
												gio.options[gio.selectedIndex].value);
						if (gio.options[gio.selectedIndex].value != mydate.getDate() ||
							mes.options[mes.selectedIndex].value != (mydate.getMonth() + 1) ||
							ann.options[ann.selectedIndex].value != mydate.getFullYear())
							errMsg += "Il campo " + formFields[i].label + " non e' stato valorizzato correttamente.\n";
						}
					break;


				}
			}
		}
	if (errMsg != '')
		return alert(errMsg);
	return true;

	}

// ****************************************************************************
function waform_validateForm(theForm)
	{
	return validateForm(theForm);
	}
function validateForm(theForm)
	{
	if (eval(theForm.name + 'Cancel == true'))
		return true;
	if (eval(theForm.name + 'Delete == true'))
		{
		eval(theForm.name + 'Delete = false');
		return confirm('Confermi cancellazione?');
		}
	if (waform_formalCheck(theForm))
		{
		if(waform_checkMandatoryFields(theForm))
			return confirm('Confermi operazione?');
		}
	return false;
	}

// ****************************************************************************
function waform_checkInteger(ctrl)
	{
	return checkInteger(ctrl);
	}
function checkInteger(ctrl)
	{
	if ((ctrl.value * 2 / 2) == (ctrl.value + ''))
		return true;
	return false;
	}
	
// ****************************************************************************
function waform_checkMailAddress(address)
	{
	if (address.value == '')
		return true;
	//var emailPattern = /^(\w+(?:\.\w+)*)@((?:\w+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	var emailPattern = /^[\w-\.]{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,4}$/i;
	return (emailPattern.test(address.value) ? true : false);
	}

// ****************************************************************************
function waform_toggleDeleteFileCheck(deleteCheck)
	{
	var fileCtrlName = deleteCheck.name.substr(('waform_delfile_').length);

	waform_enableElem(deleteCheck.form.elements[fileCtrlName], !deleteCheck.checked);
	return true;
		
	}

// ****************************************************************************
function waform_emptySelectX(fldName)
	{
	var ctrl = window.document.forms[0].elements[fldName];
	for (var i = ctrl.options.length - 1; i > 0; i--)
		ctrl.options[i] = null;
	ctrl.selectedIndex = 0;
	
	return ctrl;
		
	}
	
// ****************************************************************************
function waform_fillSelectX(ctrl, values, toSelect)
	{
	var opt;
	var toret = false;
	for (var i = 0; i < values.length; i += 2)
		{
		opt = new Option(values[i+1], values[i]);	
		ctrl.options[(i / 2) + 1] = opt;
		if (values[i] == toSelect)
			{
			ctrl.selectedIndex = (i / 2) + 1;
			toret = true;
			}
		}
	return toret;
	}
	
// ****************************************************************************
function waform_reloadSelectX(fldName, descrFldName, tblName, 
								relKeyFldName, toSelect)
	{
	ctrl = waform_emptySelectX(fldName);
	var esito = waform_callRPCX("WASelect_StdReloadX", fldName, descrFldName, 
									tblName, relKeyFldName);
	return (esito == waform_rpc_error ? 
				esito :
				waform_fillSelectX(ctrl, esito, toSelect));
	}

// ****************************************************************************
// funzioncelle scherzose da usare qua e la'
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

function rPad(str, len, pad)
	{
	str = str + '';
    var result = str;
    for (var i=str.length; i<len; i++)
        result = result + pad;
    return result;
	}

