/* Functions to handle date selection boxes (eg. Day Month Year) */

function trim(str) {
  // Strip leading/trailing whitespace from a string
  return str.replace(/^\s+|\s+$/g, '');
}

function leadingZeroes(inputNumber, minimumDigits) {
	var outputNumber = inputNumber.toString();
	while (outputNumber.length < minimumDigits) {
		outputNumber = "0" + outputNumber;
	}
	return outputNumber;
}

function validUKDateTime(datetimeString) {
	/* Validates UK format dates and/or times (eg. dd/mm/yy hh:mm:ss)*/
  return datetimeString.match(/^(?=\d)(?:(?!(?:(?:0?[5-9]|1[0-4])(?:\.|-|\/)10(?:\.|-|\/)(?:1582))|(?:(?:0?[3-9]|1[0-3])(?:\.|-|\/)0?9(?:\.|-|\/)(?:1752)))(31(?!(?:\.|-|\/)(?:0?[2469]|11))|30(?!(?:\.|-|\/)0?2)|(?:29(?:(?!(?:\.|-|\/)0?2(?:\.|-|\/))|(?=\D0?2\D(?:(?!000[04]|(?:(?:1[^0-6]|[2468][^048]|[3579][^26])00))(?:(?:(?:\d\d)(?:[02468][048]|[13579][26])(?!\x20BC))|(?:00(?:42|3[0369]|2[147]|1[258]|09)\x20BC))))))|2[0-8]|1\d|0?[1-9])([-.\/])(1[012]|(?:0?[1-9]))\2((?=(?:00(?:4[0-5]|[0-3]?\d)\x20BC)|(?:\d{4}(?:$|(?=\x20\d)\x20)))\d{4}(?:\x20BC)?)(?:$|(?=\x20\d)\x20))?((?:(?:0?[1-9]|1[012])(?::[0-5]\d){0,2}(?:\x20[aApP][mM]))|(?:[01]\d|2[0-3])(?::[0-5]\d){1,2})?$/) != null;	
}

function validDateTimeSelects(dayInput, monthInput, yearInput, hourInput, minuteInput) {
	/* Checks the contents of a set of date select fields (eg. Day Month Year) for a valid date. */
	/* If the date is optional (ie. if there are more than 12 options for month), and no values  */
	/* have been selected, the check is bypassed. */
	if(typeof dayInput == 'undefined' || typeof monthInput == 'undefined' || typeof yearInput == 'undefined') {
		alert('validDateSelects(dayInput, monthInput, yearInput): Required parameter missing');
	  return false;
	}
	dayInputObject = document.getElementById(dayInput);
	dayValue = dayInputObject[dayInputObject.selectedIndex].value;
	monthInputObject = document.getElementById(monthInput);
	monthValue = monthInputObject[monthInputObject.selectedIndex].value;
	yearInputObject = document.getElementById(yearInput);
	yearValue = yearInputObject[yearInputObject.selectedIndex].value;
	if(typeof hourInput != 'undefined' && typeof minuteInput != 'undefined') {
		hourInputObject = document.getElementById(hourInput);
		hourValue = hourInputObject[hourInputObject.selectedIndex].value;
		minuteInputObject = document.getElementById(minuteInput);
		minuteValue = minuteInputObject[minuteInputObject.selectedIndex].value;
		if(monthInputObject.length > 12 && dayInputObject.selectedIndex == 0 && monthInputObject.selectedIndex == 0 && yearInputObject.selectedIndex == 0 && hourInputObject.selectedIndex == 0 && minuteInputObject.selectedIndex == 0){
			return true;
		}
		if(!validUKDateTime(leadingZeroes(dayValue, 2) + '/' + leadingZeroes(monthValue, 2) + '/' + leadingZeroes(yearValue, 4) + ' ' + leadingZeroes(hourValue, 2) + ':' + leadingZeroes(minuteValue, 2))){
			return false;
		}
	} else {
		if(monthInputObject.length > 12 && dayInputObject.selectedIndex == 0 && monthInputObject.selectedIndex == 0 && yearInputObject.selectedIndex == 0){
			return true;
		}
		if(!validUKDateTime(leadingZeroes(dayValue, 2) + '/' + leadingZeroes(monthValue, 2) + '/' + leadingZeroes(yearValue, 4))){
			return false;
		}
	}
	return true;
}

