var focus = false;
var validator = new FormValidator({
	onFailure: function(id, message){
		var ct = message;
		ct = '<div class="error_message_form">' + '&nbsp;&nbsp;&nbsp;' + ct + '</div>';
		Element.insert($(id).up(), {top: ct});
		if(!focus){
			$(id).focus();
			focus = true;
		}
	}
});

function initialize(){
	initialize_err();
	Event.observe($('submit_create'), 'click', submit_form);
	Event.observe($('country'), 'change', select_country);
	Event.observe($('couty'), 'change', select_region);
	validator.add({
		id : ['username', 'password','password_confirm','email','name', 'firstname','birthdate_day','birthdate_month','birthdate_year','activities', 'school'],
		type: 'required',
		message: "Acest câmp este obligatoriu."
	});
	validator.add({
		id : 'email',
		type: 'email',
		message: 'Email-ul completat nu are o formă corectă.'
	});
	validator.add({
		id : 'password_confirm',
		type: 'equals|id=password',
		message: 'Parolele nu coincid!'
	});
	validator.add({
		id : 'username',
		type: "regexp|pattern=^[a-zA-Z0-9\-_]*$",
		message: 'Utilizatorul poate conține doar litere, cifre și caracterele "-" și "_"'
	});
	validator.register('userexists',validate_username_existance);
	validator.register('emailexists',validate_email_existance);
	validator.add({
		id : 'username',
		type: "userexists",
		message: 'Utilizatorul ales există deja in baza noastră de date.<br />Vă rugăm să alegeți altul.'
	});
	validator.add({
		id : 'email',
		type: "emailexists",
		message: 'Adresa de email există deja in baza noastră de date. <br />Vă rugăm să completați alta.'
	});
	validator.add({
		type: "least|name=sex",
		id: "sex1",
		message:'Acest câmp este obligatoriu.'
	});
	validator.add({
		type: "least|name=terms",
		id: "terms",
		message:'Acest câmp este obligatoriu.'
	});
}
function validate_username_existance(id, parameters){
	var ret = false;
	new Ajax.Request('/index/ajax-check-username/format/json', {
		asynchronous: false,
		method: 'post',
		parameters: {
			username: $(id).value
		},
		onSuccess: function(transport){
			var data = transport.responseJSON;
			if(data.exists == '0') {
				ret = true;
			}
		}
	});

	return ret;
}
function validate_email_existance(id, parameters){
	var ret = false;
	new Ajax.Request('/index/ajax-check-email/format/json', {
		asynchronous: false,
		method: 'post',
		parameters: {
			email: $(id).value
		},
		onSuccess: function(transport){
			var data = transport.responseJSON;
			if(data.exists == '0') {
				ret = true;
			}
		}
	});

	return ret;
}
function submit_form(){
	clear_errors();
	if(validator.validateAll()){
		$('birthdate').value = compose_date();
		$('create_form').submit();
	}else{

	}

}
function clear_errors(){
	var ed = $A($$('.error_message_form'));
	ed.each(function(item){
		Element.remove(item);
	});
	focus = false;
}

function getSelectedIndex(a,value){
	aR=a.getElementsByTagName('option');
	for(i=0;i<aR.length;i++){
		if(aR[i].value==value) return i;
	}
	return 0;
}

Event.observe(window,'load',select_country_first);

function select_country_first(){
	if (country == 176) {
		//user has chosen Romania
		//so we will give him a list of regions and cities
		$('other_region').hide();
		$('couty').show();
		$('other_city').hide();
		$('city').show();
		$('couty').selectedIndex=getSelectedIndex($('couty'),mregion);
		new Ajax.Request('/index/ajax-get-cities-of-region/format/html', {
			parameters: {
				region: parseInt(mregion)
			},
			onSuccess : function(transport){
				$('city_div').innerHTML=transport.responseText;
				//alert(transport.responseText);
				$('city').selectedIndex=getSelectedIndex($('city'),nelcity);
			}
		});
	}else{
		// let him fill in the region and the city in textboxes
		$('other_region').show();
		$('couty').hide();
		$('other_city').show();
		$('city').hide();
	}
}

function select_country(event){
	var elem = event.target;
	if (parseInt(elem.value) == 176) {
		//user has chosen Romania
		//so we will give him a list of regions and cities
		$('other_region').hide();
		$('couty').show();
		$('other_city').hide();
		$('city').show();
	}else{
		// let him fill in the region and the city in textboxes
		$('other_region').show();
		$('couty').hide();
		$('other_city').show();
		$('city').hide();
	}
}
function select_region(event){
	var elem = event.target;
	if (!$('country') || parseInt($('country').value) != 176) return;
	new Ajax.Updater('city_div', '/index/ajax-get-cities-of-region/format/html', {
		parameters: {
			region: parseInt(elem.value)
		}
	});

}
function compose_date(){
	var day = parseInt($('birthdate_day').value);
	var mon = parseInt($('birthdate_month').value);
	var year = parseInt($('birthdate_year').value);
	if (mon < 10) {
		mon = '0' + mon;
	}
	var date = day + "-" + mon + "-" + year;
	return date;
}
Event.observe(window,'load', initialize);
