var CurrentDate = function(){
	var gdCurDate = new Date();
	this.giYear = gdCurDate.getFullYear();
	this.giMonth = gdCurDate.getMonth();
	this.giDay = gdCurDate.getDate();

	this.mouseDown = this.doMouseDown.bindAsEventListener(this);

	Event.observe($('today'),'click', this.mouseDown);		

	//어제, 지난달, 이번달 들은 특정페이지에만 구현 되어있기 때문에  해당 엘리먼트가 없을경우 에러 방지 
	if($('preDay') != null){
		Event.observe($('preDay'),'click', this.mouseDown);
	}
	if($('lastMonth') != null){
		Event.observe($('lastMonth'),'click', this.mouseDown);
	}
	if($('month') != null){
		Event.observe($('month'),'click', this.mouseDown);
	}
	Event.observe($('preWeek'),'click', this.mouseDown);
	Event.observe($('preMonth'),'click', this.mouseDown);
	Event.observe($('pre3Month'),'click', this.mouseDown);
	Event.observe($('pre6Month'),'click', this.mouseDown);
	Event.observe($('preYear'),'click', this.mouseDown);
}

CurrentDate.prototype = {
	yearFormat : function(value){
		var year = value.toString();
		return year.substring(2);
	},
	format : function (value){
		if( value < 10 )
			return "0" + value;
		else
			return value.toString();
	},
	dateFormat : function (year, month, day){
		return this.yearFormat(year)+this.format(month)+this.format(day);
	},
	setDate : function(days){
		if(0==days){
			var preDate = new Date(this.giYear, this.giMonth, this.giDay - days);
			$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, preDate.getDate());
			$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth+1, this.giDay);
		}
		if(1==days){
			var preDate = new Date(this.giYear, this.giMonth, this.giDay - days);
			$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, preDate.getDate());
			if((this.giDay-1) != 0){
				$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth+1, this.giDay-1);
			}else{
				$('SEARCH_END_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, preDate.getDate());
			}
			
		}
		if(1<days){
			var preDate = new Date(this.giYear, this.giMonth, this.giDay - days);			
			if((this.giDay-1) != 0){
				$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, preDate.getDate());
				$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth+1, this.giDay-1);
			}else{
				$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, 1);
				$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth, 31);
			}			
		}
		//이번달
		if(-2==days){
			var preDate = new Date(this.giYear, this.giMonth, this.giDay);
			$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth()+1, 1);
			$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth+1, this.giDay);
		}
		//지난달
		if(-1==days){
			var preDate = new Date(this.giYear, this.giMonth, this.giDay);
			$('SEARCH_START_DATE').value = this.dateFormat(preDate.getFullYear(), preDate.getMonth(), 1);
			$('SEARCH_END_DATE').value = this.dateFormat(this.giYear, this.giMonth, 31);
		}
	},
	doMouseDown : function(ev){
		var event = Event.element(ev);
		if(event.id=='today'){
			this.setDate(0);
		} else if(event.id=='preDay'){
			this.setDate(1);
		} else if(event.id=='preWeek'){
			this.setDate(7);
		} else if(event.id=='preMonth'){
			this.setDate(30);
		} else if(event.id=='pre3Month'){
			this.setDate(90);
		} else if(event.id=='pre6Month'){
			this.setDate(180);
		} else if(event.id=='preYear'){
			this.setDate(365);
		} else if(event.id=='lastMonth'){
			//지난달
			this.setDate(-1);
		} else if(event.id=='month'){
			//이번달
			this.setDate(-2);
		} else {

		}
	}
}


