/*
-------------------------------------------------------------
Document:	'my account' script
Project:	KanslietOnline
Author:		Micke Andersson (anderssonweb.se)
Created:	2011-01-28 13:21 GMT+8
Copyright:	© 2011 Kansliet AB
-------------------------------------------------------------*/

/* initialize payment via Paynova
-------------------------------------------------------------*/
function NewPayment(invoiceId){
	$("input[name='InvoiceId']").val(invoiceId);
	var v = $("#InitPayment");
	v.dialog("destroy");
	v.dialog({
		width: 330,
		height: 150,
		modal: true,
		resizable: false,
		closeOnEscape: true,
		zIndex: 1,
		buttons: {
			"Fortsätt": function() {
				v.dialog('close');
				var opt = $("select[name='paymentList']").val();
				var iid = $("input[name='InvoiceId']").val();
				InitPayment(opt, iid);
			}
		},
		close: function(event, ui){
		
		}
	});	
}

function InitPayment(PayMethod, invoiceId){
	$.ajax({
		url: ABSPATH + "ajax/ajax.Paynova.Init.php",
		type: "POST",
		data: {
			invoiceId: invoiceId,
			payMethod: PayMethod
		},
		dataType: "json",
		success: function(data){
			if(data.hasError){
				throwError(data);
				
			}else{
				alert(data.returnMsg);
				window.location.href = data.returnUri;
			}
		}
	});
}

function notifyDetails(notId, notHash){
	$('#notifyDetails').hide();
	$('#notifyDetails').html("");
	$.ajax({
		url: ABSPATH + "ajax/ajax.account.notify.details.php",
		type: "POST",
		data: {
			notId: notId,
			notHash: notHash
		},
		dataType: "json",
		success: function(data){
			if(data.hasError){
				throwError(data);

			}else{
				var details = "";
				/*
				$.each(data.receipt['header'], function(i, header){
					details += "<h2>" + header.title + "</h2>\n";
					details += "<table cellpadding=\"0\" cellspacing=\"0\" class=\"resultset\">\n"
							  +"<tbody>\n";

					for(var j = 0; j <= data.receipt['header'][i]['content'].length - 1; j++){
						details += "<tr class=\"" + (j%2 == 0 ? "row-odd" : "row-even") + "\">"
								  +"<td style=\"width: 175px;\"><strong>" + data.receipt['header'][i]['content'][j] + "</strong></td>"
								  +"<td>" + data.receipt['values'][i][j] + "</td>"
								  +"</tr>";
					}
					details += "</tbody>\n"
							  +"</table><br />\n";
				});
				*/
				$.each(data.Receipt, function(x, Part){
					details += "<h2>" + Part.Title + "</h2>\n";
					details += "<table cellpadding=\"0\" cellspacing=\"0\" class=\"resultset\">\n"
							  +"<tbody>\n";
				
					$.each(Part['Data'], function(y, Row){
						details += "<tr class=\"" + (y%2 == 0 ? "row-odd" : "row-even") + "\">"
								  +"<td style=\"width: 175px;\"><strong>" + Row.Name + "</strong></td>"
								  +"<td>" + Row.Value + "</td>"
								  +"</tr>";
					});

					details += "</tbody>\n"
							  +"</table><br /><br />\n";
				});
				details += "<a href=\"#top\"><strong>Tillbaka</strong></a>";

				$('#notifyDetails').append(details);
				$('#notifyDetails').fadeIn();
				window.location.href = '#detaljer';
			}
		}
	});
}

$(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");
		});
		$('.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').removeAttr('disabled');
			}
		});
	});
	$('#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");
		});
		$('.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').removeAttr('disabled');
			}
		});
	});
	$('#imgremove').click( function(e){
		if(!confirm("Vill du ta bort den nuvarande bilden?\n")) return false;
		$.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>");
			}
		});
	});

	$('#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");
		});
		$('.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').removeAttr('disabled');
				}else{
					alert(data.returnMsg);
					window.location.href = ABSPATH;
				}
			}
		});
	});
	$('#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");
		});
		$('.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);
				}else{
					alert(data.returnMsg);
					window.location.href = data.returnUrl;
				}
				$('.large-button').removeAttr('disabled');
			}
		});
	});
	$('#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");
		});
		$('.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').removeAttr('disabled');
				}else{
					window.location.href = data.returnUrl;
				}
			}
		});
	});
	$('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");
		}
	});
});
