
function setValidationMark(fieldIsValid, fieldNameValidation) 
{
	// Sets the picture on check_[fieldNameValidation] according to validation, red or green
	var imageName = 'check_' + fieldNameValidation;

	if (document.images[imageName]) 
	{
		if (fieldIsValid == false) 
		{
			document.images[imageName].src='img/red.gif';
		} 
		else 
		{
			document.images[imageName].src='img/green.gif';
		}
	}
}

function personnummer(nr){
	this.valid=false;
	//if(!nr.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})$/)){ return false; }
	//if(!nr.match(/^(\d{2})(\d{2})(\d{2})(\d{4})$/)){ return false; }
	if(!nr.match(/^(\d{2})(\d{2})(\d{2})(\d{4})$/))
	{ 
		if(!nr.match(/^(\d{2})(\d{2})(\d{2})\-(\d{4})$/))
		{
			return false; 
		}
	}
	this.now=new Date(); this.nowFullYear=this.now.getFullYear()+""; this.nowCentury=this.nowFullYear.substring(0,2); this.nowShortYear=this.nowFullYear.substring(2,4);
	this.year=RegExp.$1; this.month=RegExp.$2; this.day=RegExp.$3; this.controldigits=RegExp.$4;
	this.fullYear=(this.year*1<=this.nowShortYear*1)?(this.nowCentury+this.year)*1:((this.nowCentury*1-1)+this.year)*1;
	var months = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
	if(this.fullYear%400==0||this.fullYear%4==0&&this.fullYear%100!=0){ months[1]=29; }
	if(this.month*1<1||this.month*1>12||this.day*1<1||this.day*1>months[this.month*1-1]){ return false; }
	this.alldigits=this.year+this.month+this.day+this.controldigits;
	var nn="";
	for(var n=0;n<this.alldigits.length;n++){ nn+=((((n+1)%2)+1)*this.alldigits.substring(n,n+1)); }
	this.checksum=0;
	for(var n=0;n<nn.length;n++){ this.checksum+=nn.substring(n,n+1)*1; }
	this.valid=(this.checksum%10==0)?true:false;
	this.sex=parseInt(this.controldigits.substring(2,3))%2;
}

function isit_text(num) 
{ 
	ist_text=true;
	var characters="abcdefghijklmnopqrstuvwxyzåäöüß-. ";
    pruef_text=document.getElementById(num).value.toLowerCase();

    if(pruef_text.length==0)
	{
         ist_text=false;
	}
    else
	{
        for(n=0;n<pruef_text.length;n++)
        {
			//if(pruef_text.charAt(n)<"A"||pruef_text.charAt(n)>"Z")
			if(characters.indexOf(pruef_text.charAt(n))==-1)
			{	
				ist_text=false;
			}
		}
		if(ist_text) 
		{
			return true;
		}
		else 
        {
			return false;
        } 
	}
} 

function isit_number(num) 
{ 
	ist_text=true;
	var characters="0123456789-";
    pruef_text=document.getElementById(num).value;

    if(pruef_text.length==0)
	{
         ist_text=false;
	}
    else
	{
        for(n=0;n<pruef_text.length;n++)
        {
			//if(pruef_text.charAt(n)<"0"||pruef_text.charAt(n)>"9")
			if(characters.indexOf(pruef_text.charAt(n))==-1)
			{	
				ist_text=false;
			}
		}
		if(ist_text) 
		{
			return true;
		}
		else 
        {
			return false;
        } 
	}
} 

function IsNumeric(sText)
{
   var ValidChars = "0123456789-";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }
   return IsNumber;
}

