/*-------------------------------------------------------
date.js
depends on prototype.js(http://prototype.conio.net/)
@version  V1.0
@author   youngjin.shin (2007/04/20)
Copyright (c) 2007 Rakuten Travel, Inc. All Rights Reserved.
--------------------------------------------------------*/
Util.createDate = function  (id, yearObj, monthObj, dayObj, syncObj, diffVal ) {
	   if(this.instances == null) this.instances = new Array(); 	   
	   var instance = null;
  	   for (var i = 0; i < this.instances.length ; i++) {
  		    if( this.instances[i].id == id ){
  				return this.instances[i];
  			}
  		}
  		instance = new Util.Date(id, yearObj, monthObj, dayObj, syncObj, diffVal);
  		this.instances.push(instance);	 
    },

Util.Date.prototype = {
		id   : null,
  		Year : null,
   		Month: null, 
   		Day  : null,
		Sync : null,
		Diff : null,
		date : null,
		syncronize:null, 
		
   		initialize:function(id, yearObj, monthObj, dayObj, syncObj , diffVal ) {
   		   this.id = id;
   		   this.Year = $(yearObj);
   		   this.Month= $(monthObj);
   		   this.Day  = $(dayObj);
   		   this.Diff = diffVal;
   		   this.date = new Date();
   		   this.setAll();
   		   if(syncObj){
   		   		this.Sync   = syncObj; 
   		   		this.syncronize = true; 
   		   }
   		   this.bindEvent();
   		},

  		bindEvent: function(){
    		Event.observe(this.Year,  'change', this.changeSyncDate.bindAsEventListener(this), false);
    		Event.observe(this.Month, 'change', this.changeSyncDate.bindAsEventListener(this), false);
   		 	Event.observe(this.Day,   'change', this.changeSyncDate.bindAsEventListener(this), false);
  		},

		getDate:function(){
			return new Date(this.getYear(),this.getMonth()-1,this.getDay());
		},
   		
   		getYear:function(){
   			return this.Year.value;
   		},
   		
   		getMonth:function(){
   			return this.Month.value;
   		},
   		
   		getDay:function(){
   			return this.Day.value;
   		},

		setDate:function(date){
			this.date = date ;
			this.setAll();
		},

		setAll:function(){
		   this.setYear(this.date.getFullYear());
   		   this.setMonth(this.date.getMonth()+1);
   		   this.setDay(this.date.getDate());
		},
		   		
   		setYear:function(year){
   			this.Year.value = year;
   		},
   		
   		setMonth:function(month){
  			if ( month < 10 ){
    			this.Month.value = "0" + month;
			}
			else {
    			this.Month.value =  month;
			} 		
   		},
   		
   		setDay:function(day){
			if ( day < 10 ){
	    		this.Day.value =   "0" + day;
			}
			else {
        		this.Day.value = day;
    		}  		
   		},
   		
   		addDay:function(diff){
			this.date.setDate(this.date.getDate()+diff); 
			this.setAll();
			this.changeSyncDate();  		
   		},
   		
	    changeSyncDate:function(event) {
	    	this.date = this.getDate();
	    	if(this.isValidDate()) {
	    	 	if(this.syncronize){
	    	   		this.Sync.setDate(this.getDate())
	    	   		this.Sync.addDay(this.Diff);
	    	   		//alert(this.Sync.getYear()+this.Sync.getMonth()+this.Sync.getDay());
	    		}
	    	} else {
	    		alert("invalid date"); 
	    	}	   	
 		},	
 		
 		isValidDate:function(){
 		    if(this.getMonth() >= 1 && this.getMonth() <= 12 && this.getDay()>= 1 && this.getDay() <= 31){ 
        		if(isNaN(this.date)){ 
            		return false;
        		}else if(this.date.getFullYear() == this.getYear() &&
        		         this.date.getMonth()    == this.getMonth()-1 &&
        		         this.date.getDate()     == this.getDay()){ 
            		return true; 
        		}else{ 
            		return false; 
        		}
        	}
        	return false; 
 		}
}