// JavaScript Document
//
//   set you custom checks at the end of script
//   before *check if everything is ok in the form*
//
//
//////////////////// options /////////////////////
//
// items in the form ---------------- insert your data! ----------------
theFormContainer = 'pers_data';
erroLabelSuffix = '_error_label';
language_choice_div = 'lang_choice';
warn_missing_data = 'missing_data_div';
privacy_info_div = 'privacy_info';
consenso_privacy = 'consenso_privacy';
warn_privacy_approval ='warn_privacy_approval_div';
additional_check_trigger ='additional_check_trigger_div';
// class names ---------------- insert your data! ----------------
labelErrorStyle = 'error';
warnBoxStyle = 'warn';
// it error messages ---------------- insert your data! ----------------
itErrorRequired = 'Questo campo e\' necessario.'; // corrisponde alla class requiredField
itErrorPhone = 'Inserire un numero di telefono valido.'; // corrisponde alla class requiredNumber
itErrorMailFax = 'Inserire un numero di fax o un indirizzo e-mail.'; // corrisponde alla class requiredMailFax
itErrorMailPhone = 'Inserire un numero di telefono o un indirizzo e-mail.'; // corrisponde alla class requiredMailPhone
itErrorMail = 'E \' necessario un indirizzo e-mail valido.'; // corrisponde alla class requiredMail
itErrorPostCode = 'Inserire un C.A.P. valido.'; // corrisponde alla class requiredZip
itErrorDate = 'Inserite una data valida.'; // corrisponde alla class requiredDate
itErrorData = 'Mancano dei dati, oppure non sono corretti.';
itErrorPrivacy = 'In mancanza del consenso del trattamento dei dati non sara\' possibile soddisfare la vostra richiesta.';
// en error messages ---------------- insert your data! ----------------
enErrorRequired = 'This is a required field.'; // corrisponde alla class requiredField
enErrorPhone = 'Please, insert a valid phone number.'; // corrisponde alla class requiredNumber
enErrorMailFax = 'Please, insert a fax number or an e-mail address.'; // corrisponde alla class requiredMailFax
enErrorMailPhone = 'Please, insert a phone number or an e-mail address.'; // corrisponde alla class requiredMailPhone
enErrorMail = 'Please, insert a valid e-mail address.'; // corrisponde alla class requiredMail
enErrorPostCode = 'The zip code is required.'; // corrisponde alla class requiredZip
enErrorDate = 'Please, insert a valid date.'; // corrisponde alla class requiredDate
enErrorData = 'Some data are missing or wrong.';
enErrorPrivacy = 'If you do not agree the privacy policy it will be impossible to satisfy your request.';
// alert error messages ---------------- insert your data! ----------------
itAlertMex = 'Mancano alcuni dati, oppure non sono corretti.<br />Per favore controllate. Grazie.';
enAlertMex = 'Some data are missing or not valid.<br />Please check. Thanks.';
//
//
//
// regular expression to match required date format
re = /^\d{1,2}\/\d{1,2}\/\d{4}$/;
// language definition
function language_def(){
	if (document.getElementById(language_choice_div).className === 'it'){
		prefix_lang = 'it';
	} else if (document.getElementById(language_choice_div).className === 'en'){
		prefix_lang = 'en';
	}
}
//
function validate_jo(){
	language_def();
	global_errs = 0;
	var the_error_count = 0;
	var theFormContainerBox = document.getElementById(theFormContainer);
	var reqData = theFormContainerBox.getElementsByTagName('input');
	var additional_check = document.getElementById(additional_check_trigger); 
	// check required personal data
	for(i = 0; i < reqData.length; i++){
		var theItemName = (reqData[i].id);
		var theErrorLabel = (theItemName + erroLabelSuffix);
		var theErrorField = document.getElementById(theErrorLabel);
		if (theErrorField.value !== ''){
			while (theErrorField.firstChild){
				theErrorField.className = '';
				theErrorField.removeChild(theErrorField.firstChild);
				}
		}
		if(reqData[i].className === "requiredField" && reqData[i].value === ''){
			var the_error_label_mex = window[prefix_lang+'ErrorRequired'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredNumber" && (! parseInt(reqData[i].value) > 0)){
			var the_error_label_mex = window[prefix_lang+'ErrorPhone'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredMailFax" && reqData[i].value === ''){
			var the_error_label_mex = window[prefix_lang+'ErrorMailFax'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredMailFax" && reqData[i].value !== ''){
			if (! (parseInt(reqData[i].value) > 0)){
				if ((reqData[i].value.indexOf("@") === -1) || (reqData[i].value.indexOf(".") === -1)){
					var the_error_label_mex = window[prefix_lang+'ErrorMailFax'];
					theErrorField.className = labelErrorStyle;
					theErrorField.appendChild(document.createTextNode(the_error_label_mex));
					the_error_count +=1;
				}
			}
		}
		if(reqData[i].className === "requiredMailPhone" && reqData[i].value === ''){
			var the_error_label_mex = window[prefix_lang+'ErrorMailPhone'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredMailPhone" && reqData[i].value !== ''){
			if (! (parseInt(reqData[i].value) > 0)){
				if ((reqData[i].value.indexOf("@") === -1) || (reqData[i].value.indexOf(".") === -1)){
					var the_error_label_mex = window[prefix_lang+'ErrorMailPhone'];
					theErrorField.className = labelErrorStyle;
					theErrorField.appendChild(document.createTextNode(the_error_label_mex));
					the_error_count +=1;
				}
			}
		}
		if(reqData[i].className === "requiredMail" && ((reqData[i].value.indexOf("@") === -1) || (reqData[i].value.indexOf(".") === -1))){
			var the_error_label_mex = window[prefix_lang+'ErrorMail'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredZip" && (! parseInt(reqData[i].value) > 0)){
			var the_error_label_mex = window[prefix_lang+'ErrorPostCode'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
		if(reqData[i].className === "requiredDate" && !reqData[i].value.match(re)){
			var the_error_label_mex = window[prefix_lang+'ErrorDate'];
			theErrorField.className = labelErrorStyle;
			theErrorField.appendChild(document.createTextNode(the_error_label_mex));
			the_error_count +=1;
		}
	}
	// cleaning section error boxes
	var theDataWarnBox = document.getElementById(warn_missing_data);
	if (theDataWarnBox.value !== ''){
		while (theDataWarnBox.firstChild){
			theDataWarnBox.className = '';
			theDataWarnBox.removeChild(theDataWarnBox.firstChild);
		}
	}
	/* --> disab -- messaggio errore box privacy
	var consensoCheckBox = document.getElementById(consenso_privacy);
	var thePrivacyWarnBox = document.getElementById(warn_privacy_approval);
	var privacyInfoBox = document.getElementById(privacy_info_div);
	if (thePrivacyWarnBox.value !== ''){
		while (thePrivacyWarnBox.firstChild){
			thePrivacyWarnBox.className = '';
			thePrivacyWarnBox.removeChild(thePrivacyWarnBox.firstChild);
		}
	}
	*/
	// check personal data errors
	if (the_error_count > 0){
		var personal_data_error_mex = window[prefix_lang+'ErrorData'];
		theDataWarnBox.style.display = 'block';
		theDataWarnBox.className = warnBoxStyle;
		theDataWarnBox.appendChild(document.createTextNode(personal_data_error_mex));
		// expand container of personal data if collapsed
		if (theFormContainerBox.style.display == 'none'){
			theFormContainerBox.style.display = '';
		}
		global_errs +=1;
	}
	/* --> disab
	// check if privacy checkbox is checked
	if (consensoCheckBox.checked === false) {
		var personal_data_error_mex = window[prefix_lang+'ErrorPrivacy'];
		thePrivacyWarnBox.style.display = 'block';
		thePrivacyWarnBox.className = warnBoxStyle;
		thePrivacyWarnBox.appendChild(document.createTextNode(personal_data_error_mex));
		// expand container of personal data if collapsed
		if (privacyInfoBox.style.display == 'none'){
			privacyInfoBox.style.display = '';
		}
		global_errs +=1;
	}
	*/
	//////////////////////////////////// custom checks ////////////////////////////////////
	if (additional_check.className === 'yes'){
	//
	additional_check_routine();
	//
	}
	//////////////////////////////////// end custom checks ////////////////////////////////////
	// check if everything is ok in the form
	if (global_errs > 0){
		var the_error_alert_mex = window[prefix_lang+'AlertMex'];
		var alertDiv = document.getElementById('alert_div');
		alertDiv.innerHTML = the_error_alert_mex;
		//alertDiv.appendChild(document.createTextNode('maaaaaaaaaaaaaaaaaa'));
		$('#alert_div').modal(); // jQuery object
		return false;
	}
	//finish
	return true;
}
