// AUTHOR:		Jason Noftall
// DATE:		Jan. 30, 2001
// DESCRIPTION:		Validates a file's extension ensuring it is either a JPG or GIF.
// PARAMETERS:		txtField = reference to an HTML text element
// RETURNS:		Boolean, True if valid...False if otherwise.
function validPicExtension(txtField)
{
	var strErrorMsg = "Invalid image name.  File must be either a JPEG (jpg/jpeg) or GIF (gif) image.";

	var strPic = trimString(txtField.value).toLowerCase();
	if (strPic.length > 0)
	{
		var intLoc = strPic.lastIndexOf(".") + 1;
		if (intLoc == 0)
		{
			alert(strErrorMsg);
			txtField.select();
			txtField.focus();
			return false;
		} // if (intLoc == 0)
		else
		{
			var strExt = strPic.substring(intLoc);
			switch (strExt)
			{
				case "jpg":
				case "jpeg":
				case "gif":
					break;
				default:
				{
					alert(strErrorMsg);
					txtField.select();
					txtField.focus();
					return false;
				}
			} // switch (strExt)
		} // else (intLoc == 0)
	} // if (strPic.length > 0)
	else
	{
		alert(strErrorMsg);
		txtField.select();
		txtField.focus();
		return false;
	} // else (strPic.length > 0)

	return true;
}

function confirmDelete(strURL)
{
	// Prompt the user with an "OK/Cancel" dialog...
	if (confirm("Are you sure you wish to delete this item?")) 
	{
		// If yes, then go to the specified URL...
		window.document.location = strURL
	}

	// If "No" then do nothing.
} 

// AUTHOR:		Kenny Young
// DATE:		March 26, 2002
// DESCRIPTION:		Validates the Patron's form element on add
// PARAMETERS:		None
// RETURNS:		Nothing - Submits the form if all data is valid
function validatePatron()
{

	// Validate the patron's first name
	if (trimString(window.document.frmPatron.txtFName.value) == '')
	{
		alert('Please enter a patron\'s first name.');
		window.document.frmPatron.txtFName.focus();
	}
	// Validate the patron's last name
	else if (trimString(window.document.frmPatron.txtLName.value) == '')
	{
		alert('Please enter a patron\'s last name.');
		window.document.frmPatron.txtLName.focus();
	}

	// Validate the patron's Bid Number
	// @@@ function not needed since the bid and ticket numbers are the same
	else if (!ensureNumeric(window.document.frmPatron.txtTicketNum.value,1) || window.document.frmPatron.txtTicketNum.value.length != 4 )
	{
		alert('Please enter a patron\'s bid number. This must be a 4-digit numeric value.');
		window.document.frmPatron.txtTicketNum.focus();
		window.document.frmPatron.txtTicketNum.select();
	}

	// Validate the patron's Ticket Number
	else if (trimString(window.document.frmPatron.txtTicketNum.value) == '')
	{
		alert('Please enter a patron\'s ticket number.');
		window.document.frmPatron.txtTicketNum.focus();
	}
	
	// Validate the patron's Table Number
	//else if (trimString(window.document.frmPatron.txtTableNum.value) == '')
	//{
	//	alert('Please enter a patron\'s table number.');
	//	window.document.frmPatron.txtTableNum.focus();
	//}
	
	// Submit the form
	else
	{
		window.document.frmPatron.submit();
	}
	
}


// AUTHOR:		Kenny Young
// DATE:		Feb 27, 2002
// DESCRIPTION:		Validates the product form element on add
// PARAMETERS:		None
// RETURNS:		Nothing - Submits the form if all data is valid
function validateProduct()
{
	// Validate the product name
	if (trimString(window.document.frmProduct.txtName.value) == '')
	{
		alert('Please enter the \'Product Name\'.');
		window.document.frmProduct.txtName.focus();
	}
	// Validate the cost to ensure it is a number
	else if (!ensureNumeric(window.document.frmProduct.txtCost.value,3))
	{
		alert('Please enter a number for the \'Product Cost\', without the \'$\'.');
		window.document.frmProduct.txtCost.select();
		window.document.frmProduct.txtCost.focus();
	}
	else if (!ensureNumeric(window.document.frmProduct.txtRBAmount.value,3))
	{
		alert('Please enter a number for the \'Reserved Bid\', without the \'$\'.');
		window.document.frmProduct.txtRBAmount.select();
		window.document.frmProduct.txtRBAmount.focus();
	}
	else if (!ensureNumeric(window.document.frmProduct.txtValue.value,3))
	{
		alert('Please enter a number for the \'Actual/Estimated Retail Value\', without the \'$\'.');
		window.document.frmProduct.txtValue.select();
		window.document.frmProduct.txtValue.focus();
	}
	else if (!ensureNumeric(window.document.frmProduct.txtMinimumBid.value,3))
	{
		alert('Please enter a number for the \'Opening Bid\', without the \'$\'.');
		window.document.frmProduct.txtMinimumBid.select();
		window.document.frmProduct.txtMinimumBid.focus();
	}
	else if (!ensureNumeric(window.document.frmProduct.txtMinimumBidIncr.value,3))
	{
		alert('Please enter a number for the \'Minimum Bid Increments\', without the \'$\'.');
		window.document.frmProduct.txtMinimumBidIncr.select();
		window.document.frmProduct.txtMinimumBidIncr.focus();
	}
	// Validate the product prize collector
	else if (trimString(window.document.frmProduct.txtPrizeCollector.value) == '')
	{
		alert('Please enter the name of the \'Product Prize Collector\'.');
		window.document.frmProduct.txtPrizeCollector.focus();
	}
	else
	{
		// Has an image been uploaded?
		//if (trimString(window.document.frmProduct.txtPhoto.value) == '')
		//{
			// No image was uploaded, submit the form...
		//	window.document.frmProduct.submit();
		//} // if (trimString(window.document.frmPr...
		//else
		//{
	//		if (validPicExtension(window.document.frmProduct.txtPhoto))
	//		{
				// Valid image, submit the form...
				window.document.frmProduct.submit();
	//		} // if (validPicExtension(window.docu...
	//	} // else (trimString(window.document.frmPr...
	}
}


