//date validation code

function getMaxDay(iMonth, iYear)
{
    //function to calculate maximum day number in month
    //note javascript months are Jan=0,.. Dec=11
    var iMaxDay;

    iMonth--;   //decrement to convert to javascript month
    //if december, set to jan
    if(iMonth == 11)
    {
        iMonth = 0;
    }
    else
    {
        //else increment month
        iMonth++;
    }

    //create test date variable and set to first of next month
    var objTestDate = new Date(iYear, iMonth, 1);

    //subtract 1 day from date,
    //this will give the last day in user selected month
    objTestDate.setDate(objTestDate.getDate() - 1);

    iMaxDay = objTestDate.getDate();

    //return the maximum day
    return iMaxDay;
}


function isValidUKDate(xszDate)
{
    //must be of the form dd/mm/yyyy
    var blIsValid = true;

    if(xszDate.length < 10)
    {
        blIsValid = false;
    }
    else
    {
        var arrSplit = xszDate.split('/');

        if(arrSplit.length == 3)
        {
            blIsValid = isValidDateParts(arrSplit[2], arrSplit[1], arrSplit[0]);
        }
        else
        {
            blIsValid = false;
        }
    }
    return blIsValid;
}

function isValidDateParts(iYear, iMonth, iDay)
{
    //compares day selected with max possible day for that month
    var blIsValid = true;

    //check if iMonth is number (it could be string 'Jan')

    arrMonthNames = new Array("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
                               "Aug", "Sep", "Oct", "Nov", "Dec");

    if(isNaN(iMonth))
    {
        //convert to numeric month
        for (i=0; i< arrMonthNames.length; i++)
        {
            if(arrMonthNames[i] == iMonth)
            {
                iMonth = i+1;
                break;
            }
        }
    }

    if((iMonth <= 0) || (iMonth > 12))
    {
        blIsValid = false
    }
    else
    {
        if(iDay > getMaxDay(iMonth, iYear))
        {
            blIsValid = false;
        }
    }

    return blIsValid;
}