function findCity(obj) { //find city list based on country dropdown

	var id = obj.options[obj.selectedIndex].value;

	$('#fk_city_id').dropdown({
		url:'/remote/get_cities.php',
		type: 'post',
		dataType: 'json',
		params: {fk_country_id:id},
		firstoption:{id:'', name:'- Select a city -'},
		success: function () {
		//empty - nothing happens on success - a city list will be loaded in by json
		}
	});

}

function doRegister(){
	$("#message").html('');
	if ( $("#registerForm").valid() ){
		$("#message").html('processing...');
		$("#registerForm").ajaxSubmit({
			url: '/register/register_process.php',
			type: 'post',
			dataType: 'json',
			success: function(json) {
				$("#message").html('received server response...');
				var jsonData = eval(json); // IE6 and Safari compatibility
				if (jsonData['error'] != '') alert(jsonData['error']);
				if (jsonData['status'] == true || jsonData['status'] == 'true') {
					$("#message").html('loading next page...');
					if (jsonData['add'] == true || jsonData['add'] == 'true') {
						top.location.href = '/register/confirm.php?add=true';
					}else{
						top.location.href = '/register/confirm.php';	
					}
					
				}
			}
		});
	}
}

$(document).ready(function(){
	//validate form
	$('#registerForm').validate({
		rules: {
			user_fullname: {
				required: true,
				minlength: 3,
				maxlength: 70
			},
			user_email: {
				required: true,
				email: true,
				minlength: 5
			},
			user_email_confirm: {
				required: true,
				email: true,
				minlength: 5,
				equalTo: "#user_email"
			},
			user_password: {
				required: true,
				minlength: 3
			},
			user_password_confirm: {
				required: true,
				minlength: 3,
				equalTo: "#user_password"
			}
		},
		messages: {
			user_fullname: {
				required: '<br />Please fill in the Press Item title.',
				minlength: $.format('<br />At least {0} characters required!'),
				maxlength: $.format('<br />A maximum of {0} characters only!')
			},
			user_email: {
				required: 'Please fill in your email address.',
				minlength: $.format('At least {0} characters required!')
			},
			user_email_confirm: {
				required: 'Please confirm your email address.',
				minlength: $.format('At least {0} characters required!')
			},
			user_password: {
				required: 'Please fill in your password.',
				minlength: $.format('At least {0} characters required!')
			},
			user_password_confirm: {
				required: 'Please fill in your password.',
				minlength: $.format('At least {0} characters required!')
			}
		}
	});
	
	// check if confirm password is still valid after password changed
	$("#user_password").blur(function() {
		$("#user_password_confirm").valid();
	});
	//check same for email
	$("#user_email").blur(function() {
		$("#user_email_confirm").valid();
	});

	//run city function if there is an id for country
	findCity(document.getElementById('fk_country_id'));
	
});
