Ext.namespace("Ntp.form");
Ntp.form.AdvancedDateField = Ext.extend(Ext.form.TriggerField, {
	format : "m/d/Y",
	altFormats : "m/d/Y|n/j/Y|n/j/y|m/j/y|n/d/y|m/j/Y|n/d/Y|m-d-y|m-d-Y|m/d|m-d|md|mdy|mdY|d|Y-m-d",
	dateMenu:	false,
	singleDate: false,
	toDate: false,
	fromDate: false,

	strings:{
		today:"Azi",
		thisWeek:"Saptamana curenta",
		thisMonth:"Luna curenta",
		lastMonth:"Luna anterioara",
		day:"Ziua de",	
		interval:"Interval",
		iFromDay:"De la",
		iToDay:"Pana la",
		any:"Oricand",
		last3Days:"Ultimele 3 zile"
		
	},
	hiddenElements:{},
	_value:{
		type:0,
		singleDate:false,
		fromDate:false,
		toDate:false
	},
	syncValues: function(){
	    this.hiddenElements.type .setAttribute("value", this._value.type);
	    this.hiddenElements.singleDate .setAttribute("value", this._value.singleDate?this._value.singleDate:0);
	    this.hiddenElements.fromDate .setAttribute("value", this._value.fromDate?this._value.fromDate:0);
	    this.hiddenElements.toDate .setAttribute("value", this._value.toDate?this._value.toDate:0);
	},
	menuListeners : {
			select: function(m, d){    			
				switch(m.parentMenu.id){
					case this.singleDate.id:
						 this.singleDate.parentMenu.activeItem.setChecked(true);		
						 this.fromM.setChecked(false);
						 this.toM.setChecked(false);
						 this._value.type=1;
						 this._value.singleDate=this.formatDate(this.parseDate(d));  
						 Ext.form.DateField.superclass.setValue.call(this, this._value.singleDate);
						 this._value.toDate=false;
						 this._value.fromDate=false;
					break;
					case this.fromDate.id:
						if(this._value.type!=2){
							this._value.toDate="";
						}
						this.fromDate.parentMenu.activeItem.setChecked(true);
						this.fromDate.parentMenu.parentMenu.activeItem.setChecked(true);
						this._value.type=2;

						this._value.fromDate=this.formatDate(this.parseDate(d));      				
						Ext.form.DateField.superclass.setValue.call(this, (this._value.fromDate?this._value.fromDate:"") + " - " + (this._value.toDate?this._value.toDate:""));
						if(this.toDate.picker.el){
							this.toDate.picker.setMinDate(this.fromDate.picker.getValue());
						}else{
						   this.toDate.picker.minDate=this.fromDate.picker.getValue();
						}	
					break;
					case this.toDate.id:
						if(this._value.type!=2){
							this._value.fromDate="";
						}
						this.toDate.parentMenu.activeItem.setChecked(true);
						this.toDate.parentMenu.parentMenu.activeItem.setChecked(true);
						this._value.type=2;
						this._value.toDate=this.formatDate(this.parseDate(d));      				
						Ext.form.DateField.superclass.setValue.call(this, (this._value.fromDate?this._value.fromDate:"") + " - " + (this._value.toDate?this._value.toDate:""));
						if(this.fromDate.picker.el){
							this.fromDate.picker.setMaxDate(this.toDate.picker.getValue());
						}else{
						   this.fromDate.picker.maxDate=this.toDate.picker.getValue();
						}
					break;

				}
				
				
				this.syncValues();
				this.fireEvent('change',this,this.getValue());
				


			}, 
			show : function(){ // retain focus styling
				this.onFocus();
			},
			hide : function(){
//	            this.focus.defer(10, this);
//	            var ml = this.menuListeners;
//	            this.fromDate.un("select", ml.select,  this);
//	            this.fromDate.un("show", ml.show,  this);
//	            this.fromDate.un("hide", ml.hide,  this);
			}
	},

	onDestroy : function(){
			if(this.menu) {
				this.menu.destroy();
			}
			if(this.wrap){
				this.wrap.remove();
			}
			Ext.form.DateField.superclass.onDestroy.call(this);
	},

	onItemCheck: function(item){
		 
		 Ext.form.TextField.superclass.setValue.apply(this,[item.text]);
		 this._value.toDate=false;
		 this._value.fromDate=false;
		 this._value.startDate=false;
		 this.fromM.setChecked(false);
		 this.toM.setChecked(false);
		 this._value.type=item.value;
		 this.syncValues();
		 this.fireEvent('change',this,this.getValue());
		 

		 
	},

	onTriggerClick : function(){

		if(this.disabled){
			return;
		}
		
		if(!this.dateMenu){
				this.singleDate = new Ext.menu.DateMenu();  
				this.toDate  = new Ext.menu.DateMenu();
				this.fromDate=new Ext.menu.DateMenu();        		
				this.dateMenu = new Ext.menu.Menu({
				items:[
						   {
							value:10,
							text: this.strings.today,
							checked:  this._value.type==10?true:false,
							group:  this.id+'dateOption',
							checkHandler: this.onItemCheck,
							scope : this
					   },
					   {
							value:11,
							text:this.strings.thisWeek,
							checked: this._value.type==11?true:false,
							group: this.id+'dateOption',
							checkHandler: this.onItemCheck,
							scope : this
					   },
					   {
							value:12,
							text: this.strings.thisMonth,
							checked:  this._value.type==12?true:false,
							group: this.id+'dateOption',      
							checkHandler: this.onItemCheck,
							scope : this
					   },
					   {
							value:15,
							text: this.strings.lastMonth,
							checked:  this._value.type==15?true:false,
							group: this.id+'dateOption',      
							checkHandler: this.onItemCheck,
							scope : this
					   },
					   {
							value:14,
							text: this.strings.last3Days,
							checked:  this._value.type==14?true:false,
							group: this.id+'dateOption',      
							checkHandler: this.onItemCheck,
							scope : this
					   },
					   {
						   text:this.strings.day,
						   menu:this.singleDate,
							checked: false,
							group: this.id+'dateOption'
					   },
					   {
						   text: this.strings.interval,
						   checked: false,
						   group: this.id+'dateOption',  	                
						   menu:{
								items:[
								this.fromM=new Ext.menu.CheckItem({
									checked: false,
									text:this.strings.iFromDay,	
									menu:this.fromDate
								}),
								this.toM=new Ext.menu.CheckItem({
								   checked: false,
								   text:this.strings.iToDay,	
								   menu:this.toDate
								})

								]
						   }        	            	

					   },
					   {
							value:13,
							text: this.strings.any,
							checked:  this._value.type==13?true:false,
							group: this.id+'dateOption',
							checkHandler: this.onItemCheck,
							scope : this
						}
					]
			});
		}
		
		this.fromDate.on(Ext.apply({}, this.menuListeners, {
			scope:this
		}));

		this.toDate.on(Ext.apply({}, this.menuListeners, {
			scope:this
		}));
		
		this.singleDate.on(Ext.apply({}, this.menuListeners, {
			scope:this
		}));

		this.dateMenu.on(Ext.apply({}, this.menuListeners, {
			scope:this
		}));
		

		this.singleDate.picker.setValue(this.parseDate(this._value.singleDate) || new Date());
		this.toDate.picker.setValue(this.parseDate(this._value.toDate) || new Date());
		this.fromDate.picker.setValue(this.parseDate(this._value.fromDate) || new Date());
		this.dateMenu.show(this.el,"tr-br?")
		
	
	},

	beforeBlur : function(){
		// TODO:
//	    var v = this.parseDate(this.getRawValue());
//        if(v){
//            this.setValue(v);
//        }
	},
	initHiddenElements: function(){
		var myForm =Ext.query("form",this.findParentByType("form").el.dom);

		myForm=myForm[0];
		this.hiddenElements={};
		
		if(myForm) {

    			this.hiddenElements.type = document.createElement("input");
    			this.hiddenElements.type .setAttribute("type", "hidden");
    			this.hiddenElements.type .setAttribute("name", this.name+"[type]");			
    			myForm.appendChild(this.hiddenElements.type );
    
    			this.hiddenElements.fromDate = document.createElement("input");
    			this.hiddenElements.fromDate .setAttribute("type", "hidden");
    			this.hiddenElements.fromDate .setAttribute("name", this.name+"[fromDate]");
    			myForm.appendChild(this.hiddenElements.fromDate );
    
    			this.hiddenElements.toDate = document.createElement("input");
    			this.hiddenElements.toDate .setAttribute("type", "hidden");
    			this.hiddenElements.toDate .setAttribute("name", this.name+"[toDate]");
    			myForm.appendChild(this.hiddenElements.toDate );
    			
    			this.hiddenElements.singleDate = document.createElement("input");
    			this.hiddenElements.singleDate .setAttribute("type", "hidden");
    			this.hiddenElements.singleDate .setAttribute("name", this.name+"[singleDate]");
    			myForm.appendChild(this.hiddenElements.singleDate );
				
				this.syncValues();
		}
		
	},
	getValue : function(){
		//this.parseDate(Ext.form.DateField.superclass.getValue.call(this)) || "";
		return this._value;
	},

	setValue : function(date){
		//Ext.form.DateField.superclass.setValue.call(this, this.formatDate(this.parseDate(date)));
		this._value=date;
		this.initHiddenElements();
		var inText='';
		if(this._value.type){
			this._value.type=parseInt(this._value.type);
		}
		switch(this._value.type){
			case 10:
				inText=this.strings.today;
			break;
			case 11:
				inText=this.strings.thisWeek;
			break;
			case 12:
				inText=this.strings.thisMonth;
			break;
			case 13:
				inText=this.strings.any;
			break;
			case 14:
				inText=this.strings.last3Days;
			break;
			case 15:
				inText=this.strings.lastMonth;
				break;
			case 2:
				inText=this._value.fromDate + " - "+this._value.toDate;
				break;
			case 1:
				inText=this._value.singleDate;
				break;
				
		}
		Ext.form.TextField.superclass.setValue.apply(this,[inText]);
		
	},

	parseDate : function(value){

		if(!value || Ext.isDate(value)){
			return value;
		}

		var v = Date.parseDate(value, this.format);

		if(!v && this.altFormats){

			if(!this.altFormatsArray){
				this.altFormatsArray = this.altFormats.split("|");
			}

			for(var i = 0, len = this.altFormatsArray.length; i < len && !v; i++){
				v = Date.parseDate(value, this.altFormatsArray[i]);
			}
		}
		return v;
	},

	formatDate : function(date){
		return Ext.isDate(date) ? date.dateFormat(this.format) : date;
	},
	validate : function(value){
		return true;
	}
});
Ext.reg('advanceddatefield', Ntp.form.AdvancedDateField);