// AUTHOR:		Jason Noftall
// DATE:		Apr. 16, 2002
// DESCRIPTION:		Validates the Prize on Add/Edit.
// PARAMETERS:		None
// RETURNS:		Nothing - Submits the form if all data is valid
function validatePrize()
{
	// Validate the Prize name...
	if (trimString(window.document.frmPrize.txtName.value) == '')
	{
		alert('Please enter the \'Prize Name\'.');
		window.document.frmPrize.txtName.focus();
		return;
	}

	window.document.frmPrize.submit();
}


// AUTHOR:		Jason Noftall
// DATE:		Oct. 16, 2001
// DESCRIPTION:		Trims any leading/trailing spaces from the incoming string.
// PARAMETERS:		strIn = string that needs to be trimmed
// RETURNS:		String, input string less any leading/trailing spaces.
function trimString(strIn)
{
	var strOut = new String(strIn);						// Return value

	// If there are trailing spaces, instantiate a new
	// String object with the original string less the spaces...
	while (strOut.charAt(strOut.length - 1) == ' ')
	{
		strOut = new String(strOut.substring(0, strOut.length - 1));
	}

	// If there are leading spaces, instantiate a new
	// String object with the original string less the spaces...
	while (strOut.charAt(0) == ' ')
	{
		strOut = new String(strOut.substring(1, strOut.length));
	}

	return strOut;
}


// AUTHOR:		Jason Noftall
// DATE:		Nov. 1, 2001
// DESCRIPTION:		Validates strings to check for certain numeric attributes.
// PARAMETERS:		strIn = string that needs numeric validation
//			intFormat = numeric value indicating the type of
//			validation to perform.
// 			1 = Whole numbers, positive only
// 			2 = Whole numbers, positive and negative
// 			3 = Decimal numbers, positive only
// 			4 = Decimal numbers, positive and negative
// RETURNS:		Boolean, True if valid...False if otherwise.
function ensureNumeric(strIn,intFormat)
{
	blnReturn = false;					// Return value
	strNew = trimString(strIn);				// Trim spaces from input string
	var strChar;						// Get 1 char at a time
	var intDecCount = 0;					// Decimal count

	// intFormat
	// ---------
	// 1 = Whole numbers, positive only
	// 2 = Whole numbers, positive and negative
	// 3 = Decimal numbers, positive only
	// 4 = Decimal numbers, positive and negative

	for (var i=0; i < strNew.length; i++)
	{
		// Get 1 char at a time...
		strChar = strNew.substring(i,i+1);

		switch (intFormat)
		{
			case 1:
			{
				// whole positive numbers only
				switch (strChar)
				{
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
						blnReturn = true;
						break;
					default:
						return false;
				} // switch (strChar)

				break;
			} // case 1:

			case 2:
			{
				// whole positive or negative numbers only
				switch (strChar)
				{
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
						blnReturn = true;
						break;
					case "-":
						if (i == 0)
						{
							blnReturn = true;
							break;
						}
						else
						{
							return false;
						}
					default:
						return false;
				} // switch (strChar)

				break;
			} // case 2:

			case 3:
			{
				// decimal positive numbers only
				switch (strChar)
				{
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
						blnReturn = true;
						break;
					case ".":
						intDecCount++;
						if (intDecCount == 1)
						{
							blnReturn = true;
							break;
						}
						else
						{
							return false;
						}
					default:
						return false;
				} // switch (strChar)

				break;
			} // case 3:

			case 4:
			{
				// decimal positive numbers only
				switch (strChar)
				{
					case "0":
					case "1":
					case "2":
					case "3":
					case "4":
					case "5":
					case "6":
					case "7":
					case "8":
					case "9":
						blnReturn = true;
						break;
					case "-":
						if (i == 0)
						{
							blnReturn = true;
							break;
						}
						else
						{
							return false;
						}

					case ".":
						intDecCount++;
						if (intDecCount == 1)
						{
							blnReturn = true;
							break;
						}
						else
						{
							return false;
						}
					default:
						return false;
				} // switch (strChar)

				break;
			} // case 4:

		} // switch (intFormat)
	} // (var i=0; i < strNew.length; i++)

	return blnReturn;
}

