/* Start generic components on load */
window.addEvent('domready', function() {
	// collapse and expand elements
	initCollapseExpand();
	// field helps
	initFieldHelp();
	initImageBox();
});


/*============== SLIDER FOR A SINGLE IMAGE-TEXT */
function initSlider(speed) {
	list = $ES("div").filterByClass('slideArea');
	for(i=0;i<list.length;i++) {
		sl = new eventSlider(list[i],{'duration':speed});
	}

}

function initCollapseExpand() {
	list = $ES("div").filterByClass('expandCollapse');
	for(i=0;i<list.length;i++) {
		sl = new collapseBox(list[i]);
		sl.fire();
	}

}

function initDatepicker() {
	list = $ES("div").filterByClass('date');
	for(i=0;i<list.length;i++) {
		sl = new datePicker(list[i]);
		sl.check();
	}

}

function initFieldHelp() {
	list =  $E('body').getElements('img[class=fieldHelp]');
	if(list.length) {
		for(i=0;i<list.length;i++) {
			fld = new fieldHelp(list[i]);
		}
	}
}

function initImageBox() {
	list =  $E('body').getElements('a[class=fullSize]');
	if(list.length) {
		for(i=0;i<list.length;i++) {
			fld = new imageBox(list[i]);
		}
	}
}




/*###################################################################*/

var eventSlider = new Class({
	options: {
		duration: 200
	},

	initialize: function(elm, options){
		this.setOptions(options);
		this.box = elm.getElement('div[class^=eventSlider]');
		this.direction = (this.box.hasClass('vertical')) ? 'top' : 'left';
		this.slider = elm.getElement('div[class=eventSliderBox]');
		this.animation = new Fx.Style(this.slider, this.direction, {duration:this.options.duration,transition: Fx.Transitions.Quad.easeIn});
		elm.addEvent('mouseenter', this.slideIn.bindWithEvent(this));
		elm.addEvent('mouseleave', this.slideOut.bindWithEvent(this));
	},
	slideIn: function() {
		p = (this.direction == 'left') ? this.box.getCoordinates().width : this.box.getCoordinates().height;
		this.animation.stop();
		this.animation.start(-p);
	},
	slideOut: function() {
		this.animation.stop();
		this.animation.start(0);
	}
});

eventSlider.implement(new Options, new Events);



/*########################################## POSTCODE CLASS*/
var postcode = new Class({
	initialize: function(id){
		this.hasCode = false;
		this.id = id;
		this.input = $(this.id);
		this.confirmation = $(this.id+"_confirmation");
		if(this.input) {
			this.input.addEvent('keyup', this.check.bindWithEvent(this));
			this.input.addEvent('blur', this.onBlur.bindWithEvent(this));
			if(this.input.getValue()) this.getPostCode(this.input.getValue());
		}

	},

	check: function(event) {
		v = this.input.getValue();
		this.input.value = v.replace(/[^0-9]/gi,"");
		this.getPostCode(this.input.getValue());
	},

	onBlur: function(event) {
		if(!this.hasCode && this.input.getValue()) this.setConfirmation(translate('NOCITY'),true);
	},

	removeBadChars: function(str) {
		var newStr = "";
		if(str.length) {
			for(i=0;i<str.length;i++) {
				c = str.charAt(i).charCodeAt();
				if (c > 47 && c < 58) newStr += str[i];
			}
		}
		return newStr;
	},

	getPostCode: function(index) {
		if(this.confirmation) {

			if(index.length == 4) {
				arr = zipArray[index];
				if(arr) {
					if(arr.length>1) {
						this.setConfirmation(this.createSelect(arr));
					}
					else {
						this.setConfirmation(arr[0]);
						this.createHidden(arr[0]);
					}
					this.hasCode = true;
				}
				else {
					this.setConfirmation(translate('NOCITY'),true);
					this.removeHidden();
					this.hasCode = false;
				}
			}
			else {
				this.setConfirmation("");
				this.removeHidden();
				this.hasCode = false;
			}

		}
	},

	createSelect: function(arr) {

		select = new Element('select').setProperties({id:this.id+'_city',name:this.id+'_city'});
		foption = new Element('option').setProperties({value:'0'}).appendText(translate('MAKECHOISE')).injectInside(select);
		for(i=0;i<arr.length;i++) {
			city = arr[i];
			new Element('option').setProperties({value:city}).appendText(city).injectInside(select);
		}
		return select;
	},

	createHidden: function(str) {
		this.removeHidden();
		h = new Element('input').setProperties({id:this.id+'_city',type:'hidden',name:this.id+'_city',value:str}).injectAfter(this.confirmation);
	},


	removeHidden: function() {
		hidden = $(this.id+'_city');
		if(hidden) hidden.remove();
	},

	setConfirmation: function(str,error) {
		pre = this.confirmation.getElement('span');
		confirmation = new Element('div').addClass('confirmation').setProperty('id','confirmation_'+this.id);
		span = new Element('span').appendText(pre.getText()).injectInside(confirmation);
		if(typeof(str) == 'string') {
			insert = new Element('span');
			if(error) insert.addClass('error');
			insert.setText(str);
		}
		else {
			insert = str;
		}
		insert.injectInside(confirmation);
		//arr = this.confirmation.getChildren();
		//arr.each(function(item, index){ item.remove();});
		this.confirmation.empty();
		confirmation.injectInside(this.confirmation);

	}
});

