$(document).ready(function() {
   	init();
});

function init(){
	
	/* Display form
	------------------------------------- */
	if($(".kurs_1")) {
		$(".kurs_1").click(function() { 
			$(".form_1").css({'display': "block"});
			$(".formhead strong").html('Anm\344l dig till e-kursen "Jag i naturen"');
			$(".subject").val("Kurs 1");
			$(".submitmsg").css({'display': "none"});
			return false;
		});
	}
	
	if($(".kurs_2")) {
		$(".kurs_2").click(function() { 
			$(".form_1").css({'display': "block"});
			$(".formhead strong").html('Anm\344l dig till e-kursen "Recept f\366r naturupplevelser"');
			$(".subject").val("Kurs 2");
			$(".submitmsg").css({'display': "none"});
			return false;
		});
	}
	
}



//Validate form
function validateForm() {
	missinginfo = "";
	if (document.getElementById('namn').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('namn').style.border = "1px solid red";
	} else {
		document.getElementById('namn').style.border = "1px solid #e0e0e0";
	}
	    
	if (document.getElementById('comment').value == "") {
		missinginfo += "\n - Kommentar";
		document.getElementById('comment').style.border = "1px solid red";
	} else {
		document.getElementById('comment').style.border = "1px solid #a1a1a1";
	}
	      
	if (missinginfo != "") {
		missinginfo ="\n" +
		"Kontrollera att f\366ljande f\344lt \344r korrekt ifyllda:\n" + missinginfo;
		//alert(missinginfo);
		return false;
	}
		else return true;   
}


//Count chars
function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
    	limitField.value = limitField.value.substring(0, limitNum);
    } else {
    	limitCount.value = limitNum - limitField.value.length;
    }
}

//Pnr
function personnummer(nr){
	this.valid=false;
	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;
}

//Validate form 3

function validateForm3() {
	missinginfo = "";
	if (document.getElementById('fnamn').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('fnamn').style.border = "1px solid red";
	} else {
		document.getElementById('fnamn').style.border = "1px solid #969696";
	}
	
	//missinginfo = "";
	if (document.getElementById('adress').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('adress').style.border = "1px solid red";
	} else {
		document.getElementById('adress').style.border = "1px solid #969696";
	}
	
	//missinginfo = "";
	if (document.getElementById('personnummer').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('personnummer').style.border = "1px solid red";
	} else {
		document.getElementById('personnummer').style.border = "1px solid #969696";
	}
	//missinginfo = "";
	var sPNum = document.getElementById('personnummer').value
	if(!new personnummer(sPNum).valid) {
		//alert('ogiltigt');
		missinginfo += "\n - Personnummer";
		document.getElementById('personnummer').style.border = "1px solid red";
	}else {
		document.getElementById('personnummer').style.border = "1px solid #969696";
	}
	//validatePNum(sPNum);
	
	//missinginfo = "";
	if (document.getElementById('postort').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('postort').style.border = "1px solid red";
	} else {
		document.getElementById('postort').style.border = "1px solid #969696";
	}
	
	//missinginfo = "";
	if (document.getElementById('postnummer').value == "") {
		missinginfo += "\n - Namn";
		document.getElementById('postnummer').style.border = "1px solid red";
	} else {
		document.getElementById('postnummer').style.border = "1px solid #969696";
	}
	
	if ((document.getElementById('email').value == "") ||
		(document.getElementById('email').value.indexOf('@') == -1) ||
		(document.getElementById('email').value.indexOf('.') == -1)) {
		missinginfo += "\n - E-postadress";
		document.getElementById('email').style.border = "1px solid red";
	} else {
		document.getElementById('email').style.border = "1px solid #969696";
	}
	      
	if (missinginfo != "") {
		missinginfo = "\n" +
		"Kontrollera att f\366ljande f\344lt \344r korrekt ifyllda:\n" +
		missinginfo;
		//alert(missinginfo);
		return false;
	}
	else {
		return true;
	}
}