function CheckFloat(val, min, max) {
    val = CleanVariable(val);

    if(val == "")
        return false;

    if(!IsFloat(val))
        return false;

    if(eval(val) < min || eval(val) > max)
        return false;

    return true;
}

function CheckInt(val, min, max) {
    val = CleanVariable(val);
    if(val == "")
        return false;
        
    if(!IsInteger(val))
        return false;   
        
    if(eval(val) < min || eval(val) > max)
        return false;
        
    return true;
}

function CleanVariable(val) {
    val = replaceSubstring(val, ",", "");
    val = replaceSubstring(val, "$", "");
    val = replaceSubstring(val, " ", "");
    val = replaceSubstring(val, "%", "");
    return val;
}

function replaceSubstring(inputString, fromString, toString) {
   // Goes through the inputString and replaces every occurrence of fromString with toString
   var temp = inputString;
   if (fromString == "") {
      return inputString;
   }
   if (toString.indexOf(fromString) == -1) { // If the string being replaced is not a part of the replacement string (normal situation)
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } else { // String being replaced is part of replacement string (like "+" being replaced with "++") - prevent an infinite loop
      var midStrings = new Array("~", "`", "_", "^", "#");
      var midStringLen = 1;
      var midString = "";
      // Find a string that doesn't exist in the inputString to be used
      // as an "inbetween" string
      while (midString == "") {
         for (var i=0; i < midStrings.length; i++) {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) { tempMidString += midStrings[i]; }
            if (fromString.indexOf(tempMidString) == -1) {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
      } // Keep on going until we build an "inbetween" string that doesn't exist
      // Now go through and do two replaces - first, replace the "fromString" with the "inbetween" string
      while (temp.indexOf(fromString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
      }
      // Next, replace the "inbetween" string with the "toString"
      while (temp.indexOf(midString) != -1) {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
      }
   } // Ends the check to see if the string being replaced is part of the replacement string or not
   return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function


function IsNumeric(TextObject)
{
   if (!isNaN(TextObject.value)) return false;
   else
   {
    TextObject.select();
    TextObject.focus();
   }
   return true;
}



function OnlyCharacters(Value)
{
	if (!(Value.search(/(a-z)+/))) return false;

	return true;
}

function IsInteger(s){
	var i;
	
	s = replaceSubstring(s, "$", "");
	s = replaceSubstring(s, ",", "");
	s = replaceSubstring(s, " ", "");
	if(isNaN(s) || s=="") return false;//<RR - 07/05/2007> Check if s is not a number. If it is (,.- etc) then return false.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if(c == "-") continue;
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function IsFloat (s)
{   
	var i, Count;
	Count = 0;
	s = replaceSubstring(s, "$", "");
	s = replaceSubstring(s, ",", "");
	s = replaceSubstring(s, "%", "");
	s = replaceSubstring(s, " ", "");
	if(isNaN(s) || s=="") return false; //<RR - 07/05/2007> Check if s is not a number. If it is (,.- etc) then return false.
	for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if(c == ".") Count++;
        if(Count == 2) return false;
        if(c == "-") continue;
        if (((c < "0") || (c > "9")) && (eval(s.charCodeAt(i)) != 46)) return false;
    }
    return true;
}

function cleanVariables(s)
{
	if(s != "")
	{
		s = replaceSubstring(s, "$", "");
		s = replaceSubstring(s, ",", "");
		s = replaceSubstring(s, "%", "");
		s = replaceSubstring(s, " ", "");
	}
	return(s);
}

function Clear(s)
{
	if(s != "")
	{
		s = replaceSubstring(s, "$", "");
		s = replaceSubstring(s, ",", "");
		s = replaceSubstring(s, "%", "");
	}
	return(s);
}

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 IsDate(dtStr)
{
	var dtCh = "/";
	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 (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month");
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[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 date");
		return false;
	}
	return true;
}

  var separator = ",";  // use comma as 000's separator
  var decpoint = ".";  // use period as decimal point
  var percent = "%";
  var currency = "$";  // use dollar sign for currency

  function formatNumber(number, format, print) {  // use: formatNumber(number, "format")
    if (print) document.write("formatNumber(" + number + ", \"" + format + "\")<br>");

    if (number - 0 != number) return null;  // if number is NaN return null
    var useSeparator = format.indexOf(separator) != -1;  // use separators in number
    var usePercent = format.indexOf(percent) != -1;  // convert output to percentage
    var useCurrency = format.indexOf(currency) != -1;  // use currency format
    var isNegative = (number < 0);
    number = Math.abs (number);
    if (usePercent) number *= 100;
    format = strip(format, separator + percent + currency);  // remove key characters
    number = "" + number;  // convert number input to string

     // split input value into LHS and RHS using decpoint as divider
    var dec = number.indexOf(decpoint) != -1;
    var nleftEnd = (dec) ? number.substring(0, number.indexOf(".")) : number;
    var nrightEnd = (dec) ? number.substring(number.indexOf(".") + 1) : "";

     // split format string into LHS and RHS using decpoint as divider
    dec = format.indexOf(decpoint) != -1;
    var sleftEnd = (dec) ? format.substring(0, format.indexOf(".")) : format;
    var srightEnd = (dec) ? format.substring(format.indexOf(".") + 1) : "";

     // adjust decimal places by cropping or adding zeros to LHS of number
    if (srightEnd.length < nrightEnd.length) {
      var nextChar = nrightEnd.charAt(srightEnd.length) - 0;
      nrightEnd = nrightEnd.substring(0, srightEnd.length);
      if (nextChar >= 5) nrightEnd = "" + ((nrightEnd - 0) + 1);  // round up

 // patch provided by Patti Marcoux 1999/08/06
      while (srightEnd.length > nrightEnd.length) {
        nrightEnd = "0" + nrightEnd;
      }

      if (srightEnd.length < nrightEnd.length) {
        nrightEnd = nrightEnd.substring(1);
        nleftEnd = (nleftEnd - 0) + 1;
      }
    } else {
      for (var i=nrightEnd.length; srightEnd.length > nrightEnd.length; i++) {
        if (srightEnd.charAt(i) == "0") nrightEnd += "0";  // append zero to RHS of number
        else break;
      }
    }

     // adjust leading zeros
    sleftEnd = strip(sleftEnd, "#");  // remove hashes from LHS of format
    while (sleftEnd.length > nleftEnd.length) {
      nleftEnd = "0" + nleftEnd;  // prepend zero to LHS of number
    }

    if (useSeparator) nleftEnd = separate(nleftEnd, separator);  // add separator
    var output = nleftEnd + ((nrightEnd != "") ? "." + nrightEnd : "");  // combine parts
    output = ((useCurrency) ? currency : "") + output + ((usePercent) ? percent : "");
    if (isNegative) {
      // patch suggested by Tom Denn 25/4/2001
      output = (useCurrency) ? "(" + output + ")" : "-" + output;
    }
    return output;
  }

  function strip(input, chars) {  // strip all characters in 'chars' from input
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++)
      if (chars.indexOf(input.charAt(i)) == -1)
        output += input.charAt(i);
    return output;
  }

  function separate(input, separator) {  // format input using 'separator' to mark 000's
    input = "" + input;
    var output = "";  // initialise output string
    for (var i=0; i < input.length; i++) {
      if (i != 0 && (input.length - i) % 3 == 0) output += separator;
      output += input.charAt(i);
    }
    return output;
  }
	

// Start Working Date Function
	
function valDateFmt(datefmt) 
{
	myOption = -1;
	
	for (i=0; i<datefmt.length; i++) 
	{
		if (datefmt[i].checked) { myOption = i; }
	}
	
	if (myOption == -1) { alert("You must select a date format"); return ' '; }
	return datefmt[myOption].value;
}

function valDateRng(daterng) 
{
	myOption = -1;
	for (i=0; i<daterng.length; i++) 
	{
		if (daterng[i].checked) { myOption = i; } 
	}

	if (myOption == -1) { alert("You must select a date range"); return ' '; }
	return daterng[myOption].value;
}

function stripBlanks(fld) 
{
	var result = "";
	for (i=0; i<fld.length; i++) 
	{
		if (fld.charAt(i) != " " || c > 0) 
		{
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") 
				c = result.length;
		}
	}
	return result.substr(0,c);
}

var numb = '0123456789';

function isValid(parm,val) 
{ 
	if (parm == "") return true;
	
	for (i=0; i<parm.length; i++) 
	{
		if (val.indexOf(parm.charAt(i),0) == -1)
			return false;
	}
	return true;
}

function isNum(parm) { return isValid(parm,numb); }

var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

// Format Field
//      U-u ================== MM/DD/YYYY
//      J-j ================== DD/MM/YYYY
//      W-w ================== YYYY/MM/DD


// Range Field
//      A-a ================== Any
//      P-p ================== Past
//      F-f ================== Future

function validateDate(fld,fmt,rng) 
{
	var dd, mm, yy;
	var today = new Date;
	var t = new Date;
	var d1 = 0;
	fld = stripBlanks(fld);
	
	if (fld == '') return false;
	
	var strSeparatorArray = new Array("-"," ","/",".");
	//var d1 = fld.split('\/');
	
	for (var intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) 
	{
		if (fld.indexOf(strSeparatorArray[intElementNr]) != -1) d1 = fld.split(strSeparatorArray[intElementNr]);
	}
	
	if (d1.length != 3) d1 = fld.split(' ');
	if (d1.length != 3) return false;
	if (fmt == 'u' || fmt == 'U') 
	{
		dd = d1[1]; 
		mm = d1[0]; 
		yy = d1[2];
	}
	else if (fmt == 'j' || fmt == 'J') 
	{
		dd = d1[2]; 
		mm = d1[1]; 
		yy = d1[0];
	}
	else if (fmt == 'w' || fmt == 'W')
	{
		dd = d1[0]; 
		mm = d1[1]; 
		yy = d1[2];
	}
	else return false;
	
	var n = dd.lastIndexOf('st');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('nd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('rd');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf('th');
	if (n > -1) dd = dd.substr(0,n);
	n = dd.lastIndexOf(',');
	if (n > -1) dd = dd.substr(0,n);
	n = mm.lastIndexOf(',');
	if (n > -1) mm = mm.substr(0,n);
	if (!isNum(dd)) return false;
	if (!isNum(yy)) return false;
	if (!isNum(mm)) 
	{
		var nn = mm.toLowerCase();
		for (var i=1; i < 13; i++) 
		{
			if (nn == mth[i] || nn == mth[i].substr(0,3)) { mm = i; i = 13; } 
		}
	}

	if (!isNum(mm)) return false;
	dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
	if (yy < 100) yy += 2000;
	if (yy < 1582 || yy > 4881) return false;
	if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[mm-1]++;
	if (mm < 1 || mm > 12) return false;
	if (dd < 1 || dd > day[mm-1]) return false;
	t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
	if (rng == 'p' || rng == 'P') 
	{
		if (t > today) return false;
	}
	else if (rng == 'f' || rng == 'F') 
	{
		if (t < today) return false;
	}
	else if (rng != 'a' && rng != 'A') return false; 
	
	return true;
}

// End Date Function