function updateDateSelectDays(dayInput, monthInput, yearInput) {
  // Change the number of days in a the dayInput select box, based on the selections
	// in the monthInput and yearInput select boxes.

  var y = document.getElementById(yearInput).options[document.getElementById(yearInput).selectedIndex].value;
  var m = document.getElementById(monthInput).options[document.getElementById(monthInput).selectedIndex].value;
  var d;
	if ( (m == 4) || (m == 6) || (m == 9) || (m == 11) ) {days = 30;}
	else if (m == 2) {
		if ( (Math.floor(y/4) == (y/4)) && ((Math.floor(y/100) != (y/100)) || (Math.floor(y/400) == (y/400))) )
			days = 29;
		else
			days = 28;
	}
	else {days = 31;}
	if(document.getElementById(monthInput).length > 12){
		if ((days + 1) > document.getElementById(dayInput).length) {
			for (i = document.getElementById(dayInput).length; i < (days + 1); i++) {
				document.getElementById(dayInput).length = (days + 1);
				document.getElementById(dayInput).options[i].text = i;
				document.getElementById(dayInput).options[i].value = i;
			}
		} else if ((days + 1) < document.getElementById(dayInput).length) {
			var selectedDay = document.getElementById(dayInput).selectedIndex;
			document.getElementById(dayInput).length = (days + 1);
			if (selectedDay > days) 
				document.getElementById(dayInput).selectedIndex = days;
		}
	}else{
		if (days > document.getElementById(dayInput).length) {
			for (i = document.getElementById(dayInput).length; i < days; i++) {
				document.getElementById(dayInput).length = days;
				document.getElementById(dayInput).options[i].text = i + 1;
				document.getElementById(dayInput).options[i].value = i + 1;
			}
		} else if (days < document.getElementById(dayInput).length) {
			var selectedDay = document.getElementById(dayInput).selectedIndex;
			document.getElementById(dayInput).length = days;
			if (selectedDay > days - 1) 
				document.getElementById(dayInput).selectedIndex = days - 1;
		}
	}

}

function copyDateSelects(fromDayInput, fromMonthInput, fromYearInput, toDayInput, toMonthInput, toYearInput) {
  // Copy date values from one group of select boxes to another identical group.

  document.getElementById(toMonthInput).selectedIndex = document.getElementById(fromMonthInput).selectedIndex;
  document.getElementById(toYearInput).selectedIndex = document.getElementById(fromYearInput).selectedIndex;
	updateDateSelectDays(toDayInput, toMonthInput, toYearInput);
  document.getElementById(toDayInput).selectedIndex = document.getElementById(fromDayInput).selectedIndex;
}

function copyDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput, toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput) {
  // Copy date values from one group of select boxes to another identical group.

  document.getElementById(toMonthInput).selectedIndex = document.getElementById(fromMonthInput).selectedIndex;
  document.getElementById(toYearInput).selectedIndex = document.getElementById(fromYearInput).selectedIndex;
	updateDateSelectDays(toDayInput, toMonthInput, toYearInput);
  document.getElementById(toDayInput).selectedIndex = document.getElementById(fromDayInput).selectedIndex;
  document.getElementById(toHourInput).selectedIndex = document.getElementById(fromHourInput).selectedIndex;
  document.getElementById(toMinuteInput).selectedIndex = document.getElementById(fromMinuteInput).selectedIndex;
}

