function isLeeg(cTk)
{
        for (var i=0;i < cTk.length;i++)
                {
                if (cTk.substring(i,i+1) != " ") return(false);
                }
        return(true);
}

 function FormatDatum(cDag,cMaand,cJaar){
 
                if (cDag.length > 2) cDag = cDag.substring(0,2);
                if (cMaand.length > 2) cMaand = cMaand.substring(0,2);
                if (cJaar.length == 4) cJaar = cJaar.substring(0,4);
                if (cDag.length == 1) cDag = "0" + cDag;
                if (cMaand.length == 1) cMaand = "0" + cMaand;
                if (cJaar.length == 1) cJaar = "0" + cJaar;
                if (cDag.length == 0) cDag = "  ";
                if (cMaand.length == 0) cMaand = "  ";
                if (cJaar.length == 0) cJaar = "  ";
                              
                if (isLeeg(cJaar + cMaand + cDag)) return("00000000");
                return(cDag + "-"+cMaand+ "-"+ cJaar);
}


/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(iDag,iMaand,iJaar,MinJ,MaxJ){
	// Declaring valid date character, minimum year and maximum year
	
	var dtStr = FormatDatum(iDag,iMaand,iJaar);
	var dtCh= "-";
	var minYear=MinJ;
	var maxYear=MaxJ;

	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("Het datum formaat moet zijn : dd-mm-yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Vul a.u.b. een juiste maand in")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Vul a.u.b. een juiste dag in")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Vul a.u.b. een juist 4 cijferig jaar in tussen "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Vul a.u.b. een juiste datum in")
		return false
	}
return true
}

function ValidateForm(dt){
	//var dt=document.frmSample.txtDate
	if (isDate(dt.value)==false){
		dt.focus()
		return false
	}
    return true
 }

function convertDate(d){ 
    var aPart = d.split('-'); 
    var b = new Date(aPart[2], aPart[1]-1, aPart[0]); 
    return(b.getTime()); 
} 

function ValidateVermbevaldat(d,u){
	
	var aPart = d.split('-'); 
  
	var vandaag = new Date();
	var    d1   = vandaag.getTime();
	
	 document.getElementById(u).value=FormatDatum(aPart[0], aPart[1], aPart[2]); 
   	    
	    if (convertDate(d) - d1 > 1000 * 3600 * 24 * 280 || convertDate(d) - d1 < 0 ){ 
        alert('foute datum'); }
}

function ValideerVermbevaldat(d){
	
	var aPart = d.split('-');
	var d2 = FormatDatum(aPart[0], aPart[1], aPart[2]); 
	
	var vandaag = new Date();
	var    d1   = vandaag.getTime();
	  
	      if (convertDate(d2) - d1 > 1000 * 3600 * 24 * 280 || convertDate(d2) - d1 < 0 ){
	      alert('foute datum')
	      return false }
  return true      
}
