$(document).ready(function() {
	$('#createFrm').submit( function(e){
		$('.form-field input:text,input:password,input:radio,textarea').each(function(){
			$(this).removeClass('error');
			var $img = $(this).parent().parent().find('img:first');
			$img.attr("src", ABSPATH + "pics/spacer.gif");
		});
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$('.large-button').attr('disabled', 'disabled');
		e.preventDefault();
		$.ajax({
			url: ABSPATH + "ajax/ajax.account.create.php",
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);
					
				}else{
					alert(data.returnMsg);
					window.location.href = data.returnUrl;
				}
				$('.large-button').attr('disabled', '');
				$.unblockUI();
			}
		});
	});
	$('#updateFrm').submit( function(e){
		$('.form-field input:text,input:password,input:radio,textarea').each(function(){
			$(this).removeClass('error');
			var $img = $(this).parent().parent().find('img:first');
			$img.attr("src", ABSPATH + "pics/spacer.gif");
		});
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$('.large-button').attr('disabled', 'disabled');
		e.preventDefault();
		$.ajax({
			url: ABSPATH + "ajax/ajax.account.settings.php",
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);
					
				}else{
					alert(data.returnMsg);
					window.location.reload();
				}
				$('.large-button').attr('disabled', '');
				$.unblockUI();
			}
		});
	});
	$('#imgremove').click( function(e){
		if(!confirm("Vill du ta bort den nuvarande bilden?\n")) return false;
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$.ajax({
			url: ABSPATH + "ajax/ajax.avatar.php",
			type: "POST",
			data: "removeImg=true&table=school_user&userid=" + $('input[name=user_id]').val(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);

				}else{
					$('#imgbrowse').show();
					$('#imgremove').hide();
				}
				$('#user_avatar').attr("src", data.fileName);
				$('#imgbrowse').html("<strong>Ladda upp bild</strong>");
				$.unblockUI();
			}
		});
	});

	$('#resetFrm1,#resetFrm2').submit( function(e){
		$('.form-field input:text,input:password,textarea').each(function(){
			$(this).removeClass('error');
			var $img = $(this).parent().parent().find('img:first');
			$img.attr("src", ABSPATH + "pics/spacer.gif");
		});
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$('.large-button').attr('disabled', 'disabled');
		e.preventDefault();
		$.ajax({
			url: ABSPATH + "ajax/ajax.account.reset.passwd.php",
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);
					$('.large-button').attr('disabled', '');
				}else{
					alert(data.returnMsg);
					window.location.href = ABSPATH;
				}
				$.unblockUI();
			}
		});
	});
	$('#newPasswdFrm').submit( function(e){
		$('.form-field input:text,input:password,textarea').each(function(){
			$(this).removeClass('error');
			var $img = $(this).parent().parent().find('img:first');
			$img.attr("src", ABSPATH + "pics/spacer.gif");
		});
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$('.large-button').attr('disabled', 'disabled');
		e.preventDefault();
		$.ajax({
			url: ABSPATH + "ajax/ajax.account.change.passwd.php",
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);
					$('.large-button').attr('disabled', '');
				}else{
					alert(data.returnMsg);
					window.location.href = data.returnUrl;
				}
				$.unblockUI();
			}
		});
	});
	$('#loginFrm1,#loginFrm2').submit( function(e){
		$('.form-field input:text,input:password,textarea').each(function(){
			$(this).removeClass('error');
			var $img = $(this).parent().parent().find('img:first');
			$img.attr("src", ABSPATH + "pics/spacer.gif");
		});
		$.blockUI({ theme: true, title: $('div#loadMsg p#loadTitle').html(), message: $('div#loadMsg p#loadBody') });
		$('.medium-button').attr('disabled', 'disabled');
		e.preventDefault();
		$.ajax({
			url: ABSPATH + "ajax/ajax.account.login.php",
			type: "POST",
			data: $(this).serialize(),
			dataType: "json",
			success: function(data){
				if(data.hasError){
					throwError(data);
					$('.medium-button').attr('disabled', '');
				}else{
					window.location.href = data.returnUrl;
				}
				$.unblockUI();
			}
		});
	});
	$('input').focus(function(){
		$('.hlp').each(function(){
			$(this).html("");
		});
	});
	$('#acode1,#acode2,#acode3').focus(function(e){
		var $obj = $(this).parent().find('div');
		$obj.html($('#divCountryList').html());
		$obj.fadeIn('slow');
		$('.theList').addClass('highlight').removeClass('highlight', 50);
	});

	$("input[name='user_postnum']").autocomplete({
		source: function(request, response) {
			$.ajax({
				url: "http://ws.geonames.org/postalCodeSearchJSON",
				dataType: "jsonp",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 15,
					postalcode_startsWith: request.term,
					country: [
						'SE',
						'NO',
						'FI',
						'DK'
					],
					lang: 'sv'
				},
				success: function(data) {
					response($.map(data.postalCodes, function(item) {
						return {
							label: item.postalCode + ", " + item.placeName + (item.adminName1 ? ", " + item.adminName1 : ""),
							place: item.placeName,
							country: item.countryCode,
							value: item.postalCode
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
			$(this).val(ui.item.value);
			$("input[name='user_postaddr']").val(ui.item.place);
			$("select[name='user_country']").val(ui.item.country);
		},
		open: function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close: function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});
	$("input[name='user_postaddr']").autocomplete({
		source: function(request, response) {
			$.ajax({
				url: "http://ws.geonames.org/searchJSON",
				dataType: "jsonp",
				data: {
					featureClass: "P",
					style: "full",
					maxRows: 15,
					name_startsWith: request.term,
					country: [
						'SE',
						'NO',
						'FI',
						'DK'
					],
					lang: 'sv'
				},
				success: function(data) {
					response($.map(data.geonames, function(item) {
						return {
							label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
							country: item.countryCode,
							value: item.name
						}
					}))
				}
			})
		},
		minLength: 2,
		select: function(event, ui) {
			$(this).val(ui.item.value);
			$('select[name=user_country]').val(ui.item.country);
		},
		open: function() {
			$(this).removeClass("ui-corner-all").addClass("ui-corner-top");
		},
		close: function() {
			$(this).removeClass("ui-corner-top").addClass("ui-corner-all");
		}
	});
});