function _0(num){
	return (num<10?"0":"") + num;
}

theFirstDate = new Date();
theSecondDate = new Date();
theSecondDate.setDate(theSecondDate.getDate()+1);

Search = {
	options:{
		from:"bform[checkin]",
		to:"bform[checkout]",
		trigger_from:"f_trigger_a",
		trigger_to:"f_trigger_b",
		override:false
	},
	calendarSetup:function(){
		var cal_from = Calendar.setup({
			inputField     :    Search.options.from,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			button         :    Search.options.trigger_from,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDays,
			onUpdate	   :	Search.upDate,
			electric       :    false
		})
		var cal_to = Calendar.setup({
			inputField     :    Search.options.to,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			button         :    Search.options.trigger_to, 
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDaysTheSecond,
			onUpdate       :    Search.upDateTheSecond,
			electric       :    false
		});
		var cal_from2 = Calendar.setup({
			inputField     :    Search.options.from,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDays,
			onUpdate	   :	Search.upDate,
			electric       :    false
		});
		var cal_to2 = Calendar.setup({
			inputField     :    Search.options.to,           //*
			ifFormat       :    "%d-%m-%Y",
			showsTime      :    false,
			weekNumbers	   :	false,
			step           :    1,
			firstDay       :    1,
			dateStatusFunc :	Search.noOldDaysTheSecond,
			onUpdate       :    Search.upDateTheSecond,
			electric       :    false
		})
	},
	init:function(){
		if(arguments.length == 1 && typeof(arguments[0]) == "object"){
			for(var ind in arguments[0]){
				Search.options[ind] = arguments[0][ind];
			}
		}
		if(Search.options.override){
			for(o in Overrides){
				Calendar.prototype[o] = Overrides[o];
			}
		}
		Search.calendarSetup();
	},
	noOldDays:function(date,y,m,d){
		var blockDate = new Date();
		blockDate.setDate(blockDate.getDate()-1);
		if(date<(blockDate)){
			return true;
		}else{
			return false;
		}
	},
	upDate:function(cal){
		theFirstDate = cal.date;
		if(theSecondDate.getTime()<=cal.date.getTime()){
			theSecondDate = cal.date;
			theSecondDate = new Date(theFirstDate.getFullYear(),theFirstDate.getMonth(),theFirstDate.getDate());
			theSecondDate.setDate(theSecondDate.getDate()+1);
			var theString = _0(theSecondDate.getDate())+"-"+_0(theSecondDate.getMonth()+1)+"-"+theSecondDate.getFullYear();
			document.getElementById(Search.options.to).value = theString;
		}
	},
	upDateTheSecond:function(cal){
		theSecondDate = cal.date;
	},
	noOldDaysTheSecond:function(date,y,m,d){
		var fgiorno = theFirstDate.getDate();
		var fmese = theFirstDate.getMonth();
		var fanno = theFirstDate.getFullYear();
	
		var secondBlockDate = new Date(fanno,fmese,fgiorno);
//		secondBlockDate.setDate(secondBlockDate.getDate()-1);
	
		if(date<secondBlockDate){
			
			return true;
		}else{
			return false;
		}
	}
}

