// JavaScript Document
//  This file sets the swapping of a START and an END date field 
//	based on a range (i.e. Last 2 Months) from a select control 

function setNewDate(id) {
	var now = new Date();
	var range = document.getElementById(id).value;
	var startDateField = document.getElementById("startDate");
	var endDateField = document.getElementById("endDate");
	var dateSummaryBlock = document.getElementById("dateRangeSummary");
	var customBlock = document.getElementById("customDateBlock");
	//range -= 0
	
	
	//for the custom date range
	if (range == "cust") { 
		customBlock.style.display = "block"; //swap the display for the blocks
		dateSummaryBlock.style.display = "none";
		
		var startDate = "";// update the input field value
		var endDate = "";
	}
	// if passed 0 from the caption option item
	else if (range == 0) {
		customBlock.style.display = "none";
		dateSummaryBlock.style.display = "none";
		
	}
	// if passed 30 or greater, 
	// set the date back the number of days in passed in range
	// Expected: 30, 60 or 90 days
	else if (range >= 30) {
		customBlock.style.display = "none";
		dateSummaryBlock.style.display = "";
		today = now.getDate();
		newDate = today - range;
		now.setDate(newDate);
		
		var newNow = new Date();
		
		var startDate = formatTwos((now.getMonth()+1)) + "/" + formatTwos(now.getDate()) + "/" + now.getFullYear();
		var endDate = formatTwos((newNow.getMonth()+1)) + "/" + formatTwos(newNow.getDate()) + "/" + newNow.getFullYear();
	}
	// if passed 3 or less, 
	// set the month back the number of months in passed in range
	// Expected: 1, 2 or 3 months
	else if (range <= 3 && range != 0) {		
		customBlock.style.display = "none";
		dateSummaryBlock.style.display = "";
		today = now.getMonth();
		newDate = today - range;
		now.setDate(1);
		now.setMonth(newDate);//this now contains the date of the first day of the range
		
		var startDate = formatTwos((now.getMonth()+1)) + "/" + formatTwos(now.getDate()) + "/" + now.getFullYear();
		
		// find the last day of last month to set the end of the date range
		var newNow = new Date();
		var thisMonth = newNow.getMonth();
		newNow.setMonth(thisMonth-1);
		
		var year = newNow.getYear();
		var month = newNow.getMonth();
		var lastDay = daysInMonth(year, month);
		newNow.setDate(lastDay);//this now contains the date of the last day of last month
		
		var endDate = formatTwos((newNow.getMonth()+1)) + "/" + formatTwos(newNow.getDate()) + "/" + newNow.getFullYear();
	}
		
		dateSummaryBlock.innerHTML = startDate + "&nbsp; to &nbsp;" + endDate //update the summary area
		
		startDateField.value = startDate // update the input fields
		endDateField.value = endDate
}
	
	
	
// This returns the number of days in a month
function daysInMonth(year, month) {
		var newDate = new Date(year, month);
		var days = 28 //days in the shortest possible month
		var nextMonth = false
		
		while (!nextMonth) {
			newDate.setDate(days + 1);
			var newMonth = newDate.getMonth();
			
			if (month != newMonth) {
				nextMonth = true;
			}
			else {
				days++;
			}
		}
		return days;
}

// This ensures we have a two digit date and month
function formatTwos(number) {
	var numberString = number.toString();
	if (numberString.length != 2) {
		number = "0" + number
	}
	return number;
}


// This resets the form
function resetForm() {

	if (document.getElementById('formRequired')) {document.getElementById('formRequired').reset();}
	
	// close expanding areas
	var customBlock = document.getElementById("customDateBlock");
	var dateSummaryBlock = document.getElementById("dateRangeSummary");
	var startTyping = document.getElementById("startTyping");
	
	if (customBlock) {customBlock.style.display = "none";}
	if (dateSummaryBlock) {dateSummaryBlock.style.display = "none";}
	if (startTyping) {clearTextLabel(startTyping, "clear"); myfilter.reset();}
}

// This function clears the content of a text input
// It changes the color, and returns the field to it's default value
function clearTextLabel(textID, eventName) {
	var defaultText = textID.defaultValue;
	
	if (eventName == "focus") {
		if (textID.value == defaultText) {
		textID.value = "";
		textID.style.color = "";
		}
	}
	else if (eventName == "blur") {
		if (textID.value == "") {
		textID.value = defaultText;
		textID.style.color = "#999";
		}
	}
	else if (eventName == "clear") {
		textID.value = defaultText;
		textID.style.color = "#999";
	}
	
}

// This resets the filter and label on an onunload event
function clearFilterText(formID, fieldID) {
	var inputId = document.getElementById(fieldID);
	//var inputDirect = document.formID.fieldID
	if (inputId) {
		clearTextLabel(inputId, 'clear');
		myfilter.reset(); 
		//if (inputDirect) {inputDirect = document.formID.fieldID.defaultValue}
		//alert("I read: " + inputId);
		}
		else {
			alert("Could not find the object: " + inputId.name);
		}
}
