window.addEvent('domready', function() {
var id = $('smcatid').value, Itemid = $('Itemid').value, count = $('count').value, start = $('addquestion'), log = $('time');
var url = 'index.php?option=com_smfaq&task=addquestion&format=raw&catid='+id+'&Itemid='+Itemid;
/* our ajax istance */
var ajax = new Ajax(url, { 
	update: log,
	method: 'post',
	onComplete: function() {
		log.removeClass('ajax-loading');
		var box = $('prot');
		$('t1').value=count
		$('question').addEvent('keyup', function(e) {
		a=$('question').value.length;
		if((a)>count)$('question').value=$('question').value.substring(0,count);
		$('t1').value=count-a;
		});
		if ($('updatecaptcha') != null) {
		$('updatecaptcha').addEvent('click', function() {
			$('captchaimg').set({src: $('captchaimg').src.replace(/&ac=\d+/g, '&ac='+new String(Math.floor(Math.random()*100000)))})
		})}
		if (box) {
		var fx = box.effects({transition: Fx.Transitions.Quint.easeOut});
		fx.start({
		}).chain(function() {
		this.start.delay(4000, this, {'opacity' : 0});
		}).chain(function() {
		box.style.display="none";
		this.start.delay(0100, this, {'opacity' : 1});
		box.remove();
	   });
		} else {
		start.style.display="none";
		$('QuestionForm').addEvent('submit', function(e) {
		new Event(e).stop();
		var box = $('box');
		box.empty().addClass('ajax-loading');
		box.style.display="block";
		this.send({
		   onComplete: function(response) {
				box.style.display="none";
				box.removeClass('ajax-loading');
				var res = Json.evaluate(response);
				res.items.each(function(field) {
				   if(field.status)
					 $(field.name).removeClass('error');
				   else {
				   	 var desc = new Element('div').setHTML(field.msg).injectAfter(box);
					 desc.addClass('errmsg');
					 var fx = desc.effects({transition: Fx.Transitions.Quint.easeOut});
					 fx.start({
					 }).chain(function() {
					 this.start.delay(2000, this, {'opacity' : 0});
					 $(field.name).addClass('error');
					 }).chain(function() {
					 desc.style.display="none";
					 this.start.delay(0100, this, {'opacity' : 1});
					 desc.remove();
					 });
				   }
				   
				 });
			  if(res.valid) {
				  var message =	$('QuestionForm').replaceWith(new Element('div').setHTML(res.msg));
				  message.addClass('okmsg');
				  var fx = message.effects({transition: Fx.Transitions.Quint.easeOut});
				  fx.start({
				  }).chain(function() {
				  this.start.delay(3000, this, {'opacity' : 0});
			      }).chain(function() {
				  this.start.delay(0100, this, {'opacity' : 1});
				  message.remove();
				  start.style.display="inline";
				 });
			}
			}
		});
	});
	}
	}
});

if (start) {
// start and stop click events
start.addEvent('click', function(e) {
	// prevent default
	new Event(e).stop(); 
	start.setStyle('font-weight', 'bold');
	log.empty().addClass('ajax-loading'); 
	ajax.request(); 
});
}

}); 