/*########################################## RADIO LIST: To use for subordinates*/
var radioList = new Class({
	initialize: function(formId,name) {
		this.sub = new Array();
		this.form = $(formId);
		this.name = name;
		this.control = this.form[this.name]; // the radiolist
		this.controlList = new Array();
		this.queu = new Array();

		this.controlList = this.form.getElements('input[name='+this.name+']');
		for(i=0;i<this.controlList.length;i++) {
			this.controlList[i].addEvent('click', this.fire.bindWithEvent(this));
		}
	},

	fire: function() {
		active = false;
		this.queu = new Array();
		for(i=0;i<this.control.length;i++) {
			if(this.control[i].checked) {
				active = this.control[i].id;
				break;
			}
		}

		if(this.sub.length) {
			for(i=0;i<this.sub.length;i++) {
				sub = this.sub[i];
				if(sub.master == active) {
					// put object in queu, must be fired last
					this.queu.push(sub);
					//if(sub.type == 'showhide') showHide(sub.obj,true);
					//else setDisabled(sub.obj,true);
				}
				else {
					// disable or hide
					if(sub.type == 'showhide') showHide(sub.obj,false);
					else setDisabled(sub.obj,false);
				}
			}
		}
		this._fireQueu();


	},

	_fireQueu: function() {
		for(i=0;i<this.queu.length;i++) {
			sub = this.queu[i];
			if(sub.type == 'showhide') showHide(sub.obj,true);
			else setDisabled(sub.obj,true);
		}
	},

	addSubordinate: function() {
		type = (arguments[2]) ? arguments[2] : false;
		object = new String(arguments[0]);
		obArr = object.split(',');
		for(i=0;i<obArr.length;i++) {
			this.sub.push({'obj':$(obArr[i]), 'master':arguments[1], 'type':type});
		}

	}
})

/*########################################## CHECKBOX LIST: To use for subordinates*/
var checkBox = new Class({
	initialize: function(id) {
		this.sub = new Array();
		this.control = $(id); // the select
		if(this.control) {
			this.control.addEvent('click', this.fire.bindWithEvent(this));
		}
	},

	fire: function() {
		if(this.control) {
			active = (this.control.checked) ? true : false;
			for(i=0;i<this.sub.length;i++) {
				sub = this.sub[i];
				if(sub.type == 'showhide') showHide(sub.obj,active);
				else setDisabled(sub.obj,active);
			}

		}

	},

	addSubordinate: function() {
		//arguments: str sub_id [, str disable|visibility]
		type = (arguments[1]) ? arguments[1] : false;
		this.sub.push({'obj':$(arguments[0]), 'type':type});
	}
})

