/*
''----	function isBlank(element)
''	input:		element - Name of the element on the form to be checked 
''			(E.X document.formName.elementName)
''	Output: 	True - The element is empty
''			False - The element is not empty
''	Description:	checks the element on the form to see if it is blank
*/
function isBlank(element)
{
	if(element.value.length == 0)
		return true;
	else
		return false;
}

/*
''----	function isEmail(email)
''	input:		email - Name of the element on the form that is contains the email
''			(E.X document.formName.txtEmail)
''	Output: 	True - Is a valid E-Mail address
''			False - Not a valid E-Mail address
''	Description:	Validates an E-Mail address
*/
function isEmail(email)
{
	//return email.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	return email.value.match(/\b(^(\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*)$)\b/gi)
}
/*
''----	function isNumber(inputVal)
''	input:		inputVal - Name of the element on the form that contains a number that needs to be validated
''			(E.X document.formName.txtNumber)
''	Output: 	True - Is a valid number
''			False - Not a valid number
''	Description:	Validates a number 
*/
function isNumber(inputVal){
	var oneDecimal = false
	
	inputStr = inputVal.toString()
	for (var i = 0; i<inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (oneChar == "." && !oneDecimal){
			oneDecimal = true
			continue
		}else{
			if (oneChar <"0" || oneChar >"9"){
				return false
			}
		}
	}
	return true
		
}			


/*
''----	function isURL(URL)
''	input:		URL- Name of the element on the form that contains a URL that needs to be validated
''			(E.X document.formName.txtEmail)
''	Output: 	True - Is a valid URL
''			False - Not a valid URL
''	Description:	Validates a URL - Needs improvements....Pattern search would help.
*/
function isURL(URL)
{
	if(URL.value.indexOf(".") == -1 || URL.value.length != "")
	{
		return false;
	}
	return true;
}

/*
''----	function stripNonNumbers(inNumStr) 
''	input:		inNumStr - String that needs all numeric characters stripped out
''	Output: 	New string with no numbers
''	Description:	Loops through the string and takes out dashes and other non numeric chars

*/
function stripNonNumbers(inNumStr) 
{
	var outNumStr = '';
	var i;
	for (i=0; i<inNumStr.length; i++)
		if (isDigit(inNumStr.charAt(i)))
			outNumStr = outNumStr + inNumStr.charAt(i);
	return outNumStr;
}

/*
''----	function isDigit(ch)
''	input:		ch - Character that is checked
''	Output: 	Numeric numbers
''	Description:	Return only numeric characters

*/
function isDigit(ch)
{
	return ((ch >= '0') && (ch <= '9'));
}

/*
''----	function isValidCCNumber(inCCNumb, inCCType)
''	input:		inCCNumb - Credit card number to check
''			inCCType - Type of credit card(Supported types are Visa, MasterCard, American Express)
''	Output: 	false - Not valid
''			True - Valid
''	Description:	Send in the card number and the type and the function determines 
''			if the card is a valid card number.  Card number can be sent in with dashes the function
''			will look over them.
''	Required Functions:	stripNonNumbers, isDigit
''
*/
function isValidCCNumber(inCCNumb, inCCType)
{
	// Visa Card
	var ectVisa = 'VI';
	// Master Card
	var ectMasterCard = 'MC';
	// American Express
	var ectAmex = 'AM';
	// Discover
	var ectDiscover = 'DI';
	
	// Take out dashes or any non numeric characters.
	var tmpCCNum = stripNonNumbers(inCCNumb);
	
	// This is a Visa card do the checking on it
	if(inCCType == ectVisa)
	{
		if((tmpCCNum.length != 16) && (tmpCCNum.length != 13)) return false;
		if(tmpCCNum.substring(0,1) != '4') 
			return false;
		else
			return true;
	}
	// This is a Master Card do the checking on it
	else if(inCCType == ectMasterCard)
	{
		if(tmpCCNum.length != 16) return false;
		if((tmpCCNum.charAt(0) == '5') && (tmpCCNum.charAt(1) >= '1') && (tmpCCNum.charAt(1) <= '5'))
			return true;
		else
			return false;
	}
	// This is a American Express card do the checks on that.
	else if(inCCType == ectAmex)
	{
		if(tmpCCNum.length != 15) return false;
		if((tmpCCNum.substring(0,2) != '34') && (tmpCCNum.substring(0,2) != '37'))
			return false;
		else
			return true;
	}
	else if(inCCType == ectDiscover)
	{
		if(tmpCCNum.substring(0,4) != "6011")
			return false;
		if(number.length != 16)
			return false;
		else
			return true;
	}
	return false;
}

/*
''----	function ValidatePhone(ac,pr,pn)
''	input:		ac - area code field value from form
''				pr - prefix field value from form
''				pn - number field value from form 
''			(E.X document.formName.elementName.value)
''	Output: 	errmsg. Returns the appropriate message to the 
''				caller, so it can be put into an alert.
''				true. All values are present and valid.
''	Description:	checks the elements on the form to see 
''				if a valid phone number has been entered.
*/
function ValidatePhone(ac,pr,pn)
{
	var errmsg=""
	
		if (ac.length < 3)
		{
			errmsg += "Please enter an area code.\n"
		}
		if (pr.length != 3 ||
			pn.length != 4)
		{
			errmsg += "Please enter the full ten digit phone number.\n"
		 }
		if (ac != 888)
		//check to see if the area code is all the same number	
		{
	 		if (ac.substring(0,1) ==
				ac.substring(1,2) &&
				ac.substring(1,2) ==
				ac.substring(2,3))
			{
				errmsg += "Please enter a valid area code.\n"
		 	}
	 	}
	 	if (! isPosInteger(ac))
		{
			errmsg += "Please enter a valid area code.\n"
		}	
		if (! isPosInteger(pr) || ! isPosInteger(pn))
		{
			errmsg += "Please enter a valid phone number.\n"
		}	
	 
		if (errmsg != "")
		{ 
			return errmsg;
		}else{
			return true;
		}	
	
		
}			

