/*										      ______________
______________________________________________CONTROLLI FORM________________________________________________
VINCOLI PER L'UTILIZZO DELLO SCRIPT
------------------------------------------------------------------------------------------------------------
E' necessario nominare i campi utilizzando il seguente schema:
*tipocontrollo-nomedelcampo (se obbligatorio) oppure tipocontrollo-nomecampo (se il campo non ט obbligatorio)
NOTA: I campi -obbligatori- vanno preceduti dal prefisso "*" es: *nomecampo
per "tipocontrollo" fare riferimento alla seguente tabella:

Caratteri [a-z]	 	-->		az-    		--> Solo caratteri 
Numerico [0-9]	 	-->		09-    		--> Solo numeri 
Caratteri e numeri 	--> 	az09-		--> Solo caratteri semplici e numeri
Decimale 			--> 	dec-		--> Numero decimale
Intero 				-->		int-		--> Numero intero
Testo standard	 	-->		txt-		--> Solo caratteri standard (a-z,0-9,[:space:],אטילעש,',",&,+,-,?,=,(,),*,/,@,#,:,;,.,,)

Nome (e/o) Cognome 	-->		name-	 	--> Deve contenere solo caratteri standard (a-z,אטילעש,',[:space:])
Password		 	-->		pwd#-	 	--> Per la lunghezza minima utilizzare il carattere # (0 per non impostarla)
E-mail			 	-->		email-
URL			 		-->		url-
Cap				 	-->		cap-		--> Solo 5 numeri
Codice Fiscale	 	-->		cf-			--> alfa-numerico maiuscolo min 11 e max 16 (se presenti caratteri, allora 16 - altrimenti 11)
Partita Iva		 	-->		piva-		--> 11 numeri 
Numero tel.		 	-->		tel-		--> Il campo deve iniziare per + o 0 e contenere solo numeri
Data			 	-->		date-		--> Il campo deve avere questo tipo di formattazione "gg/mm/aaaa"
Data			 	-->		dateIT-		--> Il campo deve avere questo tipo di formattazione "gg/mm/aaaa"
Data			 	-->		dateEN-		--> Il campo deve avere questo tipo di formattazione "mm/gg/aaaa"
Credit Card			--> 	ccard-		--> esattamente 20 numeri 		
Indirizzo IP		--> 	IP-			--> xxx.xxx.xxx.xxx
 
Consenso Privacy 	-->		privacy 	--> LEGGERE LE SPECIFICHE PER L'ELEMENTO PRIVACY!!!

ELEMENTO PRIVACY
Per il consenso ai dati personali ט necessario utilizzare una checkbox nominata 
			ESCLUSIVAMENTE -->privacy<-- senza essere preceduto nט seguito da -
____________________________________________________________________________________________________________
*/

