//___________________________________________________________________
DateProperty.months_arr		= new Array('January','February','March','April','May','June','July','August','September','October','November','December');
//___________________________________________________________________
function DateProperty(property,label_str,editAuth){
	
	var self				= this;
	var date_dte			= new Date();
	
	var broadcaster			= new Broadcaster(this);
	broadcaster.createEventType('valueChange');
	
	property.addEventListener('valueChange',handlePropertyUpdate);
	
	this.__defineGetter__('label',function(){return label_str;});
	this.__defineGetter__('value',function(){return new Date(date_dte.toUTCString());});
	this.__defineSetter__('value',setValue);
	
	property.value? null : setValue(new Date());
	
	//---------------------------------------------------------------
	function handlePropertyUpdate(){
		
		var newDate_dte		= parseEVDBDate(property.value);
		editAuth.valid? setValue(newDate_dte) : null;
		
		if(date_dte.toString() != newDate_dte.toString()){
			date_dte		= newDate_dte;
			broadcaster.triggerEvent('valueChange');
		}
	};
	//---------------------------------------------------------------
	function parseEVDBDate(date_str){
		
		var matches_arr		= date_str.match(/(\d{4})-(\d{2})-(\d{2})\s*(\d{2}):(\d{2}):(\d{2})/);
		var newDate_dte		= new Date();
		if(matches_arr){
			newDate_dte.setUTCFullYear(parseInt(matches_arr[1],10),parseInt(matches_arr[2],10)-1,parseInt(matches_arr[3],10));
			newDate_dte.setUTCHours(parseInt(matches_arr[4],10),parseInt(matches_arr[5],10),parseInt(matches_arr[6],10));
		}
		
		return newDate_dte;
	};
	//---------------------------------------------------------------
	function convertToEVDBDate(date_dte){
		
		var date_str		= date_dte.toUTCString();
		var date_str		= date_dte.getUTCFullYear()+'-'+make2digit(date_dte.getUTCMonth()+1);
		date_str			+= '-'+ make2digit(date_dte.getUTCDate());

		var hrs_str			= make2digit(date_dte.getUTCHours());
		var mins_str		= make2digit(date_dte.getUTCMinutes());
		date_str			+= ' '+hrs_str+':'+mins_str+':00';
	
		return date_str;
		//-----------------------------------------------------------
		function make2digit(value_int){
			
			return value_int<10? '0'+value_int : value_int+'';
		};
		//-----------------------------------------------------------
	};
	//---------------------------------------------------------------
	function setValue(newDate_dte){
		
		property.value		= convertToEVDBDate(newDate_dte);
	};
	//---------------------------------------------------------------
	function dateToString(date_dte){
		
		var date_str		= date_dte.toUTCString();
		var matches_arr		= date_str.match(/([^$]*\s*\d{2}:\d{2}):\d{2}/);
		var date_str		= matches_arr[1];
		return date_str;
	};
	//_______________________________________________________________
	this.createInputCell = function(parent_h){
		
		var container_h		= parent_h.createChild('div','date_inputs');
		var dateContainer_h	= container_h.createChild('div');
		dateContainer_h.createChild('span','label',null,'Date');
		dateContainer_h.appendChild(this.dateInput.input_h);
		dateContainer_h.appendChild(this.monthInput.input_h);
		dateContainer_h.appendChild(this.yearInput.input_h);
		
		var timeContainer_h	= container_h.createChild('div');
		timeContainer_h.createChild('span','label',null,'Time');
		timeContainer_h.appendChild(this.hourInput.input_h);
		timeContainer_h.appendChild(this.minuteInput.input_h);
	};
	//_______________________________________________________________
	this.getString = function(){
		
		return dateToString(date_dte);
	};
	//_______________________________________________________________
	this.defineValue = function(newDate_dte){
		
		property.defineValue(convertToEVDBDate(newDate_dte));
	};
	//_______________________________________________________________
	this.bindValueToInnerHTML = function(element_h){
		
		// define a function to update element_h upon change in Property.value
		// replace line breaks with <br /> tags
		var refreshElement	= function(){element_h.innerHTML = dateToString(date_dte)};
		refreshElement();
		
		// execute function on value change
		this.addEventListener('valueChange',refreshElement);
	};
	//________________________________________________________________
	this.bindInput2 = function(input){
		
		//------------------------------------------------------------
		function refreshInputValue(){
			
			input.setValue(date_dte);
		};
		//------------------------------------------------------------
		function refreshInputAttributes(){
			
			input.input_h.disabled= !editAuth.valid;
		};
		//------------------------------------------------------------
		function takeInputValue(){
			
			self.value 		= new Date(input.getValue());
		};
		//------------------------------------------------------------
		refreshInputAttributes();
		refreshInputValue();
		
		// add parentObj listeners that will trigger value syncing
		this.addEventListener('valueChange',refreshInputValue);
		editAuth.addEventListener('validate',refreshInputAttributes);
		editAuth.addEventListener('invalidate',refreshInputAttributes);
		input.input_h.addEventListener('valueChange',takeInputValue,false);
		input.input_h.addEventListener('change',takeInputValue,false);
	};
	//________________________________________________________________
	
	this.yearInput			= new DateProperty.YearInput(self);
	this.dateInput			= new DateProperty.DateInput(self);
	this.monthInput			= new DateProperty.MonthInput(self);
	this.hourInput			= new DateProperty.HourInput(self);
	this.minuteInput		= new DateProperty.MinuteInput(self);
	
};	//________________________________________________________________
//____________________________________________________________________
DateProperty.DateInput = function(dateProperty){
	
	this.input_h			= document.createElement('select')
	for(var i=0; i<31; i++){
		var option_h		= this.input_h.createChild('option',null,null,i+1);
		option_h.value		= i;
	}
	
	//________________________________________________________________
	this.getValue = function(){
		
		var date_dte		= dateProperty.value;
		date_dte.setUTCDate(parseInt(this.input_h.value,10)+1);
		return date_dte;
	};
	//________________________________________________________________
	this.setValue = function(date_dte){
		
		this.input_h.value		= date_dte.getUTCDate()-1;
	};
	//________________________________________________________________
	dateProperty.bindInput2(this);
};	//________________________________________________________________
//____________________________________________________________________
DateProperty.MonthInput = function(dateProperty){
	
	this.input_h				= document.createElement('select')
	for(var i=0; i<12; i++){
		var option_h		= this.input_h.createChild('option',null,null,DateProperty.months_arr[i]);
		option_h.value		= i;
	}
	
	//________________________________________________________________
	this.getValue = function(){
		
		var date_dte		= dateProperty.value;
		date_dte.setUTCMonth(parseInt(this.input_h.value,10));
		return date_dte;
	};
	//________________________________________________________________
	this.setValue = function(date_dte){
		
		this.input_h.value		= date_dte.getUTCMonth();
	};
	//________________________________________________________________
	dateProperty.bindInput2(this);
};	//________________________________________________________________
//____________________________________________________________________
DateProperty.YearInput = function(dateProperty){
	
	var baseDate_int		= 2006;
	
	this.input_h				= document.createElement('select')
	for(var i=0; i<4; i++){
		var year_int		= baseDate_int + i;
		var option_h		= this.input_h.createChild('option',null,null,year_int);
		option_h.value		= year_int;
	}
	
	//________________________________________________________________
	this.getValue = function(){
		
		var date_dte		= dateProperty.value;
		date_dte.setUTCFullYear(parseInt(this.input_h.value,10));
		return date_dte;
	};
	//________________________________________________________________
	this.setValue = function(date_dte){
		
		this.input_h.selectedIndex = date_dte.getUTCFullYear() - baseDate_int;
	};
	//________________________________________________________________
	dateProperty.bindInput2(this);
};	//________________________________________________________________
//____________________________________________________________________
DateProperty.HourInput = function(dateProperty){
	
	this.input_h			= document.createElement('select')
	for(var i=0; i<24; i++){
		var hrs_str			= i<10? '0'+i : i+'';
		hrs_str				= hrs_str=='0'? '00' : hrs_str;
		var option_h		= this.input_h.createChild('option',null,null,hrs_str);
		option_h.value		= i;
	}
	
	//________________________________________________________________
	this.getValue = function(){
		
		var date_dte		= dateProperty.value;
		date_dte.setUTCHours(parseInt(this.input_h.value,10));
		return date_dte;
	};
	//________________________________________________________________
	this.setValue = function(date_dte){
		
		this.input_h.selectedIndex = date_dte.getUTCHours();
	};
	//________________________________________________________________
	dateProperty.bindInput2(this);
};	//________________________________________________________________
//____________________________________________________________________
DateProperty.MinuteInput = function(dateProperty){
	
	this.input_h				= document.createElement('select')
	for(var i=0; i<60; i++){
		var mins_str		= i<10? '0'+i : i+'';
		mins_str			= mins_str=='0'? '00' : mins_str;
		var option_h		= this.input_h.createChild('option',null,null,mins_str);
		option_h.value		= i;
	}
	
	//________________________________________________________________
	this.getValue = function(){
		
		var date_dte		= dateProperty.value;
		date_dte.setUTCMinutes(parseInt(this.input_h.value,10));
		return date_dte;
	};
	//________________________________________________________________
	this.setValue = function(date_dte){
		
		this.input_h.selectedIndex = date_dte.getUTCMinutes();
	};
	//________________________________________________________________
	dateProperty.bindInput2(this);
};	//________________________________________________________________
//____________________________________________________________________