/*########################################## SELECT LIST: To use for subordinates*/
var dropdownList = new Class({
	initialize: function(id) {
		this.sub = new Array();
		this.control = $(id); // the select
		if(this.control) {
			this.control.addEvent('change', this.fire.bindWithEvent(this));
		}
	},

	fire: function() {
		if(this.control) {
			active = new Array();
			for(i=0;i<this.control.options.length;i++) {
				if(this.control.options[i].selected) {
					active.push(this.control.options[i].value);
				}
			}
			if(this.sub.length && active.length) {
				for(i=0;i<this.sub.length;i++) {
					sub = this.sub[i];
					for(j=0;j<active.length;j++) {
						if(sub.master == active[i]) {
							setDisabled(sub.obj,true);
						}
						else {
							setDisabled(sub.obj,false);
						}
					}
				}
			}
		}

	},

	addSubordinate: function() {
		//arguments: str sub_id, str option_value [, str disable|visibility]
		type = (arguments[2]) ? arguments[2] : false;
		this.sub.push({'obj':$(arguments[0]), 'master':arguments[1], 'type':type});
	}
})

function setDisabled(obj, status) {
	label = $E('body').getElement('label[for='+ obj.id +']');
	if(status) {
		$$(obj, label).removeClass("disabled");
		obj.removeProperty("disabled");
	}
	else {
		$$(obj, label).addClass("disabled");
		obj.setProperty("disabled","disabled");
	}
}

function showHide(obj, show) {
	if(show) {
		obj.setStyles({
   			display:'block'
		});
	}
	else {
		obj.setStyles({
   			display:'none'
		});
	}
}


/*########################################## FIELD HELP */
var fieldHelp = new Class({

	initialize: function(obj) {
		this.obj = obj;
		p = this.obj.getParent();
		this.obj.addEvent('click', this.fire.bindWithEvent(this,p.id));
		p.href="javascript:void(null)";
	},

	fire: function(event) {
		e = new Event(event);
		index = arguments[1];
		img = e.target;
		obj = img.getParent();
		this.create();
		pos = img.getCoordinates();
		this.container = new floatContainer({left:pos.left-pos.width,top:pos.top+pos.height,draggable:true});
		this.container.addEvent('onRemoveEnd', this.deleteContainer.bind(this));
		this.container.add(this.box);
		this.container.display();
	},

	close: function() {
		if(this.container) this.container.forceRemove();
	},

	deleteContainer: function() {
		if(this.container) this.container = false;
	},

	setHeader: function() {
		str = document.getElementById(index+"_head").innerHTML;
		if(str) {
			h = new Element('span');
			h.setHTML(str);
			h.injectInside(this.header);
		}
	},

	setBody: function() {
		str = document.getElementById(index+"_body").innerHTML;
		if(str) {
			b = new Element('p');
			b.setHTML(str);
			a = b.getElement('a[class=anchor]');
			if(a) a.remove();
			b.injectInside(this.body);
		}
	},

	create: function() {
		this.box = new Element('div',{'class': 'block help'});
		this.header = new Element('h3',{'class': 'title'}).injectInside(this.box);
		this.body = new Element('div',{'class': 'content'}).injectInside(this.box);
		this.setHeader();
		this.setBody();
		this.closeButton = new Element('span',{'class': 'close'}).injectInside(this.header);
		this.closeButtonText = new Element('span').appendText(translate('CLOSE')).injectInside(this.closeButton);
		this.closeButton.addEvent('click', this.close.bind(this));
	}
})


