/*
 * Funzione che imposta il modulo di inserimento dati se azienda o privato
 */
function changeTipo(tipo) {
	if (tipo == 'A') {
		// valobj[1] = new validate('0','pariva','Partita IVA','document.datianagrafici.cod_fiscale_par_iva');		
		document.getElementById('ragsocialeLabel').innerHTML = "Ragione sociale:";
		if(document.getElementById('codfiscparivaLabel')) document.getElementById('codfiscparivaLabel').innerHTML = "Partita IVA:";
		if (navigator.appName == "Microsoft Internet Explorer") {
			document.getElementById('personarifLabel').style.display = "block";
			document.getElementById('personarifPix').style.display = "block";
			document.getElementById('personarifInput').style.display = "block";
		} else {
			document.getElementById('personarifLabel').style.visibility = "visible";
			document.getElementById('personarifPix').style.visibility = "visible";
			document.getElementById('personarifInput').style.visibility = "visible";
		}
	} else {
		// valobj[1] = new validate('0','codfisc','Codice Fiscale','document.datianagrafici.cod_fiscale_par_iva');
		document.getElementById('ragsocialeLabel').innerHTML = "Nome e Cognome:";	
		if(document.getElementById('codfiscparivaLabel')) document.getElementById('codfiscparivaLabel').innerHTML = "Codice Fiscale:"; 
		if (navigator.appName == "Microsoft Internet Explorer") {
			document.getElementById('personarifLabel').style.display = "none";
			document.getElementById('personarifPix').style.display = "none";
			document.getElementById('personarifInput').style.display = "none";
		} else {
			document.getElementById('personarifLabel').style.visibility = "hidden";
			document.getElementById('personarifPix').style.visibility = "hidden";
			document.getElementById('personarifInput').style.visibility = "hidden";
		}
	}
}

/*
 * Funzione di validazione dei campi
 */
function validate(formid) {
	var	returnValue = true;
	var isValid = true;
	var altValue = "";
	var validate = new Array();
	var firstErrElem = 0;
	valori = formid.getElementsByTagName("input");	
	
	for(var elem=0; elem < (valori.length) ; elem++) {
		
		isValid = true;
		altValue = valori[elem].alt;
		validate = altValue.split("-");
		realobj = valori[elem];
		
		if(realobj.type == 'text' && validate[0] == 1) {
		
			// realobj.style.backgroundColor = "white";
			realobj.style.border = "1px solid #666699";
			
			switch (validate[1]) {
				case 'data':
					if (realobj.value == "" || !isDate(realobj)) {
						isValid = false;
					}
					break;
				case 'euro':
					if (realobj.value == "" || !isEuro(realobj)) {
						isValid = false;
					} 
					break;
				case 'intero':
					if (realobj.value == "" || !isIntero(realobj)) {
						isValid = false;
					}
					break;
				case 'decimale':
					if (realobj.value == "" || !isDecimale(realobj)) {
						isValid = false;
					}
					break;
				case 'email':
					if (realobj.value == "" || !isEmail(realobj)) {
						isValid = false;
					}
					break;
				case 'telefono':
					if (realobj.value == "" || !isTelefono(realobj)) {
						isValid = false;
					}
					break;
				case 'url':
					if (realobj.value == "" || !isURL(realobj)) {
						isValid = false;
					}
					break;
				case 'codfisc':
					if (realobj.value == "" || !isCodFisc(realobj)) {
						isValid = false;
					}
					break;
				case 'pariva':
					if (realobj.value == "" || !isParIva(realobj)) {
						isValid = false;
					}
					break;
				case 'parivacodfisc':
					if (realobj.value == "" || !isParIvaCodFisc(realobj)) {
						isValid = false;
					}
					break;
				case 'password':
					if (realobj.value == "" || !isPassword(realobj)) {
						isValid = false;
					}		
					break;
				case 'username':
					if (realobj.value == "" || !isUsername(realobj)) {
						isValid = false;
					}
					break;
				case 'stringa':
					if (realobj.value == "" || !isStringa(realobj)) {
						isValid = false;
					}
					break;
				case 'select':
					isSelect(realobj);
					break;
				case 'provincia':
					if (realobj.value == "" || !isProvincia(realobj)) {
						isValid = false;
					}
					break;
				case 'cap':
					if (realobj.value == "" || !isCAP(realobj)) {
						isValid = false;
					}
					break;
				case 'equal':
					if (realobj.value == "" || !isEqual(realobj,realobj_eq)) {
						isValid = false;
					}
			}
		
			if(!isValid) {
				if(!firstErrElem) firstErrElem = elem;
				// realobj.style.backgroundColor = "#eeeeee";
				realobj.style.border = "1px solid #FF0000";
				returnValue = false;
			}
		}
	}

	valori[firstErrElem].focus();
	return returnValue;
}

