function onOpenFiles(){
  $('#modalbox iframe').hide;
  $('#modalbox iframe').attr('src','');
}

function OpenFiles(filename){
  $('#modalbox').modal({onOpen: onOpenFiles()});
  $('#modalbox iframe').attr('src',filename);
  $('#modalbox iframe').show;
}

function SwapCheckbox(idname){
	if (idname == null) { abort();  };
	if ($(idname).attr('checked')) {
		$(idname).attr('checked', '');
	}
	else {
		$(idname).attr('checked','true');
	}
}

function validatemessage(idmsg){
	v_msg = 'Errore! / Error!'
	if ($('html').attr('lang')=='it'){
	 	switch(idmsg) {
		   	case 'EMPTY'	: v_msg = 'Un campo del modulo è stato lasciato vuoto!';
							break;
			case 'NOMAIL'	: v_msg = 'Inserire un indirizzo email valido.';
							break;
			case 'CHECK'	: v_msg = 'E\' necessario accettare l\'informativa sulla privacy e le condizioni d\'uso del sito.';
							break;
			default: v_msg = 'Errore nella elaborazione del modulo.';
		}
	} else {
		switch(idmsg) {
			case 'EMPTY'	: v_msg = 'A field on the form has been left empty!';
							break;
			case 'NOMAIL'	: v_msg = 'Enter a valid email address.';
							break;
			case 'CHECK'	: v_msg = 'You must accept the privacy policy and terms of use of the site.';
							break;				
			default: v_msg = 'Error while processing the form.';
		}
	}
	alert(v_msg);
}

function showerror(idname,show){
	if (show){ 
			$(idname).css('border','solid 1px red');
			$(idname).focus(); 
			}
	 else {
	 		$(idname).css('border','solid 1px silver');
		}
	return show;
}

function validatetext(idname){
	if (idname==null) { return false; }
	  else {
	  	var v_idname = $(idname).val();
		var isbad = (v_idname==null || v_idname=='');
		if (isbad) { validatemessage('EMPTY'); }
		showerror(idname,isbad);
		return !isbad;			
	}
}

function validatemail(idname){
	var email_reg_exp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-]{2,})+\.)+([a-zA-Z0-9]{2,})+$/;
	if (idname == null) { return false;	}
		else {
			var v_idname = $(idname).val();
			var isbad = (!email_reg_exp.test(v_idname) || (v_idname == null) || (v_idname == ''));
			if (isbad) { validatemessage('NOMAIL'); }
			showerror(idname,isbad);
			return !isbad; 
		}
}

function validatecheckbox(idname){
	if (idname==null) { return false; }
	  else {
	  		var isbad = (!$(idname).attr('checked'));
			if (isbad) { validatemessage('CHECK'); }
			showerror(idname,isbad);
			return !isbad;
	}
}

function ValidateForm(){
	if (!validatetext('#realname')) { return false;
	} else if (!validatemail('#email')) { return false;
		} else if (!validatetext('#messaggio')) { return false; 
			} else if (!validatecheckbox('#privacycheck')) {return false; 
				} else if (!validatecheckbox('#condicheck')) {return false;
					} else {
							$('#mailform').attr('action','/cgi-bin/FormMail.pl');
							$('#mailform').submit();
							}
}

