/*This a grouping of code used to 
format information and validate 
form elements

this code is the property of Compusolutions Inc
Compusolutions inc does not warranty in any way the use
of this code in any product

*/

function IsNumber(inputVal,blnStop) 

	{
	var oneDecimal
	oneDecimal = false 
	inputStr = inputVal
	for (var i = 0; i < inputStr.length; i++) { 
		var oneChar = inputStr.charAt(i) 
	
	/*
	if (oneChar == "-") 
		{ continue } 
	if (oneChar == ")") 
		{ continue } 
	if (oneChar == "(") 
		{ continue } 
	if	(oneChar == "." && !oneDecimal) { 
		oneDecimal = true 
		continue 
	} 
	*/
	
	
	if 	(oneChar < "0" || oneChar > "9") { 

		if (blnStop == "true") {
			alert("Not a valid number!")
		}

		return false
		} 
	} 
	return true 
} 

function IsCurrency(oField,blnStop)	{


	var inputStr = oField.value
	
	if (inputStr == "") {
		
		//format with zeros
		//inputStr = "$0.00" 
		//oField.value = inputStr
		return true
	}else{

		var oneDecimal
		//there is data in the field
		//Remove junk from field
		while (inputStr.indexOf("$") != -1) {
			inputStr = inputStr.substring(1,inputStr.length)
		}
		while (inputStr.indexOf(",") != -1) {
			inputStr = inputStr.replace(",","")
		}

		//Test for more than one decimal
		//oneDecimal = false 

		//pick out the decimal
		var dec = inputStr.indexOf(".");
		
		//if there is a decimal
		if (dec != -1) {
			var iOne = inputStr.substring(0,dec)
			var iTwo = inputStr.substring(dec + 1,inputStr.length)
		}else{
			var iOne = inputStr
			var iTwo = "00"
		}			
		
		//Test and make sure both halfs are numeric
		if (IsNumber(iOne,"false")) {
		
		}else{

			if (blnStop = "true") {
				alert("Not a valid currency amount!")
				oField.focus()
				oField.select()
				
			}
				return false
			
		}
		
		if (IsNumber(iTwo,"false")) {
		
		}else{
			if (blnStop = "true") {
				alert("Not a valid currency amount!")
				oField.focus()
				oField.select()
			
			}
				return false
		}

		
		//format the currency amount

		inputStr = "$" + iOne + "." + iTwo

		//everything ok
		oField.value = inputStr
		return true 
	}
} 

function isPhoneNumber(s) 
{
     // Check for correct phone number
     rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);

     if (!rePhoneNumber.test(s)) {
          //alert("Phone Number Must Be Entered As: (555) 555-1234");
          return false;
     }

}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
  strEmail = document.forms[0].email.value;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      //alert('A valid e-mail address is required.\nPlease amend and retry');
      return false;
    } 
    return true; 
}


function IsPhone(oField,blnStop){


	var inputStr = oField.value
	
	if (IsEmpty(inputStr)) {
		return true
	}

	//==============================================
		//there is data in the field
		//Remove junk characters
		
	
		while (inputStr.indexOf("(") != -1) {
			inputStr = inputStr.replace("(","")
		}
		
		while (inputStr.indexOf(")") != -1) {
			inputStr = inputStr.replace(")","")
		}
		
		while (inputStr.indexOf("-") != -1) {
			inputStr = inputStr.replace("-","")
		}
	
	//End of junk charcters	
	//=============================================
			
	
	if (IsValidLength(inputStr,10)){ 
		
		if (IsNumber(inputStr,'false')){
			
			//format new value

			var iOne = parseInt(inputStr.substring(0,3),10)
			var iTwo = parseInt(inputStr.substring(3,6),10)	
			var iThree = parseInt(inputStr.substring(6,inputStr.length),10)
			oField.value = "(" + iOne + ")" + iTwo + "-" + iThree	
			return true
		}else{

			if (blnStop = "true") {
				alert("Not A Valid Phone Number!")
				oField.focus()
				oField.select()
				
			}

			return false
		}
	}else{

			if (blnStop = "true") {
				alert("Not A Valid Phone Number!")
				oField.focus()
				oField.select()
				
			}
	
		return false
		
	}
} 



//This function will identify if there is a valid
//styled social security number