/*
''----	function ValidateName(fn,ln)
''	input:		fn - first name field value from form
''				ln - last name field value from form
''			(E.X document.formName.elementName.value)
''	Output: 	errmsg. Returns the appropriate message to the 
''				caller, so it can be put into an alert.
''				true. All values are present and valid.
''	Description:	checks the elements on the form to see 
''				if a valid phone number has been entered.
*/
function ValidateName(fn,ln) 
{
	var errmsg=""
	if (fn == "" ||
		ln == "" ||
		fn.substring(0, 2) == "  " ||
		ln.substring(0, 2) == "  " )
		{
			errmsg = "Please enter the name.\n"
			
		}
		if (errmsg != "")
		{ 
			return errmsg;
		}else{
		return true;
		}
}

/*
''----	function ValidateRadio(field)
''	input:		field - radio button field from the form
''			(E.X document.formName.elementName)
''	Output: 	Returns the index of the button that was selected 
''				of the field. If nothing is selected, return -1.
''				
''	Description:	checks the radio buttons on the form to see 
''				if a radio button has been checked.
*/


function ValidateRadio(field)
{
	for (var x=0; x < field.length; x++)
	{
		if (field[x].checked)
		{
			return x
			
		}
	}
	return -1
}


/*
''----	function ValidateSelect(field)
''	input:		field - select list from the form
''			(EXAMPLE: document.formName.elementName)
''	Output: 	Returns the index of the item that was selected 
''				of the field. If nothing is selected, return -1.
''				
''	Description:	checks the select box on the form to see 
''				if something has been selected.
*/

function ValidateSelect(field) 
	{
		for (var x=0; x < field.length; x++)
		{	
			if (field.options[x].selected)
			{
				return x
			}	
		}
	return -1
	}
	
	
/*
''----	function IsPosInteger(inputVal)
''	input:		inputVal - string value
''			
''	Output: 	Returns true if the value is a positive integer.
''				
''	Description:	checks a value to see if it is a positive integer.
''				
*/

function isPosInteger(inputVal)
{
	
	inputStr = inputVal.toString()
	for (var i = 0; i< inputStr.length; i++)
		{
			var oneChar = inputStr.charAt(i)
			if (oneChar <"0" || oneChar > "9")
			{
				return false
			}
		}
		return true
}	



/*
''----	function Trim(str)
''	input:		str - string value
''			
''	Output: 	Returns a copy of a string without leading or trailing spaces
''				
''	Description:	Returns a copy of a string without leading or trailing spaces
''				
*/

function Trim(str)

{
   return RTrim(LTrim(str));
}


/*
''----	function RTrim(str)
''	input:		str - string value
''			
''	Output: 	Returns a copy of a string without trailing spaces
''				
''	Description:	Returns a copy of a string without trailing spaces
''				
*/

function RTrim(str)
{
   // We don't want to strip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...

      var i = s.length - 1;       // Get length of string

      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;


      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}


/*
''----	function LTrim(str)
''	input:		str - string value
''			
''	Output: 	Returns a copy of a string without leading spaces
''				
''	Description:	Returns a copy of a string without leading spaces
''				
*/
function LTrim(str)

{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

/*
''----- function checkMonthDays(month,day,year)
''	Input:	month = numeric month value (January = 1, February = 2, etc.)
''				day = numeric day value (1-31)
''				year = 4 digit numeric year value (2003, 2004, etc.)
''	Output: Returns boolean (true/false)
''	
''	Description:	This function checks for valid month-date combinations (ex. February 30th is not a valid combination)
''					Used with the month/day/year select boxes or any date value passed in the mm/dd/yyyy format.
*/
function checkMonthDays(month,day,year){
	//Check February date based on if year is leap year or not.
	if(month == 2){
		//check for leap year
		if (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0)){
			if(day > 29)
			{
				return false;
			}
		}else{
			if(day > 28)	{
				return false;
			}
		}
	}
	
	if((month == 4) || (month == 6) || (month == 9) || (month == 11)){
		if(day > 30)	{
			return false;
		}
	}
	
	return true;
}

function TextAreaMaxLength(element, maxlength)
{
	if(element.value.length > maxlength)
	{
		alert("There is a maximum of "+maxlength.toString()+" characters and you have typed in "+element.value.length);
		element.value = element.value.substring(0, maxlength);
	}
}

function isDate(dDate) {

	var dateToCheck = new Date(dDate);

	// Check date
	if (isNaN(dateToCheck)) {
	
		return false;
		
	} else {
	
		var month = dDate.substring(0, dDate.indexOf("/"));
		var day = dDate.substring(dDate.indexOf("/") + 1, dDate.lastIndexOf("/"));
		var year = dateToCheck.getFullYear();
		var today = new Date();
		var leap = 0;

		// Date must be in the future.
		if (dateToCheck <= today) {
			return false;
		}
		
		// Check leap-year / february / day
		if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
		   leap = 1;
		}

		if ((month == 2) && (leap == 1) && (day > 29)) {
			return false;
		}
	
		if ((month == 2) && (leap != 1) && (day > 28)) {
			return false;
		}

		// Validation of other months
		if ((day > 31) && ((month == "1") || (month == "3") || (month == "5") || (month == "7") || (month == "8") || (month == "10") || (month == "12"))) {
			return false;
		}
	
		if ((day > 30) && ((month == "4") || (month == "6") || (month == "9") || (month == "11"))) {
			return false;
		}
		
	}

	return true;

}
