function isBlank(v) {
	if (v=='') {
		return true;
	} else {
		return false;
	}
}

function isValidDate(s) {
	var syntaxOk = false;
	var arr = s.split("/");

	if (isNaN(0 + arr[0]) || isNaN(0 + arr[1]) || isNaN(0 + arr[2]))return false;
	if (!isNaN(0 + arr[3])) return false;
	if ((0 + arr[0]) > 12) return false
	if ((0 + arr[1]) > 31) return false	
	return true; 
}
	
function isValidEmail(s) {
	var syntaxOk = false;
				
	pttrn = /(^[\w\-\.]*)\@[\w\-\.]*\.[a-zA-Z]{2,3}/;
	if (pttrn.test(s)) {
		syntaxOk = true;
	}
	return syntaxOk; 
}

function isValidEmail2(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return re.test(emailAddress);
}

/*
--------------------------------------------------------------------------------
Function
--------
Replace( sExpression, sFind, sReplaceWith, iStart, iCount )
--------------------------------------------------------------------------------
Purpose
-------
Find a substring within a string, and replace it with another substring. Returns 
string with substitutions made. 
--------------------------------------------------------------------------------
Arguments
---------
sExpression	- Required. String expression containing substring to replace. 
sFind		- Required. Substring being searched for.
sReplaceWith	- Required. Replacement substring.
iStart		- Optional. Position within expression where substring search 
				is to begin. 0 is default. 
iCount		- Optional. Number of substring substitutions to perform. If 
				omitted, the default value is -1, which means make 
				all possible substitutions. This can only be 
				specified if the start parameter is specified.
--------------------------------------------------------------------------------
Usage
-----
Replace( "Hello, World", "World", "Everybody" )		returns "Hello, Everybody"
Replace( "ABC_ABC_ABC_ABC", "ABC", "XYZ", 4 )		returns "ABC_XYZ_XYZ_XYZ"
Replace( "ABC_ABC_ABC_ABC", "ABC", "XYZ", 0, 1 )	returns "XYZ_ABC_ABC_ABC"
Replace( "ABC_ABC_ABC_ABC", "ABC", "XYZ", 8, 1 )	returns "ABC_ABC_XYZ_ABC"
--------------------------------------------------------------------------------
Error Conditions
----------------
If the expression string, find substring, or replace substring are null, then 
null will be returned. If find substring is equal to the replace substring, then 
the expression will be returned unchanged. If the length of expression string or 
find substring are 0, then the expression will be returned. If the start position 
specified is greater than the length of the expression, then the expression 
will be returned.
--------------------------------------------------------------------------------	
*/
function Replace( sExpression, sFind, sReplaceWith, iStart, iCount ) {
	
	var iLengthExp 		= 0;
	var iRemoveLength 	= 0;
 	var iStartPosition 	= 0;
	var iSubstringLength 	= 0;
	var iLoop		= 0;
	var sFirstPart 		= "";
	var sThirdPart 		= "";
	var sStartSubstring 	= "";
	var sReturnValue 	= "";

	// check for nulls in required parameters	
	if (( sExpression == null ) || ( sFind == null ) || ( sReplaceWith == null )) {
		return null;
	};

	if ( sFind == sReplaceWith ) { return sExpression; }		// prevent nasty recursion later
	if (( iStart == null ) || ( iStart < 0 )) { iStart = 0; };	// parameter check, default to 0
	if (( iCount == null ) || ( iCount < 1 )) { iCount = -1; }; 	// parameter check, default to -1

	iLengthExp 	= sExpression.length;	// check length of expression
	iRemoveLength 	= sFind.length;		// check length of sFind

	// check if sExpression or sFind are empty/blank
	if (( iLengthExp < 1 ) || ( iRemoveLength < 1 )) {	
		return sExpression;			
	};

	if ( iStart > iLengthExp ) { return sExpression; };	// sanity check start vs. expression length

	if ( iStart > 0 ) {	// do this if the user specified a place to start the replacement(s)

		// extract substring left of the start position
		sLeftOfStart 	= sExpression.substring( 0, iStart );
		
		// replace sExpression with the substring to the right of the iStart specified
		sRightOfStart	= sExpression.substring( iStart, iLengthExp);	
		sExpression	= sRightOfStart;	
	};

	iStartPosition 	= sExpression.indexOf( sFind );	// check to see if sFind is in sExpression 
	
	while( iStartPosition != -1 ) {		// do this while sFind is found within sExpression

		// sExpression's length will probably change in this loop
		iLengthExp = sExpression.length;		
		
		// based on the new iStartPosition, determine where 
		// we will get sExtract from sExpression
		iSubstringLength = iStartPosition + iRemoveLength;

		// get everything to the left of the substring being removed
		sFirstPart = sExpression.substring( 0, iStartPosition );
		
		// get everything to the right of the substring being removed
		sThirdPart = sExpression.substring( iSubstringLength, iLengthExp );

		// reform sExpression with the left substring, the replacement 
		// substring, and the right substring
		sExpression = sFirstPart + sReplaceWith + sThirdPart;

		// check to see if there is another instance of the sFind 
		// substring left in sExpression
		iStartPosition = sThirdPart.indexOf( sFind );

		// if substring still remains in the string, move the cursor up
		// to prevent any nasty recursion
		if ( iStartPosition > -1 ) {
			iStartPosition = sFirstPart.length + sReplaceWith.length + iStartPosition;
		};
		
		iLoop++;	

		// if the user specified a count, only do as many replacements as specified
		if (( iCount > -1 ) && ( iLoop == iCount )) { iStartPosition = -1; };

	};		// end while( iStartPosition != -1 )
	
	// if user specified start point, put the substring from before the start point back 
	// with the substring that had the replacement(s) performed on it
	if ( iStart > 0 ) { 					
		sExpression = sLeftOfStart + sExpression;
	};
	sReturnValue = sExpression;

	return sReturnValue;

// end Replace()
};


function Trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = /  /g;
   while (temp.match(obj)) { temp = temp.replace(obj, " "); }
   return temp;
}

function isValidNumber(inpString) {
   return /^[-+]?\d+(\.\d+)?$/.test(inpString);
}

function isValidIPAddress(ipaddr) {
   var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
   if (re.test(ipaddr)) {
      var parts = ipaddr.split(".");
      if (parseInt(parseFloat(parts[0])) == 0) { return false; }
      for (var i=0; i<parts.length; i++) {
         if (parseInt(parseFloat(parts[i])) > 255) { return false; }
      }
      return true;
   } else {
      return false;
   }
}

function isValidCreditCard(type, ccnum) {
   if (type == "Visa") {
      // Visa: length 16, prefix 4, dashes optional.
      var re = /^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "MC") {
      // Mastercard: length 16, prefix 51-55, dashes optional.
      var re = /^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "Disc") {
      // Discover: length 16, prefix 6011, dashes optional.
      var re = /^6011-?\d{4}-?\d{4}-?\d{4}$/;
   } else if (type == "AmEx") {
      // American Express: length 15, prefix 34 or 37.
      var re = /^3[4,7]\d{13}$/;
   } else if (type == "Diners") {
      // Diners: length 14, prefix 30, 36, or 38.
      var re = /^3[0,6,8]\d{12}$/;
   }
   if (!re.test(ccnum)) return false;
   // Checksum ("Mod 10")
   // Add even digits in even length strings or odd digits in odd length strings.
   var checksum = 0;
   for (var i=(2-(ccnum.length % 2)); i<=ccnum.length; i+=2) {
      checksum += parseInt(ccnum.charAt(i-1));
   }
   // Analyze odd digits in even length strings or even digits in odd length strings.
   for (var i=(ccnum.length % 2) + 1; i<ccnum.length; i+=2) {
      var digit = parseInt(ccnum.charAt(i-1)) * 2;
      if (digit < 10) { checksum += digit; } else { checksum += (digit-9); }
   }
   if ((checksum % 10) == 0) return true; else return false;
}

function isValidTime(value) {
   var hasMeridian = false;
   var re = /^\d{1,2}[:]\d{2}([:]\d{2})?( [aApP][mM]?)?$/;
   if (!re.test(value)) { return false; }
   if (value.toLowerCase().indexOf("p") != -1) { hasMeridian = true; }
   if (value.toLowerCase().indexOf("a") != -1) { hasMeridian = true; }
   var values = value.split(":");
   if ( (parseFloat(values[0]) < 0) || (parseFloat(values[0]) > 23) ) { return false; }
   if (hasMeridian) {
      if ( (parseFloat(values[0]) < 1) || (parseFloat(values[0]) > 12) ) { return false; }
   }
   if ( (parseFloat(values[1]) < 0) || (parseFloat(values[1]) > 59) ) { return false; }
   if (values.length > 2) {
      if ( (parseFloat(values[2]) < 0) || (parseFloat(values[2]) > 59) ) { return false; }
   }
   return true;
}

function isValidDateWFormat(dateStr, format) {
   if (format == null) { format = "MDY"; }
   format = format.toUpperCase();
   if (format.length != 3) { format = "MDY"; }
   if ( (format.indexOf("M") == -1) || (format.indexOf("D") == -1) || _
      (format.indexOf("Y") == -1) ) { format = "MDY"; }
   if (format.substring(0, 1) == "Y") { // If the year is first
      var reg1 = /^\d{2}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
      var reg2 = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/
   } else if (format.substring(1, 2) == "Y") { // If the year is second
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{2}\1\d{1,2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{4}\1\d{1,2}$/
   } else { // The year must be third
      var reg1 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{2}$/
      var reg2 = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
   }
   // If it doesn't conform to the right format (with either a 2 digit year or 4 digit year), fail
   if ( (reg1.test(dateStr) == false) && (reg2.test(dateStr) == false) ) { return false; }
   var parts = dateStr.split(RegExp.$1); // Split into 3 parts based on what the divider was
   // Check to see if the 3 parts end up making a valid date
   if (format.substring(0, 1) == "M") { var mm = parts[0]; } else _
      if (format.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (format.substring(0, 1) == "D") { var dd = parts[0]; } else _
      if (format.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (format.substring(0, 1) == "Y") { var yy = parts[0]; } else _
      if (format.substring(1, 2) == "Y") { var yy = parts[1]; } else { var yy = parts[2]; }
   if (parseFloat(yy) <= 50) { yy = (parseFloat(yy) + 2000).toString(); }
   if (parseFloat(yy) <= 99) { yy = (parseFloat(yy) + 1900).toString(); }
   var dt = new Date(parseFloat(yy), parseFloat(mm)-1, parseFloat(dd), 0, 0, 0, 0);
   if (parseFloat(dd) != dt.getDate()) { return false; }
   if (parseFloat(mm)-1 != dt.getMonth()) { return false; }
   return true;
}

function trim(s) 
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}


function TRIM(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


