// JavaScript Document
function MatchExp(reg,val) {
	var re = new RegExp(reg);
	if (val.match(re)) {
		return true;
	} else {
		return false;
	}
}

function IsEmail(val){
	var re = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
	return MatchExp(re,val);
}

function IsUrl(val)
{
	var re = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return MatchExp(re,val);
}

function IsSSN(val){
	//var re = /^[0-9]{4}$/
	var re = /^[0-9]{6}$/
	return MatchExp(re,val);
}
function IsSSNAdv(val){
	var re = /^(\([0-9]{3}\)|[0-9]{3})[-.\/ ]?[0-9]{2}[-.\/ ]?[0-9]{4}$/
	return MatchExp(re,val);
}

function IsIFE(val){
	//var re = /^[0-9]{4}$/
	var re = /^[0-9]{13}$/
	return MatchExp(re,val);
}

function IsUSZip_Short(val){
	//var re = /\d{5}(-\d{4})?/
	var re = /^[0-9]{5}$/
	return MatchExp(re,val);
}

function IsUSZip_Long(val){
	//var re = /\d{5}(-\d{4})?/
	var re = /^[0-9]{5}-[0-9]{4}$/
	return MatchExp(re,val);
}

function IsUSZip(val){
	if (val.indexOf("-") == -1){
		return IsUSZip_Short(val);
	}
	else{
		return IsUSZip_Long(val);
	}
}

function IsExtension(val){
	var re = /^[0-9]{0,10}$/
	return MatchExp(re,val);
}

/*
function IsAreaCode(val){
	var re = /^[0-9]{3}$/
	return MatchExp(re,val);
}

function IsPrefix(val){
	var re = /^[0-9]{3}$/
	return MatchExp(re,val);
}

function IsPhoneNumber(val){
	var re = /^[0-9]{4}$/
	return MatchExp(re,val);
}
*/

function IsPhone(val){
	var re = /^[0-9]{10}$/
	return MatchExp(re,val);
}

function IsDate(val){
	//var re = /(0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])[- \/.](19|20)\d\d/
	var re = /(19|20)\d\d[- \/.](0[1-9]|1[012])[- \/.](0[1-9]|[12][0-9]|3[01])/
	return MatchExp(re,val);
}

function IsPhoneAdv(val){
	var re = /^(\([0-9]{3}\)|[0-9]{3})[-. ]?[0-9]{3}[-. ]?[0-9]{4}$/
	return MatchExp(re,val);
}

function Mod10( ccNumb )
{  // v2.0
	var len = ccNumb.length;
	var iCCN = parseInt(ccNumb);  // integer of ccNumb
	var sCCN = ccNumb.toString();  // string of ccNumb
		sCCN = sCCN.replace (/^\s+|\s+$/g,'');  // strip spaces
	var iTotal = 0;  // integer total set at zero
	var temp;  // temp variable for parsing string
	var calc;  // used for calculation of each digit

	if ( isNaN( ccNumb ) || len == 0 )
	{
		return false;
	}

	// ccNumb is a number and the proper length - let's see if it is a valid card number
	if( len >= 15 )
	{  // 15 or 16 for Amex or V/MC
		for( var i=len;i>0;i-- )
		{  // LOOP throught the digits of the card
			calc = parseInt(iCCN) % 10;  // right most digit
			calc = parseInt(calc);  // assure it is an integer
			iTotal += calc;  // running total of the card number as we loop - Do Nothing to first digit
			i--;  // decrement the count - move to the next digit in the card
			iCCN = iCCN / 10;                               // subtracts right most digit from ccNumb
			calc = parseInt(iCCN) % 10 ;    // NEXT right most digit
			calc = calc *2;                                 // multiply the digit by two
			// Instead of some screwy method of converting 16 to a string and then parsing 1 and 6 and then adding them to make 7,
			// I use a simple switch statement to change the value of calc2 to 7 if 16 is the multiple.
			switch( calc )
			{
			case 10: calc = 1; break;       //5*2=10 & 1+0 = 1
			case 12: calc = 3; break;       //6*2=12 & 1+2 = 3
			case 14: calc = 5; break;       //7*2=14 & 1+4 = 5
			case 16: calc = 7; break;       //8*2=16 & 1+6 = 7
			case 18: calc = 9; break;       //9*2=18 & 1+8 = 9
			default: calc = calc;           //4*2= 8 &   8 = 8  -same for all lower numbers
			}                                               
			iCCN = iCCN / 10;  // subtracts right most digit from ccNum
			iTotal += calc;  // running total of the card number as we loop
		}  // END OF LOOP
		if ( ( iTotal%10 ) !=0 )
		{  // check to see if the sum Mod 10 is zero
			return false;
		}
	}

	return true;
}
