// JavaScript Document

var ws_validate_form_mare = {
		rules : [	{	id: '#porti_andata select:eq(0)', 
								msgs:["Selezionare un porto di partenza per l'andata!",
											'Select a port of departure for the outward!'],
													r:	function(id){
																return $(id).val().length > 0;
															}
							},
							{	id: '#ITI_AND', 
								msgs:["Selezionare un porto di arrivo per l'andata!",
											'Select a port of arrival for the first leg!'],
								r:	function(id){
											return $(id).val().length > 0;
										}
							},							
							{	id: '#cal_DATA_ANDATA', 
								msgs:["Scegliere la data per il viaggio di andata!",
											'Choose depart date!'],
								r:	function(id){
											return /^[0123]?\d-[01]?\d-\d{4,}$/.test($('#DATA_ANDATA').val());
										}
							},							
							{	id: '#porti_ritorno select:eq(0)', 
								msgs:["Selezionare un porto di partenza per il ritorno!",
											'Select a port of departure for the return!'],
								r:	function(id){
											if ($('#ritEn')[0].checked){
												return $(id).val().length > 0;	
											}else{
												return true;
											}
										}
							},
							{	id: '#ITI_RIT', 
								msgs:["Selezionare un porto di partenza per il ritorno!",
											'Select a port of arrival for the return!'],
								r:	function(id){
											if ($('#ritEn')[0].checked){
												return $(id).val().length > 0;	
											}else{
												return true;
											}
										}
							},
							{	id: '#cal_DATA_RITORNO', 
								msgs:["Scegliere la data per il viaggio di ritorno! La Data di ritorno deve essere succcesiva alla data di partenza.",
											'Choose return date after departure date!'],
								r:	function(id){
											var da, dr, a, r, res;
											if ($('#ritEn')[0].checked){
												res = /^[0123]?\d-[01]?\d-\d{4,}$/.test($('#DATA_RITORNO').val());
												if (res){
													a = $('#DATA_ANDATA').val().split('-');
													r = $('#DATA_RITORNO').val().split('-');
													da = Date( parseInt(a[2],10), parseInt(a[1],10)-1, parseInt(a[0],10));	
													dr = Date(parseInt(r[2],10), parseInt(r[1],10)-1, parseInt(r[0],10));	
													res = dr.getTime() > da.getTime();
												}
												return res;
											}else{
												return true;
											}
										}
							}							
						],
		validate: function(){
					var error_message='', res = true;
					var language = 0;
					if($('body.lang_en').size()>0){language = 1;}
					var rules = ws_validate_form_mare.rules;
					for (var i in rules){
						res2 = rules[i].r(rules[i].id);
						
						if (res2==false){
							error_message+=rules[i].msgs[language]+'\n';
							res2 = true;
							res = false;
						}
					}
					if (res == false) alert(error_message);
					return res;
				}

	}

$(document).ready(function(){
	$('form#sceltaviaggio').submit(ws_validate_form_mare.validate);
	if(location.search=='?debug'){
		
	}
});
