window.addEvent('domready', function(){
	$$('input, textarea').each(function(el){
		el.addEvent('focus', function(){
			if($(this).getProperty('title') != null && $(this).value.trim() == $(this).getProperty('title').trim()){
				$(this).value = '';
			}
			$(this).addClass('active');
		});
		el.addEvent('blur', function(){
			if($(this).getProperty('title') != null && $(this).value.trim().length == 0){
				$(this).value = $(this).getProperty('title');
				$(this).removeClass('active');
			}
		});
		if(el.getProperty('title') != null && el.value.trim() != el.getProperty('title').trim()){
			el.addClass('active');
		}else if(el.getProperty('title') == null && el.value.trim() != ''){
			el.addClass('active');
		}
	})

	$$('select[preselected]').each(function(el){
		var options = $(el).getChildren('option[value]');
		for(var o=0; o<options.length; o++){
			if( options[o].value == $(el).getProperty('preselected') ){
				$(el).selectedIndex = o;
				break;
			}
		}
	});

	$$('form.validate').each(function(form){
		form.addEvent('submit', function(){
			return checkForm(this);
		});
	});

	$$('textarea').each(function(ta){
		ta.addEvent('keyup', function(){
			if($(this).getScrollSize().y.toInt() > $(this).getStyle('height').toInt()){
				$(this).setStyle('height', $(this).getScrollSize().y.toInt());
			}
		});
		if(ta.getScrollSize().y.toInt() > ta.getStyle('height').toInt()){
			ta.setStyle('height', ta.getScrollSize().y.toInt());
		}
	});

});

function showFormError(fi){
	if(!fi) return;
	$(fi).removeClass('valid');
	$(fi).addClass('invalid');
	if($('mark_'+fi.getProperty('id')) != null) $('mark_'+fi.getProperty('id')).setStyle('visibility', 'visible');
}

function hideFormError(fi){
	if(!fi) return;
	$(fi).removeClass('invalid');
	$(fi).addClass('valid');
	if($('mark_'+fi.getProperty('id')) != null) $('mark_'+fi.getProperty('id')).setStyle('visibility', 'hidden');
}

function clearFormError(fi){
	if(!fi) return;
	$(fi).removeClass('invalid');
	$(fi).removeClass('valid');
	if($('mark_'+fi.getProperty('id')) != null) $('mark_'+fi.getProperty('id')).setStyle('visibility', 'hidden');
}

function checkForm(f){
	if(!f || f == null) return false;
	var items = $$('form#' + f.getProperty('id') + ' .required');
	if(items.length == 0) return true;
	var success = true;
	var first_error = false;
	for(var i=0; i<items.length; i++){
		var item = items[i];
		var item_error = false;
		if(item.hasClass('notempty')){
			if(item.value.trim().length == 0){
				showFormError(item);
				success = false;
				item_error = true;
			}else if(item.getProperty('title') != null && item.value.trim() == item.getProperty('title').trim()){
				showFormError(item);
				success = false;
				item_error = true;
			}else{
				hideFormError(item);
			}
		}
		if(!item_error && item.hasClass('notnull')){
			if(item.value == '0'){
				showFormError(item);
				success = false;
				item_error = true;
			}else{
				hideFormError(item);
			}
		}
		if(!item_error && item.hasClass('mail')){
			val=/\w.+@\w.+\.\w.+/;
			if(!val.test(item.value)){
				showFormError(item);
				success = false;
				item_error = true;
			}else{
				hideFormError(item);
			}
		}
		if(!success && !first_error) first_error = item;
	}
	if(!success){
		if(first_error.getCoordinates().top.toInt() < window.getScroll().y.toInt()){
			var myFx = new Fx.Scroll(window, {'offset':{'x':0, 'y':-20}}).toElement(first_error, 'y');
		}
		first_error.focus();
		if(first_error.nodeName == 'input' && (first_error.getProperty('type').toLowerCase() == 'text' || first_error.getProperty('type').toLowerCase() == 'password')){
			first_error.select();
		}
	}
	return success;
}

