function limitText(limitField, limitCount, limitNum){
	if(limitField.value.length > limitNum)
		limitField.value = limitField.value.substring(0, limitNum);
  }

var one_day = 1000 * 60 * 60 * 24;
var one_month = 1000 * 60 * 60 * 24 * 30;
var one_year = 1000 * 60 * 60 * 24 * 30 * 12;

function displayage(yr, mon, day, unit, decimal, roundi){
	var today = new Date();
	var pastdate = new Date(yr, mon-1, day);
	var countunit = unit;
	var decimals = decimal;
	var rounding = roundi;
	
	var finalunit = (countunit == "days")? one_day : (countunit == "months")? one_month : one_year;
	var decimals = (decimals <= 0)? 1 : decimals * 10;
	
	if(unit != "years")	{
		if(rounding == "rounddown")
			return Math.floor((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals;
		else
			return Math.ceil((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals;
	  }
	else{
		var yearspast = today.getFullYear() - yr - 1;
		var tail = (today.getMonth() > mon - 1 || today.getMonth() == mon - 1 && today.getDate() >= day)? 1 : 0;
		pastdate.setFullYear(today.getFullYear());
		var pastdate2 = new Date(today.getFullYear() - 1, mon - 1, day);
		tail = (tail == 1)? tail + Math.floor((today.getTime() - pastdate.getTime()) / (finalunit) * decimals) / decimals : Math.floor((today.getTime() - pastdate2.getTime()) / (finalunit) * decimals) / decimals;
		return yearspast + tail;
	  }
  }
	
function tarkista_alku(f){
	var virhe = 'The following information is missing\n\n';	
	var hylkaa = 0;

	var oppilaitosvalue = 0;		
	for(i = 0; i < document.lomake.H_Oppilaitos.length; i++){
    if (document.lomake.H_Oppilaitos[i].selected == true){
		  oppilaitosvalue = document.lomake.H_Oppilaitos[i].value;
	    }		
	  }
  if (document.lomake.H_Oppilaitos[0].selected == true) {
    virhe += 'Applicants place of study\n';
	  hylkaa = 1;
	  }		
	
	if (hylkaa == 1){
		alert(virhe);
		return false;
	  }
	else{
    if(document.lomake.H_Oppilaitos.value == "University of Oulu exchange student"){
			document.lomake.action = "application2.php";
		  }
		else if(document.lomake.H_Oppilaitos.value == "Oulu University Academic Research"){
			document.lomake.action = "application3.php";
		  }		
		else{
	    document.lomake.action = "application1.php";
		  }				
    f.submit();
    return true;				
		}
  }

function tarkista(){
	var virhe = 'The following information is missing\n\n';	
	var hylkaa = 0;

	if (document.lomake.H_Nimi.value == "") {
	  virhe += 'Applicants name\n'
		hylkaa = 1;
	  }
	if (document.lomake.dd.value == "x" || document.lomake.mm.value == "x" || document.lomake.yy.value == "x") {
		virhe += 'Applicants date of birth\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Nami[0].selected == true) {
		virhe += 'Applicants gender\n'
		hylkaa = 1;
	  }	
	if (document.lomake.H_Postios.value == "") {
		virhe += 'Applicants address\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Postinro.value == "") {
		virhe += 'Applicants postal code\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Postitp.value == "") {
		virhe += 'Applicants city\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Puhelin.value == "") {
		virhe += 'Applicants telephone\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Sahkoposti.value == "") {
		virhe += 'Applicants email address\n'
		hylkaa = 1;
	  }				
	/*
	if (document.lomake.H_Oppilaitos[0].selected == true) {
		virhe += 'Applicants place of study\n'
		hylkaa = 1;
	  }
	*/					
	if (document.lomake.dd2.value == "x" || document.lomake.mm2.value == "x" || document.lomake.yy2.value == "x") {
		virhe += 'Date of enrollment\n'
		hylkaa = 1;
	  }
  if (document.lomake.H_Tulot.value == "") {
    virhe += 'Applicants income\n'
    hylkaa = 1;
    }
  if (document.lomake.H_Varallisuus.value == "") {
    virhe += 'Applicants property\n'
    hylkaa = 1;
    }

	var eikohdetta0 = 1;
	
  if (document.lomake.dd7.value != "x" && document.lomake.mm7.value != "x" && document.lomake.yy7.value != "x") {
	  eikohdetta0 = 0;		
		}
	if (eikohdetta0 == 1) {
		virhe += 'Need of the apartment\n';
		hylkaa = 1;
	}	
		
	var eikohdetta = 1;

	if(document.lomake.kohde1.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde2.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde3.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde4.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde5.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde7.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde8.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde9.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde10.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde11.checked == true){
		eikohdetta = 0;}
	if(document.lomake.kohde12.checked == true){
		eikohdetta = 0;}
		
	if (eikohdetta == 1) {
		virhe += 'Select at least one housing area\n';
		hylkaa = 1;
	}

	var eityyppia = 1;
	for(a = 0; a < document.lomake.tyyppi.length; a++){ 
    if(document.lomake.tyyppi[a].checked == true){ 
      eityyppia = 0;
      } 
    } 
  if (eityyppia == 1){
 		virhe += 'Select apartment type\n'
		hylkaa = 1;
	  }
		
  if(document.lomake.P_Nimi.value != ""){	
		
		if (document.lomake.dd3.value == "x" || document.lomake.mm3.value == "x" || document.lomake.yy3.value == "x") {
  		virhe += 'Date of birth of accompanying spouse is missing\n'
			hylkaa = 1; 
			}
			
		}
  if (document.lomake.H_Maxvuokra.value == "") {
    virhe += 'Max rent\n'
    hylkaa = 1;
    }		
	if (document.lomake.vercodekentta.value == "") {
	  virhe += 'Verification code\n'
		hylkaa = 1;
	  }
			
	if (hylkaa == 1){
		alert(virhe);
		return false;
	  }
	else{
		return true;
		}
  }

function tarkista_uusi(){
	var virhe = 'The following information is missing\n\n';	
	var hylkaa = 0;

	if (document.lomake.H_Sukunimi.value == "") {
	  virhe += 'Applicants last name\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Etunimi.value == "") {
	  virhe += 'Applicants first name\n'
		hylkaa = 1;
	  }		
	if (document.lomake.dd.value == "x" || document.lomake.mm.value == "x" || document.lomake.yy.value == "x") {
		virhe += 'Applicants date of birth\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Nami[0].selected == true) {
		virhe += 'Applicants gender\n'
		hylkaa = 1;
	  }	
	if (document.lomake.H_Postios.value == "") {
		virhe += 'Applicants street address\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Postinro.value == "") {
		virhe += 'Applicants postal code\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Postitp.value == "") {
		virhe += 'Applicants city\n'
		hylkaa = 1;
	  }
	if (document.lomake.H_Henkikirjpaikka.value == "") {
		virhe += 'Applicants country\n'
		hylkaa = 1;
	  }		
	if (document.lomake.H_Sahkoposti.value == "") {
		virhe += 'Applicants e-mail address\n'
		hylkaa = 1;
	  }		
 	if (document.lomake.H_Organisaatio[0].selected == true) {
  	virhe += 'Applicants exchange programme\n'	  
	  hylkaa = 1;
		}			
  if (document.lomake.mm7.value == "x" || document.lomake.yy7.value == "x") {
		virhe += 'Start of the time period\n'
		hylkaa = 1;	
	  }
	if (document.lomake.mm8.value == "x" || document.lomake.yy8.value == "x") {
		virhe += 'End of the time period\n'
		hylkaa = 1;	  
	  }				
	
	var mm7 = document.lomake.mm7.value;
	var yy7 = document.lomake.yy7.value;
	
	var mm8 = document.lomake.mm8.value;
	var yy8 = document.lomake.yy8.value;	
	
	var dd7 = "1";
	var dd8 = "28";
		
	var alku = new Date(yy7, mm7, dd7);
	var loppu = new Date(yy8, mm8, dd8);

	if(alku >= loppu){
		virhe += 'Time period is wrong\n'
		hylkaa = 1;
	  }	
  if (document.lomake.vercodekentta.value == "") {
	  virhe += 'Verification code\n'
		hylkaa = 1;
	  }
	if (hylkaa == 1){
		alert(virhe);
		return false;
	  }
	else{
		return true;
		}
  }	

