 
function toggleCal(element, datefield) {
	if($(element).style.display=='none'){
//		cancelCalHide();
		new Effect.Appear($(element),{duration:.2});
		fixGraphics();
		tryToFixIE(element);
		if($(element+'_iefix')) Element.show(element+'_iefix');
		
		//uzdarom kita kalendoriu
		if(element=='cal'){
			otherelement='cal2';
		}else{
			otherelement='cal';
		}
		if($(otherelement).style.display==''){
			if($(otherelement+'_iefix')) Element.hide(otherelement+'_iefix');
			new Effect.Fade($(otherelement),{duration:.2});
		}

	}else{
		if($(element+'_iefix')) Element.hide(element+'_iefix');
		new Effect.Fade($(element),{duration:.2});
	}
	
}

function fixGraphics(){
	addRange();
	setclasses($('departure').value, 'departure', 'cal');
	setclasses($('return').value, 'return', 'cal2');
}

function tryToFixIE(element){
	element=$(element);

	iefix=false;
    if((navigator.appVersion.indexOf('MSIE')>0) && (navigator.userAgent.indexOf('Opera')<0) && (Element.getStyle(element, 'position')=='absolute')) {
      new Insertion.After(element, 
       '<iframe id="' + element.id + '_iefix" '+
       'style="display:none;position:absolute;filter:progid:DXImageTransform.Microsoft.Alpha(opacity=0);" ' +
       'src="javascript:false;" frameborder="0" scrolling="no"></iframe>');
      iefix = $(element.id+'_iefix');
    }
    if(iefix) setTimeout('fixIEOverlapping(\''+element.id+'\')', 50);
  
}


function fixIEOverlapping(element) {
  iefix=$(element+'_iefix');
  element=$(element);
    Position.clone(element.id, iefix.id, {setTop:(!element.style.height)});
    iefix.style.zIndex = 1;
    element.style.zIndex = 2;
    Element.show(iefix);
}

function removeRange(){

	if($('backcal').style.display=='none' || $('departure').value==g_default_cal_val || $('return').value==g_default_cal_val) return false;
	
	try {
		fromdate=Date.parseDate($('departure').value,DATE_FORMAT);
		todate=Date.parseDate($('return').value,DATE_FORMAT);
		fromdate.setDate(fromdate.getDate()+1);
	
		if(fromdate.getTime()<todate.getTime()){
			thisdate=new Date(fromdate);
			go=true;
			while(go){
			//alert('remove'+thisdate);
				Element.removeClassName($('cal'+thisdate.dateFormat(DATE_FORMAT)),'c4');
				Element.removeClassName($('cal2'+thisdate.dateFormat(DATE_FORMAT)),'c4');
				thisdate.setDate(thisdate.getDate()+1);
				if(thisdate.getTime()>=todate.getTime()) go=false;
			}
		}
	
	} catch(e) { };
	
}

function addRange(){

	try {

		if($('backcal').style.display=='none' || $('departure').value==g_default_cal_val || $('return').value==g_default_cal_val) return false;
		fromdate=Date.parseDate($('departure').value,DATE_FORMAT);
		todate=Date.parseDate($('return').value,DATE_FORMAT);
	
		fromdate.setDate(fromdate.getDate()+1);
		
		if(fromdate.getTime()<todate.getTime()){
			thisdate=new Date(fromdate);
			go=true;
			while(go){
				Element.addClassName($('cal'+thisdate.dateFormat(DATE_FORMAT)),'c4');
				Element.addClassName($('cal2'+thisdate.dateFormat(DATE_FORMAT)),'c4');
				thisdate.setDate(thisdate.getDate()+1);
				if(thisdate.getTime()>=todate.getTime()) go=false;
			}
		}
	
	} catch (e) {};
	
}

function setclasses(date, element, cal){
	thiscal=cal;
	thiselement=element;
	if(thiscal=='cal'){
		othercal='cal2';
		otherelement='departure';
	}else{
		othercal='cal';
		otherelement='return';
	}


	//remove old classes
	Element.removeClassName($(thiscal+$(thiselement).value),'c3');
	Element.removeClassName($(othercal+$(thiselement).value),'c5');


	//add new classes
	Element.addClassName($(thiscal+date),'c3');
	Element.addClassName($(othercal+date),'c5');


}

function sD(date, element, cal){
	removeRange();
	setclasses(date, element, cal);
	$(element).value=date;
	addRange();
	new Effect.Fade($(cal),{duration:.2});
	if($(cal+'_iefix')) Element.hide(cal+'_iefix');
}





