count=0;
function checkForm(frm) {
	var minutes = 1000*60;
	var hours = minutes*60;
	var days = hours*24;

	if(frm.localita){ // radio version
		for(var i=0; i<frm.localita.length;i++){
			if(frm.localita[i].checked==true){
				if(frm.localita[i].value != "freeSearch"){
					frm.place_id.value=frm.localita[i].value;
					frm.search_txt.value=frm.place_id.value;
				}
				else
					frm.place_id.value=0;
			}
		}
		
		if(frm.id_hotels && frm.l.value!=0){ // call-center
				v=frm.idcity.value;
				frm.l.value=v.substring(0,v.indexOf("|"));
				frm.id_hotels.value=v.substring(v.indexOf("|")+1);
			}
	}
	
	/*
	if(!frm.destination && frm.selectedCity){	// regioni povere
		frm.idcity.value=frm.selectedCity.value;
	}
	*/

	if(frm.search_txt){
		if(frm.search_txt.value.length<3) { //no city
			if(!count){ 
				showError(1,txtBadCity,txtBadTitle,frm.name);
				return false
			}	
		}
	}

	frm.checkin.value = i = frm.meseannoarr.value+pad(frm.giornoarr.value);
	frm.checkout.value = o = frm.meseannopar.value+pad(frm.giornopar.value);

	var checkin = new Date(i.substring(0,4),i.substring(4,6)-1,i.substring(6));
	var checkout = new Date(o.substring(0,4),o.substring(4,6)-1,o.substring(6));
	
	//Controllo che le date siano valide
	if(checkin.getDate()!=frm.giornoarr.value){
		if(!count){ 
			showError(0,txtFalseCheckin,txtBadTitle,frm.name);
			return false
		}			
	}
	if(checkout.getDate()!=frm.giornopar.value){
		if(!count){ 
			showError(0,txtFalseCheckout,txtBadTitle,frm.name);
			return false
		}	
	}

	checkin=checkin.getTime();
	checkout=checkout.getTime();
	
	officeoffset = -120;
	if(officeoffset < 0)	officeoffset = Math.abs(officeoffset) + 720;
	else	officeoffset = 720 - officeoffset;
	
	visitortime = new Date();
	visitoroffset = visitortime.getTimezoneOffset();
	if(visitoroffset < 0)	visitoroffset = Math.abs(visitoroffset) + 720;
	else	visitoroffset = 720 - visitoroffset;
	
	diff = officeoffset - visitoroffset;
	officetime = new Date();
	officetime.setTime(Number(visitortime) + (diff * 60000));

	now = new Date();
	now.setHours(0);
	now.setMinutes(0);
	now.setSeconds(0);
	now.setMilliseconds(0);
	
	//Checkin maggiore o uguale a oggi
	if(checkin<now.getTime()) {
		if(!count){ 
			showError(0,txtBadCheckIn,txtBadTitle,frm.name);
			return false
		}	
	}
	
	if(frm.leadtime){
		if(frm.leadtime.value>(checkin-now.getTime())/days){
				txtError = txtLeadTime1+" "+frm.leadtime.value+" "+txtLeadTime2;
				showError(0,txtError,txtBadTitle,frm.name);
				return false;
		}
	}
	
	//Checkin dopo le 18 di oggi
	if(checkin==now.getTime()) {
		if(officetime.getHours()>=17){
			if(!count){ 
				showError(0,txtSameDayError,txtBadTitle,frm.name);
				return false
			}
		}
	}	
	
	//Checkin anteriore a checkout
	if(checkin>checkout) {
		if(!count){ 
			showError(0,txtBadCheckOut,txtBadTitle,frm.name);
			return false
		}	
	}
	
	//Checkin diverso da checkout
	if(checkin==checkout) {
		if(!count){ 
			showError(0,txtBadCheckInOut,txtBadTitle,frm.name);
			return false
		}	
	}
	//Intervallo max per le query - 14?
	if(Math.abs(checkin-checkout)/oneDay >= 30) {
		if(!count){ 
			showError(0,txtBadInterval,txtBadTitle,frm.name);
			return false
		}	
	}
	
	if(frm.place_txt){ // form del listing
		if(frm.place_txt.value==frm.search_txt.value && frm.place_id.value!=0)
			frm.search_txt.value="";
		else
			frm.place_id.value=0;
	}
	count=1;
}
function sendForm(frm){
	if(checkForm(frm)==false){
		return false
	}else{
		frm.submit();
	}
}
function showError(error_type,error_txt,error_title,name_form){
		removeErrors("dateErrorMsg","cityErrorMsg","opErrorMsg");
		switch(error_type){
			case 0:
			error_id = "dateErrorMsg";
			break;
			case 1:
			error_id = "cityErrorMsg";
			break;
			case 2:
			error_id = "opErrorMsg";
			break;
		}
		var el,p;
		spa=document.createElement('span');
		spa.appendChild(document.createTextNode(error_txt))
		el=document.getElementById(error_id);
		el.appendChild(spa)
		el.className = "error_on";	
		el.style.backgroundImage="url('http://www.traveleurope.it/img/alert.gif')";
		el.style.backgroundRepeat="no-repeat";
		el.style.backgroundPosition="2px 1px";
		el.style.paddingLeft="16px";
}
function removeErrors(){
	for(i=0;i<arguments.length;i++){
		el=document.getElementById(arguments[i]);
		if(el!=null && el.hasChildNodes()){
			el.className="error_off";
			len=el.childNodes.length;
			for(j=0;j<len;j++){
				el.removeChild(el.lastChild);
			}
		}
	}
}
/*
function focusOnSelectCity(wich){
	for(var i=0; i<wich.form.destination.length;i++){
	if(wich.form.destination[i].value == "selectCity")
	wich.form.destination[i].checked=true;
	}
}
*/
function focusOnFreeSearch(wich){
	for(var i=0; i<wich.form.localita.length;i++){
		if(wich.form.localita[i].value == "freeSearch")
			wich.form.localita[i].checked=true;
	}
}

function fillDate(g_sel,my_sel,d,my) {
	out_g=document.getElementById(g_sel);
	out_my=document.getElementById(my_sel);

	date=new Date(my.substr(0,4),my.substr(4,2)-1,d);
	my = date.getFullYear()+""+pad((date.getMonth()+1));
	d = date.getDate();
	setSelIndexByValue(out_g,d);
	setSelIndexByValue(out_my,my);
}
function setSelIndexByValue(sel,val){
	for(i=0;i<sel.options.length;i++){
		if(sel.options[i].value==val) sel.options[i].selected="selected";
	}
}
function initDatePicker(){
	in_g=document.getElementById("giornoarr");
	in_my=document.getElementById("meseannoarr");
	
	if(in_g.className && in_g.className=="single"){
		return false;
	}
	else{
		in_g.onchange = in_my.onchange = function(){
			curyear=new Date().getFullYear();
			in_y=2;
			if(in_my.value.substr(0,4)==curyear) in_y=1;
			
			date=new Date(in_my.value.substr(0,4),in_my.value.substr(4,2)-1,in_g.value);
			my = date.getFullYear()+""+pad((date.getMonth()+1));
			d = date.getDate();

			setSelIndexByValue(in_g,d);
			setSelIndexByValue(in_my,my);
			fillDate("giornopar","meseannopar",d+1,my);
			
	}
	}
}
addLoadEvent(initDatePicker);