function isEqual(campo,campo_eq) {
	if (campo.value == campo_eq.value) {
		return true;
	} else {
		return false;
	}
}

function isDate(campo) {
	// controlla se l'argomento passato � una data valida
	var tmp = new String();
	var giornook = new String();
	var giorno;
	var mese;
	var anno;
	var maxgiorni;
	var data = campo.value;
	if (data != '') {
		var data_div = new Array();
		// se la data � stata inserita con il separatore / li sostituisco con il separatore -
		while (data.indexOf('/') >= 0) {
			data = data.replace('/','-');
		}
		// se la data � stata inserita con il separatore . li sostituisco con il separatore -
		while (data.indexOf('.') >= 0) {
			data = data.replace('.','-');
		}
		// inserisco giorno, mese e anno della data inserita in un'array
		data_div = data.split('-');
		if (!data_div[0]) data_div[0] = '';
		if (!data_div[1]) data_div[1] = '';
		if (!data_div[2]) data_div[2] = '';
		if (data_div[2].length < 4) data_div[2] = '19' + data_div[2];
		giorno = parseInt(data_div[0]);
		mese = parseInt(data_div[1]);
		anno = parseInt(data_div[2]);
		if (mese < 0 || mese > 12) {
			campo.select();
			return false;
		} 
		if (anno < 1970 || anno.length > 4) {
			campo.select();
			return false;
		}
		// controllo numero max giorni per mese specificato
		if (mese == 1 || mese == 3 || mese == 5 || mese == 7 || mese == 8 || mese == 10 || mese == 12) {
			maxgiorni = 31;
		}
		else if (mese == 4 || mese == 6 || mese == 9 || mese == 11) {
			maxgiorni = 30;
		}
		else if (mese == 2) {
			var bisestile = false;
			if ((anno % 4) == 0) bisestile = true;
			if ((anno % 100) == 0) bisestile = false;
			if ((anno % 400) == 0) bisestile = true;
			(bisestile) ? maxgiorni = 29 : maxgiorni = 28;
		}
		// fine controllo
		if (giorno < 0 || giorno > maxgiorni) {
			campo.select();
			return false;
		}
		giornook = ((giorno < 10) ? '0' + giorno.toString() : giorno.toString()) + '-';
		giornook += ((mese < 10) ? '0' + mese.toString() : mese.toString()) + '-';
		giornook += anno.toString();
		campo.value = giornook;
	}
	return true;
}

function isEuro(campo) {
	// controllo se il valore del text passato come argomento � un importo euro valido
	var numero = new Array();
	var tmp = new String();
	var achar = new String();
	var okchar = '0123456789.';
	var data = campo.value;
	if (data != '' && data != '0') {
		if (data.indexOf(',') >= 0) {
			// se il separatore di decimali � , lo sostituisco con .
			data = data.replace(',','.');
		}
		// separo l'unit� dai decimali
		numero = data.split('.');
		if (numero.length > 2) {
			campo.value = "";
			return false;
		} else {
			if (numero[0] == '') numero[0] = '0';
			if ((!numero[1]) || numero[1] == '') numero[1] = '0';
			tmp = numero[0];
			for (x = 0;x < tmp.length;x ++) {
				achar = tmp.substr(x,1);
				if (okchar.indexOf(achar) == -1) {
					campo.value = "";
					return false;
				}
			}
			tmp = numero[1];
			if (tmp.length > 2) {
				campo.value = "";
				return false;
			} else {
				for (x = 0;x < tmp.length;x ++) {
					achar = tmp.substr(x,1);
					if (okchar.indexOf(achar) == -1) {
						campo.value = "";
						return false;
					}
				}
			} 
			data = numero[0] + "." + numero[1];
			campo.value = data;
		}
	} else {
		campo.value = '0';
	}
	return true;
}