var IT = new Array();
IT[0]="ATTENZIONE!\nE' necessario compilare tutti i campi obbligatori!";
IT[1]="ATTENZIONE!\nPer poter proseguire occorre dare il consenso al trattamento dei dati personali!";
IT[2]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo caratteri semplici [a-z] oppure spazi";
IT[3]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo numeri interi positivi [0-9]";
IT[4]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo caratteri semplici [a-z] oppure numeri [0-9]";
IT[5]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo numeri decimali positivi o negativi";
IT[6]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo numeri interi positivi o negativi";
IT[7]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo caratteri standard e numeri:\n([a-z][0-9] אטילעש ' & + - ? ! = ( ) * / @ : ; . , _)";
IT[8]="ATTENZIONE!\nNel campo evidenziato si possono digitare solo caratteri semplici e eventuali spazi\n([a-z]אטילעש ')\nSe il tuo nome contiene caratteri diversi contatta il webmaster. Grazie!";
IT[9]="ATTENZIONE!\nLa password deve essere una combinazione da 6 a 10 caratteri scelti tra quelli consentiti:\n([a-z][A-Z][0-9] _ @ - .)";
IT[10]="ATTENZIONE!\nL'indirizzo e-mail che hai digitato non ט corretto!\nControlla e se invece ט giusto contatta il webmaster.";
IT[11]="ATTENZIONE!\nIl CAP indicato contiene errori. Verificare...";
IT[12]="ATTENZIONE!\nIl codice fiscale digitato non ט corretto!\nRicorda che il codice fiscale va digitato con caratteri maiuscoli!";
IT[13]="ATTENZIONE!\nIl numero di partita iva indicato non risulta corretto!\nVerificalo attentamente e in caso contrario contatta il webmaster.";
IT[14]="ATTENZIONE!\nIl numero di telefono indicato contiene errori: il numero puע contenere solo numeri (e +).\nNon utilizzare spazi bianchi o qualsiasi altro carattere";
IT[15]="ATTENZIONE!\nLa data introdotta non ט corretta! Il formato data accettato ט:\ngg/mm/yyyy - es: 15/11/2003";
IT[16]="ATTENZIONE!\nIl campo valuta ט espresso in EURO! Il formato dell'euro ט xxxx,cc\nesempio: 1500,00 euro"; //euro
IT[17]="ATTENZIONE!\nNumero di carta di credito non corretto!"; //credit card
IT[18]="ATTENZIONE!\nErrore nella digitazione dell'indirizzo IP. Il formato corretto ט:\nxxx.xxx.xxx.xxx"; //IP address
// NOME MODELLO BARCA
IT[19]="ATTENZIONE!\nDigitare il nome del modello utilizzando solamente i seguenti caratteri ammessi:\n[a-z] [A-Z] [0-9] אטילעש ' & - ( ) . ,";
IT[20]="ATTENZIONE!\nPer indicare il periodo di produzione indicare solamente gli anni:\n(inizio-fine) produzione separati con il carattere - (meno)\nsemza alcuno spazio bianco! es. 1995-2003";
IT[21]="ATTENZIONE!\nErrore nella digitazione dell'indirizzo URL. es: http://www.tuodominio.com"; //URL
IT[22]="ATTENZIONE!\nErrore nella digitazione dello ShipCode: il formato giusto ט S#####"; //ShipCode

var EN = new Array();
EN[0]="PAY ATTENTION!\nYou must type in all required fields!";
EN[1]="PAY ATTENTION!\nIn order to proceed you must accept our privacy laws!";
EN[2]="PAY ATTENTION!\nThe evidenced field requires only standard chars [a-z] and white space";
EN[3]="PAY ATTENTION!\nThe evidenced field requires only integer numbers unsigned [0-9]";
EN[4]="PAY ATTENTION!\nThe evidenced field requires only standard chars [a-z] or integer numbers unsigned [0-9]";
EN[5]="PAY ATTENTION!\nThe evidenced field requires only (positive or negative) decimal numbers";
EN[6]="PAY ATTENTION!\nThe evidenced field requires only (positive or negative) integer numbers";
EN[7]="PAY ATTENTION!\nThe evidenced field requires only standard chars or numbers:\n([a-z][0-9] אטילעש ' & + - ? ! = ( ) * / @ : ; . , _)";
EN[8]="PAY ATTENTION!\nThe evidenced field requires only standard chars and white spaces:\n([a-z]אטילעש ')\nIf your name has different characters please contact our webmaster. Thank you!";
EN[9]="PAY ATTENTION!\nYour password must be of 6 to 10 characters long and you can choose only characters like these:\n([a-z][A-Z][0-9] _ @ - .)";
EN[10]="PAY ATTENTION!\nThe e-mail address you typed maight be wrong!\nPlease verify it and in case it is correct, please inform our webmaster!";
EN[11]="PAY ATTENTION!\nThe ZIP code you typed seems to be wrong! Please verify it!";
EN[12]="ERROR: your fiscal code cannot be processed!\nVerify it and in case it is correct, please inform our webmaster.";
EN[13]="ERROR: your company registration code cannot be processed!\nPlease verify it and in case it is correct, please inform our webmaster.";
EN[14]="PAY ATTENTION!\nThe phone number you typed seems to be wrong. Please type only digits without white spaces and any other character!";
EN[15]="PAY ATTENTION!\nThe date you typed contains some error! The only accepted format for dates is:\nmm/dd/yyyy - es: 11/15/2003";
EN[16]="PAY ATTENTION!\nOur money value is expressed in EUROS! The euro format is xxxx,cc\nexample: 1500,00 euros";
EN[17]="ERROR: your credit card number is invalid!";
EN[18]="ERROR: please verify the IP address you typed! The valid format is:\nxxx.xxx.xxx.xxx";
// NOME MODELLO BARCA
EN[19]="PAY ATTENTION!\nType the ship model name correctly, by using only these characters:\n[a-z] [A-Z] [0-9] אטילעש ' & - ( ) . ,";
EN[20]="PAY ATTENTION!\nType the production period by using these roles:\nfour numbers for years (start-end) production separated with a - (minus)\nwithout any space! ex. 1995-2003";
EN[21]="PAY ATTENTION!\nSome error found in your URL address. ex.: http://www.yourdomain.com"; //URL
EN[22]="PAY ATTENTION!\nSome error found in your ShipCode: standard format - S#####"; //ShipCode

// MOSTRA ERRORI
function showAlert(index) {
	switch (lang) {
		case "IT": msgErrore=IT; break;
		case "EN": msgErrore=EN; break;
		default: msgErrore=EN; break;
	}
	alert(msgErrore[index]);
}

//FUNZIONE CONTROLLO FORM
function parseForm(modulo) {
	if (checkRequired(modulo)) return checkRoules(modulo);
	else return false;
}

function checkRequired(modulo) {
	// CONTROLLO CAMPI OBBLIGATORI
	var n=0;
	flgPrivacy=true;
	for (i=0;i<modulo.length;i++) {
		currFieldName=modulo.elements[i].name;
		currField = new getObj(currFieldName);
		if (currField.type) {
			currField.style.background = "none";
			if (currFieldName.indexOf('*')==0) { // CAMPO OBBLIGATORIO
				if (currField.value.length==0 || currField.value==0) {
					currField.style.background="#FFDDDD";
					modulo.elements[i].focus();
					n++;
				}
			} 
			if (currFieldName=="privacy") { // FLG PRIVACY
				if (modulo.privacy.checked==false){
					currField.style.background="#FFDDDD";
					flgPrivacy=false;
				}
			}
		} //else alert(currField.value);
	}
	if (n>0) {
		showAlert(0);  // CAMPI OBBLIGATORI MANCANTI
		return false;
	} else if (!flgPrivacy) {
		showAlert(1);  // FLAG PRIVACY NON SELEZIONATO
		return false;
	} else return true;
}

function checkRoules(modulo) {
	// CONTROLLO REGOLE DI INPUT
	for (i=0;i<modulo.length;i++) {
		currFieldName=modulo.elements[i].name;
		currField = new getObj(currFieldName);
		roule=currFieldName.replace('*','').split("-");
		if (roule.length>1) currFieldRoule=roule[0];
		else currFieldRoule=false;
		if (currFieldRoule && currField.value) {
			switch (currFieldRoule) {
			
// #######################################
// REGOLE GENERALI PER TUTTI I MODULI
// #######################################
				case 'az': // caratteri semplici [a-z] e spazi
					regExp=/^([a-zA-Z ]+)$/;
					error=2;
				break;
				case '09': // numerico [0-9] non negativo
					regExp=/(^\d\d*$)/;
					error=3;
				break;
				case 'az09': // char-num e spazi
					regExp=/^([a-zA-Z0-9_ ]+)$/;
					error=4;
				break;
				case 'dec': // decimale -[0-9\.\,] anche negativo
					regExp=/(^-?\d\d*\.\d*$)|(^-?\d\d*\,\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
					error=5;
				break;
				case 'int': // numero intero anche negativo
					regExp=/(^-?\d\d*$)/;
					error=6;
				break;
				case 'txt': // caratteri standard ,אטילעש,',",&,+,-,?,=,(,),*,/,@,#,:,;,.,,
					regExp=/^([a-zA-Z0-9 אטילעש\'\"\&\+\-\?\!\=\(\)\*\/\@\:\;\.\,\_\r\n]+)$/;
					error=7;
				break;
				case 'name': // Nome e Cognome: solo caratteri standard e spazi
					regExp=/^([a-zA-Z אטילעש\']+)$/;
					error=8;
				break;
				case 'pwd': // Password: solo caratteri standard e underscore @ - .
					regExp=/^([a-zA-Z0-9_@\-\.]{6,10})$/;
					error=9;
				break;
				case 'email': // email xxx@yyy.zzz
					regExp=/^([a-zA-Z0-9_\.\-]{2,})+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z]{2,4})$/;
					error=10;
				break;
				case 'url': // url http://www.yourdomain.com
					//regExp=(^|[ \t\r\n])((http|https): (([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]));
					//regExp=(^|[ \t\r\n])((ftp|http|https|gopher|mailto|news|nntp|telnet|wais|file|prospero|aim|webcal): (([A-Za-z0-9$_.+!*(),;/?:@&~=-])|%[A-Fa-f0-9]{2}){2,}(#([a-zA-Z0-9][a-zA-Z0-9$_.+!*(),;/?:@&~=%-]*))?([A-Za-z0-9$_+!*();/?:~-]));
					error=21;
				break;
				case 'cap': //US ZIP CODE:  99999 or 99999-9999
					regExp=/(^\d{5}$)|(^\d{5}-\d{4}$)/;
					error=11;
				break;
				case 'cf': // Codice Fiscale: 11 numeri oppure 16 caratteri maiuscoli
					regExp=/(^\d{11}$)|(^[A-Z]{6}\d{2}[A-Z]{1}\d{2}[A-Z]{1}\d{3}[A-Z]{1}$)/;
					error=12;
				break;
				case 'piva': // Partita iva 11 digits
					regExp=/^\d{11}$/;
					error=13;
				break;
				case 'tel': //Telefono con o senza prefisso internaz.
					regExp=new RegExp("^([+]?)([0-9]{5,15})$");
					error=14;
				break;
				case 'date':
					regExp=/^\d{1,2}(\-|\/)\d{1,2}\1\d{4}$/;
					error=15;
				break;
				case 'dateIT':
					regExp=/^\d{1,2}(\-|\/)\d{1,2}\1\d{4}$/;
					error=15;
				break;
				case 'dateEN':
					regExp=/^\d{1,2}(\-|\/)\d{1,2}\1\d{4}$/;
					error=15;
				break;
				case 'euro': // VALUTA EURO con virgola e due decimali
					regExp=/^(\d\d*)\,(\d){2}$/;
					error=16;
				break;
				case 'ccard': // CREDIT CARD : 20 numeri
					regExp=/^[0-9]{20}$/;
					error=17;
				break;
				case 'IP': // IP ADDRESS 
					regExp=/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
					error=18;
				break;
// #######################################################
// REGOLE AGGIUTE PER MODULO SPECIFICO (scheda barca)
//########################################################
				case 'sname': // Nome Modello - come txt ma depotenziato
					regExp=/^([a-zA-Z0-9 אטילעש\'\&\-\(\)\.\,]+)$/;
					error=19;
				break;
				case 'years': // 1997-2005
					regExp=/^([0-9]{4}(\-[0-9]{4})?)$/;
					error=20;
				break;
				case 'mt':  // decimale non negativo max 5 digit
					regExp=/(^(\d\d*\.\d\d){1,5}$)|(^(\d\d*\,\d\d){1,5}$)|(^(\d\d*){1,5}$)/; 
					error=5;
				break;
				case 'mq': // decimale non negativo max 5 digit
					regExp=/(^(\d\d*\.\d\d){1,5}$)|(^(\d\d*\,\d\d){1,5}$)|(^(\d\d*){1,5}$)/; 
					error=5;
				break;
				case 'lt': // intero max 5 digit
					regExp=/(^(\d\d*){1,5}$)/;
					error=6;
				break;
				case 'kg': // intero max 5 digit
					regExp=/(^(\d\d*){1,5}$)/;
					error=6;
				break;
				case 'ft': // decimale non negativo max 5 digit
					regExp=/(^(\d\d*\.\d\d){1,5}$)|(^(\d\d*\,\d\d){1,5}$)|(^(\d\d*){1,5}$)/; 
					error=5;
				break;
				case 'sqft': // intero max 5 digit
					regExp=/(^(\d\d*){1,5}$)/;
					error=6;
				break;
				case 'gls': // intero max 5 digit
					regExp=/(^(\d\d*){1,5}$)/;
					error=6;
				break;
				case 'lbs': // intero max 5 digit
					regExp=/(^(\d\d*){1,5}$)/;
					error=6;
				break;
				case 'scode': // S#####
					regExp=/(^S(\d\d*){1,5}$)/;
					error=22;
				break;
// ########################
// FINE REGOLE SPECIFICHE
// ########################
				default: regExp=false;
			} 
			if (regExp && error) {
				if (!regExp.test(currField.value)) {
					currField.style.background="#FFDDDD";
					modulo.elements[i].focus();
					showAlert(error); 
					return false;
				}
			}
		}
	}
	return true;
}

function getObj(name) {
	
	if (document.getElementById)
	{
		if (document.getElementById(name)) {
			this.obj = document.getElementById(name);
			this.style = document.getElementById(name).style;
			this.type = document.getElementById(name).type;
			this.value = document.getElementById(name).value;
		} else return false;

	}
	else if (document.all)
	{
		if (document.all[name]) {
			this.obj = document.all[name];
			this.style = document.all[name].style;
			this.type = document.all[name].type;
			this.value = document.all[name].value;
		} else return false;
	}
	else if (document.layers)
	{
		if (document.layers[name]) {
			this.obj = document.layers[name];
			this.style = document.layers[name].style;
			this.type = document.layers[name].type;
			this.value = document.layers[name].value;
		} else return false;
	}
	else return false;
}

function totalSail(unitsVal) {
	var mainSail=parseFloat(document.getElementById(unitsVal+'-mainsailArea').value.replace(',','.'));
	var genoa=parseFloat(document.getElementById(unitsVal+'-genoaArea').value.replace(',','.'));
	if(isNaN(mainSail)) mainSail=0;
	//else mainSail=mainSail.toFixed(2);
	if(isNaN(genoa)) genoa=0;
	//else genoa=genoa.toFixed(2);
	var totArea=(mainSail+genoa).toFixed(2);
	document.getElementById(unitsVal+'-sailArea').value=totArea;
}