//
//'****************************************************************************
//'* functions.js 		                                                      
//'*                                                                          
//'* Description:                                                             
//'*            Validates form entries.
//'*                                                                          
//'* Calls from:                                                              
//'*												                          
//'* Calls to:                                                                
//'*                                                                          
//'*                                                                         
//'*                                                                          
//'****************************************************************************
//'* Revision History:                                                        
//'*                                                                          
//'*    Date     Programmer      Description                                  
//'* ----------  --------------  -------------------------------------------- 
//'* 11/04/2002  Bridget Penk     Created				                      
//'*                                                                          
//'****************************************************************************
//

// ************************************************************
// The following code implements the onchange() event for the phone number text fields
// The functions are:
//
//     1) isDigit(string)
//     2) makeNumeric(string)
//     3) addHyphens(string)
// ************************************************************



	// This function tests a string to see if it is equal to a numeric character
	function isDigit(strNumber){
		if (	(strNumber == "0")		||
			(strNumber == "1")		||
			(strNumber == "2")		||
			(strNumber == "3")		||
			(strNumber == "4")		||
			(strNumber == "5")		||
			(strNumber == "6")		||
			(strNumber == "7")		||
			(strNumber == "8")		||
			(strNumber == "9")		) {
			
			return true
			
		} else {
		
		 	return false
		 	
		 }
	}


	// This function converts any string into a new string containing only
	// numeric characters (0-9)
	//    Precondition:   argument, myString, must be a string of characters
	//    Postcondition: returns newString, which contains only numeric characters.
	function makeNumeric(myString) {
		var newString = ""   // This stores the new string

		// This loop iterates through the old string
		for (var i = 0; i < myString.length; i++) {
			next = myString.charAt(i) // Read a character
			
			if (isDigit(next)) {			// If the character is a number, store it in the new string
				newString += next
			}
		} 
		
		return newString
	}



	// This function adds hyphens to a string in the format of a phone number
	// (ie. "0123456789" is changed to "012-345-6789")
	//    Precondition:   argument, myString, must be a string of characters
	//    Postcondition: returns newString, which contains two hyphens.   
	function addHyphens(myString) {
		var strHyphen = "-" // This stores a hyphen for later insertion
		var newString = ""   // This stores the new string

		// This loop iterates through the old string
  		for (var i = 1; i <= 10; i++) {
  		
			if ((i == 4)||(i == 7))  {		// If the 4th or 7th positions are being read, insert a hyphen
				newString += strHyphen
			}
			
			newString += myString.charAt(i - 1)
		}
		
		return newString
	}    
    
    
    
    
    
    // This function is called from the Home_Phone textfield's onchange() event-handler. It calles two
    // functions which remove any non-numeric characters from the Home_Phone string and then formats
    // the string with the proper parentheses and hyphens.
    function onChangePhone(myName)
    {
    		var strPhone = document.myForm[myName].value
    		
		if (strPhone != "") {
			strPhone = makeNumeric(strPhone)
    			strPhone = addHyphens(strPhone)
    			document.myForm[myName].value = strPhone
    		}
    }
    
// ************************************************************
// End Phone Number Functions
// ************************************************************ 

    
    
    
    
 
// ************************************************************
// The following code implements the validation for the date fields
// The functions are:
//
//     1) getDaysInMonth(month, year)
//     2) getNext(string, myTarget)
//     3) isDate(string)
// ************************************************************



	// Read the next item in a string (myFlag is a flag string of any length,
	// to indicate when the end of the next item has been reached)
	// returns the next item as a string
	// Note: 	Returns an empty string if there are no characters preceding the myTarget string
	// 			Returns myFlag if the myTarget string was not found
	function getDaysInMonth (month, year) {
	
		var feb = 28
		if ((year % 4) == 0) {
			feb = 29			// If it's a leap year
		}
		var monthArray = [31, feb, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
	
		return monthArray[month - 1]	
	}


	// Attempts to find myTarget in myString and returns the position if found, else returns failed
	function findTargetPosition(myString, myTarget) {
	
		if (debug) alert ("finding myTarget: " + myTarget + "in" + myString)
	
		// Searches for myTarget in myString
		for (var i = 0; i < myString.length; i++) {
			if (myString.substring(i,i + myTarget.length) == myTarget) {
				return i
			}
		}
		return "failed"
	}
	
	
	
	// Read the next item in a string (myFlag is a flag string of any length,
	// to indicate when the end of the next item has been reached)
	// returns the next item as a string
	// Note: 	Returns an empty string if there are no characters preceding the myTarget string
	// 		Returns myFlag if the myTarget string was not found
	function getNext (myString, myFlag) {
	
		//alert ("Getting from: " + myString + " until: " + myFlag)
		
		var next = ""
		var result = ""
		var position = 0
		
		
		while (position == 0) {
			result = findTargetPosition(myString, myFlag)
			if (result == "failed") {
				// if myTarget wasn't found, return myTarget
				return myFlag
			} else {
				position = Number(result)
				if (position > 0) {
					// give the "next" string the value of the characters before the flag
					next = myString.substring(0, position - myFlag.length + 1)
					return next
				}
			}
		}
	}
	


	// Returns a new string with the myTarget string removed
	function removeTarget (myString, myTarget) {
		var newString = ""
		var remainingString = ""
		var characterCount = 0
	
		var nextPart = ""
		
				
		if (myString.length > 0) {
			do {
				remainingString = myString.substring(characterCount, myString.length)
				nextPart = getNext(remainingString, myTarget)
				if (nextPart == myTarget) { // Target was not found so the rest of the string should be selected
					nextPart = remainingString
					characterCount = myString.length
				}
				characterCount += nextPart.length + myTarget.length
				
				newString += nextPart
						
			} while (characterCount < myString.length)	
		}
		return newString
	}

	
	// If dateString is in the format mm/dd/yyyy this function will
	// remove any non-numeric characters other than "/" from
	// the dateString and then return it. 
	// Else returns "failed"
	function makeValidDate(dateString) {
	
		var remainingString = ""
		var characterCount = 0
	
		var nextPart = ""
		var nextNumber = 0
		var myTarget = "/"
		var month
		var day
		var year
		
		var dateArray = []
		var loopCount = 0
		
		
		if (dateString.length > 0) {
			do {
				remainingString = dateString.substring(characterCount, dateString.length)
				nextPart = getNext(remainingString, myTarget)
				if (nextPart == myTarget) {	// Target was not found so the rest of the string should be selected
					nextPart = remainingString
					characterCount = dateString.length
				}
				characterCount += nextPart.length + myTarget.length
				
				nextPart = makeNumeric(nextPart)
				nextNumber = Number(nextPart)
				
				dateArray[loopCount] = nextNumber
				loopCount++
						
			} while (characterCount < dateString.length)	
		}
		
		//alert("month: " + dateArray[0] + "day: " + dateArray[1] + "year: " + dateArray[2])
		
		
		month = dateArray[0]
		day = dateArray[1]
		year = dateArray[2]
		
		if 	(	((month >= 1) 	&& (month <= 12)) 	&&
				((day >= 1) 		&& (day <= getDaysInMonth(month, year)))		&&
				((year >= 1900) 	&& (year <= 2100)) 	){
				
			return month + "/" + day + "/" + year
			
		} else {
			return "failed"
		}
	}
	
	

// ************************************************************
// End Date Functions
// ************************************************************ 