var Overrides = {
	showAtElement:function (el, opts) {
	var self = this;
	var p = Calendar.getAbsolutePos(el);
	if (!opts || typeof opts != "string") {
		this.showAt(p.x, p.y + el.offsetHeight);
		return true;
	}
	function fixPosition(box) {
		if (box.x < 0)
			box.x = 0;
		if (box.y < 0)
			box.y = 0;
		var cp = document.createElement("div");
		var s = cp.style;
		s.position = "absolute";
		s.right = s.width = "0px";
		document.body.appendChild(cp);
		var br = Calendar.getAbsolutePos(cp);
		document.body.removeChild(cp);
		if (Calendar.is_ie) {
			br.x += document.body.scrollLeft;
		} else {
			br.x += window.scrollX;
		}
		var tmp = box.x + box.width - br.x;
		if (tmp > 0) box.x -= tmp;
	}
	this.element.style.display = "block";
	Calendar.continuation_for_the_fucking_khtml_browser = function() {
		var w = self.element.offsetWidth;
		var h = self.element.offsetHeight;
		self.element.style.display = "none";
		var valign = opts.substr(0, 1);
		var halign = "l";
		if (opts.length > 1) {
			halign = opts.substr(1, 1);
		}
		// vertical alignment
		switch (valign) {
			case "T": p.y -= h; break;
			case "B": p.y += el.offsetHeight; break;
			case "C": p.y += (el.offsetHeight - h) / 2; break;
			case "t": p.y += el.offsetHeight - h; break;
			case "b": break; // already there
		}
		// horizontal alignment
		switch (halign) {
			case "L": p.x -= w; break;
			case "R": p.x += el.offsetWidth; break;
			case "C": p.x += (el.offsetWidth - w) / 2; break;
			case "l": p.x += el.offsetWidth - w; break;
			case "r": break; // already there
		}
		p.width = w;
		p.height = h + 40;
		self.monthsCombo.style.display = "none";
		fixPosition(p);
		self.showAt(p.x, p.y);
	};
	if (Calendar.is_khtml)
		setTimeout("Calendar.continuation_for_the_fucking_khtml_browser()", 10);
	else
		Calendar.continuation_for_the_fucking_khtml_browser();
	}
}
var append = function(func,callback){
	return function(){
		try{
			func();
		}catch(e){};
		try{
			callback();
		}catch(e){};
	}
}

/////////////    FUNZIONI PER IL BOX BOOKING         /////////////////////////////////////////////////////////////

/*function setChildAge(nchild, chdid, roomn, label_children_age){
	//alert("ok " + nchild + " " + chdid + " " +roomn);
	var selectHtml = "";
	if(nchild > 0){
		selectHtml = '<td width="78"><div align="right" class="des_campi_home">'+ label_children_age +'&nbsp;</div></td>';
		for (var cc=1; cc<=nchild; cc++) {
			selectHtml =  selectHtml + '<td width="45"><select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']" class="campi_booking_home3">' +
                '<option value="0">&lsaquo; 1</option>';
				for(var k=1; k<=18; k++){
					selectHtml =  selectHtml + "\n" + '<option value="'+ k +'">'+ k +'</option>';
				}
				selectHtml =  selectHtml + '</select></td>';
		}
	}
	if(document.getElementById(chdid)){
		document.getElementById(chdid).innerHTML = selectHtml;
	}
}

function createSelect(num, init, label, selected){
	if(label!=""){
		label = " "+ label;
	}
	var mySel = "";
	for(var k=init; k<=num; k++){
		if (selected != 0 && selected == k){
			mySel = mySel + "\n" + '<option value="'+ k +'" selected="selected">'+ k + label +'</option>';
		} else {
			mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + label +'</option>';
		}
	}
	return mySel;
}

function setRoom(nrooms, label_adults, label_children, label_children_age, label_room){
	var myResults = "";
	for (var i=1; i<=nrooms; i++){
		myResults = myResults + '';
		if ( nrooms >= 1){
			myResults = myResults + '<table width="256" border="0" cellpadding="0" cellspacing="0" id="optionSearch_'+i+'">';
		}
		myResults = myResults +
		'		<tr>\n'+
		'		<td><div align="right" class="des_campi_home" >Camera '+i+':&nbsp;</div></td>\n'+
		'       </tr>\n'+
		'		<tr><td colspan="4"><img src="immagini/spacer.gif" width="1" height="5" /></td></tr>\n'+
		'		<tr><td width="75"><div align="right" class="des_campi_home" >'+ label_adults +'&nbsp;</div></td>\n'+
		'		<td width="58">\n'+
		'		<select name="bform[reqRooms]['+ i +'][adults]" id="bform[reqRooms]['+ i +'][adults]" class="campi_booking_home2">\n'+
		'			'+ createSelect(5, 1, "", 1) + 

		'		</select>\n'+
		'		</td>'+
		'		<td width="60">\n'+
		'		<div align="right" class="des_campi_home" >'+ label_children +'&nbsp;</div></td>\n'+
		'		<td>\n'+
		'		<select name="bform[reqRooms]['+ i +'][child]" id="bform[reqRooms]['+ i +'][child]" onchange="setChildAge(this.value,\'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\');" class="campi_booking_home2">\n'+
		'			'+ createSelect(3, 0, "", 0) + 
		'		</select>\n'+
		'		</td>'+
		'		</tr>\n'+
		'		<tr><td colspan="4"><img src="immagini/spacer.gif" width="1" height="5" /></td></tr>\n'+
		'		<tr id="chdAge_'+i+'"></tr>\n'+
		'		<tr><td colspan="4"><img src="immagini/spacer.gif" width="1" height="15" /></td></tr>\n'+
		'		</table>\n';
	}
	document.getElementById('optionSearch').innerHTML = myResults;
}*/

