﻿function setID(control) {
	control.id = ($('#kommundropdown').val());
}
//function ShowMedlemsForm() {
//	$('.ickemedlem').css('display', 'none');
//	$('.medlem').css('display', 'block');
//	SetMedlemToValidate2();
//	$('#paytype').css('display', 'block');

//}
//function ShowIckeMedlemsForm() {
//	$('.medlem').css('display', 'none');
//	$('.ickemedlem').css('display', 'block');

//	SetIckeMedlemToValidate2();
//	$('#paytype').css('display', 'block');

//}
//function SetIckeMedlemToValidate2() {
//	$('#medlem .required').each(function () {
//		this.className = "ok";
//	});
//	$('#ickemedlem .ok').each(function () {
//		this.className = "required";
//	});
//	ClearForm();
//	$('#submitButton').attr('disabled', 'disabled'); ;
//}
//function SetMedlemToValidate2() {
//	$('#ickemedlem .required').each(function () {
//		this.className = "ok";
//	});
//	$('#medlem .ok').each(function () {
//		this.className = "required";
//	});
//	ClearForm2();

//	$('#submitButton').attr('disabled', 'disabled');
//}
//function ClearForm2() {
//	$('#medlem :input').each(function () {
//		if (this.name != 'Land')
//			this.value = '';
//		this.style.backgroundColor = '#FFFFFF';
//	});
//	$('#ickemedlem :input').each(function () {
//		if (this.name != 'Land')
//			this.value = '';
//		this.style.backgroundColor = '#FFFFFF';
//	});
//}
$(function () {

	$('#fastsearch').click(function () {
		$.ajax({
			type: "GET",
			cache: false,
			url: "/forening/quicksearch/?searchvalue=" + $('#searchvalue').val(),
			success: loadquicksearch
		});


	});
})
$(function () {
	$('.mainlinks').hover(function () {
		$('.mainlinks').children('ul').hide();
		$(this).children('ul').show();
	}, function () {
		$('.mainlinks').children('ul').hide();


	});
});

function swapImages() {
	var $active = $('.imageswapgallery .active');
	var $next = ($('.imageswapgallery .active').next().length > 0) ? $('.imageswapgallery .active').next() : $('.imageswapgallery img:first');
	$active.fadeOut(function () {
		$active.removeClass('active');
		$next.fadeIn().addClass('active');

	});

}
function HideLoadSearch() {
	$('#loadsearch').hide();
}
function loadquicksearch(data, status) {
	$('#quicksearch').html(data)

}
$(function () {

	$('.infolinks').click(function () {
		$('.opendialog').each(function () {
			$(this).remove();
		});
		var $link = $(this);
		var $dialog = $("<div class='opendialog'></div>")
			.load('/site/tooltip?fieldguiid=' + $link.attr('id'))
			.dialog({
				autoOpen: false,
				width: 500,
				height: 600
			});

		$dialog.dialog('open');
		return false;
	});
})
$(function () {
	$('.questionmarks').live("click", function () {
		var $link = $(this);
		var $dialog = $('<div></div>')
			.load($link.attr('href'))
			.dialog({
				autoOpen: false,
				title: $link.attr('title'),
				width: 850,
				height: 500
			});
		$dialog.dialog('open');
		return false;
	});
})
$(function () {
	$('.ajaxexpand').live("click", function () {
		var ids = $(this).attr('id').split('.');
		var href = $(this).children()[0].id;
		$.ajax({
			type: "GET",
			cache: false,
			url: href + ids[0] + "?pageid=" + ids[1],
			success: loadpage
		});

	});
})
function loadpage(data, status) {
	$('#Detail').html(data)
}
function redirectToIndex() {
	move("/forening");
	alert("Ett fel uppstog, du skickas till startsidan");
}
function show(id) {
	$(id).show('200');
}
function checkFormBokning(form) {
	var choice = document.getElementById('editChoice').value;
	var epost = document.getElementById('Epost');
	var medlem = document.getElementById('medlem')
	if (choice == 2 && epost.value == "" && medlem.value == 0) {
		epost.style.backgroundColor = "#ffa4a4";
		document.getElementById("error." + epost.id).style.display = "inline";
		document.getElementById('submitButton').disabled = true;
		return false;
	}
	var antalMedlem = document.getElementById('antalMedlem');
	var antalIckeMedlem = document.getElementById('antalIckeMedlem')
	if (antalMedlem.value == 0 && antalIckeMedlem.value == 0) {
		antalIckeMedlem.style.backgroundColor = "#ffa4a4"
		antalMedlem.style.backgroundColor = "#ffa4a4"
		document.getElementById('submitButton').disabled = true;
		return false;
	}
}


