
/* ======================================================================
FUNCTION:  	IsNum
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
      		that the value is a number (int or float)
RETURN:  	true, if all characters represent a valid integer or float
     		false, otherwise.
====================================================================== */
function IsNum( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;

	var isValid = true;
	var decCount = 0;		// number of decimal points in the string

	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for negative numbers (first char == '-')
	// and a single decimal point (any one char in string == '.').   
	for (z = 0; z < numstr.length; z++) {
		// track number of decimal points
		if (numstr.charAt(z) == ".")
			decCount++;

    	if (!((numstr.charAt(z) >= "0") && (numstr.charAt(z) <= "9") || 
				(numstr.charAt(z) == "-") || (numstr.charAt(z) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(z) == "-" && z != 0) ||
				(numstr.charAt(z) == "." && numstr.length == 1) ||
			  (numstr.charAt(z) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(z) >= "0") && (numstr.charAt(z) <= "9")) || 
   } // END for   
   
   	return isValid;
}  // end IsNum



/* ======================================================================
FUNCTION:  	IsMoney
INPUT:  	numstr (string/number) - the string that will be tested to ensure 
      		that the value is a number (int or float),
      		fldName - the name of the form field 
RETURN:  	true, if all characters represent a valid integer or float
     		false, otherwise.
====================================================================== */
function IsMoney( numstr ) {
	// Return immediately if an invalid value was passed in
	if (numstr+"" == "undefined" || numstr+"" == "null" || numstr+"" == "")	
		return false;
	
	var isValid = true;
	var decCount = 0;		// number of decimal points in the string
	var i = 0;
	
	// convert to a string for performing string comparisons.
	numstr += "";	

	// Loop through string and test each character. If any
	// character is not a number, return a false result.
 	// Include special cases for dollar sign (first char == '$'), comma (any char in string == ','),
	// and a single decimal point (any one char in string == '.').   
	for (i = 0; i < numstr.length; i++) {
		// track number of decimal points
		if (numstr.charAt(i) == ".")
			decCount++;

    	if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9") || (numstr.charAt(i) == ",") || 
				(numstr.charAt(i) == "$") || (numstr.charAt(i) == "."))) {
       	isValid = false;
       	break;
		} else if ((numstr.charAt(i) == "$" && i != 0) ||
				(numstr.charAt(i) == "." && numstr.length == 1) ||
				(numstr.charAt(i) == "," && numstr.length < 4) ||
				(numstr.charAt(i) == "." && decCount > 1)) {
       	isValid = false;
       	break;
      }         	         	       
//if (!((numstr.charAt(i) >= "0") && (numstr.charAt(i) <= "9")) || 
   } // END for   
   
  // alert ("isValid=" + isValid)
   if (isValid){
		var strRes = StripNonNumericMoney(numstr)
		if (strRes !=""){
		 	isValid = true;
		}else{
			isValid = false;
		}	
	}
	return isValid;
}  // end IsMoney



/* ======================================================================
FUNCTION:  	StripNonNumeric
INPUT:    	str (string)
RETURN:  	true, if the string contains digits and dashes in the form 111-12-3456;
CALLS:		IsInt(), which is defined elsewhere in the Script Library
====================================================================== */
function StripNonNumeric( strN ) {
	// Return immediately if an invalid value was passed in
	if (strN+"" == "undefined" || strN+"" == "null" || strN+"" == "")	
		return "";
		
	strN += "";	// make sure it's a string
	var strD = strN;

	for (j = 0; j < strN.length; j++) {
		if (strD+"" == "" || strD.length == 0) return;
		
		// track number of decimal points
		if (strN.charAt(j) == " " || !IsNum(strN.charAt(j))) {
			strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			j-- ;
		}else{
			if (strN.charAt(j) == "-") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == "(") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == ")") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
			if (strN.charAt(j) == " ") strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
		}
		strN = strD;
	}
	

// alert(strD);
 	return strD;
} // end StripNonNumeric

/* ======================================================================
FUNCTION:  	StripNonNumericMoney
INPUT:    	str (string)
RETURNS:  	if successful, will set form field to string of numbers
			(all characters stripped except decimal);
CALLS:		IsInt(), which is defined elsewhere in the Script Library
====================================================================== */
function StripNonNumericMoney( strN) {
	// Return immediately if an invalid value was passed in
	if (strN+"" == "undefined" || strN+"" == "null" || strN+"" == "")	
		return "";
		
	strN += "";	// make sure it's a string
	var strD = strN;

	
	for (j = 0; j < strN.length; j++) {
		if (strD+"" == "" || strD.length == 0) return;
		
		// track number of decimal points
		
		if (strN.charAt(j) == "-") return "";
		if (!IsNum(strN.charAt(j))){
			if (strN.charAt(j)!= ".") {
				strD = strN.substring(0,j) + strN.substring(j+1,strN.length);
				j-- ;
			}
			else
			if (strN.charAt(j) == ".") {
				strD = strN.substring(0,j);
				break;
			}
		}
		strN = strD;
	}

 	return strD;
} // end StripNonNumericMoney


/* ======================================================================
FUNCTION:  	IsValidPhone
INPUT:    	str (string) - an phone number to be tested
			incAreaCode (boolean) - if true, area code is included (10-digits);
			if false or undefined, area code not included
RETURN:  	true, if the string contains a 7-digit phone number and incAreaCode == false
			or is undefined 
			true, if the string contains a 10-digit phone number and incAreaCode == true
			false, otherwise
CALLS:		StripNonNumeric(), which is defined elsewhere in the Script Library
====================================================================== */
function IsValidPhone(str,  incAreaCode) {
	// Return immediately if an invalid value was passed in
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")	
		return false;

	// Set default value for incAreaCode to true, if undefined or null
	if (incAreaCode+"" == "undefined" || incAreaCode+"" == "null")	
		incAreaCode = true;

	var isValid = true;

	str += "";

	// After stripping out non-numeric characters, such as dashes, the
	// phone number should contain 7 digits (no area code) or 10 digits (area code)
	str = StripNonNumeric(str+"");

	if (str.length != 10) isValid = false;
	
   	return isValid;
} // end IsValidPhone

/**
 * DATE VALIDATION SCRIPT 
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

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 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this;
}

function DaysForMonth (month)  {
if (month==4 || month==6 || month==9 || month==11)  {
	return 30;
}
if (month==2) {
	return 29;
}
return 31;

}

function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy");
		
		return false;
	}
	if (month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (day<1 || day>31) {
		alert("Please enter a valid day");
		return false;
	}

	if (month==2 && day > daysInFebruary(year) )  {
		alert("Please enter a valid day");
		return false;
	}

	 if (day > DaysForMonth(month)) {
		alert("Please enter a valid day");
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid day");
		return false;
	}
return true;
}
// END OF DATE VALIDATION SCRIPT 