/*########################################## IMAGE BOX */
var imageBox = new Class({

	initialize: function(obj) {
		this.obj = obj;
		this.src = this.obj.getProperty('href');

		if(this.src) {
			this.obj.addEvent('click', this.fire.bindWithEvent(this));
		}
	},

	fire: function(event) {
		e = new Event(event);
		e.stop();
		this.create();
		this.container = new floatContainer({left:100,top:window.getScrollTop()+20,draggable:true,dropshadow:false});
		this.container.addEvent('onRemoveEnd', this.deleteContainer.bind(this));
		this.container.add(this.box);
		this.container.display();

	},

	close: function() {
		if(this.container) this.container.forceRemove();
	},

	deleteContainer: function() {
		if(this.container) this.container = false;
	},

	onImageLoad: function() {
		if(this.container) {
			this.container.resetIframe();
		}
	},

	create: function() {
		this.box = new Element('div',{'class': 'block imageBlock'});
		this.header = new Element('h3',{'class': 'title'}).appendText("").injectInside(this.box);
		this.body = new Element('div',{'class': 'content'}).injectInside(this.box);
		mycontent = new Element('img',{'src': this.src}).injectInside(this.body);
		if(window.ie) mycontent.addEvent('load',this.onImageLoad.bind(this));
		this.closeButton = new Element('span',{'class': 'close'}).injectInside(this.header);
		this.closeButtonText = new Element('span').appendText(translate('CLOSE')).injectInside(this.closeButton);
		this.closeButton.addEvent('click', this.close.bind(this));
	}
})



/*########################################## CONTAINER */

var floatContainer = new Class({

	options: {
		draggable: false,
		fitInWindow : true,
		dropshadow : 'right',
		left:0,
		top:0,
		transition:false,
		hideOnClickOutside:true,
		draggable:false,
		iframe:false
	},

	initialize: function(options) {
		this.setOptions(options);
		this.create();
	},

	create:function(){
		this.box = new Element('div').addClass('cf_container');
		this.body = new Element('div').addClass('cf_container_body').injectInside(this.box);
		// initialize drag method
		if (window.Drag && this.options.draggable) {
			this.drag = new Drag.Move(this.box, {
				onDrag: function() {
					if (window.ie6 || this.options.iframe) { this.iframe.setStyles({ left: this.box.style.left, top: this.box.style.top }); }
				}.bind(this)
			});
		}
	},

	add: function(content) {
		content.injectInside(this.body);
	},

	display: function() {

		if(!this.box) return;
		this.fireEvent('onDisplayStart', this);
		this.box.addEvent('mouseenter', this.mouseenter.bindWithEvent(this));
		this.box.addEvent('mouseleave', this.mouseleave.bindWithEvent(this));
		/*
		if(window.ie) {
			this.box.setStyles({
				position: 'absolute',
				top: -9999,
				left: -9999,
				zIndex: 1000
			});
		}
		*/
		this.box.injectInside(document.body);
		coord = this.box.getCoordinates();
		x = this.options.left;
		y = this.options.top;



		if(this.options.fitInWindow) {
			windowWidth = window.getWidth()+window.getScrollLeft();
			windowHeight = window.getHeight()+window.getScrollTop();
			if(x+coord.width > windowWidth) x = x-((x+coord.width)-windowWidth)-5;
			if(y+coord.height > windowHeight) y = y-((y+coord.height)-windowHeight)-5;
		}

		if(window.ie) {
			this.box.setStyles({
				top: y,
				left: x
			});
		}
		else this.box.setProperty('style','position:absolute;left:'+x+'px;top:'+y+'px;z-index:1000'); //Use setProperty for Opera

		if (window.ie6 || this.options.iframe) {
			this.iframe = new Element('iframe', {
				'styles': { height: coord.height + 'px', left: x + 'px', position: 'absolute', top: y + 'px', width: coord.width + 'px', zIndex: 998, border:'none'}
			}).injectInside(document.body);
			if(window.ie) this.iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
		}

		if(this.options.dropshadow) {
			offset = (this.options.dropshadow == 'right') ? 2 : -2;
			if(window.ie) {
			this.body.setStyles({
   				position: 'absolute',
   				zIndex: 1000
			});
			}
			else this.body.setProperty('style','position:absolute;z-index:1000'); //Use setProperty for Opera
			this.shadow = new Element('div', {
				'styles': { background: '#000000', height: coord.height + 'px', width: coord.width + 'px', left: offset + 'px', top: offset + 'px', position: 'absolute', zIndex: 999}
				}).setOpacity(0.2).injectInside(this.box);
		}

		document.addEvent('mousedown', this.remove.bind(this));
		this.fireEvent('onDisplayEnd', this);
		//this.box.injectInside(document.body);
	},

	resetIframe: function() {
		coord = this.box.getCoordinates();
		this.iframe.setStyles({width:coord.width,height:coord.height});
	},

	mouseenter: function() {
		this.hasFocus=true;
	},

	mouseleave: function() {
		this.hasFocus=false;
	},

	forceRemove: function() {
		 this._remove();
	},

	remove: function(event) {
		if(typeof this.box == "object") {
			e = new Event(event);
			target = e.target;
			if(this.box.hasChild(target)) return;
			this._remove();
		}

	},

	_remove: function() {
		if(typeof this.box == "object") {
			this.fireEvent('onRemoveStart', this);
			this.box.setProperty('style','display:none;'); // for Opera
			this.box.remove();
			this.box = false;
			if(this.iframe) {
				this.iframe.remove();
				this.iframe = false;
			}
			document.removeEvent('mousedown', this.remove);
			this.fireEvent('onRemoveEnd', this);
		}
	}

})

