function check_din2 (){

	var entered_din = document.din_form2.din_number.value
	
	var valid_dins = new Array()
	valid_dins[0] = "6355472748"
	valid_dins[1] = "6355472647"
	valid_dins[2] = "6355472648"
	valid_dins[3] = "6355472749"
	valid_dins[4] = "6355472751"
	valid_dins[5] = "6355472752"
	valid_dins[6] = "6355472753"
	valid_dins[7] = "6355472752"
	
	var auth = false
	if (entered_din ==-1){ 
		// match failed, not 8 digits, keep auth = false
	} else {
		var encoded_din = encode_entered_din (entered_din)
			
		for (x in valid_dins){
			if(valid_dins[x] == encoded_din){
				auth="true"
      }
		}
	}
  
   if(auth){

			// Build the expiration date string:
			var d = new Date("December 31, 2099");
 			var cd = d.toGMTString();
		 
		 	// Build the set-cookie string:
			var cookie_string = "feiba_user_level=patient";

			// Create/update the cookie:
			document.cookie = cookie_string + ";expires=" + cd + ";path=/";
			
			window.location = document.din_form2.auth_redirect.value;
   } else {
			alert(document.din_form2.invalid_message.value);
   }
}

function encode_entered_din (din){
	var encoded_din = "";

	for(i = 0; i < din.length; i++) {
		encoded_din += din.charCodeAt(i) - 23;
	}
	return encoded_din
}

function getCookieValue (cookieName) {
  var exp = new RegExp (escape(cookieName) + "=([^;]+)");
  if (exp.test (document.cookie + ";")) {
    exp.exec (document.cookie + ";");
    return unescape(RegExp.$1);
  }
  else return false;
}


//if previously validated, move them directly to the overview page
function check_is_already_authenticated_fr2() {
	 if (getCookieValue ("feiba_user_level")){
			window.location = "feiba/feiba.html";
	}
}

//if previously validated, move them directly to the overview page
function check_is_already_authenticated_en2() {
	 if (getCookieValue ("feiba_user_level")){
			window.location = "feiba/feiba.html";
	}
}
