
function checkpnr(nr){
	if (nr.length == 0) {
		return true
	}
	
	if (nr.length != 10) {
		return false
	}
	
	var checksum=0;
	var nn="";
	for(var n=0;n<10;n++){ nn+=((((n+1)%2)+1)*nr.substring(n,n+1)); }
	
	for(var n=0;n<nn.length;n++){ checksum+=nn.substring(n,n+1)*1; }
	
	return (checksum%10==0)?true:false;

}

function hidediv(whichLayer,whichLayer2,height) {
	var elem2;
	elem = document.getElementById( whichLayer );
	elem2 = document.getElementById( whichLayer2 );
	elem.style.height = height;
	elem2.style.display = 'none';
	
}

function popUp(URL)
{
	window.open( URL, "", "status = 1, height = 425, width = 550, resizable = 0" )
}

function NonZeroSumma(thisform)
{
	if (thisform.Summa.value == 0)
	{
		alert("Det finns inga artiklar i korgen!");
		return false;
	
	} else {return true;}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		var felmed="Felaktig emailadress"
		
		if (str.indexOf(at)==-1){
		   alert(felmed)
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(felmed)
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   alert(felmed)
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		   alert(felmed)
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   alert(felmed)
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
			alert(felmed)
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
			alert(felmed)
		    return false
		 }

 		 return true					
	}

function validate_email(field)
{
	with (field)
	{
		if (echeck(NyhetsEmail.value)==false) {
			return false
		}
	}
	return true
}
function validate_required(field,alerttxt)
{
	with (field)
	{
		if (value==null||value=="")
		{
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_form(thisform)
{
	with (thisform)
	{
		if (validate_required(OrderNamn,"Namn är inte ifyllt.")==false)
		{
			OrderNamn.focus();
			return false;
		}
		
		with (OrderNamn)
		{
			apos=value.indexOf(" ")
			if (apos<1) 
			{alert("För- och efternamn måste vara ifyllt");return false}
		}

		if (validate_required(OrderAdress,"Gatuadress är inte ifyllt.")==false)
		{
			OrderAdress.focus();
			return false;
		}
		
		if (validate_required(OrderPostnr,"Postnummer är inte ifyllt.")==false)
		{
			OrderPostnr.focus();
			return false;
		}
		
		if (validate_required(OrderPostort,"Postort är inte ifyllt.")==false)
		{
			OrderPostort.focus();
			return false;
		}		
		
		if (echeck(OrderEmail.value)==false)
		{
			OrderEmail.focus();
			return false;
		}
		
		if (validate_required(OrderTelefon,"Telefon är inte ifyllt.")==false)
		{
			OrderTelefon.focus();
			return false;
		}
		
		if (checkpnr(OrderPersonnummer.value) == false)
		{
			alert("Fel personnummer. Fyll i korrekt (10 siffror utan bindestreck) eller lämna blankt");
			OrderPersonnummer.focus();
			return false;
		}
		
		if (OrderPassword.value != OrderPassword2.value)
		{
			alert("Lösenord är ej identiska.");
			OrderPassword.value = "";
			OrderPassword2.value = "";
			OrderPassword.focus();
			return false;
		}		
		
	}
}
