var validchar=" abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var validuserchar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890&/()- ";
var validtnchar=" ()-.0123456789/";
var onlynumbers="0123456789";
var validzipchar="0123456789-/";
var validloginchar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@._-!$(*";
var validinputchar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@!_/.$&(), ";

/*******************************************************
 * Edits the string for characters only and a the minimum length required 
 *******************************************************/
function ChkForAllChars(string, minstrlen)
{ 
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}

/*******************************************************
 * Edits the string for characters plus add chars and a the minimum length required 
 *******************************************************/
function ChkForCharsPlus(string, minstrlen)
{ 
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validuserchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}


/*******************************************************
 * Edits the string for characters plus add chars and a the minimum length required 
 *******************************************************/
function ChkForCharsLogin(string, minstrlen)
{ 
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validloginchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}

/*******************************************************
 * Edits the string for characters plus add chars and a the minimum length required 
 *******************************************************/
function ChkForValidInputChars(string)
{ 
	for (var i=0; i<string.length; i++)
	{
	    if (validinputchar.indexOf(string.charAt(i)) != -1)
			 string.replace(string.charAt(i), "")
			 alert(string);
	}
	return string;           
}   

/*******************************************************
 * Edits the string for characters only and a the minimum length required 
 *******************************************************/
function ChkForNumerics(string, minstrlen)
{ 
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (onlynumbers.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}
/*******************************************************
 * Edits the string for characters only and a the minimum length required 
 *******************************************************/
function ChkForValidUserChars(string, minstrlen)
{ 
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validuserchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}

/*******************************************************
 * Edits the string for valid Email address
 * Must be in the proper format as well
 *******************************************************/
function ChkForValidEmail(string)
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) == -1)
		return false;
}

/*******************************************************
 * Edits the string for valid Postal code
 * Must be in the proper format as well
 *******************************************************/
function ChkForPostalCode(string)
{
	if ((string.search(/^[0-9A-Za-z]+((-)[0-9A-Za-z]+)*$/) == -1) || (string.length < 4))
		return false;
}

/*******************************************************
 * Edits the string for valid Telephone number
 *******************************************************/
function ChkForValidTN(string, minstrlen)
{
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validtnchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}


/*******************************************************
 * Edits the string for valid Telephone number
 *******************************************************/
function ChkForValidZIP(string, minstrlen)
{
	if (string.length < minstrlen)
		return false;
	for (var i=0; i<string.length; i++)
	{
	    if (validzipchar.indexOf(string.charAt(i)) < 0)
			return false;
	}
	return true;           
}

/*******************************************************
 * Do some Validation checks on the credit card
 *******************************************************/
function ChkForValidCC(string, cctype)
{
	var bRet = true;

	switch (cctype)
	{
		case "A":
		{
		   if (string.length != 15)
			{
				bRet = false;
				break;
			}		
			if ((ChkForNumerics(string,15)) == false)
				bRet = false;
			if ((string.search(/^([34]|[37])([0-9])+$/) == -1))
				bRet = false;
			break;
		}
		case "D":
		{
			if ((ChkForNumerics(string,16)) == false)
				bRet = false;
			if ((string.search(/^[6][0][1][1]([0-9])+$/) == -1))
				bRet = false;
			break;
		}
		case "M":
		{
			if ((ChkForNumerics(string,16)) == false)
				bRet = false;
			if ((string.search(/^[5][1-5]([0-9])+$/) == -1))
				bRet = false;
			break;
		}
		case "V":
		{
		   if ((string.length != 13) && (string.length != 16))
			{
				bRet = false;
				break;
			}
			if ((ChkForNumerics(string,13)) == false)
				bRet = false;
			if ((string.search(/^[4]([0-9])+$/) == -1))
				bRet = false;
			break;
		}

	}
	
	if (bRet == true)
		bRet = PerformMod10CCCheck(string);
	
	return bRet;
}

/*******************************************************
 * Perform the mod10 check sum routine on the 
 *  digits in the card number
 *    1) Go through the Credit Card Number digits, starting on 
 *        the RIGHT.
 *            If the position is odd
 *                 add the digit to the checksum tally. 
 *            If the position is even
 *                 multiply the digit by 2
 *                 if the result is greater than 9
 *                     divide the result by 10 
 *                     and add the remainder 
 *                         to the checksum tally
 *                     add 1 to the checksum tally
 *                 if the result is 9 or less
 *                     add the result to the checksum tally
 *        Repeat for each digit.
 *    2) Divide the checksum tally by 10
 *    3) If there is a remainder
 *                     the Credit Card Number is not valid.

 *******************************************************/
function PerformMod10CCCheck(string)
{
	var iChkSum = 0;
	var validCard = true;
	
// ----------------------------------------------------
// for loop to look at the Credit Card Number
// ----------------------------------------------------
	for (var x=1;x <= string.length; x++)
	{
		var currentDigit = string.charAt(string.length-x);
		// ---------------------------------------------
		// even position in credit card number 
		// (2nd, 4th, etc. from RIGHT of Credit Card #)
		// ----------------------------------------------
		if (x % 2 == 0)
		{
			var workDigit = currentDigit * 2;
			if (workDigit > 9)
			{
				iChkSum = iChkSum + (1-0);
				iChkSum = iChkSum + (workDigit % 10);
			}
			else
			{
				iChkSum = iChkSum + (workDigit - 0)
			}
		}
		// ----------------------------------------------
		// odd position in credit card number 
		// (1st, 3rd, etc. from RIGHT of Credit Card #)
		// ----------------------------------------------
		else
		{
			iChkSum = iChkSum + (currentDigit - 0);
		}
	}  //end for llop
		
	if (iChkSum % 10)
	{
		validCard = false;
	}
	return(validCard);
}


/*******************************************************
 * Remove possibly malicious chars
 *******************************************************/
function RemoveBadChars(sTemp) 
{ 
    sTemp = sTemp.replace(/\<|\>|\"|\'|\%|\;|\(|\)|\&|\+|\-/g,""); 
    return sTemp;
}

/********************************************************************************
 * Validate Date
 * The function takes an input string and an optional format string. 
 * The format string is only 3 characters long and determines the relative 
 * positions of the month, day, and year. 
 * If the format string is omitted, then it will be "MDY" 
 * (month first, then day, then year). 
 * There are three different types of dividers that can be used in the date string. 
 * These are the slash (/), the period (.) and the dash (-). 
 * Years can be either 2 digits 
 * (00-49 are assumed to be 21st century and 50-99 are assumed to be 20th century) 
 * or 4 digits
 **********************************************************************************/
function isValidDate(dateStr, sformat) {
   if (sformat == null) { sformat = "MDY"; }
   sformat = sformat.toUpperCase();
   if (sformat.length != 3) { sformat = "MDY"; }
   if ( (sformat.indexOf("M") == -1) || (sformat.indexOf("D") == -1) || (sformat.indexOf("Y") == -1) ) { sformat = "MDY"; }
   if (sformat.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 (sformat.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 sformat (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 (sformat.substring(0, 1) == "M") { var mm = parts[0]; } 
   else
      if (sformat.substring(1, 2) == "M") { var mm = parts[1]; } else { var mm = parts[2]; }
   if (sformat.substring(0, 1) == "D") { var dd = parts[0]; } 
   else
      if (sformat.substring(1, 2) == "D") { var dd = parts[1]; } else { var dd = parts[2]; }
   if (sformat.substring(0, 1) == "Y") { var yy = parts[0]; } 
   else 
      if (sformat.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;
}
    

