var defaultEmptyOK = false

//====================================================================================================
//	File Name		:	validate.js
//----------------------------------------------------------------------------------------------------
//====================================================================================================
//	Function Name	:	IsEmpty
//	Purpose			:	checks whether a field has value or is blank, it returns false if a field
//						is empty otherwise true.  (!?!? see below, crw)
//----------------------------------------------------------------------------------------------------
function IsEmpty(fld,msg)
{
	if( (fld.value === "" || fld.value.length === 0) && (msg === "") )
	{
		return false;
	}
	if(fld.value === "" || fld.value.length === 0)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

/*
Based on IsEmpty, but named to reflect reality, i.e., returns
true if the field is not empty.
*/
function IsNotEmpty(fld,msg)
{
	if( (fld.value === "" || fld.value.length === 0 || fld.value===null) && (msg === "") )
	{
		return false;
	}
	if(fld.value === "" || fld.value.length === 0 || fld.value===null)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function IsName(fld,msg)
{
    bFlag=true;
    
 		c = fld.value.charAt(0);
		if ((c < "a"  || c > "z") && (c < "A"  || c > "Z") && (c != " "))
		{
		   bFlag = false;
		}
   
		for (i = 1; i < fld.value.length; i++)
		{
		  c = fld.value.charAt(i);
			if ((c < "a" || c > "z") && (c < "A" || c > "Z") && (c < "0" || c > "9") && (c != " "))
			{
		     bFlag = false;
			}
		}
  
	if(bFlag === false)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}



//====================================================================================================
//	Function Name	:	IsSelected
//	Purpose			:	checks whether a option is selected, it returns false if a option
//						is selected otherwise true.
//----------------------------------------------------------------------------------------------------
function IsSelected(fld,msg)
{
	if((fld.value === "" || fld.value === "0" || fld.value.length === 0) && (msg === ""))
	{
		return false;
	}
	if(fld.value === "" || fld.value === "0" || fld.value.length === 0)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsEmail
//	Purpose			:	checks Email validity. Email must have character @ followed by one or more
//						dots. It returns flase if Email is invalid otherwise true.
//----------------------------------------------------------------------------------------------------
function IsEmail(fld,msg)
{
	var regex = /^\w[\w|\-|\.]+(\.[\w|\-]+)*@(\w[\w|\-]+\.)+[a-zA-Z]{2,7}$/ ;
	if(!regex.test(fld.value))
	{
		if (msg != "") { alert(msg); }
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsValidString
//	Purpose			:	checks if field value contains only alphanumeric and '_' charactes. Also checks
//						that alphabetical chars. and '_' must have to be come first and followed by
//						numbers. It returns false if above conditions will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsValidString(fld,msg)
{
	var regex = /^[_]*[a-zA-Z_]*[a-zA-Z0-9_]*$/;
	if(!regex.test(fld.value))
	{
		if (msg != "") { alert(msg); }
		fld.focus();
		return false;
	}
	return true;
}

function IsValidString2(fld,msg)
{
	var regex = /^[_]*[a-zA-Z_\-\.@]*[a-zA-Z0-9_\-\.@]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

function IsValidUserName(fld,msg)
{
	var regex = /^[_]*[a-z_]*[a-z0-9_]*$/;
	if(!regex.test(fld.value))
	{
		if (msg != "" ) { alert(msg); }
		fld.focus();
		return false;
	}
	return true;
}

/* accommodates recent change that allows emails as username */
function IsValidStringOrEmail(controlName,msg)
{
	var fld = getObjectAnyBrowser(controlName);
	if (fld===null) {
		alert ("Unable to find control '" + controlName + "'");
		return false
	}
	if (!IsEmail(fld,"") && !IsValidString(fld,"")) {
		if (msg!="") { alert (msg); }
		else { alert ("Please use an 'ordinary' user name (letters,numbers,underscore) or a valid email addresss (name@domain.xxx)"); }
		return false
	}
	return true
}

//====================================================================================================
//	Function Name	:	IsPassword
//----------------------------------------------------------------------------------------------------
function IsPassword(fld,msg)
{
	var regex = /^[_]*[a-zA-Z]+[0-9]+[a-zA-Z0-9]*$/;
	if(!regex.test(fld.value))
  	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsLen
//	Purpose			:	checks if field value has number of characters between two specified limits.
//						It returns false if no. of chars. is < min. length or > max. length
//						otherwise true.
//----------------------------------------------------------------------------------------------------
function IsLen(fld, minlen, maxlen, msg)
{
	if(fld.value.length < minlen || fld.value.length > maxlen)
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsCurrency
//	Purpose			:	checks if Currency value is in proper format i.e. ',' must be after 1(at first place)
//						or 3 digits also dot '.' must be followed by ',' . '$' is optinal as a first char.
//						It returns false if above condition will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsCurrency(fld,msg)
{
    val = fld.value.replace(/\s/g, "");

	regex = /^\$?\d{1,3}(,?\d{3})*(\.\d{1,2})?$/;

    if(!regex.test(val)) {
         alert(msg);
		 fld.focus();
		 return false;
    }
	return true;
}
//====================================================================================================
//	Function Name	:	IsPhone
//	Purpose			:	checks if phone field has following characters : 0-9, '-', '+', '(' , ')' .
//						It returns false if there are other than above characters otherwise true .
//	Parameters		:	fld1	-  area code to be checked
//					:	fld2	-  city code to be checked
//					:	fld3	-  actual phone no to be checked
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsPhone(fld1,fld2,fld3,msg)
{
	var regex = /^[\d\-+\(\)]+$/;

	var phone = "(" + fld1.value + ")" + fld2.value + "-" + fld3.value;
	if(!regex.test(phone))
	{
		alert(msg);
		fld1.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsZip
//	Purpose			:	checks if zip field value is of length 5 or 9 . (for U.S. zip code).
//						It returns false if it contains alphabetic chars. or length is not as
//						specified.
//----------------------------------------------------------------------------------------------------
function IsZip(fld,msg)
{
	var num = /^\d{5}$/;

	if(!num.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsDate
//	Purpose			:	checks if date is valid according to month selected.
//						i.e. Feb must have 28 or 29 days and also April, June, Sept. and Nov. have
//						30 days. It returns false if above condition will not satisfy otherwise true.
//	Parameters		:	m	-  month field
//						d   -  day field
//						y   -  year field
//					    msg -  error message to be displayed
//----------------------------------------------------------------------------------------------------
function IsDate(m,d,y,msg)
{
  var val1= m.value;
  var val2= d.value;
  var val3= y.value;
	if(val2 > daysInFebruary(val3) && val1 == "02")
	{
		alert(msg);
		d.focus();
		return false;
	}
	if((val1 == "04" || val1 == "06" || val1 == "09" || val1 == "11" ) && (val2 > "30"))
	{
		alert(msg);
		d.focus();
		return false;
	}

  dt= val1 + '/' + val2 + '/' + val3;
  return true;
}
//====================================================================================================
//	Function Name	:	allDigits
//----------------------------------------------------------------------------------------------------
function allDigits(str)
{
	return inValidCharSet(str,"0123456789");
}
//====================================================================================================
//	Function Name	:	inValidCharSet
//----------------------------------------------------------------------------------------------------
function inValidCharSet(str,charset)
{
	var result = true;
	for (var i=0;i<str.length;i++)
		if (charset.indexOf(str.substr(i,1))<0)
		{
			result = false;
			break;
		}
	return result;
}
//====================================================================================================
//	Function Name	:	daysInFebruary
//	Purpose			:	To check days in Feb
//----------------------------------------------------------------------------------------------------
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 Name	:	checkExpDate
//	Purpose			:	Also it checks whether card is expired or not. It returns false if card is
//						expired otherwise true.
//----------------------------------------------------------------------------------------------------
function checkExpDate(fldmonth,fldyear,msg)
{
		var result = true;
 		var expired = false;
 		if (result)
 		{
 			var month = fldmonth.value;
 			var year = fldyear.value;

 			var now = new Date();
 			var nowMonth = now.getMonth() + 1;
 			var nowYear = now.getFullYear();
 			expired = (nowYear > year) || ((nowYear == year ) && (nowMonth > month));
		}
		if (expired)
		{
 			result = false;
			fldmonth.focus();
 			alert(msg);
		}

	return result;
}
//====================================================================================================
//	Function Name	:	checkFileType
//	Purpose			:	It checks the file type. It must be either doc or pdf.
//----------------------------------------------------------------------------------------------------
function checkFileType(fld,msg)
{
	var regex = /(.doc|.pdf)$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	checkImageType
//	Purpose			:	It checks the image type. It must be either jpg or gif.
//----------------------------------------------------------------------------------------------------
function checkImageType(fld,msg)
{
	var regex = /(\.jpg|\.jpeg|\.JPG|\.JPEG|\.gif|\.GIF)$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}

//====================================================================================================
//	Function Name	:	IsUrl
//	Purpose			:	It check that if url starts with http://
//----------------------------------------------------------------------------------------------------
function IsUrl(fld,msg)
{
	var regex = /^(http:\/\/|https:\/\/)/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsFileSize
//	Purpose			:	It ckecks the size of the image file.
//----------------------------------------------------------------------------------------------------
function IsFileSize(fld,msg)
{
	var img = new Image();
	img.src = fld.value;
//	alert('Dimensions:' + img.width + 'x' + img.height);
//	alert('File Size:' + img.fileSize);
	if(img.fileSize > 102400)
	{
		alert(msg);
		fld.focus();
		return false;
	}

	return true;
}
//====================================================================================================
//	Function Name	:	IsValidColor
//	Purpose			:	checks if field value contains only alphanumeric(a to f & 0 to 9). 
//						It returns false if above conditions will not satisfy otherwise true.
//----------------------------------------------------------------------------------------------------
function IsValidColor(fld,msg)
{
//	var regex = /[a-fA-F0-9][a-fA-F0-9]*/;
	var regex = /[a-fA-F0-9]+[a-fA-F0-9]*$/;
//	var regex = /*[a-zA-Z0-9]+[a-zA-Z0-9_]*$/;
	if(!regex.test(fld.value))
	{
		alert(msg);
		fld.focus();
		return false;
	}
	return true;
}
//====================================================================================================
//	Function Name	:	IsRadioBtnChecked
//----------------------------------------------------------------------------------------------------
function IsRadioBtnChecked(fld,msg)
{
	if(fld.length)
	{
		for(var i=0 ; i<fld.length ; i++ )
			if(fld[i].checked)
				return true;
		alert(msg);
		return false;
	}
	else
	{
		if(fld.checked)
			return true;
		alert(msg);
		return false;	
	}
	
}
//====================================================================================================
//	Function Name	:	IsCheckBoxChecked
//----------------------------------------------------------------------------------------------------
function IsCheckBoxChecked(fld,msg)
{
	if(fld.length)
	{
		for(var i=0 ; i<fld.length ; i++ )
			if(fld[i].checked)
				return true;
		alert(msg);
		return false;
	}
	else
	{
		if(fld.checked)
			return true;
		alert(msg);
		return false;	
	}
}
//====================================================================================================
//	Function Name	:	getcheckFieldLength
//----------------------------------------------------------------------------------------------------
function getcheckFieldLength(fld)
{
		var counter = 0;
		
		if(fld.length)
		{
			for(i=0 ; i<fld.length ; i++ )
				if(fld[i].checked)
					counter++;
		}
		else 	
		{
			if(fld.checked)
				counter++;
		}
		
	 return counter;	
}
//====================================================================================================
//	Function Name	:	unCheck
//----------------------------------------------------------------------------------------------------
function unCheck(fld)
{
	
		if(fld.length)
		{
		 
			for(i=0 ; i<fld.length ; i++ )
				if(fld[i].checked)
					fld[i].checked = false;
		}
		else 	
		{
			if(fld.checked)
				fld.checked = false;
		}

		return true;
}
//====================================================================================================
//	Function Name	:	IsPriordate
//----------------------------------------------------------------------------------------------------
function IsPriordate(fld,msg)
{
	today          = new Date();
	date           = today.getDate();
	month          = today.getMonth() + 1;
	year           = today.getFullYear();
	if(date < 10)
		  date = '0'+date;
	if(month < 10)
		  month = '0'+month;
		  
	var checkdate = '';
	checkdate = year+'-'+month+'-'+date;	  	
	
	if(fld.value < checkdate)
	{
		 alert(msg);
		 fld.focus();	
		 return false;
	}	
	return true;
}


function countit(what){
formcontent=what.form.charcount.value
what.form.displaycount.value=formcontent.length
}

function validateTextArea(field,maxlimit)
{
	formcontent=field.value
	displaycount=formcontent.length
	//alert(displaycount);
	if(displaycount > maxlimit)
	{
		 alert("Cannot enter more than "+ maxlimit + " characters.");
		 field.focus();	
		 return false;

		//field.blur();
		//open_error_window('El área de texto no puede contener más de '+maxlimit+' caracteres.');
		//field.value=field.value.substring(0,maxlimit-1);
	}
	return true;
}

// Credit Card Number check
function isCreditCard(fld,msg) {
	var fldvalue; 
		fldvalue = fld.value;	
		alert(fldvalue.length);
  if (fldvalue.length > 19)
    {
		 alert(msg);
		 fld.focus();	
		 return false;
	}

  sum = 0; mul = 1; l = fldvalue.length;
  for (i = 0; i < l; i++) {
    digit = fldvalue.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
  if ((sum % 10) == 0)
    return (true);
  else
	{
	  alert(sum);
		alert(msg);
		 fld.focus();	
		 return false;
	}

}


function validateCreditCard(s) {
var v = "0123456789";
var w = "";
for (var i=0; i < s.length; i++) {
x = s.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
var j = w.length / 2;
if (j < 6.5 || j > 8 || j == 7) return false;
var k = Math.floor(j);
var m = Math.ceil(j) - k;
var c = 0;
for (var i=0; i<k; i++) {
a = w.charAt(i*2+m) * 2;
c += a > 9 ? Math.floor(a/10 + a%10) : a;
}
for (var i=0; i<k+m; i++) c += w.charAt(i*2+1-m) * 1;
return (c%10 == 0);
}


function isEmptyV(s)
{   return ((s == null) || (s.length == 0))
}

function isDay (s)
{   if (isEmptyV(s))
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);
    return isIntegerInRange (s, 1, 31);
}

function isMonth (s)
{   if (isEmptyV(s))
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

function isSignedInteger (s)
{   if (isEmpty(s))
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];
    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}

function isYear (s)
{   
	if (isEmptyV(s))
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

function isIntegerInRange (s, a, b)
{   if (isEmptyV(s))
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    if (!isInteger(s, false)) return false;

    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}


function isInteger (s)
{   var i;

    if (isEmptyV(s))
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}

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 check()
{

  if (window.event.keyCode<48 || window.event.keyCode >57) 
  {
	window.event.keyCode = "0"
	alert("Please enter only digits")
   } 
}


function LTrim(str)
{
if(str==null)
	{
		return str;
	}
for(var i=0;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i++);
return str.substring(i,str.length);
}

function RTrim(str)
{
if(str==null)
	{
		return str;
	}
for(var i=str.length-1;str.charAt(i)==" " || str.charAt(i)=="\n" || str.charAt(i)=="\t";i--);
return str.substring(0,i+1);
}

function Trim(str)
{
	return LTrim(RTrim(str));
}





/*  ================================================================
    Credit card verification functions
    Originally included as Starter Application 1.0.0 in LivePayment.
    20 Feb 1997 modified by egk:
           changed naming convention to initial lowercase
                  (isMasterCard instead of IsMasterCard, etc.)
           changed isCC to isCreditCard
           retained functions named with older conventions from
                  LivePayment as stub functions for backward 
                  compatibility only
           added "AMERICANEXPRESS" as equivalent of "AMEX" 
                  for naming consistency 
    ================================================================ */


/*  ================================================================
    FUNCTION:  isCreditCard(st)
 
    INPUT:     st - a string representing a credit card number

    RETURNS:  true, if the credit card number passes the Luhn Mod-10
		    test.
	      false, otherwise
    ================================================================ */

function isCreditCard(st) {
  // Encoding only works on cards with less than 19 digits
  if (st.length > 19)
    return (false);

  sum = 0; mul = 1; l = st.length;
  for (i = 0; i < l; i++) {
    digit = st.substring(l-i-1,l-i);
    tproduct = parseInt(digit ,10)*mul;
    if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
// Uncomment the following line to help create credit card numbers
// 1. Create a dummy number with a 0 as the last digit
// 2. Examine the sum written out
// 3. Replace the last digit with the difference between the sum and
//    the next multiple of 10.

//  document.writeln("<BR>Sum      = ",sum,"<BR>");
//  alert("Sum      = " + sum);

  if ((sum % 10) == 0)
    return (true);
  else
    return (false);

} // END FUNCTION isCreditCard()



/*  ================================================================
    FUNCTION:  isVisa()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid VISA number.
		    
	      false, otherwise

    Sample number: 4111 1111 1111 1111 (16 digits)
    ================================================================ */

function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return isCreditCard(cc);
  return false;
}  // END FUNCTION isVisa()




/*  ================================================================
    FUNCTION:  isMasterCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid MasterCard
		    number.
		    
	      false, otherwise

    Sample number: 5500 0000 0000 0004 (16 digits)
    ================================================================ */

function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isMasterCard()





/*  ================================================================
    FUNCTION:  isAmericanExpress()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid American
		    Express number.
		    
	      false, otherwise

    Sample number: 340000000000009 (15 digits)
    ================================================================ */

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isAmericanExpress()




/*  ================================================================
    FUNCTION:  isDinersClub()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Diner's
		    Club number.
		    
	      false, otherwise

    Sample number: 30000000000004 (14 digits)
    ================================================================ */

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isCarteBlanche()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Carte
		    Blanche number.
		    
	      false, otherwise
    ================================================================ */

function isCarteBlanche(cc)
{
  return isDinersClub(cc);
}




/*  ================================================================
    FUNCTION:  isDiscover()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid Discover
		    card number.
		    
	      false, otherwise

    Sample number: 6011000000000004 (16 digits)
    ================================================================ */

function isDiscover(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) && (first4digs == "6011"))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isDiscover()





/*  ================================================================
    FUNCTION:  isEnRoute()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid enRoute
		    card number.
		    
	      false, otherwise

    Sample number: 201400000000009 (15 digits)
    ================================================================ */

function isEnRoute(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 15) &&
      ((first4digs == "2014") ||
       (first4digs == "2149")))
    return isCreditCard(cc);
  return false;
}



/*  ================================================================
    FUNCTION:  isJCB()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is a valid JCB
		    card number.
		    
	      false, otherwise
    ================================================================ */

function isJCB(cc)
{
  first4digs = cc.substring(0,4);
  if ((cc.length == 16) &&
      ((first4digs == "3088") ||
       (first4digs == "3096") ||
       (first4digs == "3112") ||
       (first4digs == "3158") ||
       (first4digs == "3337") ||
       (first4digs == "3528")))
    return isCreditCard(cc);
  return false;

} // END FUNCTION isJCB()



/*  ================================================================
    FUNCTION:  isAnyCard()
 
    INPUT:     cc - a string representing a credit card number

    RETURNS:  true, if the credit card number is any valid credit
		    card number for any of the accepted card types.
		    
	      false, otherwise
    ================================================================ */

function isAnyCard(cc)
{
  if (!isCreditCard(cc))
    return false;
  if (!isMasterCard(cc) && !isVisa(cc) && !isAmericanExpress(cc) && !isDinersClub(cc) &&
      !isDiscover(cc) && !isEnRoute(cc) && !isJCB(cc)) {
    return false;
  }
  return true;

} // END FUNCTION isAnyCard()



/*  ================================================================
    FUNCTION:  isCardMatch()
 
    INPUT:    cardType - a string representing the credit card type
	      cardNumber - a string representing a credit card number

    RETURNS:  true, if the credit card number is valid for the particular
	      credit card type given in "cardType".
		    
	      false, otherwise
    ================================================================ */

function isCardMatch (cardType, cardNumber)
{

	cardType = cardType.toUpperCase();
	var doesMatch = true;

	if ((cardType == "VISA") && (!isVisa(cardNumber)))
		doesMatch = false;
	if ((cardType == "MASTERCARD") && (!isMasterCard(cardNumber)))
		doesMatch = false;
	if ( ( (cardType == "AMERICANEXPRESS") || (cardType == "AMEX") )
                && (!isAmericanExpress(cardNumber))) doesMatch = false;
	if ((cardType == "DISCOVER") && (!isDiscover(cardNumber)))
		doesMatch = false;
	if ((cardType == "JCB") && (!isJCB(cardNumber)))
		doesMatch = false;
	if ((cardType == "DINERS") && (!isDinersClub(cardNumber)))
		doesMatch = false;
	if ((cardType == "CARTEBLANCHE") && (!isCarteBlanche(cardNumber)))
		doesMatch = false;
	if ((cardType == "ENROUTE") && (!isEnRoute(cardNumber)))
		doesMatch = false;
	return doesMatch;

}  // END FUNCTION CardMatch()




/*  ================================================================
    The below stub functions are retained for backward compatibility
    with the original LivePayment code so that it should be possible
    in principle to swap in this new module as a replacement for the  
    older module without breaking existing code.  (There are no
    guarantees, of course, but it should work.)

    When writing new code, do not use these stub functions; use the
    functions defined above.
    ================================================================ */

function IsCC (st) {
    return isCreditCard(st);
}

function IsVisa (cc)  {
  return isVisa(cc);
}

function IsVISA (cc)  {
  return isVisa(cc);
}

function IsMasterCard (cc)  {
  return isMasterCard(cc);
}

function IsMastercard (cc)  {
  return isMasterCard(cc);
}

function IsMC (cc)  {
  return isMasterCard(cc);
}

function IsAmericanExpress (cc)  {
  return isAmericanExpress(cc);
}

function IsAmEx (cc)  {
  return isAmericanExpress(cc);
}

function IsDinersClub (cc)  {
  return isDinersClub(cc);
}

function IsDC (cc)  {
  return isDinersClub(cc);
}

function IsDiners (cc)  {
  return isDinersClub(cc);
}

function IsCarteBlanche (cc)  {
  return isCarteBlanche(cc);
}

function IsCB (cc)  {
  return isCarteBlanche(cc);
}

function IsDiscover (cc)  {
  return isDiscover(cc);
}

function IsEnRoute (cc)  {
  return isEnRoute(cc);
}

function IsenRoute (cc)  {
  return isEnRoute(cc);
}

function IsJCB (cc)  {
  return isJCB(cc);
}

function IsAnyCard(cc)  {
  return isAnyCard(cc);
}

function IsCardMatch (cardType, cardNumber)  {
  return isCardMatch (cardType, cardNumber);
}


function removeSpaces(string) {
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++)
	tstring += splitstring[i];
	return tstring;
}

/*
crw 4/4/2008 function to limit password to reasonable characters.
can't provide length restraints (maybe {5,15}) until sorting out what we intend
current lengths run from 1 to 20.  Suggest we add length spec
only for creation, not for logging in, unless we want to get in the
business of changing users' passwords
*/
function isValidPassword(fld, msg) {
	var regex = new RegExp("^[a-zA-Z0-9!#$&()*-./:;@^_]+$");
	//regex = /^[a-zA-Z0-9!#$&()*-./:;@^_]+$/

	if(!regex.test(fld.value))
	{
		if (msg > "") { alert(msg); }
		fld.focus();
		return false;
	}
	return true;
}
