var streepjes = false;
var AllesGoed = true;  

function DetectNS()
{
     var agt=navigator.userAgent.toLowerCase();
     var is_major = parseInt(navigator.appVersion);
     var is_minor = parseFloat(navigator.appVersion);
     var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1));
	if (is_nav) return true;
}

function GetCurrentDate()
{
     var datum= new Date(), Day, Month, Year, CurrentDate

	Day=datum.getDate().toString();
	Month=datum.getMonth()+1;
	Month=Month.toString();
	Year=DetectNS()?(datum.getYear()+1900).toString():datum.getFullYear().toString();
	
	if (Day.length==1) Day="0"+Day;
	if (Month.length==1) Month="0"+Month;
	if (Year.length==2)Year="20"+Year;
	
	CurrentDate = Year +  Month +  Day
	
	return CurrentDate;
}

function GetMaxDays(Month,Year)
{
	MaxDays=0;
	switch (Month)
	{
		case 1  : { MaxDays=31; break; }
		case 2  : {
                         if( ((Year%4==0)&&((Year%100!=0)||(Year%400==0))) )
                         {
                         	MaxDays=29
                         }
                         else MaxDays=28;
                         break
			     }
		case 3  : { MaxDays=31; break; }
		case 4  : { MaxDays=30; break; }
		case 5  : { MaxDays=31; break; }
		case 6  : { MaxDays=30; break; }
		case 7  : { MaxDays=31; break; }
		case 8  : { MaxDays=31; break; }
		case 9  : { MaxDays=30; break; }
		case 10 : { MaxDays=31; break; }
		case 11 : { MaxDays=30; break; }
		case 12 : { MaxDays=31; break; }
	}
     return MaxDays;
}

function IsNumeric(x)
{
     var valid = true;
     for (q=0; q<x.length; q++) { 
          if (x.charCodeAt(q)<48 || x.charCodeAt(q)>57) {
               valid=false;
               break;
          }
     }
     return valid;
}

function CheckOpLetters(dDate)
{
  var doorgaan = true;
  var AllesOke = true;
  
  for (i=0;i<dDate.length;i++)  
  {
    if (!(((dDate.charAt(i) >= 0) && (dDate.charAt(i) <= 9)) || (dDate.charAt(i) == "-"))) 
    //wanneer er GEEN getal tussen de 0 en 9 staat of er staat GEEN streepje of een "/",
    //dan is het fout, en moet er gestopt worden
    {
      doorgaan = false;
      AllesOke = false;
      break;
    }
    if (!doorgaan) break;
  }
  if (AllesOke)
  {
    return true;
  }
    else
    {
      return false;
    }
}

function TelStreepjes(dDate)
{
  var teller = 0;
  var antwoord;
  
  for (i=0;i<dDate.length;i++)
  {
    if (dDate.charAt(i) == "-")
    {
      teller++;
    }
  }
  
  if ((teller == 2) || (teller == 0))
  {
    return true;
  }
    else
    {
      return false;
    }     
}

function VoorAchter(dDate)
{
  if ((dDate.charAt(0) == "-") || ((dDate.charAt(dDate.length)-1) == "-")) 
  {
    return false;
  }
    else
    {
      return true;
    }
}

function CheckTussen(dDate)
{
  var rest,rest2;
  var aantal;
  var i,j;
  
  i = dDate.indexOf("-");
  rest = dDate.substr(i+1,dDate.length);
  j = rest.indexOf("-");
  rest2 = rest.substr(j+1,dDate.length);
  aantal = rest2.length;
    
  if ((((i==1 || i==2)) && ((j==1 || j==2))) && (aantal == 2 || aantal == 4))
  {
    return true;
  }
    else
    {
      return false;
    }  
}

function ControleerLengte(dDate)
{
 if ((dDate.length < 6) || (dDate.length >= 11) || (dDate.length == 7) || (dDate.length == 9))
 {
   return false;
 }
   else
   { 
     return true;
   }
}

function TransformDate(dDate, TypeOfCheck)
{

  AllesGoed = true;  
  var dDateValue = dDate.value;
  if (dDateValue == "")
  {
    AllesGoed = false;    
  }
    else
    {
      if ((!ControleerLengte(dDateValue)) && (AllesGoed))
      {
        AllesGoed = false;
      }
        else
        {
          if ((!CheckOpLetters(dDateValue)) && (AllesGoed))
          {
            AllesGoed = false;            
          }
            else
            {
              if ((!VoorAchter(dDateValue)) && (AllesGoed))
              {
                AllesGoed = false;
              }
                else
                {
                  if ((!TelStreepjes(dDateValue)) && (AllesGoed))
                  {
                    AllesGoed = false;
                  }
                    else
                    {                              
                      pos = dDateValue.indexOf("-");
                      if (pos >= 0)
                      {
                        streepjes = true;
                        if ((!CheckTussen(dDateValue)) && (AllesGoed))
                        {
                          AllesGoed = false;
                        }                        
                      }
                    }
                }
            }
        }
    }

    if (AllesGoed) 
    { 
      dDate.value = ControleerInvoer(dDateValue, TypeOfCheck)
      
      /*Check date */
      day   = parseInt(dDate.value.substring(0,2),10);
      month = parseInt(dDate.value.substring(3,5),10);
      year  = parseInt(dDate.value.substring(6,10),10);
	
      if( day < 1 || day > 31 )
      {
				dDate.value = "Ongeldig";
				return false;
      }
      switch( month )
      {
      	case 1:case 3:case 5:case 7:case 8:case 10:case 12:
			if( day > 31 )
			{
				dDate.value = "Ongeldig";
				return false;
			}
			break;
		case 4:case 6:case 9:case 11:
			if( day > 30 )
			{
				dDate.value = "Ongeldig";
				return false;
			}
			break;
		case 2:
			if ( (year % 4!=0) && ((year % 100==0) || (year % 400!=0)) ) //No Leap year
			{
				if( day > 28 )
				{
					dDate.value = "Ongeldig";
					return false;
				}
			}
			else //Leap Year
			{
				if( day > 29 )
				{
					dDate.value = "Ongeldig";
					return false;
				}
			}
			break;
		default:
			dDate.value = "Ongeldig";
			return false;
      }
 
      if( year < 1900 )
      {
      	dDate.value = "Ongeldig";
      	return false;
      }
     
      return true;
    }
    else 
    {
      dDate.value = "Ongeldig";
      return false;
    }
} 