function fixToDateSelects(fromDayInput, fromMonthInput, fromYearInput, toDayInput, toMonthInput, toYearInput) {
	if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput) && validDateTimeSelects(toDayInput, toMonthInput, toYearInput)) {
		// Construct 'from' date comparison string
		fromDayInputObject = document.getElementById(fromDayInput);
		fromDayValue = fromDayInputObject[fromDayInputObject.selectedIndex].value;
		fromMonthInputObject = document.getElementById(fromMonthInput);
		fromMonthValue = fromMonthInputObject[fromMonthInputObject.selectedIndex].value;
		fromYearInputObject = document.getElementById(fromYearInput);
		fromYearValue = fromYearInputObject[fromYearInputObject.selectedIndex].value;
		fromDate = leadingZeroes(fromYearValue, 4) + leadingZeroes(fromMonthValue, 2) + leadingZeroes(fromDayValue, 2);
		// Construct 'to' date comparison string
		toDayInputObject = document.getElementById(toDayInput);
		toDayValue = toDayInputObject[toDayInputObject.selectedIndex].value;
		toMonthInputObject = document.getElementById(toMonthInput);
		toMonthValue = toMonthInputObject[toMonthInputObject.selectedIndex].value;
		toYearInputObject = document.getElementById(toYearInput);
		toYearValue = toYearInputObject[toYearInputObject.selectedIndex].value;
		toDate = leadingZeroes(toYearValue, 4) + leadingZeroes(toMonthValue, 2) + leadingZeroes(toDayValue, 2);
		// Compare dates
		if (fromDate > toDate) {
			copyDateSelects(fromDayInput, fromMonthInput, fromYearInput, toDayInput, toMonthInput, toYearInput);
		}
	} else if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput)) {
		copyDateSelects(fromDayInput, fromMonthInput, fromYearInput, toDayInput, toMonthInput, toYearInput);
	}
}

function fixFromDateSelects(fromDayInput, fromMonthInput, fromYearInput, toDayInput, toMonthInput, toYearInput) {
	if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput) && validDateTimeSelects(toDayInput, toMonthInput, toYearInput)) {
		// Construct 'from' date comparison string
		fromDayInputObject = document.getElementById(fromDayInput);
		fromDayValue = fromDayInputObject[fromDayInputObject.selectedIndex].value;
		fromMonthInputObject = document.getElementById(fromMonthInput);
		fromMonthValue = fromMonthInputObject[fromMonthInputObject.selectedIndex].value;
		fromYearInputObject = document.getElementById(fromYearInput);
		fromYearValue = fromYearInputObject[fromYearInputObject.selectedIndex].value;
		fromDate = leadingZeroes(fromYearValue, 4) + leadingZeroes(fromMonthValue, 2) + leadingZeroes(fromDayValue, 2);
		// Construct 'to' date comparison string
		toDayInputObject = document.getElementById(toDayInput);
		toDayValue = toDayInputObject[toDayInputObject.selectedIndex].value;
		toMonthInputObject = document.getElementById(toMonthInput);
		toMonthValue = toMonthInputObject[toMonthInputObject.selectedIndex].value;
		toYearInputObject = document.getElementById(toYearInput);
		toYearValue = toYearInputObject[toYearInputObject.selectedIndex].value;
		toDate = leadingZeroes(toYearValue, 4) + leadingZeroes(toMonthValue, 2) + leadingZeroes(toDayValue, 2);
		// Compare dates
		if (fromDate > toDate) {
			copyDateSelects(toDayInput, toMonthInput, toYearInput, fromDayInput, fromMonthInput, fromYearInput);
		}
	} else if (validDateTimeSelects(toDayInput, toMonthInput, toYearInput)) {
		copyDateSelects(toDayInput, toMonthInput, toYearInput, fromDayInput, fromMonthInput, fromYearInput);
	}
}

function fixToDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput, toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput) {
	if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput) && validDateTimeSelects(toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput)) {
		// Construct 'from' date/time comparison string
		fromDayInputObject = document.getElementById(fromDayInput);
		fromDayValue = fromDayInputObject[fromDayInputObject.selectedIndex].value;
		fromMonthInputObject = document.getElementById(fromMonthInput);
		fromMonthValue = fromMonthInputObject[fromMonthInputObject.selectedIndex].value;
		fromYearInputObject = document.getElementById(fromYearInput);
		fromYearValue = fromYearInputObject[fromYearInputObject.selectedIndex].value;
		fromHourInputObject = document.getElementById(fromHourInput);
		fromHourValue = fromHourInputObject[fromHourInputObject.selectedIndex].value;
		fromMinuteInputObject = document.getElementById(fromMinuteInput);
		fromMinuteValue = fromMinuteInputObject[fromMinuteInputObject.selectedIndex].value;
		fromDateTime = leadingZeroes(fromYearValue, 4) + leadingZeroes(fromMonthValue, 2) + leadingZeroes(fromDayValue, 2) + leadingZeroes(fromHourValue, 2) + leadingZeroes(fromMinuteValue, 2);
		// Construct 'to' date/time comparison string
		toDayInputObject = document.getElementById(toDayInput);
		toDayValue = toDayInputObject[toDayInputObject.selectedIndex].value;
		toMonthInputObject = document.getElementById(toMonthInput);
		toMonthValue = toMonthInputObject[toMonthInputObject.selectedIndex].value;
		toYearInputObject = document.getElementById(toYearInput);
		toYearValue = toYearInputObject[toYearInputObject.selectedIndex].value;
		toHourInputObject = document.getElementById(toHourInput);
		toHourValue = toHourInputObject[toHourInputObject.selectedIndex].value;
		toMinuteInputObject = document.getElementById(toMinuteInput);
		toMinuteValue = toMinuteInputObject[toMinuteInputObject.selectedIndex].value;
		toDateTime = leadingZeroes(toYearValue, 4) + leadingZeroes(toMonthValue, 2) + leadingZeroes(toDayValue, 2) + leadingZeroes(toHourValue, 2) + leadingZeroes(toMinuteValue, 2);
		// Compare dates
		if (fromDateTime > toDateTime) {
			copyDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput, toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput);
		}
	} else if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput)) {
		copyDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput, toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput);
	}
}

function fixFromDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput, toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput) {
	if (validDateTimeSelects(fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput) && validDateTimeSelects(toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput)) {
		// Construct 'from' date/time comparison string
		fromDayInputObject = document.getElementById(fromDayInput);
		fromDayValue = fromDayInputObject[fromDayInputObject.selectedIndex].value;
		fromMonthInputObject = document.getElementById(fromMonthInput);
		fromMonthValue = fromMonthInputObject[fromMonthInputObject.selectedIndex].value;
		fromYearInputObject = document.getElementById(fromYearInput);
		fromYearValue = fromYearInputObject[fromYearInputObject.selectedIndex].value;
		fromHourInputObject = document.getElementById(fromHourInput);
		fromHourValue = fromHourInputObject[fromHourInputObject.selectedIndex].value;
		fromMinuteInputObject = document.getElementById(fromMinuteInput);
		fromMinuteValue = fromMinuteInputObject[fromMinuteInputObject.selectedIndex].value;
		fromDateTime = leadingZeroes(fromYearValue, 4) + leadingZeroes(fromMonthValue, 2) + leadingZeroes(fromDayValue, 2) + leadingZeroes(fromHourValue, 2) + leadingZeroes(fromMinuteValue, 2);
		// Construct 'to' date/time comparison string
		toDayInputObject = document.getElementById(toDayInput);
		toDayValue = toDayInputObject[toDayInputObject.selectedIndex].value;
		toMonthInputObject = document.getElementById(toMonthInput);
		toMonthValue = toMonthInputObject[toMonthInputObject.selectedIndex].value;
		toYearInputObject = document.getElementById(toYearInput);
		toYearValue = toYearInputObject[toYearInputObject.selectedIndex].value;
		toHourInputObject = document.getElementById(toHourInput);
		toHourValue = toHourInputObject[toHourInputObject.selectedIndex].value;
		toMinuteInputObject = document.getElementById(toMinuteInput);
		toMinuteValue = toMinuteInputObject[toMinuteInputObject.selectedIndex].value;
		toDateTime = leadingZeroes(toYearValue, 4) + leadingZeroes(toMonthValue, 2) + leadingZeroes(toDayValue, 2) + leadingZeroes(toHourValue, 2) + leadingZeroes(toMinuteValue, 2);
		// Compare dates
		if (fromDateTime > toDateTime) {
			copyDateTimeSelects(toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput, fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput);
		}
	} else if (validDateTimeSelects(toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput)) {
		copyDateTimeSelects(toDayInput, toMonthInput, toYearInput, toHourInput, toMinuteInput, fromDayInput, fromMonthInput, fromYearInput, fromHourInput, fromMinuteInput);
	}
}

function toggleTimeSelects(hourInput, minuteInput, isAllDay) {
	hourInputObject = document.getElementById(hourInput);
	minuteInputObject = document.getElementById(minuteInput);
	if (isAllDay) {
		hourInputObject.disabled = true;
		minuteInputObject.disabled = true;
	} else {
		hourInputObject.disabled = false;
		minuteInputObject.disabled = false;
	}
}