/*************** JavaScript functions in this file **************
Filename: utility.js; as of Friday,
 FUNCTION                                                      LINE
  convert2digits(cvtString)                                    205  
  daysInFebruary (year)                                        179  
  fmtPrice(value)                                              257  
  isBlank( testStr)                                            245  
  isCurrency(testStr)                                          237  
  isEmail(strEmail)                                            270  
  isFutureDate(sDate,showError)                                93   
  isPhoneNumber(testStr)                                       221  
  isValidDate(sDate,showError)                                 28   
  isWebImage(fname)                                            188  
  isZipCode(testStr)                                           229  
  validChars(testString)                                       278  
  changeURL(newurl)                                            289
************ (end JavaScript functions in this file) ***********/
/*
	Filename         : utility.js
	Last modified by : dstone
	Created          : 02/06/2000 22:51:08
	Last Updated     : Sunday March 13, 2005 (added changeURL)
	Comments         :  javascript utility functions
*/

//=====================================================
// return true if passed date is a valid date
//=====================================================
function isValidDate(sDate,showError) {
	var dateArray;
	var iMonth, iDay, iYear;
	var datechk = /\d{1,2}\/\d{1,2}\/\d{4}/;

	if (sDate.length == 0) {
		if (showError)
			alert("Date cannot be left empty.");
		return false;
		}
	if (!datechk.test(sDate)) {
		if (showError)
			alert("Date must be in the format of MM/DD/YYYY.");
		return false;
		}
		
	dateArray = sDate.split("/");
	iMonth = parseInt(dateArray[0],10);
	iDay = parseInt(dateArray[1],10);
	iYear = parseInt(dateArray[2],10);
	
	if (iYear < 1900) {
		if (showError)
			alert("Year needs to be after 1900.");
		return false;
		}
	if ((iMonth < 1) || (iMonth > 12)) {		// check month limits
		if (showError)
			alert("Month must be between 1 and 12.");
		return false;
		}
	if ((iDay < 1) || (iDay > 31)) {		// check day number limits
		if (showError)
			alert("The day number must be between 1 and 31.");
		return false;
		}

	//  check day number for max 30 or 31
	if (iMonth != 2) {
		if ((iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11)) {		// months with 30 days
			if (iDay > 30) {
				if (showError)
					alert("Chosen month has only 30 days in it.");
				return false;
				}
			}
		}

	// make sure, if Feb date that the day number is correct
	if (iMonth == 2) {
		if (iDay > daysInFebruary(iYear)) {
			if (showError)
				alert("February of the chosen year does not have " + iDay + " days in it.");
			return false;
			}
		}
	
	return true;
}



//=====================================================
// return true if passed date is a valid date in the future
//=====================================================
function isFutureDate(sDate,showError) {
	var dateArray;
	var iMonth, iDay, iYear;
	var datechk = /\d{1,2}\/\d{1,2}\/\d{4}/;
	var oCurrentDate = new Date();

	if (sDate.length == 0) {
		if (showError)
			alert("Date cannot be left empty.");
		return false;
		}
	if (!datechk.test(sDate)) {
		if (showError)
			alert("Date must be in the format of MM/DD/YYYY.");
		return false;
		}
		
	dateArray = sDate.split("/");
	iMonth = parseInt(dateArray[0],10);
	iDay = parseInt(dateArray[1],10);
	iYear = parseInt(dateArray[2],10);
	
	if ((iMonth < 1) || (iMonth > 12)) {		// check month limits
		if (showError)
			alert("Month must be between 1 and 12.");
		return false;
		}
	if ((iDay < 1) || (iDay > 31)) {		// check day number limits
		if (showError)
			alert("The day number must be between 1 and 31.");
		return false;
		}

	// check for future year
	if (iYear < oCurrentDate.getFullYear()) {
		if (showError)
			alert("The year cannot be before the current year.");
		return false;
		}
		
	// if current year, make sure date is not earlier then present
	if (iYear == oCurrentDate.getFullYear()) {
		if (iMonth < (oCurrentDate.getMonth() + 1)) {
			if (showError)
				alert("Month cannot be before the current month for this year.");
			return false;
			}
		if (iMonth == (oCurrentDate.getMonth() + 1)) {
			if (iDay <= oCurrentDate.getDate()) {
				if (showError)
					alert("Day number cannot be before the current day for this month and year.");
				return false;
				}
			}
		}
		
	//  check day number for max 30 or 31
	if (iMonth != 2) {
		if ((iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11)) {		// months with 30 days
			if (iDay > 30) {
				if (showError)
					alert("Chosen month has only 30 days in it.");
				return false;
				}
			}
		}

	// make sure, if Feb date that the day number is correct
	if (iMonth == 2) {
		if (iDay > daysInFebruary(iYear)) {
			if (showError)
				alert("February of the chosen year does not have " + iDay + " days in it.");
			return false;
			}
		}
	
	return true;
}