floatContainer.implement(new Options, new Events);

/*########################################## COLLAPSE EXPAND */

var collapseBox = new Class({

	options: {
		tricker:'expandCollapseTricker',
		box: 'box',
		animate:true
	},

	initialize: function(obj,options) {
		this.setOptions(options);
		this.boxHandler = obj.getFirst();
		if(obj.hasClass("noslide")) this.options.animate = false;
		this.handler = obj.getElement('[class='+ this.options.tricker +']');
		this.box = obj.getElement('div[class='+ this.options.box +']');
		if(this.handler && this.boxHandler && this.box) {
			//this.handler.addEvent('click', this.toggle.bindWithEvent(this));
			this.init();
			this.slide = new Fx.Slide(this.box);
		}
	},

	fire: function() {
		if(this.tag == 'input') {
			this.showhide();
		}
	},

	init: function() {
		if(this.handler) {
			this.tag = this.handler.getTag();
			switch(this.tag) {
				case 'span':
				this.handler.addEvent('click', this.toggle.bindWithEvent(this));
				break;

				case 'input':
				this.handler.addEvent('click', this.toggle.bindWithEvent(this));
				break;
			}
		}
	},


	// quick and dirty solution for refresh problem
	showhide: function() {
		if(this.handler.checked) {
			this.boxHandler.removeClass("closed").addClass("open");
			if(this.options.animate) this.slide.show();
		}
		else {
			if(this.options.animate)  {
				this.box.setStyle('display', 'block');
				this.slide.hide();
			}
			this.boxHandler.removeClass("open").addClass("closed");
		}
	},

	toggle: function(event) {
		//e = new Event(event);
		if(this.boxHandler.hasClass("closed")) {
			//lets open the box
			if(this.options.animate) this.slide.hide();
			this.boxHandler.removeClass("closed").addClass("open");
			if(this.options.animate) this.slide.slideIn();
		}
		else {
			//lets close the box
			if(this.options.animate)  {
				this.box.setStyle('display', 'block');
				this.slide.slideOut();
			}
			this.boxHandler.removeClass("open").addClass("closed");
		}
		//e.stop();
	}
})

collapseBox.implement(new Options, new Events);



/*########################################## DATE method Object */