function validate(inAlert)
{
	var strError='';
	var firstError='';

	//if (document.getElementById("regform").firstname.value == "")
	//if (letter.test(document.getElementById("regform").firstname.value) == false)
	if(isit_text("firstname") == false || document.getElementById("regform").firstname.value == "")
	{
		strError += "Förnamn\n";
		if (firstError == "") { firstError = "firstname"; };
		//document.getElementById("regform").firstname.focus();
		//return false;

		setValidationMark(false, "firstname");
	} 
	else
	{
		setValidationMark(true, "firstname");
	}


	if (isit_text("lastname") == false || document.getElementById("regform").lastname.value == "")
	{
	strError += "Efternamn\n";
	//document.getElementById("regform").lastname.focus();
	if (firstError == "") { firstError = "lastname"; };
	//return false;
		setValidationMark(false, "lastname");
	}
	else
	{
		setValidationMark(true, "lastname");
	}


	if (document.getElementById("regform").address.value == "")
	{
	strError += "Adress\n";
	if (firstError == "") { firstError = "address"; };
	//document.getElementById("regform").address.focus()
	//return false
		setValidationMark(false, "address");
	}
	else
	{
		setValidationMark(true, "address");
	}


	//if (document.getElementById("regform").zipcode.value.length<5 || isit_number("zipcode") == false)
	if(IsNumeric(document.getElementById("regform").zipcode.value) == false || document.getElementById("regform").zipcode.value.length<5)
	{
	strError += "Postnummer\n";
	if (firstError == "") { firstError = "zipcode"; };
	//document.getElementById("regform").zipcode.focus()
	//return false
		setValidationMark(false, "zipcode");
	}
	else
	{
		setValidationMark(true, "zipcode");
	}


	if (isit_text("city") == false || document.getElementById("regform").city.value == "")
	{
	strError += "Ort\n";
	if (firstError == "") { firstError = "city"; };
	//document.getElementById("regform").city.focus()
	//return false
	setValidationMark(false, "city");
	}
	else
	{
		setValidationMark(true, "city");
	}


	/*if (isit_text("country") == false || document.getElementById("regform").country.value == "")
	{
	strError += "Land\n";
	if (firstError == "") { firstError = "country"; };
	//document.getElementById("regform").country.focus()
	//return false
	setValidationMark(false, "country");
	}
	else
	{
		setValidationMark(true, "country");
	}*/

	if(!new personnummer(document.getElementById("regform").persnr.value).valid && document.getElementById("regform").country.value == "Sverige")
	{
		strError += "Personnummer\n";
		if (firstError == "") { firstError = "persnr"; };
		//document.getElementById("regform").persnr.focus()
		//return false
		setValidationMark(false, "persnr");
	}
	else if(document.getElementById("regform").persnr.value.length<8 && document.getElementById("regform").country.value != "Sverige")
	{
		strError += "Personnummer\n";
		if (firstError == "") { firstError = "persnr"; };
		//document.getElementById("regform").persnr.focus()
		//return false
		setValidationMark(false, "persnr");
	}
	else
	{
		setValidationMark(true, "persnr");
	}


	/*if (document.getElementById("regform").email1.value == "")
	{
	strError += "eMail\n";
	document.getElementById("regform").email1.focus()
	//return false
	} */
	reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+ //Name
					  '(\\@)'+ //@-Zeichen
					  '([a-zA-Z0-9\\-\\.]+)'+ //Domain
					  '(\\.)'+ //Punkt
					  '([a-zA-Z]{2,4})$'); //TLD

	if (reg.test(document.getElementById("regform").email1.value) == false)
	{
	strError += "E-mail\n";
	if (firstError == "") { firstError = "email1"; };
	//document.getElementById("regform").email1.focus()
	//return false
	setValidationMark(false, "email1");
	}
	else
	{
		setValidationMark(true, "email1");
	}


	if (document.getElementById("regform").email1.value != document.getElementById("regform").email2.value || reg.test(document.getElementById("regform").email1.value) == false)
	{
	strError += "Bekräftad E-Mail\n";
	if (firstError == "") { firstError = "email2"; };
	//document.getElementById("regform").email2.focus()
	//return false
	setValidationMark(false, "email2");
	}
	else
	{
		setValidationMark(true, "email2");
	}

	//if (document.getElementById("regform").phone.value.length<5)
	if(IsNumeric(document.getElementById("regform").phone.value) == false || document.getElementById("regform").phone.value.length<5)
	{
		//alert("Please enter your phone number")
		//document.getElementById("regform").phone.focus()
		//return false
		strError += "Telefonnummer\n";
		if (firstError == "") { firstError = "phone"; };

		setValidationMark(false, "phone");
	}
	else
	{
		setValidationMark(true, "phone");
	}

	if (document.getElementById("regform").password.value.length<6)
	{
		//alert("Please enter your phone number")
		//document.getElementById("regform").phone.focus()
		//return false
		strError += "Lösenord\n";
		if (firstError == "") { firstError = "password"; };

		setValidationMark(false, "password");
	}
	else
	{
		setValidationMark(true, "password");
	}

	if (document.getElementById("regform").password.value != document.getElementById("regform").password2.value || document.getElementById("regform").password2.value.length<6)
	{
		//alert("Please enter your phone number")
		//document.getElementById("regform").phone.focus()
		//return false
		strError += "Bekräftat lösenord\n";
		if (firstError == "") { firstError = "password2"; };

		setValidationMark(false, "password2");
	}
	else
	{
		setValidationMark(true, "password2");
	}

	if (document.getElementById("regform").captcha.value.length<5)
	{
		//alert("Please enter your phone number")
		//document.getElementById("regform").phone.focus()
		//return false
		strError += "Teckenkombination\n";
		if (firstError == "") { firstError = "captcha"; };

		setValidationMark(false, "captcha");
	}
	else
	{
		setValidationMark(true, "captcha");
	}

	if (document.getElementById("regform").termsBox.checked == 0)
	{
		//alert("Please enter your phone number")
		//document.getElementById("regform").phone.focus()
		//return false
		strError += "Villkor\n";
		if (firstError == "") { firstError = "termsBox"; };

		setValidationMark(false, "termsBox");
	}
	else
	{
		setValidationMark(true, "termsBox");
	}


	if (strError.length>0)
	{
		
		if(inAlert == true)
		{

			document.getElementById(firstError).focus();
			alert("Dessa fält var ej korrekt ifyllda: \n\n" + strError);
		}

		return false;
	} 

}



function setFocus()
{
	document.getElementById("regform").firstname.focus()
} 