function isIntero(campo) {
	// controllo se il valore del text object passato come argomento � un intero valido
	// scorro semplicemente la variabile data controllando che siano tutti numeri
	var achar = new String();
	var okchar = '0123456789';
	var data = campo.value;
	if (data != '' && data != '0') {
		for (x=0;x < data.length;x ++) {
			achar = data.substr(x,1);
			if (okchar.indexOf(achar) == -1) {
				campo.select();
				return false;
			}
		}
	} else {
		campo.value = '0';
	}
	return true
}

function isDecimale(campo) {
	// controllo se il valore del text object passato come argomento � un decimale valido
	var numero = new Array();
	var tmp = new String();
	var achar = new String();
	var okchar = '0123456789.';
	var data = campo.value;
	//alert(campo.name + " " + data);
	if (data != '' && data != '0') {
		if (data.indexOf(',') >= 0) {
			// se il separatore di decimali � , lo sostituisco con .
			data = data.replace(',','.');
		}
		// separo l'unit� dai decimali
		numero = data.split('.');
		if (numero.length > 2) {
			campo.value = "";
			return false;
		} else {
			if (numero[0] == '') numero[0] = '0';
			if ((!numero[1]) || numero[1] == '') numero[1] = '0';
			tmp = numero[0];
			for (x = 0;x < tmp.length;x ++) {
				achar = tmp.substr(x,1);
				if (okchar.indexOf(achar) == -1) {
					campo.value = "";
					return false;
				}
			}
			tmp = numero[1];
			for (x = 0;x < tmp.length;x ++) {
				achar = tmp.substr(x,1);
				if (okchar.indexOf(achar) == -1) {
					campo.value = "";
					return false;
				}
			} 
			data = numero[0] + "." + numero[1];
			campo.value = data;
		}
	} else {
		campo.value = '0';
	}
	return true;
}

