function check_din (){

	var entered_din = document.din_form.din_number.value
	
	var valid_dins = new Array()
	valid_dins[0] = "5346252647"
	valid_dins[1] = "5346252648"
	valid_dins[2] = "5346252649"
	valid_dins[2] = "5346252651"
	
	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 = "adv_user_level=patient";

			// Create/update the cookie:
			document.cookie = cookie_string + ";expires=" + cd + ";path=/";
			
			window.location = document.din_form.auth_redirect.value;
   } else {
			alert(document.din_form.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_fr() {
	 if (getCookieValue ("adv_user_level")){
			window.location = "advate/advate.html";
	}
}

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

//if previously validated, move them directly to the overview page
function check_is_already_authenticated_en() {
	 if (getCookieValue ("factor_user_level")){
			window.location = "factor/feiba-factor-direct.html";
	}
}