function get_calendar_html(start, end, cal, element){
	start_dt=Date.parseDate(start, DATE_FORMAT);
	end_dt=Date.parseDate(end, DATE_FORMAT);
	prev=true;
	next=true;
	if(start_dt.getTime()<=g_limit_start.getTime()){
		start_dt=g_limit_start;
		prev=false;
	}
	
	if(end_dt.getTime()>=g_limit_end.getTime()){
		end_dt=g_limit_end;
		next=false;
	}
	
	go=true;
	ret='';
	count=1;
	while(go){
	
		first_month_day_dt=Date.parseDate((start_dt.getFullYear()+'-'+(start_dt.getMonth()+1)+'-'+'01'),'Y-m-d');
		
		first_week_day=first_month_day_dt.getDay();
		first_week_day=(first_week_day==0)?7:first_week_day;
		month_days=first_month_day_dt.getDaysInMonth();
		last_month_day_dt=Date.parseDate(start_dt.getFullYear()+'-'+(start_dt.getMonth()+1)+'-'+month_days,'Y-m-d');
		if(last_month_day_dt.getTime()>=end_dt.getTime()) go=false;
		
		//Month header
		ret+='<table border="0" cellpadding="0" cellspacing="0" width="140">';
		ret+='<tr><td class="c0" colspan="7"><b>'+first_month_day_dt.dateFormat('Y F')+'</b></td></tr><tr class="cr">';
		ret+=Date.dayShortNamesHTML;
		ret+='</tr><tr>';
		if(first_week_day>1){
			for(x=1; x<first_week_day; x++, count++){
				ret+='<td class="c1">&nbsp;</td>';
			}
		}
		
		for (counter = 1; counter <= month_days; counter++, count++) {
			if (((count-1) % 7) == 0) {
				ret += "</tr> <tr>";
			}
			current_day_dt=Date.parseDate(start_dt.getFullYear()+'-'+(start_dt.getMonth()+1)+'-'+counter,'Y-m-d');
			cur_date=current_day_dt.dateFormat(DATE_FORMAT);
			if(current_day_dt.getTime()>=start_dt.getTime() && current_day_dt.getTime()<=end_dt.getTime()){
				fill_str='';
				if(cal=='cal' && go==false) {
					fill_first=current_day_dt.dateFormat('Y-m-')+'1';				
					next_dt=Date.parseDate(fill_first,'Y-m-d');
					next_dt.setMonth(next_dt.getMonth()+1);
					fill_next=next_dt.dateFormat('Y-m-')+next_dt.getDaysInMonth();
					
					fill_first = Date.parseDate(fill_first,'Y-m-d');
					fill_first = fill_first.dateFormat(DATE_FORMAT);
					fill_next = Date.parseDate(fill_next,'Y-m-d');
					fill_next = fill_next.dateFormat(DATE_FORMAT);
					
					fill_str='fillcalendars(\''+fill_first+'\',\''+fill_next+'\');';
				}
				ret+= '<td class="c2" id="'+cal+cur_date+'"><a href="javascript: sD(\''+cur_date+'\', \''+element+'\', \''+cal+'\'); '+fill_str+'">'+counter+'</a></td>';
			}else{
				ret+= '<td class="c1">'+counter+"</td>";
			}
		}
		while(((count-1) % 7) != 0){
				ret+='<td class="c1">&nbsp;</td>';
				count++;
		}
		ret+='</tr></table>';
		//go=false;
		if(go) ret+='<br/>';
		current_day_dt.setDate(current_day_dt.getDate()+1);
		start_dt=current_day_dt;
		
	}
	start_dt.setMonth(start_dt.getMonth()-1);
	next_start=start_dt.dateFormat('Y-m-d');
	start_dt.setMonth(start_dt.getMonth()+1);
	next_end=start_dt.dateFormat('Y-m-')+start_dt.getDaysInMonth();

	next_start = Date.parseDate(next_start,'Y-m-d');
	next_start = next_start.dateFormat(DATE_FORMAT);
	next_end = Date.parseDate(next_end,'Y-m-d');
	next_end = next_end.dateFormat(DATE_FORMAT);

	start_dt.setMonth(start_dt.getMonth()-3);
	prev_start=start_dt.dateFormat('Y-m-d');
	start_dt.setMonth(start_dt.getMonth()+1);
	prev_end=start_dt.dateFormat('Y-m-')+start_dt.getDaysInMonth();
	
	prev_start = Date.parseDate(prev_start,'Y-m-d');
	prev_start = prev_start.dateFormat(DATE_FORMAT);
	prev_end = Date.parseDate(prev_end,'Y-m-d');
	prev_end = prev_end.dateFormat(DATE_FORMAT);

	ret='<div class="ccont" style="position:absolute;width:150px; height:340px; overflow:hidden; z-index:2" id="dv'+cal+'">'+(prev?'<div style="width:100%; text-align:center"><div style="cursor:pointer;" onclick="javascript:cancelCalHide();fillcalendars(\''+prev_start+'\',\''+prev_end+'\');fixGraphics();"><a class="othercities" href="javascript:cancelCalHide();fillcalendars(\''+prev_start+'\',\''+prev_end+'\');fixGraphics();">&lt;'+txt_BACK+'</a></div></div>':'')+'<div class="spacer"></div><div>'+ret+'</div>'+(prev?'':'')+(next?'<div style="width:100%; text-align:center"><div style="height:70px;cursor:pointer;" onclick="javascript:cancelCalHide();fillcalendars(\''+next_start+'\',\''+next_end+'\');fixGraphics();"><a class="othercities" href="javascript:cancelCalHide();fillcalendars(\''+next_start+'\',\''+next_end+'\');fixGraphics();">'+txt_NEXT+'&gt;</a></div></div>':'')+'</div>';
	return ret;
}
	
function fillcalendars(start,end){
	$('cal').innerHTML=get_calendar_html(start,end,'cal','departure');
	$('cal2').innerHTML=get_calendar_html(start,end,'cal2','return');
}

add_onload('sD($(\'departure\').value, \'departure\',\'cal\');');
add_onload('sD($(\'return\').value, \'return\',\'cal2\');');