function addslashes(str) {
str=str.replace(/\'/g,'\\\'');
str=str.replace(/\"/g,'\\"');
str=str.replace(/\\/g,'\\\\');
str=str.replace(/\0/g,'\\0');
return str;
}
function ajax_calendar(prefix,calendar_id){
	this.prefix=prefix;
	this.calendar_id=calendar_id;
	this.debug=false;
	this.month=$(this.prefix+'_month');
	this.year=$(this.prefix+'_year');
	this.day_print=this.prefix+'_day_print';
	this.month_print=this.prefix+'_month_print';
	this.year_print=this.prefix+'_year_print';
	this.month.ajax_calendar=this;
	this.year.ajax_calendar=this;
	this.month.addEvent('change',function(){this.ajax_calendar.correct_date();this.ajax_calendar.update()});
	this.year.addEvent('change',function(){this.ajax_calendar.correct_date();this.ajax_calendar.update()});
	$(this.prefix+'_controls').style.className='';
	this.now=new Date();
	this.currentMonth=this.now.getMonth()+1;
	this.currentYear=this.now.getYear();
	this.day="";
	this.ajax_hash=new Hash();
	this.cells=new Array();
	this.post_body=new Object();
	this.ajax=new Ajax('',{
			evalScripts:true,
			ajax_hash:this.ajax_hash,
			postBody:this.post_body,
			onComplete:function(){
				this.options.ajax_hash.set(this.url,this.response.text)}
			});
	if(this.currentYear < 1900)this.currentYear+=1900;
	this.set_date=function(date){
		if(date == undefined)date=this.now;
		this.month.value=this.currentMonth;
		this.year.value=this.currentYear;
	}
	this.set_date();
	this.inc_month=function inc_month(){
		if(this.is_out_ranged("inc"))return false;
		if(this.month.selectedIndex < this.month.options.length-1){
			this.month.selectedIndex++;
			this.update();
			return true;
		}else if(this.inc_year(1)){
			this.month.selectedIndex=0;
			this.update();
			return true;
		}
	}
	this.dec_month=function dec_month(){
		if(this.is_out_ranged("dec"))return false;
		if(this.month.selectedIndex > 0){
			this.month.selectedIndex--;
			this.update();
			return true;
		}else if(this.dec_year(1)){
			this.month.selectedIndex=this.month.options.length-1;
			this.update();
			return true;
		}
	}
	this.inc_year=function inc_year(month_move){
		if(this.is_out_ranged("inc"))return false;
		if(this.year.selectedIndex < this.year.options.length-1){
			this.year.selectedIndex++;
			if(!month_move)this.update();
			return true;
		}
	}
	this.dec_year=function dec_year(month_move){
		if(this.is_out_ranged("dec"))return false;
		if(this.year.selectedIndex > 0){
			this.year.selectedIndex--;
			if(!month_move)this.update();
			return true;
		}
	}
	this.show_data=function(cell_key){
		var print_object;
		if(print_object=$(this.prefix + '_print')){
			if($(this.prefix + '_' + cell_key + '_cell_day_name'))this.day=$(this.prefix + '_' + cell_key + '_cell_day_name').innerHTML;
			this.update_printings();
			var temp_print_object=false;
			var temp_object=eval($(this.prefix + '_' + cell_key + '_cell_data').innerHTML);
			for(i in temp_object){
				if(temp_print_object = $(this.prefix + '_print_'+i))temp_print_object.setHTML(temp_object[i]);
			}
			print_object.style.display='block';
		}
		else if(this.debug)alert('object with id="'+this.prefix + '_print" not assigned');
	}
	this.hide_data=function(){
		var print_object;
		if(print_object=$(this.prefix + '_print'))print_object.style.display='none';
		else if(this.debug)alert('object with id="'+this.prefix + '_print" not assigned');
	}

	this.set_cell_today=function(cell_key){
		$(this.prefix + '_' + cell_key + '_td').className+=' calendar_day_today';
	}
	this.fill_cell=function(object){
        var td_object=null;
		if(!(td_object=this.cells[object.cell_key+'_td'])){
			td_object=$(this.prefix + '_' + object.cell_key + '_td');
            if(!td_object){
                alert("cell_object not found (#"+this.prefix + '_' + object.cell_key + '_td'+")");
                return;
                }
			this.cells[object.cell_key+'_td']=td_object;
		}
        var cell_object=null;
		if(!(cell_object=this.cells[object.cell_key+'_cell'])){
			cell_object=$(this.prefix + '_' + object.cell_key + '_cell');
            if(!cell_object){
                alert("cell_object not found (#"+this.prefix + '_' + object.cell_key + '_cell'+")");
                return;
            }
			this.cells[object.cell_key+'_cell']=cell_object;
		}
        var cell_data_object=null;
		if(!(cell_data_object=this.cells[object.cell_key+'_cell_data'])){
			cell_data_object=$(this.prefix + '_' + object.cell_key + '_cell_data');
            if(!cell_data_object){
                alert("cell_data_object not found (#"+this.prefix + '_' + object.cell_key + '_cell_data'+")");
                return;
            }
			this.cells[object.cell_key+'_cell_data']=cell_data_object;
		}

		
		if(object.inner_html && !object.outside_month)td_object.className='calendar_day_not_filled';
		else if(object.inner_html && object.outside_month)td_object.className='calendar_day_outside_month';
		else td_object.className='calendar_day_empty';
		cell_object.cell_key=object.cell_key;
		if(object.data_to_source){
			cell_object.setHTML(object.inner_html);
			if(object.url)cell_object.href=object.url;
			cell_object.calendar=this;
			cell_object.show_function=function(){this.calendar.show_data(this.cell_key)};
			cell_object.hide_function=function(){this.calendar.hide_data(this.cell_key)};
			if(object.type != 'contribution'){
				cell_object.addEvent('mouseover',cell_object.show_function);
				cell_object.addEvent('mouseout',cell_object.hide_function);
			}
			td_object.className='calendar_day_filled';
		}
		else{
			cell_object.removeAttribute('href');
			cell_object.removeEvent('mouseover',cell_object.show_function);
			cell_object.removeEvent('mouseout',cell_object.hide_function);
			cell_object.setHTML(object.inner_html);
		}
		if(object.type == 'contribution'){
			cell_object.removeAttribute('href');
			td_object.className+=' calendar_contribution';
		}
		cell_data_object.setHTML(object.data_to_source);
	}
	this.update=function(){
		this.ajax.options.postBody=this.post_body;
		this.ajax.url=this.ajax_update_url+'?date='+this.year.value+'-'+this.month.value+'&prefix='+this.prefix+'&limit='+this.limit+'&calendar_source='+this.calendar_source+'&calendar_id='+this.calendar_id;
		if(response = this.ajax_hash.get(this.ajax.url)){
			this.ajax.response.text=response;
			this.ajax.evalScripts();
		}
		else this.ajax.request();
		this.update_printings();
	}
	this.is_out_ranged=function(type){
		if((type == "inc" || type == "check") && this.max_range_today && (this.month.value >= this.currentMonth && this.year.value >= this.currentYear)){
			return true;
		}
		else if((type == "inc" || type == "check") && this.max_year && (this.year.value >= this.max_year.toInt() && (this.month.value >= this.max_month.toInt() || !this.max_month.toInt()))){
			return true;
		}
		else if((type == "dec" || type == "check") && this.min_year && (this.year.value <= this.min_year.toInt() && (this.month.value <= this.min_month.toInt() || !this.min_month.toInt()))){
			return true;
		}
		else return false;
	}
	this.correct_date=function(){
		if(this.is_out_ranged("check"))this.set_date();
	}
	this.update_printings=function(){
		if($(this.day_print))$(this.day_print).innerHTML=this.day;
		if($(this.month_print))$(this.month_print).innerHTML=this.month.options[this.month.selectedIndex].label;
		if($(this.year_print))$(this.year_print).innerHTML=this.year.options[this.year.selectedIndex].label;
	}
	this.update_printings();
}
