/*
* Code for handling calendar errors
*/

(function($) { 
$.ti.calendarErrors = {
	
	errorProducts: new Array,
	
	errorPositions: {},
	
	// Check that activity dates correspond within target drop dates
	// Receives: actStart - String: Start Date of Activity being dropped YYYY-MM-DD
	// Receives: actEnd - String: End Date of Activity being dropped YYYY-MM-DD
	// Receives: dropDate - String: Date of target on grid YYYY-MM-DD
	// Recieved: duration - grid length in days of activity being dropped
	checkDates: function(actStart, actEnd, dropDate, duration) {
		var count;
		
		if(dropDate) {
			// Convert dates into milliseconds for comparison
			// N.B. replacing '-' with '/' for IE		
			actStart = new Date(actStart.replace(/-/g, '/')).getTime();
			actEnd = new Date(actEnd.replace(/-/g, '/')).getTime();
			dropDate = new Date(dropDate.replace(/-/g, '/')).getTime();
		
			for(count = 0; count < duration; count += 1) {
				if(dropDate < actStart || dropDate > actEnd) {
					// No error has occured
					return false;				
				}
				// Increment drop date by one day (24 * 60 * 60 * 1000 = 86400000)
				dropDate += 86400000;
			}	
		}		
		// If no date conflict has occured
		return true;
	},
	
	remove: function(){
		var error, activity, ref;		
		
		for(activity in $.ti.calendarErrors.errorPositions) {
			if($.ti.calendarErrors.errorPositions.hasOwnProperty(activity)) {
				for(error in $.ti.calendarErrors.errorPositions[activity]) {
					if($.ti.calendarErrors.errorPositions[activity].hasOwnProperty(error)) {
						$.ti.createCalendarActivity.deleteActivity($('#' + $.ti.calendarErrors.errorPositions[activity][error] + 'Activity'));						
					}
				}				
			}
		}		
	}
	
};
})(jQuery);
