//Kontrollera norska och svenska personnummer
//Funktionerna ska returnera 1 om personnummret är korrekt
//och 0 om personnummret är falskt
//Eg:
//alert(CheckNor("230670 469 19"));
//alert(CheckSwe("770209-3555"));

function CheckSwe(str)
{
	var chksum = 0;
	var x;
	
	str = str.replace(/\D/g, "");
	if(str.length != 10)
	{
		return 0;
	}

	for (i=0;i<=8;i++)
	{
		x = parseInt(str.substr(i, 1))
		if(i%2==0)
		{
			x = x * 2
		}
		if(x>=10)
		{
			chksum = chksum + 1 + x - 10
		}
		else
		{
			chksum = chksum + x
		}
	}

	chksum = chksum % 10
	if(chksum != 0)
	{
		chksum = 10 - chksum
	}

	if(parseInt(str.substr(i, 1)) - chksum == 0)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

function CheckAgeSwe(str, brytdatum, age)
{
	var now = new Date()	
	
	str = str.replace(/\D/g, "");
	if(str.length != 10)
	{
		return 0;
	}
	yr = parseInt(str.substr(0, 2))
	mn = parseInt(str.substr(2, 2))
	dy = parseInt(str.substr(4, 2))
	if(yr<brytdatum)
		yr+=100;
	yrdiff = now.getFullYear()-(yr + 1900); //brytdatum is date which means that customer is born in 2000 instead of 1900's. So for example for org_no 0808086666 and brytdatum 10, birthdate will be 2008
	if(yrdiff>age)
		return 1;
	else if(yrdiff<age)
		return 0;
	else
	{
		mndiff = now.getMonth()-mn+1;
		if(mndiff>0)
			return 1;
		else if(mndiff<0) 
			return 0;
		else
		{
			dydiff = now.getDate()-dy;
			if(dydiff>=0)
				return 1
			else
				return 0;
		}
	}
	
}

function CheckNor(str)
{
	//Remove all non-numeric characters
	str = str.replace(/\D/g, "");

	//Check length of pers.no
	if(str.length != 11)
	{
		return 0;
	}
	
	//Run the checksum test for the two checksum digits
	if(CheckNorKey1(str) == 0 && CheckNorKey2(str) == 0 )
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//Checksum test for checksum digit 1
function CheckNorKey1(str)
{
	var value = 0;
	//This is the key to check against
	var key = "376189452";

	//Loop through the digits
	for(i = 0; i <= 8; i++)
	{
		//Multiply the value with the corresponding number in the key
		value = value + parseInt(str.substr(i, 1)) * parseInt(key.substr(i, 1));
	}

	//Add the value of the checksum
	value = value + parseInt(str.substr(9, 1))
	
	//Mod 11 should be 0
	return value % 11;
}

//Checksum test for checksum digit 2
function CheckNorKey2(str)
{
	var value = 0;
	//This is the key to check against
	var key="5432765432"

	//Loop through the digits
	for(i = 0; i <= 9; i++)
	{
		//Multiply the value with the corresponding number in the key
		value = value + parseInt(str.substr(i, 1)) * parseInt(key.substr(i, 1));
	}
	
	//Add the value of the checksum
	value = value + parseInt(str.substr(10, 1))

	//Mod 11 should be 0
	return value % 11;
}

function CheckNor(str)
{
	//Remove all non-numeric characters
	str = str.replace(/\D/g, "");

	//Check length of pers.no
	if(str.length != 11)
	{
		return 0;
	}
	
	//Run the checksum test for the two checksum digits
	if(CheckNorKey1(str) == 0 && CheckNorKey2(str) == 0 )
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

//Checksum test for checksum digit 1
function CheckNorKey1(str)
{
	var value = 0;
	//This is the key to check against
	var key = "376189452";

	//Loop through the digits
	for(i = 0; i <= 8; i++)
	{
		//Multiply the value with the corresponding number in the key
		value = value + parseInt(str.substr(i, 1)) * parseInt(key.substr(i, 1));
	}

	//Add the value of the checksum
	value = value + parseInt(str.substr(9, 1))
	
	//Mod 11 should be 0
	return value % 11;
}

//Checksum test for checksum digit 2
function CheckNorKey2(str)
{
	var value = 0;
	//This is the key to check against
	var key="5432765432"

	//Loop through the digits
	for(i = 0; i <= 9; i++)
	{
		//Multiply the value with the corresponding number in the key
		value = value + parseInt(str.substr(i, 1)) * parseInt(key.substr(i, 1));
	}
	
	//Add the value of the checksum
	value = value + parseInt(str.substr(10, 1))

	//Mod 11 should be 0
	return value % 11;
}