// File: nis.js
// by: o.
//
// Remarks: To access objects with javascript such as a form name=bob with a 
//          select name=jim.  We just need to call document.bob.jim and properties
//          for that select can be changed.
//
//          However name=jim isn't necessarily unique. If you don't want to or can't
//          call jim by using document.bob.jim, you can use the function
//          document.getElementsByName('jim')[0] For every name=jim it will be
//          stored in an element in the array.
//          ID's are unique on a page and can be accesed directly by getDlementByID
//
//          For my purposes, we must choose unique names for each object on a page.


// Start Date ***************************************************************
// base script by Arash Ramin (http://www.digitalroom.net)

// Purpose: sets the start and end dates for the forms
//          on the New Order Page. Month, Day, and Year are set to current date
// Pre: tValue is in milliseconds
function setSameStartEndDate(tValue)
{
	var currentDate = new Date();
	currentDate.setTime(tValue);
	
	setCurrentDate("start", currentDate);
	setCurrentDate("end", currentDate);
}

function setStartEndDate(startTime, endTime)
{
	var startDate = new Date();
	startDate.setTime(startTime);
	
	var endDate = new Date();
	endDate.setTime(endTime);
	
	setCurrentDate("start", startDate);
	setCurrentDate("end", endDate);
}

// Purpose: In these drop down menus, these functions sets the
//          year, month, day to today's values.
// If the current year is 2000, the drop down menu will have years 1999 to 2010
// In order to select the correct index we choose index 1, which will have year 2000
// The date and month begin at 0 or 1. so the index of months 0 1 2... are also 0 1 2...
// Pre: prefixVal - a prefix to the date values. eg. start + Year. Allows us to have one
//      function that can affect different names.
// Post: Selects the current date and sets the year, month, day to objects with the
//       matching name
function setCurrentDate(prefixVal, currentDate) 
{
	
	document.getElementsByName(prefixVal.concat("Year"))[0].selectedIndex = 1;
	document.getElementsByName(prefixVal.concat("Month"))[0].selectedIndex = currentDate.getMonth();
	
	setDays(prefixVal);  
	document.getElementsByName(prefixVal.concat("Day"))[0].selectedIndex = currentDate.getDate() - 1;
}

// Purpose: Determines the total days according to the month and year
function setDays(prefixVal)
{
	var y = document.getElementsByName(prefixVal.concat("Year"))[0].options[document.getElementsByName(prefixVal.concat("Year"))[0].selectedIndex].value;
	var m = document.getElementsByName(prefixVal.concat("Month"))[0].selectedIndex;
	var d;

	// MONTHS
	if ( (m == 3) || (m == 5) || (m == 8) || (m == 10) ) 
	{
		days = 30;
	}
	else if (m == 1) 
	{
	// check for leapyear - Any year divisible by 4, except those divisible by 100 (but NOT 400)
		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;
	}

	// DAYS
	// if (days in new month > current days) then we must add the extra days
	if (days > document.getElementsByName(prefixVal.concat("Day"))[0].length) 
	{
		for (i = document.getElementsByName(prefixVal.concat("Day"))[0].length; i < days; i++)
		{
			document.getElementsByName(prefixVal.concat("Day"))[0].length = days;
			document.getElementsByName(prefixVal.concat("Day"))[0].options[i].text = i + 1;
			document.getElementsByName(prefixVal.concat("Day"))[0].options[i].value = i + 1;
		}
	}
  
	// if (days in new month < current days) then we must delete the extra days
	if (days < document.getElementsByName(prefixVal.concat("Day"))[0].length) 
	{
		document.getElementsByName(prefixVal.concat("Day"))[0].length = days;
		
		if (document.getElementsByName(prefixVal.concat("Day"))[0].selectedIndex == -1) 
	 		document.getElementsByName(prefixVal.concat("Day"))[0].selectedIndex = days - 1;
	}
}

// Purpose: Displays the years in the years drop down menu
// Begins from the previous year until the next 10 years.
// eg. curr year = 2000.   1999-2010
function displayYearForm()
{
	var currentDate = new Date();
	var currentYear = currentDate.getYear();

	if (currentYear < 2000) 
	{
		currentYear += 1900;
	}

	var dd = currentYear - 1;
	
	for (i = 0; i < 6; i++) 
	{
		document.writeln('<OPTION VALUE="' + dd + '">' + dd);
		dd++;
	}
}

// Purpose: sets the time in the drop down menus. Does only a few checks.
// Pre: a prefix string, a hour number 1-12 and a minute 0-59
function setCurrentTime(prefixVal, hourVal, minVal) 
{	
	AMPMVal = (hourVal < 11) ? "am" : "pm";
	
	hourVal = hourVal % 12;
	
	if (hourVal == 0) 
		hourVal = 12;
	
	remainder = minVal % 5;			// round to lowest multiple of 5
	minVal -= remainder;
	
	document.getElementsByName(prefixVal.concat("Hour"))[0].value = hourVal;
	document.getElementsByName(prefixVal.concat("Min"))[0].value = minVal;
	document.getElementsByName(prefixVal.concat("AMPM"))[0].value = AMPMVal;
}

// end DATE *******************************************************************************

// Purpose: the search field has the words 'Keyword Search'. Once it comes into focus
//          it becomes blank.
// Remarks: we use just s cause search is a reserved word
function emptySearch()
{
	if (document.searchForm.s.value == "Keyword Search") 
	{ 
		document.searchForm.s.value = "";	
	}
}

// START Form Field JUMPING ***************************************************************

var downStrokeField;

// Purpose: after a number of letters have been typed into a field
//          this function automatically jumps to the next one
// Pre: fieldName - current field
//      nextFieldName - the field to jump to once letters typed
//      fakeMaxLength - the total number of chars
//      formNumber (default val) - the form on the page beginning at 0, 
//                                 we default to the forms[0] if not specified.
//
function autoJump(fieldName, nextFieldName, fakeMaxLength, formNumber)
{
	formNumber = formNumber || 0;
	var myForm = document.forms[formNumber]; //[document.forms.length - 1];

	var myField = myForm.elements[fieldName];
	myField.nextField = myForm.elements[nextFieldName];
	
	if (myField.maxLength == null)
		myField.maxLength = fakeMaxLength;
	
	myField.onkeydown = autoJumpKeyDown;
	myField.onkeyup = autoJumpKeyUp;
}

function autoJumpKeyDown()
{
	this.beforeLength = this.value.length;
	downStrokeField = this;
}

function autoJumpKeyUp()
{
	if ( (this == downStrokeField) && 
		  (this.value.length > this.beforeLength) && 
		  (this.value.length >= this.maxLength) )
		this.nextField.focus();

	downStrokeField = null;
}

// END FORM FIELD JUMPING ******************************************************************

// Purpose: Limits the total characters in a <textarea>
// Pre: the name of the field, the name of the <input> that holds the total characters
//      and the total number of chars
// Remarks; onKeyDown and onKeyUp is called in this manner
// onKeyDown="limitTextArea(this.form.description, this.form.textCount, 128);" 
function limitTextArea(limitField, limitCount, maxChars) 
{
	if (limitField.value.length > maxChars) 
		limitField.value = limitField.value.substring(0, maxChars);
	else
		limitCount.value = limitField.value.length;
}
