﻿function checkadd() {
	if ($('#ad_namn').attr('value').length < 3 || $('#ad_namn').attr('value') == 'För- & Efternamn') {
		var rutaNum = dialogrutor.length;
		dialogrutor.push($('<div></div>')
			.html('<p>Användarnamnet är för kort.</p><p>Användarens namn måste innehålla minst tre bokstäver.</p>')
			.dialog({
				resizable: false,
				autoOpen: true,
				modal: true,
				dialogClass: 'ruta_' + rutaNum,
				title: 'Skapa användare',
				buttons: {
					'OK': function() {
						$('#ad_namn').focus();
						$(this).dialog('close');
					}
				},
				close: function () {
					CloseDialog();
				}
			}));
	} else if (parseInt($('#ad_timpris').attr('value')) < 50 || $('#ad_timpris').attr('value') == 'Timpris') {
		var rutaNum = dialogrutor.length;
		dialogrutor.push($('<div></div>')
			.html('<p>Du måste ange ett timpris på minst 50 kr.</p>')
			.dialog({
				resizable: false,
				autoOpen: true,
				modal: true,
				dialogClass: 'ruta_' + rutaNum,
				title: 'Skapa användare',
				buttons: {
					'OK': function () {
						$('#ad_timpris').focus();
						$(this).dialog('close');
					}
				},
				close: function () {
					CloseDialog();
				}
			}));
		} else if (checkemail($('#ad_epost').attr('value')) == false) {
			var rutaNum = dialogrutor.length;
			dialogrutor.push($('<div></div>')
			.html('<p>Du måste ange en giltig e-postadress.</p>')
			.dialog({
				resizable: false,
				autoOpen: true,
				modal: true,
				dialogClass: 'ruta_' + rutaNum,
				title: 'Skapa användare',
				buttons: {
					'OK': function () {
						$('#ad_epost').focus();
						$(this).dialog('close');
					}
				},
				close: function () {
					CloseDialog();
				}
			}));
	} else {
		doAddPostback();
	}
}

function checkemail(email) {
	var testresults;
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (filter.test(email)) {
		testresults = true
	} else {
		testresults = false
	}
	return (testresults);
}

function doAddPostback() {
	var url = 'sida_kontroll_anvandare.asp?alt=addsave';
	$.ajax({
		type: 'POST',
		url: url,
		data: $('#addform').serialize(),
		success: function (data) {
			AddPostbackComplete(data);
		},
		dataType: 'text'
	});
}

function AddPostbackComplete(data) {
	if (data.substr(0, 2) != 'OK') {
		var rutaNum = dialogrutor.length;
		dialogrutor.push($('<div></div>')
			.html('<p>Ett fel inträffade.</p><p>Det gick inte att skapa användaren.</p><p>Var god se över informationen du angett i formuläret och försök spara igen.</p>')
			.dialog({
				resizable: false,
				autoOpen: true,
				modal: true,
				dialogClass: 'ruta_' + rutaNum,
				title: 'Skapa användare',
				buttons: {
					'OK': function () {
						$(this).dialog('close');
					}
				},
				close: function () {
					CloseDialog();
				}
			}));
	} else {
		var usr = data.substr(3, data.length - 3);
		var rutaNum = dialogrutor.length;
		dialogrutor.push($('<div></div>')
			.html('<p>Användarkontot \'' + usr + '\' har skapats.</p>')
			.dialog({
				resizable: false,
				autoOpen: true,
				modal: true,
				dialogClass: 'ruta_' + rutaNum,
				title: 'Skapa användare',
				buttons: {
					'OK': function () {
						tryLogin();
					}
				},
				close: function () {
					CloseDialog();
				}
			}));
	}
}