//=====================================================
// daysInFebruary (INTEGER year)
// 
// Given integer argument year,
// returns number of days in February of that year.
//=====================================================
function daysInFebruary (year)
{   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (  ((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0) ) ) ? 29 : 28 );
}

//=====================================================
// return true if passed filename reflects a web image format (GIF, JPG or PNG)
//=====================================================
function isWebImage(fname) {
	var tmpStr;
	
	if (fname.length == 0)
		return false;

	tmpStr = fname.toUpperCase();
//	if ((tmpStr.indexOf(".GIF") >= 0) || (tmpStr.indexOf(".JPG") >= 0) || (tmpStr.indexOf(".PNG") >= 0))
	if ((tmpStr.indexOf(".GIF") >= 0) || (tmpStr.indexOf(".JPG") >= 0))
		return true
	else
		return false;
}

//=====================================================
// convert string to numbers only - strips all non-numeric numbers from string
//=====================================================
function convert2digits(cvtString) {
var numStr = "";
var i;

	cvtString += " ";	// make sure input is a string
	
	for (i=0; i < cvtString.length; i++) {
		if (cvtString.substring(i,i+1) >= "0" && cvtString.substring(i, i+1) <= "9")
			numStr += cvtString.substring(i, i+1);
		}
	return numStr;
}

//=====================================================
// returns true if passed string is a valid phone number using all 10 digits and any separator
//=====================================================
function isPhoneNumber(testStr) {
	var filter = /^\(?\d{3}\)?( |\.|-)?\d{3}(\.|-)\d{4}/;
	//var filter = /\(\d{3}\)*\d{3}-\d{4}/;
	return (filter.test(testStr));
}

//=====================================================
// returns true if passed string is a valid zip code
//=====================================================
function isZipCode(testStr) {
	var filter = /\d{5}(-\d{4})?/;
	return (filter.test(testStr));
}

//=====================================================
// returns true if passed string is a valid currency value
//=====================================================
function isCurrency(testStr) {
	var filter = /^(\$)*\d{1,3}(,\d{3})*\.\d{2}$/;
	return (filter.test(testStr));
}

//=====================================================
// returns true if passed string is blank
//=====================================================
function isBlank( testStr) {
	if (testStr.length == 0)
		return true;
	for (var i=0; i<= testStr.length-1; i++)
		if (testStr.charAt(i) != " ")
			return false;
	return true;
}

//=====================================================
// format value as currency (xx.xx)
//=====================================================
function fmtPrice(value) {      
  var cents;

  result = Math.floor(value)+".";   
  cents = 100*(value-Math.floor(value))+0.5;   
  result += Math.floor(cents/10);   
  result += Math.floor(cents%10);   
  return result; 
}

//=====================================================
// validate a properly formatted and valid email address
//=====================================================
function isEmail(strEmail) {
	var filter = /^[A-Za-z0-9\_\-\.]+@+[A-Za-z0-9\_\-\.]+\.+[A-Za-z]{2,3}$/;
	return (filter.test(strEmail));
}

//=====================================================
// check for invalid characters for usernames / logins / passwords
//=====================================================
function validChars(testString) {
	var filter =/^[A-Za-z0-9\_\-]+$/;
	return (filter.test(testString));
}

//=====================================================
// Set up for changing URL's
//=====================================================

function changeURL(newurl) {
	if (newurl != '') {
		window.document.location.href = newurl;
	}
}

