﻿    function isDate(dateStr) {
        if(dateStr != null){
            var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
            var matchArray = dateStr.match(datePat); // is the format ok?

            if (matchArray == null) {
            return false;
            }

            month = matchArray[1]; // p@rse date into variables
            day = matchArray[3];
            year = matchArray[5];
            if (year.length == 2) {
                year = '20' + year;
            }

            if (month < 1 || month > 12 || month.length !=2) { // check month range
            return false;
            }

            if (day < 1 || day > 31 || day.length != 2) {
            return false;
            }

            if ((month==4 || month==6 || month==9 || month==11) && day==31) {
            return false;
            }

            if (month == 2) { // check for february 29th
            var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
            if (day > 29 || (day==29 && !isleap)) {
            return false;
            }
            }
            return true; // date is valid
        }
        else{
            return false;
        }
    }
    
    function checkDate (counter, txtID, arrivalID, emptyID, diffID, contentID, deliveryID, badID, timeID, timeError) { 

        HideContent(emptyID);
        HideContent(diffID);
        
        var txt = document.getElementById(txtID);
        var hdn = document.getElementById(arrivalID);
        var time = document.getElementById(timeID);
        
        var date = false;
        
        if (deliveryID == '') {
            date = true;
        } else {
            var rad = document.getElementById(deliveryID + '_1');   
            if (rad.checked) { 
                date = true;
            }
        }
                
        if (date) {
        
            if (txt.value == '') {
                ShowContent(emptyID);
                ShowContent(contentID);
                return false;
            }
            if (!(isDate(txt.value))) {
                ShowContent(badID);
                ShowContent(contentID);
                return false;
            }
            
            var end = txt.value;
            var end = end.substring(0, end.lastIndexOf('/')) + '/20' + end.substring(end.lastIndexOf('/')+1, end.length)
            
            var dtStDate = new Date(hdn.value);
            var dtEndDate = new Date(end);
            var one_day=1000*60*60*24;  
            
            //Assigning the Difference in days to the control

            var diff = Math.ceil((dtEndDate.getTime()-dtStDate.getTime())/(one_day));

            if (diff < 0) {
                ShowContent(diffID);
                ShowContent(contentID);
                return false;
            }        
            else {
                //make sure there's a selected time
                if(time.selectedIndex==0){
                    ShowContent(timeError);
                    return false;
                }
                HideContent(timeError);
                return true;
            }
            
        }
        
        HideContent(contentID);
        return true; // date is valid
    }

