/*
 * Ext JS Library 1.1 Beta 1
 * Copyright(c) 2006-2007, Ext JS, LLC.
 * licensing@extjs.com
 * 
 * http://www.extjs.com/license
 */
 
Ext.BLANK_IMAGE_URL = '/media/inc/resources/images/default/s.gif';

var Quotations = {

	elname: 'tda',
	charttypes: ['QuotationChart', 'TradingVolumesChart'],
	images: [],
	properties: {
		'areaTypes': 'micex',
		'fromDate': null,
		'toDate': null,
		'langType': null
	},

	init: function() {
		/*
		var params = location.search.split('&'), param;
		for( var i=0; i < params.length; i++ ) {
			if( params[i] == '' ) continue;
			param = params[i].split('=');
			this.properties[ param[0].replace(this.elname+'.','') ] = param[1];
		}
		*/
	
		Ext.QuickTips.init();
		Ext.form.Field.prototype.msgTarget = 'side';    
		
		for( var i in this.charttypes ) {
			this.images[i] = document.getElementById(this.charttypes[i]);
		}
		
		var now = new Date();				
		
		var dateEnd = new Ext.form.DateField({                        
            width:115,
			value: this.properties['quotes.toDate'] || this.properties['toDate'] || new Date( now ), // (this.properties['toDate'] == null) ? new Date( now ) : this.properties['toDate'],
            allowBlank:true,
			format:'d.m.Y'
        });
		dateEnd.applyTo('toDate');
		if (this.properties['toDate'] == null)
		{
		
		this.properties['toDate'] = dateEnd.getRawValue();
		}
		
		now.setMonth( now.getMonth() - 3 );
		
		var dateStart = new Ext.form.DateField({                        
            width:115,
			value: this.properties['quotes.fromDate'] || this.properties['fromDate'] || new Date( now ), // (this.properties['fromDate'] == null) ? new Date( now ) : this.properties['fromDate'],
            allowBlank:true,
			format:'d.m.Y'
        });
		dateStart.applyTo('fromDate'); 
		if (this.properties['fromDate'] == null)
		{
		
		this.properties['fromDate'] = dateStart.getRawValue();
		}
						
		this.toString(0);
		this.toString(1);
		
		if( location.search == '' ) {
			location.replace( this.getUrl() );
		}
	},
	refresh: function( form ) {
		var elem;
		for( var i=0; i < form.elements.length; i++ ) {
			elem = form.elements[i];
			if( elem.nodeName == 'SELECT' ) {
				// TODO: add collector of selected items in multiselect form field
				this.properties[ elem.name ] = elem.value;
			} else {
				this.properties[ elem.name ] = elem.value;
			}			
		}
		//this.toString(0);
		//this.toString(1);
		this.redirect();
		return false;
	},
	getUrl: function() {
		var params	 = '';
		/*
		for( var i in this.properties ) {
			params += ( params == '' ? '?' : '&' ) + i + '=' + this.properties[i];
		}
		*/
		var p = this.properties;
		params = "?";
		params += 'quotes.fromDate='+ fDate(p['quotes.fromDate'] || p['fromDate']) +'&quotes.toDate='+ fDate(p['quotes.toDate'] || p['toDate']);
		params += '&quotes.areaTypes='+ (p['quotes.areaTypes'] || p['areaTypes']) +'&quotes.langType='+ (p['quotes.langType'] || p['langType']);
			
		return location.pathname + params;
		
		function fDate(str_ddmmyyyy) {
			var dmy = str_ddmmyyyy.split(".");
			if (dmy[0].length==1) dmy[0] = "0"+ dmy[0];
			if (dmy[1].length==1) dmy[1] = "0"+ dmy[1];
			return dmy[2] + dmy[1] + dmy[0];

		}
	},
	redirect: function() {		
		location.href = this.getUrl();		
	},
	toString: function( type ) {
		var result = '?chartType=' + this.charttypes[type];
		for( var i in this.properties ) {
			result += '&' + i + '=' + this.properties[i];
		}
		//this.images[type].src = '/ChartServlet/' + result;
	}
};