var dateBase = {

	days: ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY', 'SATERDAY', 'SUNDAY'],
	months: ['JANUARY', 'FEBRUARY', 'MARCH', 'APRIL', 'MAY', 'JUNE', 'JULY', 'AUGUST', 'SEPTEMBER', 'OCTOBER', 'NOVEMBER', 'DECEMBER'],

	isLeapYear: function(d) {
		year = d.getFullYear();
		return ( ( (year%4==0)&&(year%100 != 0) ) || (year%400==0) ) ? true : false;
	},

	parseDateStr: function(str,limitArr) {
		date = false;
		limit = (limitArr) ? limitArr : [1000,9000];
		arr=str.split('-');
		// simple check
		if(arr.length == 3) {
			d = arr[0];
			m = arr[1];
			y = arr[2];
			if((y > limit[0] && y < limit[1]) && (m >= 1 && m <= 13) && (d >= 1 && m <= 31)) {
				nd = new Date();
				nd.setFullYear(y,m-1,d);
				if(nd) {
					date = (nd.getDate() == d && (nd.getMonth()+1) == m && nd.getFullYear() == y) ? nd : false;
				}
			}
		}
		return date;
	},

	dateToString: function(d) {
		return translate(this.days[this.getDayIndex(d.getDay())]).capitalize() + ' ' + d.getDate() + ' ' + translate(this.months[d.getMonth()]) + ' ' + d.getFullYear();
	},

	getDayIndex: function(i) {
		return (i==0) ? 6 : i-1;
	},

	isOutOfBoundary: function(d,boundary) {
		//cd = new Date();
		//cd.setFullYear(d.getFullYear(), d.getMonth(), d.getDate());

		check = new Date();
		check.setDate(check.getDate()+boundary);
		check = dateBase.createBlankDate(check.getFullYear(), check.getMonth(), check.getDate())
		if (boundary > 0) {
			return (d.getTime() < check.getTime()) ? true : false;
		}

		if (boundary < 0) {
			return (d.getTime() > check.getTime()) ? true : false;
		}

		return false;
	},

	createBlankDate: function(y,m,d) {
		d = new Date(y,m,d);
		d.setHours(0);
		d.setMinutes(0);
		d.setSeconds(0);
		return d;
	}


}



/*########################################## DATE */
var cf_date = new Class({
	options: {
		blocked: [],
		limit:[1900,2100],
		boundary: 0, // -1 past, 0 past + future, 1 future
		containerOptions : {
			fitInWindow : true,
			dropshadow : 'right',
			left:0,
			top:0,
			transition:false,
			hideOnClickOutside:true
		}
	},


	initialize: function(id,options) {
		this.setOptions(options);
		this.id = id;
		this.control = $(id);
		this.datepicker = $("dpr_"+this.id);
		this.confirmObj = $("confirm_"+this.id);
		if(this.datepicker) {
			this.datepicker.addEvent('click', this.createDatePicker.bindWithEvent(this));
		}
		this.control.addEvent('change', this.check.bindWithEvent(this));
		this.check();
	},

	createDatePicker: function() {
		this.cal = new cf_calendar({blocked: this.options.blocked,limit:this.options.limit,boundary:this.options.boundary}); // creates current date
		this.cal.setDate(this.control.value);
		this.cal.addEvent('onDateSelected', this.setDate.bind(this));
		c = this.cal.create();
		pos = this.datepicker.getCoordinates();
		this.container = new floatContainer({left:pos.left,top:pos.top+pos.height,draggable:true});
		this.container.add(c);
		this.container.display();
		this.container.addEvent('onRemoveEnd', this.removeDatePicker.bind(this));
	},

	setDate: function() {
		d = arguments[0];
		str = d.getDate() + '-' + (d.getMonth()+1) + '-' + d.getFullYear();
		this.control.value = str;
		this.check();
		this.container.forceRemove();
	},

	removeDatePicker: function() {
		this.cal = false;
		this.container = false;
	},

	check: function() {
		val = this.control.value;
		val.clean();
		if(val && val != "") {
			d = dateBase.parseDateStr(this.control.value,this.options.limit);
			if(d) {
				if(dateBase.isOutOfBoundary(d,this.options.boundary)) this.confirm(translate('DATEOUTOFRANGE'),'error');
				else this.confirm(dateBase.dateToString(d),'confirmation');
			}
			else {
				this.confirm(translate('BADDATE'),'error');
			}
		}
		else this.confirm();
	},

	confirm: function(str,type) {
		if(this.confirmObj) {
			this.confirmObj.setHTML('');
			if(str) {
				d = new Element('div');
				if(type=='error') d.addClass('error');
				else d.addClass('confirmation');
				d.appendText(str).injectInside(this.confirmObj);
				this.confirmObj.setStyle('display', 'block');
			}
			else {
				this.confirmObj.setStyle('display', 'none');
			}
			this.fireEvent('onConfirm', new Array(this.control.value,type));
		}

	}


})

