﻿// JScript File
function ValidateAge(source, arguments) {
    var chkDate = new Date();
    var date1 = new Date();

    chkDate = getDateObject("11/22/2009", "/");
    date1 = getDateObject(document.frmSweeps.birthday.value, "/");

    if (date1.getDate() == 'NaN')
        arguments.IsValid = false;
    else
        arguments.IsValid = isAgeValid(chkDate,date1);

    //alert(isAgeValid(chkDate,date1));
}

function getDateObject(dateString, dateSeperator)
{
      //This function return a date object after accepting 
      //a date string ans dateseparator as arguments
      var curValue=dateString;
      var sepChar=dateSeperator;
      var curPos=0;
      var cDate,cMonth,cYear;

      //extract month portion
      curPos=dateString.indexOf(sepChar);
      cMonth=dateString.substring(0,curPos);
      cMonth = cMonth - 1   //implicit conversion *watch out for bugs here*
    
      //extract day portion                       
      endPos=dateString.indexOf(sepChar,curPos+1);
      cDate=dateString.substring(curPos+1,endPos);
      
      //extract year portion                        
      curPos=endPos;
      endPos=curPos+5;                  
      cYear=curValue.substring(curPos+1,endPos);

      //Create Date Object
      dtObject=new Date(cYear,cMonth,cDate);  
      return dtObject;
}

function isAgeValid(checkDate, birthDate) {
    var diff;
    var magic = 6574;  //18 years this is the magic number :)
    
    //calculate the datediff in days
    diff = (checkDate-birthDate)/(24*60*60*1000);
    return (diff >= magic);
}


function dateDifference(strDate1,strDate2){
     datDate1= Date.parse(strDate1);
     datDate2= Date.parse(strDate2);
     alert((datDate2-datDate1)/(24*60*60*1000))
     
}