function IsSSN(oField,blnStop)	{

	var inputStr = oField.value

	//Can be zero length
	if (IsEmpty(inputStr)){
		return true
	}	
		
	
	//there is data in the field
	//Remove any dashes
	while (inputStr.indexOf("-") != -1) {
		inputStr = inputStr.replace("-","")
	}

	//Test the length for nine digits
		
	if (IsValidLength(inputStr,9)){ 
		
		if (IsNumber(inputStr,'false')){
			
			//format new value

			var iOne = parseInt(inputStr.substring(0,3),10)
			var iTwo = parseInt(inputStr.substring(3,5),10)	
			var iThree = parseInt(inputStr.substring(5,inputStr.length),10)
			oField.value = iOne + "-" + iTwo + "-" + iThree	
			return true
		}else{

			if (blnStop = "true") {
				alert("Not A Valid Social Security Number!")
				oField.focus()
				oField.select()
				
			}

			return false
		}
	}else{

			if (blnStop = "true") {
				alert("Not A Valid Social Security Number!")
				oField.focus()
				oField.select()
				
			}
	
		return false
		
	}
} 

function IsEmpty(inputVal) {
	
	var TestLength = inputVal.length
	if (TestLength == 0){
		return true
		}
	else
		{
		return false
		}
}

function IsValidLength(inputVal,ilength) 
{
	var inputStr
	inputStr = inputVal
	if (inputStr.length != ilength)
	{
		return false;
	}
}

function IsLengthBetween(inputVal,ilength,elength) {
	
	var inputStr
	inputStr = inputVal
	if (inputStr.length > ilength){
		if(inputStr.length < elength){
			return true
			}
		else	
			{
			return false
			}
		}
	else	
		{
		return false	
		}
}

/*This a grouping of code used to 
format information and validate 
form elements using dates
*/


/* This function does date validation and will determine if
a date is within a valid range
*/

//	inputStr	:The Date we wish to test
//	minYear		:The Min year
//	maxYear		:The Max Year
//	minDays		:The Min Days
//	maxDays		:The Max Days

function IsDate(oField,blnStop) {
	

	var inputStr = oField.value
	
	
	//Can be zero length
if (IsEmpty(inputStr) == true){
	return true
}else{	

		//Convert -'s in the string
		
		while (inputStr.indexOf("-") != -1) {
			inputStr = inputStr.replace("-","/")
		}


		//Check the overall format
		
		var delim1 = inputStr.indexOf("/")
		var delim2 = inputStr.lastIndexOf("/")	
		
		if (delim1 != -1 && delim1 == delim2) {

			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
		}
			
		
		//If there are the proper delimiters then 
		//tear apart the string and check the values
		
		if (delim1 != -1) {
			//there are delimiters
			//using base 10 conversion in case leading 0's
			//example 01/01/1999
			
			var mm = parseInt(inputStr.substring(0,delim1),10)
			var dd = parseInt(inputStr.substring(delim1 + 1,delim2),10)	
			var yyyy = parseInt(inputStr.substring(delim2 + 1,inputStr.length),10)
		} else {
			//there are no delimiters
			// example ... 01011999
			var mm = parseInt(inputStr.substring(0,2),10)
			var dd = parseInt(inputStr.substring(2,4),10)	
			var yyyy = parseInt(inputStr.substring(4,inputStr.length),10)
		}



		//Determine if any of the values returned is not a number
		//this would be returned by the parseInt returning NAN
		
		if (isNaN(mm) || isNaN(dd) || isNaN(yyyy)) {
			//there is a non numeric in one of the values
			

			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
		}
		
		
		//Test month and day values
		if (mm < 1 || mm > 12)	{
			
			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
		}
		
		if (dd < 1 || dd > 31)	{
			
			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
		}
		
		//Check Year To Be Between The Max And Mins
		//First Convert 2 digit years to 4 digits
		
		if (yyyy < 100) {
			//2 digit year
			// convert to 4 digit year
			
			if (yyyy >= 30) {
				yyyy += 1900
			} else {
				yyyy += 2000
			}
		}
		
		
		//check for valid days within valid months
		
		if (!checkMonthLength(mm,dd)) {
			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
		}
		if (mm == 2) {
			if(!checkLeapMonth(mm,dd,yyyy)) {
			if (blnStop == "true") {
				alert("Not A Valid Date!")
				oField.focus()
				oField.select()
					
			}
			return false
			}
		}	
		
		
		//Put the date into an acceptable format
		
		// oField.Value = mm + "/" + dd + "/" + yyyy
		oField.value = (mm + "/" + dd + "/" + yyyy)
		return true
	}
} 

function getTheYear() 
	{

	var thisYear = (new Date()).getYear()
	thisYear = (thisYear < 100)? thisYear + 1900: thisYear
	return thisYear

	}


//check the entered month for too high a value

function checkMonthLength(mm,dd) {
	var months = new
	
	Array("","January","February","March","April","May","June","July","August","September","October","November","December")

	if ((mm == 4 || mm == 6 || mm == 9 || mm == 11) & dd > 30) {
		return false
	} else if (dd > 31) {
		return false
	}
	//everything looks fine	
	return true
}

	
function checkLeapMonth(mm,dd,yyyy) {
	if (yyyy % 4 > 0  && dd > 28) {
		return false
	} else if (dd > 29) {
		return false
	}
	return true
}
	

