// JavaScript Document

// JScript code for Date Mask Formatting MM/DD/YYYY

function datomaske()
{

//var re = /^[mdy0-9]{2}\-[mdy0-9]{2}\-[mdy0-9]{4}$/
var re = /^[mdy0-3]{1}[mdy0-9]{1}\-[mdy0-1]{1}[mdy0-9]{1}\-[mdy1-2]{1}[mdy0-9]{3}$/

//Allow blank space in field
	if (event.value !="") 
	{
		if (re.test(event.value) == false) 
		{
			app.alert("Indtast venligst i dette format DD-MM-YYYY.")
			event.rc = false
		}
	}

}





var newVal="";
var fdate = null;
var dd=0;
var mm=0;
var repeatRemove=0;

	function init()
	{
	  newVal="";
	  fdate = null;
	  dd=0;
	  mm=0;
	  repeatRemove=0;
	}
	
  function getCharCode(e)
	{ return (e.which)?e.which:e.keyCode;
	}

  function restoreVal(eC,fld)
	{ if (eC==8||repeatRemove>0)
		{ 
		  if (newVal.length>repeatRemove)
			{ newVal=newVal.substring(0,newVal.length-repeatRemove)
			}
	      else
			{ newVal="";
			}
	    }
		repeatRemove=0;
		fld.value=newVal;
  	}

  function inputDate(e,fld)
	{ removeChar=false;
	  newVal=fld.value;
	  charCode = getCharCode(e);
		if ((fld.value.length==2 || fld.value.length==5) && charCode==45)
			{ newVal+="-";
			  return true;
			}
		if (fld.value.length==10 || charCode>57 || charCode<48)
			{ return false;
    		}

	newVal=fld.value+String.fromCharCode(charCode);

	switch (fld.value.length)
	  {
		case 0:
			  if (+newVal>3)
				{ repeatRemove++;
				  return false;
				}
			  break;
		  case 1:
			  if (+newVal>31)
				{ repeatRemove++;
				  return false;
				}
			  if (+newVal==00)
				{ repeatRemove++;
				  return false;
				}
			  dd = +newVal;
			  newVal+="-";
			  break;
		  case 2:
			  newVal=fld.value+"-"+String.fromCharCode(charCode);
			  if (charCode>49) 
				{ repeatRemove++;
				  return false;
				}
			  break;
		  case 3:
			  if (charCode>49)
				{ repeatRemove++;
				  return false;
				}
			  break;
		  case 4:
			mm = parseInt(newVal.substring(3),10);
			if (mm==2 && dd>29 || mm>12 || mm==00)
				{ repeatRemove++; 
				  return false;
				}
			if (dd==31 && (mm==4 || mm==6 || mm==9 || mm==11))
				{ repeatRemove++;
				  return false;
				}
			newVal+="-";
			break;
		case 5:
			  newVal=fld.value+"-"+String.fromCharCode(charCode);
			  break;
		  case 6:
			  if (charCode<49 || charCode>50)
				{ repeatRemove++;
				  return false;
				} //Tillader 1 og 2 som første tal i årstal
			  break;
		  case 7:
			  var yy = parseInt(newVal.substring(6));
			  if (yy<19 || yy>20)
				{ repeatRemove++;
				  return false;
				} //Tillader 19 og 20som første to tal i årstal
			  break;
		  case 8:
			  var yyy = parseInt(newVal.substring(6));
			  if (yyy<190 || yyy>210)
				{ repeatRemove++;
				  return false;
				} //Tillader alle tal som tredje tal, hvis første to er 19 eller 20.
			  break;
		  case 9:
			  var yyyy = parseInt(newVal.substring(6));
			  if (mm == 2 && dd == 29 && yyyy%4 !=0)
				{ repeatRemove+=8;
				  return false;
				}
	  }
	  return true;
}