cf_date.implement(new Options, new Events);

cf_staticDate = new Class({
	options: {
		add:0,
		type:'year'
	},

	initialize: function(id,object,options) {
		this.setOptions(options);
		this.placeholder = $(id);
		this.dateObject = object;
		this.dateObject.addEvent('onConfirm', this.fire.bind(this));
		this.dateObject.check();
	},

	fire: function() {
		str = arguments[0];
		type = arguments[1];
		inner = "...";
		if(type == 'confirmation') {
			date = dateBase.parseDateStr(str);
			y = date.getFullYear();
			m = date.getMonth();
			d = date.getDate();
			switch(this.options.type) {
				case 'year':
				y = y+this.options.add;
				break;

				case 'month':
				m = m+this.options.add;
				break;

				case 'day':
				d = d+this.options.add;
				break;

			}
			inner = dateBase.dateToString(new Date(y, m, d));
		}
		this.placeholder.setHTML(inner);

	}

})
cf_staticDate.implement(new Options, new Events);

var cf_table = new Class({

	initialize: function(id) {
		this.table = $(id);
		this.tricker = this.table.getElement('a[class=add]');
		if(this.tricker) {
			this.tricker.addEvent('click', this.addRow.bindWithEvent(this));
		}
	},

	addRow: function(event) {
		e = new Event(event);
		tbody = this.table.getElement('tbody');
		rows = tbody.getElements('tr');
		if(rows.length) {
			r = rows[rows.length-1];
			clone = r.clone();
			childs = clone.getElementsBySelector('input,label,select,textarea');

			if(childs.length) {
				for(i=0;i<childs.length;i++) {
					c = childs[i];
					tag = c.getTag();
					if(c.getProperty('id')) this.setIndex(c,'id');
					if(c.getProperty('name')) this.setIndex(c,'name');
					if(c.getProperty('for')) this.setIndex(c,'for');
					if(tag == 'input' || tag == 'select' || tag == 'textarea') this.resetInput(c,tag);
				}
				clone.injectInside(tbody);
			}

		}
		e.stop();
	},

	setIndex: function(elm, type) {
		prop = elm.getProperty(type);
		arr = prop.split('__');
		if(arr.length) {
			index = arr[arr.length-1].toInt();
			index = index+1;
			arr[arr.length-1] = '__' + index;
			str = "";
			for(j=0;j<arr.length;j++) {
				str += arr[j];
			}
			elm.setProperty(type,str);
			//this.resetInput(elm);
		}
	},

	resetInput: function(elm,tag) {
		switch(tag) {
			case 'input':
			elm.setProperty('value','');
			break;

			case 'textarea':
			elm.setProperty('value','');
			break;

			case 'select':
			elm.selectedIndex = 0;
			break;
		}
	}
})
cf_table.implement(new Options, new Events);