function ms(dDate)
{
  var aantalkar;
  if (dDate.charAt(2) == "-")
  {
    links = dDate.substr(0,6);
    rechts = dDate.substr(6,7);
    links = links+"20";
    dDate=links+rechts;    
  }  
    else
    {
      var links,rechts;
      dDate = "0"+dDate;
      links = dDate.substr(0,3);
      rechts = dDate.substr(3,6);
      links=links+"0";
      dDate=links+rechts;
      aantalkar = dDate.length;
      if (aantalkar == 8)
      {
        links = dDate.substr(0,6);
        rechts = dDate.substr(6,7);
        links = links+"20";
        dDate=links+rechts;
      }
    }
  return dDate;
}      

function zssp(dDate)
{
  var deel1,deel2,deel3;
  deel1 = dDate.charAt(0);
  deel2 = dDate.charAt(1);
  deel3 = dDate.substr(2,5);
  deel1="0"+deel1+"-";
  deel2="0"+deel2+"-";
  dDate=deel1+deel2+deel3;
  return dDate;
}

function zs(dDate)
{
  var links,rechts;
  links = dDate.substr(0,2);
  if (aantalkar == 6){rechts = dDate.substr(2,6);}
  if (aantalkar == 8){rechts = dDate.substr(2,7);}  
  links=links+"-";
  dDate=links+rechts;
  links = dDate.substr(0,5);
  
  if (aantalkar == 6)
  {
    rechts = dDate.substr(5,6);
    links = links+"-20";    
  }
    else
    {
      if (aantalkar == 8)
      {
        rechts = dDate.substr(5,8);
        links = links+"-";
      }
    }
  dDate=links+rechts;
  return dDate;
}

function ControleerInvoer(dDate, CheckType)
{
  var AllesGoed = true;
  aantalkar = dDate.length;  
  if ((streepjes) && (AllesGoed))
  {
    switch (aantalkar)
    {
      case 6 : dDate = ms(dDate); break;
      case 8 : dDate = ms(dDate); break;
    }
  }
  else
  {
    if (AllesGoed)
    switch (aantalkar)
    {
      case 6 : if (dDate.substr(2,5)>=2002) dDate = zssp(dDate);
               else dDate = zs(dDate);
               break;
      case 8 : dDate = zs(dDate); break; 
    }
  }
  AllesGoed = true;
  streepjes = false;
  
  if (CheckType == 1)
  {
       Nu = GetCurrentDate();
       NuJaar = parseInt(Nu.substr(0,4),10)
       xJaar = parseInt(dDate.substr(6,4),10)
       
       if (xJaar > NuJaar)
       {
          RestJaar = dDate.substr(8,2)
          temp = dDate.substr(0,6);
          dDate = temp + "19" + RestJaar
       }
  }
  
  return dDate;
}

function CheckProperDate(ddatum, Choice)
{
     var ProperDate = true;
     var dd, mm, yyyy, dpos, mpos

     if (ddatum=="dd-mm-jjjj" || ddatum=="" || ddatum.length!=10) ProperDate = false;
     dpos = ddatum.indexOf('-')
     
     if (ProperDate)
     {
          if (dpos!=2)ProperDate = false;
          else {    
                    dd = ddatum.substring(0,dpos);
                    if ( !IsNumeric(dd) ) ProperDate = false;
                    else {
                              RestDate = ddatum.substring(dpos+1,ddatum.length);
                              mpos = RestDate.indexOf('-');
                              if (mpos!=2) ProperDate = false;
                              else {    
                                        mm = RestDate.substring(0,mpos);
                                        yyyy = RestDate.substring(mpos+1,RestDate.length)
                                        if (parseInt(mm,10)<0 || parseInt(mm,10)>12 || !IsNumeric(mm) || !IsNumeric(yyyy) || yyyy.length!=4 ) ProperDate = false;
                                        else {
                                                  MaxDays = parseInt(GetMaxDays(parseInt(mm,10),parseInt(yyyy,10)),10);
                                                  if (parseInt(dd,10)<1 || parseInt(dd,10)>MaxDays) ProperDate = false;
                                                  else {
                                                            GivenDate = parseInt(yyyy+mm+dd,10);
                                                            Currentdate = parseInt(GetCurrentDate(),10);
                                                            switch (Choice)
                                                            {
                                                                 // check departure date
                                                                 case 0 : if (GivenDate <= Currentdate) ProperDate = false; break;
                                                                 // check date of birth
                                                                 case 1 : if (GivenDate >= Currentdate) ProperDate = false; break;
                                                            }
                                                       }                                                  
                                             }
                                   }
                         }
               }
     }
     return ProperDate;
}
