function isValidDate(dateStr) {
	// Checks for the following valid date formats:
	// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY

	var datePat4 = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; // 4 digit year
	var datePat2 = /^(\d{1,2})(\/)(\d{1,2})\2(\d{2})$/; // 2 digit year
	var shortyear = false;
	var matchArray = dateStr.match(datePat4); // is the format ok?
	if (matchArray == null) {
		var matchArray = dateStr.match(datePat2); // is the format ok?
		shortyear = true;
	}
	if (matchArray == null) {
		alert(dateStr + " Date is not in a valid format.")
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (shortyear) {
		year = "20" + year;
	}
	if (month < 1 || month > 12) { // check month range
		alert("Month must be between 1 and 12.");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Day must be between 1 and 31.");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Month "+month+" doesn't have 31 days!")
		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)) {
			alert("February " + year + " doesn't have " + day + " days!");
			return false;
	   	}
	}
	return true;
}


function DateDiff(start,end,interval,rounding ) {

    var iOut = 0;

    var bufferA = Date.parse( start ) ;
    var bufferB = Date.parse( end ) ;
    	
    // check that the start parameter is a valid Date. 
    if ( isNaN (bufferA) || isNaN (bufferB) ) {
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        // the user specified an incorrect interval, handle the error. 
        alert( intervalMsg ) ;
        return null ;
    }
    
    var number = bufferB-bufferA ;
    
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
        case 'd': case 'D': 
            iOut = parseInt(number / 86400000) ;
            if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
            break ;
        case 'h': case 'H':
            iOut = parseInt(number / 3600000 ) ;
            if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
            break ;
        case 'm': case 'M':
            iOut = parseInt(number / 60000 ) ;
            if(rounding) iOut += parseInt((number % 60000)/30001) ;
            break ;
        case 's': case 'S':
            iOut = parseInt(number / 1000 ) ;
            if(rounding) iOut += parseInt((number % 1000)/501) ;
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
    
    return iOut ;
}

function dateAdd( start, interval, number ) {
		
    // get the milliseconds for this Date object. 
    var buffer = Date.parse( start ) ;
	
    // check that the start parameter is a valid Date. 
    if ( isNaN (buffer) ) {
        return null ;
    }
	
    // check that an interval parameter was not numeric. 
    if ( interval.charAt == 'undefined' ) {
        return null ;
    }

    // check that the number parameter is numeric. 
    if ( isNaN ( number ) )	{
        return null ;
    }

    // so far, so good...
    //
    // what kind of add to do? 
    switch (interval.charAt(0))
    {
		case 'y': case 'Y':
			//Calculate days in year
			var ydate=new Date( buffer );
			var y = ydate.getFullYear()+1;
			var m = ydate.getMonth()+1;
			var d = ydate.getDate();
			var yend = m + '/' + d + '/' + y;
			var days = DateDiff(start,yend,"d",true)-1;
			number *= days; //years to days
			// fall through!
        case 'd': case 'D': 
            number *= 24 ; // days to hours
            // fall through! 
        case 'h': case 'H':
            number *= 60 ; // hours to minutes
            // fall through! 
        case 'm': case 'M':
            number *= 60 ; // minutes to seconds
            // fall through! 
        case 's': case 'S':
            number *= 1000 ; // seconds to milliseconds
            break ;
        default:
        // If we get to here then the interval parameter
        // didn't meet the d,h,m,s criteria.  Handle
        // the error. 		
        alert(intervalMsg) ;
        return null ;
    }
	var ndate=new Date( buffer + number );
	var dat=ndate.getDate();
	var month=ndate.getMonth()+1;
	if (dat<10) {dat="0"+dat;}
	if (month<10) {month="0"+month;}
	var newdate=dat+'/'+month+'/'+ndate.getFullYear();
    return newdate;
}

function swapDayMonth(dateStr) {
	var datePat4 = /^(\d{1,2})(\/)(\d{1,2})\2(\d{4})$/; // 4 digit year
	var datePat2 = /^(\d{1,2})(\/)(\d{1,2})\2(\d{2})$/; // 2 digit year
	var shortyear = false;
	var matchArray = dateStr.match(datePat4); // is the format ok?
	if (matchArray == null) {
		var matchArray = dateStr.match(datePat2); // is the format ok?
		shortyear = true;
	}
	if (matchArray == null) {
		return false;
	}
	first = matchArray[1];
	second = matchArray[3];
	year = matchArray[4];
	if (shortyear) {
		year = "20" + year;
	}
	dOut = second + "/" + first + "/" + year;
	return dOut;
}

function timeDiff(start,end) {
//Return difference of time (hours:minutes only)

	var S = start.split(':') ;
  	var Secs1 = (S[0]*60 + +S[1])*60;
  	var E = end.split(':') ;
  	var Secs2 = (E[0]*60 + +E[1])*60;
  	var diff=Secs2-Secs1;
  	return diff;
}