function ShowHide(id) {
	var div = document.getElementById(id);
	if (div.style.display == "none")
		div.style.display = "block";
	else
		div.style.display = "none";
}
function submitform() {
	document.myform.submit();
}
function submitThisForm(form) {
	form.submit();
}
function move(url) {
	window.location = url;
}
function ifStringIsEmpty(s) {
	var q = new String(s);
	if (q.length == 0)
		return true;
	else
		return false;
}
function ReplaceCharacters(str) {
	str = str.replace("-", "");
	str = str.replace("-", "");
	str = str.replace("-", "");
	str = str.replace("-", "");
	str = str.replace(" ", "");
	str = str.replace(" ", "");
	str = str.replace(" ", "");
	str = str.replace(",", "");
	str = str.replace(",", "");
	str = str.replace(",", "");
	str = str.replace(".", "");
	str = str.replace(".", "");
	str = str.replace(".", "");
	return str;
}

function CheckIfValid(id, validateType, form) {
	var s = id.toString();
	var submitid = "";
	var array = s.split('.');
	if (array[1] != null)
		submitid = array[1];

	if (document.getElementById(id).value != null) {
		var str = new String(document.getElementById(id).value);
		document.getElementById(id).value = str.replace(/^\s+|\s+$/g, '');
	}
	switch (validateType) {
		case 1: //sträng
			{
				var regExp = /^.+$/;
				break;
			}
		case 2: // heltal
			{
				var regExp = /^[0-9]+$/;
				break;
			}

		case 3: // decimal
			{
				var regExp = /^[0-9\,\.]+$/;
				break;
			}
		case 4: // datum
			{
				var regExp = /^[12]{1}[09]{1}[0-9]{2}[-]{1}[01]{1}[0-9]{1}[-]{1}[0123]{1}[0-9]{1}$/;
				break;
			}
		case 5: // email
			{
				var regExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
				break;
			}
		case 6: // personnummer
			{
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = ReplaceCharacters(str);
					document.getElementById(id).value = str;
				}
				var regExp = /^(?:19|20)?\d{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])?\d{4}$/;
				break;
			}
		case 12: // URL
			{
				var regExp = /^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w\.]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
				break;
			}
		case 13: // PostNr tillåter 123456789 och mellanslag, 6 tecken om ett mellanslag annars 5 tecken
			{
				var regExp = /^.+$/;
				var regExp = /^[0-9]+$/;
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = ReplaceCharacters(str);
					document.getElementById(id).value = str;
				}
				break;
			}
		case 9: //Telefonnummer tillåter "() - 0123456789"
			{
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = ReplaceCharacters(str);
					document.getElementById(id).value = str;
				}
				var regExp = /^[0-9-()]+$/;
				break;
			}
		case 10: // Ingen
			{
				var regExp = "";
				break;
			}
		case 11: // Tid
			{
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = new String(ReplaceCharacters(str));

					if (str.length == 4) {
						var start = str.substring(0, 2);
						var end = str.substring(2, 4);
						str = start + ":" + end;
					} document.getElementById(id).value = str;
				}

				var regExp = /^(20|21|22|23|[01]\d|\d)(([:][0-5]\d){1,2})$/;
				break;
			}
		case 14: // Födelseår
			{
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = ReplaceCharacters(str);
					document.getElementById(id).value = str;
				}
				var regExp = /^(?:19|20)?[0-9]{2}$/;
				break;
			}
		case 15: // Födelsedatum
			{
				if (document.getElementById(id).value != null) {
					var str = new String(document.getElementById(id).value);
					str = ReplaceCharacters(str);
					document.getElementById(id).value = str;
				}
				var regExp = /^(?:19|20)?[0-9]{2}(?:0[1-9]|1[0-2])(?:0[1-9]|[12]\d|3[01])$/;
				break;
			}

	}
	if (document.getElementById(id).className == "editor") {
		if (document.getElementById("submitButton") != null)
			document.getElementById("submitButton").disabled = false;
		if (document.getElementById("submitButton." + submitid) != null)
			document.getElementById("submitButton." + submitid).disabled = false;
	}
	else if (document.getElementById(id).className != "") {
		if (!document.getElementById(id).value.match(regExp)) {
			//document.getElementById("ok." + id).style.display = "none";
			document.getElementById("error." + id).style.display = "inline";
			document.getElementById(id).style.backgroundColor = "#ffa4a4";
			document.getElementById(id).className = "required";
		}
		else {
			document.getElementById("error." + id).style.display = "none";
			document.getElementById(id).style.backgroundColor = "#adffa0";
			//document.getElementById("ok." + id).style.display = "inline";
			document.getElementById(id).className = "ok";
		}
		if (document.getElementById("submitButton") != null)
			document.getElementById("submitButton").disabled = true;
		if (document.getElementById("submitButton." + submitid) != null)
			document.getElementById("submitButton." + submitid).disabled = true;
	}
	else {
		if (document.getElementById(id).value != "") {
			if (!document.getElementById(id).value.match(regExp)) {
				//document.getElementById("ok." + id).style.display = "none";
				if (document.getElementById("error." + id) != null)
					document.getElementById("error." + id).style.display = "inline";
				document.getElementById(id).style.backgroundColor = "#ffa4a4";
			}
			else {
				if (document.getElementById("error." + id) != null)
					document.getElementById("error." + id).style.display = "none";
				document.getElementById(id).style.backgroundColor = "#adffa0";
			}
		}
		else {
			if (document.getElementById("error." + id) != null)
				document.getElementById("error." + id).style.display = "none";
			document.getElementById(id).style.backgroundColor = "#ffffff";
		}
	}

	if (form == null) {
		form = document.getElementById(id).form;
	}
	var count = 0;
	for (i = 0; i < form.length; i++) {
		if ((form[i].className != "" && form[i].className == "required" && form[i].value == "") || form[i].style.backgroundColor == "#ffa4a4") {
			count++;
		}
		if (form[i].type == "checkbox" && (form[i].className == "required" || form[i].className == "ok") && !form[i].checked) {
			count++;
		}
	}
	if (count == 0) {
		if (document.getElementById("submitButton") != null)
			document.getElementById("submitButton").disabled = false;
		else if (document.getElementById("submitButton." + submitid) != null)
			document.getElementById("submitButton." + submitid).disabled = false;
	}
	else {
		if (document.getElementById("submitButton") != null)
			document.getElementById("submitButton").disabled = true;
		else if (document.getElementById("submitButton." + submitid) != null)
			document.getElementById("submitButton." + submitid).disabled = true;
	}
}
function clear() {
	document.getElementById('mess').innerHtml = "";
}
function GetAktuell(link, id) {
	//link.style = "black";
	$(".image img").each(function () {
		this.style.display = 'none';
	});

	document.getElementById('a.' + id).style.display = "inline";
}
function SetAktuell(link, id) {
	if (document.getElementById('aktuellflik') != null) {
		var enums = "&";

		$(".enums").each(function () {
			if (this.checked)
				enums += this.value + "=true&"
		})
		link.href = "/Medlem/setidsmall/" + id + "?aktuell=" + document.getElementById('aktuellflik').innerHTML + enums;
	}
	else {
		link.href = "/Medlem/setidsmall/" + id + "?aktuell=medlem";
	}
}
function SetAsAktuell(link, arg) {
	document.getElementById('aktuellflik').innerHTML = arg;
}
function SetHref(value, anvid) {
	document.getElementById('checkapps').href = "/admin/Checkapps?forid=" + value + "&anvid=" + anvid;
}
