/**************************************
	Funzioni essenziali di connessione
	tra JS e FORM DOM
***************************************/
function getFormField(FormName,FieldName) {
	if(!document.forms[FormName]) return false;
	var obj = document.forms[FormName].elements[FieldName];
	if(!obj) return false;
	return obj;
}
function checkbox_selectAll(FormName, FieldName, CheckValue)	{
	var objCheckBoxes = getFormField(FormName, FieldName);
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes) objCheckBoxes.checked = CheckValue;
	else for(var i = 0; i < countCheckBoxes; i++) objCheckBoxes[i].checked = CheckValue;
}
function radio_getValue(radio) {
	if (!radio.length) return false;
	for (var i=0; i < radio.length; i++) {
		if (radio[i].checked) return radio[i].value;
	}
	return undefined;
}
function select_clear(FormName, FieldName) {
	var objSelect = getFormField(FormName, FieldName);
	for (var i = objSelect.length - 1; i >= 0; i--)
		objSelect.remove(i);
}
function select_getValue(FormField) {
	return FormField.options[FormField.selectedIndex].value;
}
function select_add(FormName, FieldName, value, text, def) {
	var objSelect = getFormField(FormName, FieldName);
	try {
		objSelect.add(new Option(text,value,(def===true),(def===true)),null);
	} catch(ex){
		objSelect.add(new Option(text,value,(def===true),(def===true)));
	}
}
function select_populate(FormName, FieldName, SelectLists, DefaultValue) {
	for (var c=0; c<SelectLists.length; c++) {
		if (typeof SelectLists[c] == "array") select_add(FormName, FieldName, SelectLists[c][0], SelectLists[c][1],(DefaultValue==SelectLists[c][0]));
	}
}
/*****************************************
    Controllo della Partita I.V.A.
    Linguaggio: JavaScript
******************************************/
function input_checkPI(pi) {
    if(pi == '')  return false;
    if(pi.length != 11)
        return false;
    var validi = "0123456789";
    for(var i = 0; i < 11; i++ ){
        if(validi.indexOf( pi.charAt(i) ) == -1 ) return false;
    }
    var s = 0;
    for(var i = 0; i <= 9; i += 2 )
        s += pi.charCodeAt(i) - '0'.charCodeAt(0);
    for(var i = 1; i <= 9; i += 2 ){
        var c = 2*( pi.charCodeAt(i) - '0'.charCodeAt(0) );
        if( c > 9 )  c = c - 9;
        s += c;
    }
    if((10-s%10)%10 != pi.charCodeAt(10) - '0'.charCodeAt(0)) return false;
    return true;
}
/**************************************
	Controllo del Codice Fiscale
	Linguaggio: JavaScript
***************************************/
function input_checkCF(cf) {
	var validi, i, s, set1 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ", set2 = "ABCDEFGHIJABCDEFGHIJKLMNOPQRSTUVWXYZ", setpari = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", setdisp = "BAKPLCQDREVOSFTGUHMINJWZYX";
	if(cf == '' ) return false;
	cf = cf.toUpperCase();
	if( cf.length != 16 ) return false;
	validi = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
	for(var i = 0; i < 16; i++ ){
		if( validi.indexOf( cf.charAt(i) ) == -1 ) return false;
	}
	var s = 0;
	for(var i = 1; i <= 13; i += 2 )
		s += setpari.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	for(var i = 0; i <= 14; i += 2 )
		s += setdisp.indexOf( set2.charAt( set1.indexOf( cf.charAt(i) )));
	if(s%26 != cf.charCodeAt(15)-'A'.charCodeAt(0) )
		return false;
	return true;
}
function input_img_check(img, value) {
	document[img].src='/img/icone/'+((value) ? 'accept' : 'reject')+'.gif';
}

function input_checkString (field, nomevar, minimum, maximum) {
	var lunghezza = field.value.length;
	var errore = false;
	var frase = "";
	if ((minimum!==false && lunghezza<minimum) || (maximum!==false && lunghezza>maximum))	errore = true;
	if (maximum!==false || minimum!==false)													frase += ", puņ contenere ";
	if (maximum!==false)																	frase += " massimo "+maximum;
	if (maximum!==false && minimum!==false)													frase += " e ";
	if (minimum!==false || minimum<1)														frase += " minimo "+minimum;
	frase +=  " caratteri";
	if (errore) {
		frase = "Campo \""+nomevar+"\" non valido"+frase;
		alert(frase);
		return false;
	}
	return true;
}

function input_checkTel(tel) {
	return tel.match(/^(\+)(\d{2,5})\.\d{2,5}\.\d{2,12}$/);
}
function input_checkEmail(email) {
	return email.match(/^[A-Za-z0-9][_\.A-Za-z0-9-]+@([A-Za-z0-9][A-Z0-9a-z-\.]+)+\.([A-Za-z]{2,4})$/);
}