function isEmail(campo) {
	// controllo se il valore del text object passato come argomento � un'email valida
	var achar = new String();
	var data = campo.value;
	
	if (data != '') {
		if (data.length < 7) {
			campo.select();
			return false;
		} else {
			if (data.indexOf('@') == -1) {
				campo.select();
				return false;
			}
			else if (data.indexOf('.') == -1) {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

function isTelefono(campo) {
	// controllo se il valore del text object passato come argomento � un numero di telefono valido
	var achar = new String();
	var okchar = '0123456789';
	var data = campo.value;
	
	if (data != '') {
		if (data.length < 5 || data.length > 15) {
			campo.select();
			return false;
		} else {
			while (data.indexOf('-') >= 0) {
				data = data.replace('-','');
			}
			while (data.indexOf('.') >= 0) {
				data = data.replace('.','');
			}
			while (data.indexOf(' ') >= 0) {
				data = data.replace(' ','');
			}
			for (x = 0;x < data.length;x ++) {
				achar = data.substr(x,1);
				if (okchar.indexOf(achar) == -1) {
					campo.select();
					return false;
				}
			}
		}
	}
	return true;
}

function isURL(campo) {
	// controllo se il valore del text object passato come argomento � una URL valida
	// Il conotrollo � effettuato sulla base della lunghezza della stringa inserita, sulla presenza
	// di caratteri non validi e sulla presenza del punto
	var achar = new String();
	var kochar = '*|!()=?^[]{}"\'';
	var data = campo.value;
	
	if (data != '') {
		if (data.length < 5 || data.length > 255) {
			campo.select();
			return false;
		}
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			if (kochar.indexOf(data) >= 0) {
				campo.select();
				return false;
			}
			if (data.indexOf('.') == -1) {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

function isCodFisc(campo) {
	// controllo se il valore del text object passato come argomento � un codice fiscale valido (solo controllo formale)
	var achar = new String;
	var data = campo.value;
	
	if (data != '') {
		// primo controllo la lunghezza non � 16
		if (data.length != 16) {
			campo.select();
			return false;
		}
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			if ((x >= 0 && x <= 5) || x == 8 || x == 11 || x == 15) {
				// deve essere un carattere
				achar = achar.toUpperCase();
				if (achar < 'A' || achar > 'Z') {
					campo.select();
					return false;
				}
			}
			else if (x == 6 || x == 7 || x == 9 || x == 10 || (x >= 12 && x <= 14)) {
				// deve essere un numero
				if (!(achar >= '0' && achar <= '9')) {
					campo.select();
					return false;
				}
			}
		}
	}
	return true;
}

function isParIva(campo) {
	var pi = campo.value;
	if( pi == '' )  return false;
	if( pi.length != 11 )
		return false;
	validi = "0123456789";
	for(var p = 0; p < 11; p++ ){
		if( validi.indexOf( pi.charAt(p) ) == -1 )
			return false;
	}
	s = 0;
	for(var p = 0; p <= 9; p += 2 )
		s += pi.charCodeAt(p) - '0'.charCodeAt(0);
	for(var p = 1; p <= 9; p += 2 ){
		c = 2*( pi.charCodeAt(p) - '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;
}
function isParIvaCodFisc(campo) {
	// controllo se il valore del text object passato come argomento � un codice fiscale o una partita IVA valido
	// in base al primo carattere se � numerico lo considero partita iva altrimenti cod fiscale
	var achar = new String();
	var data = campo.value;
	
	if (data != '') {
		achar = data.substr(0,1);
		if (achar >= '0' && achar <= '9') {
			// controllo partita iva
			return isParIva(campo);
		} else {
			// controllo codice fiscale
			return isCodFisc(campo);
		}
	}
	return true;
}

function isPassword(campo) {
	// controllo se il valore del text object passato come argomento � una password valida
	var achar = new String();
	var data = campo.value;
	
	if (data != '') {
		// la password deve essere tra i 5 e i 32 caratteri
		if (data.length < 5 || data.length > 32) {
			campo.select();
			return false;
		}
		// la password pu� contenere solo numeri o caratteri
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			achar = achar.toUpperCase();
			if (!((achar >= '0' && achar <= '9') || (achar >= 'A' && achar <= 'Z'))) {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

function isUsername(campo) {
	// controllo se il valore del text object passato come argomento � una password valida
	var achar = new String();
	var data = campo.value;
	
	if (data != '') {
		// la password deve essere tra i 4 e i 16 caratteri
		if (data.length < 4 || data.length > 16) {
			campo.select();
			return false;
		}
		// la password pu� contenere solo numeri o caratteri
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			achar = achar.toUpperCase();
			if (!((achar >= '0' && achar <= '9') || (achar >= 'A' && achar <= 'Z'))) {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

function isStringa(campo) {
	// controllo se il valore del text object passato come argomento � una stringa
	var achar = new String();
	var okchar = ".' -,����";
	var index;
	var data = campo.value;
	
	if (data != '') {
		if (data.length < 3) {
			campo.select();
			return false;
		}
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			index = okchar.indexOf(achar);
			achar = achar.toUpperCase();
			if (index == -1 && (achar < 'A' || achar > 'Z')) {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

function isSelect(campo) {
	var data = campo.value;
	if (data == '') {
		campo.value = '0';
	}
	return true;
}

function findElement(source_r,valore) {
	var result = 0;
	for (x = 0;x < source_r.length;x ++) {
		if (source_r[x] == valore) {
			return true;
		}
	}
	
	return false;
}

function isProvincia(campo) {
	var data = campo.value;
	var provincie = new Array('CZ','CS','KR','RC','VV','MT','PZ','AV','BN','CE','NA','SA','BA','BR','FG','LE','TA','CB','IS','CH','AQ','PE','TE','FR','LT','RI','RM','VI','PG','TR','AR','FI','GR','LI','LU','MS','PI','PT','PO','SI','AN','AP','MC','PS','BO','FE','FO','MO','PR','PC','RA','RE','RN','AG','CL','CT','EN','ME','PA','SR','RG','TP','CA','NU','OR','SS','BL','PD','RO','TV','VE','VR','VI','GO','PN','TS','UD','BZ','TN','BG','BS','CO','CR','LC','LO','MN','MI','PV','SO','VA','AL','AT','BI','CN','NO','TO','VB','VC','AO','GE','IM','SP','SV','FC');
	
	if (findElement(provincie,data.toUpperCase())) {
		return true;
	} else {
		return false;
	}
	
	
}

function isCAP(campo) {
	var achar = new String();
	var data = campo.value;
	
	if (data != '') {
		if (data.length != 5) {
			campo.select();
			return false;
		}
		for (x = 0;x < data.length;x ++) {
			achar = data.substr(x,1);
			if (achar < '0' || achar > '9') {
				campo.select();
				return false;
			}
		}
	}
	return true;
}

