$(document).ready(function() {
	$("#mobile, #zip").keyup(function() {
		this.value = this.value.replace(/[^0-9\.]/g, '');
	});

	$('#update_profile').bind("click", function(e) {
		e.preventDefault();
		var f_name = $("#f_name").val();
		var l_name = $("#l_name").val();
		var new_password = $("#new_password").val();
		var confirm_password = $("#confirm_password").val();
		var sex = $("#sex").val();
		var birthdate_month = $("#birthdate_month").val();
		var birthdate_day = $("#birthdate_day").val();
		var birthdate_year = $("#birthdate_year").val();
	
		var current_city = $("#current_city").val();
		var hometown = $("#hometown").val();
		var mobile = $("#mobile").val();
		var address_1 = $("#address_1").val();
		var address_2 = $("#address_2").val();
		var city_town = $("#city_town").val();
		var state = $("#state").val();
		var zip = $("#zip").val();
		var country = $("#country").val();
		var website = $("#website").val();

		//Remove all errors
		$("#f_name_error").html('&nbsp;');
		$("#l_name_error").html('&nbsp;');
		$("#new_password_error").html('&nbsp;');
		$("#confirm_password_error").html('&nbsp;');
		$("#sex_error").html('&nbsp;');
		$("#birthdate_error").html('&nbsp;');

		if(f_name.length < 2 ) {
			$("#f_name_error").html('first name is too short').addClass('error_message');
			$("#update_status").html('');
			return false;
		}
		if(l_name.length < 2 ) {
			$("#l_name_error").html('last name too short').addClass('error_message');
			$("#update_status").html('');
			return false;
		}
		if(f_name == 'First Name') {
			$("#f_name_error").html('enter your first name').addClass('error_message');
			$("#update_status").html('');
			return false;
		}
		if(l_name == 'Last Name') {
			$("#l_name_error").html('enter your last name').addClass('error_message');
			$("#update_status").html('');
			return false;
		}

		if(new_password.length != 0 && (new_password !=  confirm_password)) {
			$("#confirm_password_error").html('new passwords do not match').addClass('error_message');
			$("#confirm_password").focus();
			$("#update_status").html('');
			return false;
		}
		if(sex == -1) {
			$("#sex_error").html('enter your sex').addClass('error_message');
			$("#sex").focus();
			$("#update_status").html('');
			return false;
		}
		if(birthdate_month == 0) {
			$("#birthdate_error").html('enter the month of your birthdate').addClass('error_message');
			$("#birthdate_month").focus();
			$("#update_status").html('');
			return false;
		}
		if(birthdate_day == 0) {
			$("#birthdate_error").html('enter the day of your birthdate').addClass('error_message');
			$("#birthdate_day").focus();
			$("#update_status").html('');
			return false;
		}
		if(birthdate_year == 0) {
			$("#birthdate_error").html('enter the year of your birthdate').addClass('error_message');
			$("#birthdate_year").focus();
			$("#update_status").html('');
			return false;
		}
		
		$("#update_status").html('');
	
		_lag.hover_box('#posting');	
		$.post("/includes/update_profile.php", {
			'f_name' : f_name,
			'l_name' : l_name,
			'new_password' : new_password,
			'sex' : sex,
			'birthdate_month' : birthdate_month,
			'birthdate_day' : birthdate_day,
			'birthdate_year' : birthdate_year,
			'current_city' : current_city,
			'hometown' : hometown,
			'mobile' : mobile,
			'address_1' : address_1,
			'address_2' : address_2,
			'city_town' : city_town,
			'state' : state,
			'zip' : zip,
			'country' : country,
			'website' : website
			}, function(data) {
				if(data == 'success') {
					_lag.hover_box('#posted');	
					setTimeout(function(){window.location.reload();}, 1000);
				} else {
					$(_lag.target).html(_lag.temp);
					$('#hover_box').css({'display' : 'none'});
					$('#modal-overlay').css({'display' : 'none'});
					_lag.popped = false;
					$("#update_status").html(data);
				}
		});
		return false;
	});
	$('#update_profile_image').bind("click", function(e) {
		if($('#profile_picture').val().length == 0 || $('#profile_picture').val() == null) {
			$('#update_status').html('no image selected.').addClass('error');
			$('#profile_picture').focus();
			return false;
		}
		_lag.hover_box('#posting');
		return true;
	});
});
