function registerConfirmationDialog(width, title, confirmationDiv) {
	confirmationDiv.dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: width,
		title: title,
		buttons: {
			'Lukk': function() {
				jQuery(this).dialog('close');
			}
		},
		open: function(event, ui) {
			// set width and height to fix the bug with the jquery dialog modal not covering the whole page
			jQuery(".ui-widget-overlay").css("height", jQuery(document).height()).css("width", jQuery(document).width()-25);
		}
	});
}

function registerErrorMessageDialog(width, title, errorMessageDiv) {
	errorMessageDiv.dialog({
		autoOpen: false,
		bgiframe: true,
		modal: true,
		width: width,
		title: title,
		buttons: {
			'Lukk': function() {
				jQuery(this).dialog('close');
			}
		},
		open: function(event, ui) {
			// set width and height to fix the bug with the jquery dialog modal not covering the whole page
			jQuery(".ui-widget-overlay").css("height", jQuery(document).height()).css("width", jQuery(document).width()-25);
		}
	});
}

function resetUIState(validateTipsDiv, allFields) {
	resetErrorState(validateTipsDiv, allFields);
	if(allFields) {
		allFields.val('');
	}
}

function resetErrorState(validateTipsDiv, allFields) {
	validateTipsDiv.text('');
	if(allFields) {
		allFields.removeClass('ui-state-error');
	}
}

function checkRegexp(o, regexp, text, validateTipsDiv, isDialog) {
	if (!(regexp.test(o.val()))) {
		o.addClass('ui-state-error');
		if(!isDialog) {
			validateTipsDiv.text(text);
		} else {
			validateTipsDiv.text(text);
			validateTipsDiv.dialog('open');                 
		}
		return false;
	} else {
		return true;
	}
}

function checkLengthName(o, inputLabel, min, max, validateTipsDiv) {
	if (o.val().length > max || o.val().length < min) {
		o.addClass('ui-state-error');
		validateTipsDiv.text(inputLabel + " må ha mellom " + min + " og " + max + " tegn.");
		return false;
	} else {
		return true;
	}
}

function checkLengthPhone(o, inputLabel, length, validateTipsDiv) {
	if (stripWhiteSpaces(o.val()).length != length ) {
		o.addClass('ui-state-error');
		validateTipsDiv.text(inputLabel + " må være eksakt " + length + " siffer.");
		return false;
	} else {
		return true;
	}
}

function isValidEmail(o, errorMessage, validateTipsDiv, isDialog) {
	return checkRegexp(o, /^(([A-Za-z0-9]+_+)|([A-Za-z0-9]+\-+)|([A-Za-z0-9]+\.+)|([A-Za-z0-9]+\++))*[A-Za-z0-9]+@((\w+\-+)|(\w+\.))*\w{1,63}\.[a-zA-Z]{2,6}$/i, errorMessage, validateTipsDiv, isDialog);
}

function stripWhiteSpaces(mystr) {
	var newstring = "";
	if (mystr.indexOf(' ') != -1) {
		string = mystr.split(' ');
		for (var i=0;i<string.length;i=i+1){
			newstring += string[i];
		}
		return newstring;
	}else {
		return mystr; 
	}
}

/*
 * Function that adds a btnOk css class to the first button in the jquery list of buttons
 */
function styleDialogButtons(buttons) {
	buttons.eq(0).addClass("btnOk").end();
}