function setChildAge(nchild, chdid, roomn, label_children_age){
	//alert("ok " + nchild + " " + chdid + " " +roomn);
	var selectHtml = "";
	if(nchild > 0){
		selectHtml = '<label class="age">'+ label_children_age +'</label>';
		for (var cc=1; cc<=nchild; cc++) {
			selectHtml =  selectHtml + '<select name="bform[reqRooms]['+roomn+'][childAge][' + cc + ']">' +
                '<option value="0">&lsaquo; 1</option>';
				for(var k=1; k<=18; k++){
					selectHtml =  selectHtml + "\n" + '<option value="'+ k +'">'+ k +'</option>';
				}
				selectHtml =  selectHtml + '</select>';
		}
	}
	if(document.getElementById(chdid)){
		document.getElementById(chdid).innerHTML = selectHtml;
	}
}

function createSelect(num, init, label, selected){
	if(label!=""){
		label = " "+ label;
	}
	var mySel = "";
	for(var k=init; k<=num; k++){
		if (selected != 0 && selected == k){
			mySel = mySel + "\n" + '<option value="'+ k +'" selected="selected">'+ k + label +'</option>';
		} else {
			mySel = mySel + "\n" + '<option value="'+ k +'">'+ k + label +'</option>';
		}
	}
	return mySel;
}

function setRoom(nrooms, label_adults, label_children, label_children_age, label_room){
	var myResults = "";
	for (var i=1; i<=nrooms; i++){
		myResults = myResults + '';
		if ( nrooms >= 1){
			myResults = myResults + '<div id="optionSearch_'+i+'">';
		}
		myResults = myResults + 
		'		<label class="camere"><strong>'+label_room+'&nbsp;'+i+' :</strong></label><br>\n'+
		'		<div class="select">\n'+
		'		<label>'+ label_adults +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][adults]" id="bform[reqRooms]['+ i +'][adults]">\n'+
		'			'+ createSelect(4, 1, "", 1) + 

		'		</select>\n'+
		'		</div>'+
		'		<div class="select">\n'+
		'		<label>'+ label_children +'</label>\n'+
		'		<select name="bform[reqRooms]['+ i +'][child]" id="bform[reqRooms]['+ i +'][child]" onchange="setChildAge(this.value,\'chdAge_'+ i +'\', '+ i +',\''+ label_children_age +'\');">\n'+
		'			'+ createSelect(3, 0, "", 0) + 
		'		</select>\n'+
		'		</div>'+
		'		<div id="chdAge_'+ i +'" class="child">\n'+
		'		</div>\n'+
		'		</div>\n';
	}
	document.getElementById('optionSearch').innerHTML = myResults;
}