// ==========================================================================
// error message string collection
// ==========================================================================
	var errmsg =
		{
		eml1 	:	"Please enter a valid email address. " + "\n" +"Example: JohnDoe@yourisp.com"+ "\n" ,
		eml2	:  	"The email address contains illegal characters."+ "\n" ,
		eml3	:	"Please enter your email address twice to confirm it."+ "\n",
		eml4	:	"The email addresses entered are not the same."+ "\n" ,
		pss1	: 	"You didn't enter a password."+ "\n",
		pss2	: 	"Password should be no more than 15 characters in length."+ "\n" ,
		pss3	:	"Please enter your password twice to confirm it."+ "\n",
		pss4	:	"The passwords entered are not the same."+ "\n",
		txt1	: 	" contains illegal characters." + "\n",
		cc1		:	"Invalid credit card number was entered."+ "\n",
		cc2		:	"Wrong credit card type is selected."+ "\n",
		cc3		:	"Credit card is expired."+ "\n",
		zip1	:	"The zip code contains illegal characters."+ "\n",
		zip2	:	"The zip code should be 5 or 9 digits in length."+ "\n",
		zip3	:	"If entering the city, state and zip in the same field\nplease enter in the correct format:\n'city, state, zip'."+ "\n",
		sec1	:	"Please select a question from the list and enter your answer before continuing."+ "\n",
		sec2	:	"Please enter your answer before continuing."+ "\n",
		bnk1	:	"Please fill in all required fields"+ "\n"
		};

// ==========================================================================
// string evaluator collection
// ==========================================================================
	var streval = 
		{
		emailfilter1 : /^.+@.+\..{2,4}$/,
		illegalemail : /["()<>\[\],;:]/,
		illegalchars : /[()<>,;:"[\]|&><~^%*+=$@(){}!]/,
		strip		 : /[\(\)\.\-\ ]/g
		}

// ==========================================================================
// cc validation helpers
// ==========================================================================

	var ccdb =
		{
		visa  : { pfx:'4', len:16, dsp:"Visa", 				re:/^4\d{3}-?\d{4}-?\d{4}-?\d{4}$/ },
		mc    : { pfx:'5', len:16, dsp:"Mastercard", 		re:/^5[1-5]\d{2}-?\d{4}-?\d{4}-?\d{4}$/ },	// 51-55
		disc  : { pfx:'6', len:16, dsp:"Discover", 			re:/^6011-?\d{4}-?\d{4}-?\d{4}$/ },			// 6011
		amex  : { pfx:'3', len:15, dsp:"American Express", 	re:/^3[4,7]\d{13}$/ },						// 34|37
		diner : { pfx:'3', len:14, dsp:"Diners Club", 		re:/^3[0,6,8]\d{12}$/ }						// 30|36|38
		};

	// filter out non-digit chars
	function ExtractNum( txt )
		{
		var val = "", len = txt.length;
		for( var i = 0; i < len; ++i )
			{
			var chr = txt.charAt(i);
			if ( chr >= '0' && chr <= '9' )
				val += chr;
			}
		return val;
		}

	// mod10 check
	function CalcMod10( txt )
		{
		var beg = txt.length % 2;
		var sum = beg ? parseInt(txt.charAt(0)) : 0;
		for ( var i = beg; i < txt.length; i+=2 )
			{
			var d = parseInt(txt.charAt(i))*2;
			sum += (d < 10 ? d : d - 9) + parseInt(txt.charAt(i+1));
			}
		return sum % 10 == 0;
		}

	// validate cc
	// 1 - regex match
	// 2 - mod 10

	function IsCardValid( txt )
		{
		var num = ExtractNum( txt );
		var bre = false;

		if ( num.length > 12 && num.length < 17 )
			{
			var chr = num.charAt(0);
			if      ( chr == '3' ) bre = num.match( ccdb.amex.re );
			else if ( chr == '4' ) bre = num.match( ccdb.visa.re );
			else if ( chr == '5' ) bre = num.match( ccdb.mc.re );
			else if ( chr == '6' ) bre = num.match( ccdb.disc.re );
			}

		var bcs = CalcMod10( num );
		return bre && bcs;
		}
		
	function instr(str, searchstr)
	{
		return str.indexOf(searchstr) > -1; 
	}
		
	function DoCcCheck(frm, cardnumfield, typefield)
	{
		var typebtns = frm[typefield]; 
		var type = "";
		for (var i=0;i<typebtns.length;i++)
		{
			if(typebtns[i].checked)
			{
				type = typebtns[i].value.toLowerCase(); 
				break;
			}
		}
		
		if(!type) 
		{
			alert("No Credit Card type was selected");
			try{typebtns[0].focus();} catch(e){}
			return false;
		}
		
		type = (instr(type,"mc") || instr(type,"master"))? "mc" : type; 
		type = (instr(type,"visa")) ? "visa" : type; 
		type = (instr(type,"disc")) ? "disc" : type; 
		type = (instr(type,"amer") || instr(type,"amex")) ? "amex" : type; 
		
		if(type!="mc" && type!="visa" && type!="disc" && type!="amex")
			return true; 
		
		var error = CheckCc( frm[cardnumfield].value, type ); 
		if(!error) return true; 
		else
		{
			alert(error); 
			try {frm[cardnumfield].focus();frm[cardnumfield].select();} catch(e){}
			return false; 
		}
	}

	function CheckCc( cardnum, type )
		{
		var error = "";
		for ( var key in ccdb )
			{
			if (key == type)
				{
				var pfx = cardnum.charAt(0);
				var len = cardnum.length;
				var bv = (IsCardValid(cardnum));
				if (!bv)
					{
					error = errmsg.cc1;
					return error;
					}
				var bl = (len == ccdb[key].len);
				var bp = (pfx == ccdb[key].pfx);
				if (!bl || !bp)
					error = errmsg.cc2;
				}
			}
		return error;
		}
	
	//make cc display string from cc number
	function MakeCcDisp(ccnum)
		{
		var len = ccnum.length;
		var sfx = ccnum.substring(len-4);
		var ccnum1 = "";
		for (i=0;i<len-4;i++)
			{
			ccnum1 += "*";
			}
		ccnum1 += sfx;
		return ccnum1;
		}
	
	//format exp month 
	function FormatStrExpMo( mo )
		{
		return (mo < 10) ? "0" + mo : mo;
		}

	function FormatIntExpMo( mo )
		{
		return (mo.substring(0,1) == "0") ? parseInt(mo.substring(1)) : parseInt(mo);
		}

	//current date vars for credit card validation
	var date = new Date();
	var mo = parseInt(date.getMonth()+1);
	var yr = parseInt(date.getYear());
	
	function CheckExpmo(expmo)
		{
		if (isNaN(expmo) || expmo > 12 || expmo < 1) 
			return "Please enter a valid month in the two-digit format."; 
		else
			return ""; 
		}
		
	function CheckExpyr(expyr)
		{
		if (isNaN(expyr) || expyr > 2020 || expyr < yr) 
			return "Please enter a valid year in the four-digit format."; 
		else 
			return ""; 
		}
		
	function IsCardExpired(expmo , expyr)
		{
		var error = new Object();
		if (yr > expyr)
			{
			error.msg = errmsg.cc3;
			error.yr  = true;
			}
		else if (yr == expyr)
			{
			if (mo > expmo)
				{
				error.msg = errmsg.cc3;
				error.mo  = true;
				}
			}
		return error;
		}
// ==========================================================================
// Form validation functions
// ==========================================================================

	function DoEmailCheck(frm, emailfields)
	{
		emailfields = emailfields.split(","); 
		var error = ""; 
		var emailfield; 
		for (var i=0;i<emailfields.length;i++)
		{
			emailfield = frm[trimString(emailfields[i])]; 
			var error = CheckEmail(emailfield.value);
			if(error)
			{
				alert(error); 
				try {emailfield.focus();emailfield.select();} catch(e){}
				return false; 
			}
		}
		return true; 
	}

	function CheckEmail(strng)
		{
		var error = "";
		if (!(streval.emailfilter1.test(strng)))
			{
			error = errmsg.eml1;
			}
		else
			{
			//test email for illegal characters
			if (strng.match(streval.illegalemail))
				error = errmsg.eml2;
			}
		return error;
		}

	// password - between 6-8 chars, alphanumerical
	function CheckPassword(strng)
		{
		var error = "";
		if (CheckBlank(strng))
			error = errmsg.pss1;
		
		if (strng.length > 15)
			error = errmsg.pss2;

		return error;
		}

	function CheckTxtString(strng, field)
		{
		var error = "";
		if (strng.match(streval.illegalchars))
			error =  field + errmsg.txt1;

		return error;
		}
		
	function DoZipCheck(frm, zipfield)
	{
		var error = ""; 
		zipfield = frm[zipfield];
		var zipval = zipfield.value.split(",");
		zipval = zipval[zipval.length-1];
		var error = CheckZip(zipval);
		if(error)
		{
			error = error + errmsg.zip3;
			alert(error); 
			try {zipfield.focus();zipfield.select();} catch(e){}
			return false; 
		}
		return true; 
	}

	function CheckZip(strng)
		{
		var error = "";

		var stripped = strng.replace(streval.strip, ''); //strip out acceptable non-numeric characters
			if (isNaN(parseInt(stripped)))
				error = errmsg.zip1;

			if ((stripped.length != 9) && (stripped.length != 5))
				error = errmsg.zip2;

		return error;
		}
		
	function DoPhoneCheck(frm, phonefields)
	{
		phonefields = phonefields.split(","); 
		var error = ""; 
		var phonefield;
		var phonenum = ""; 
		for (var i=0;i<phonefields.length;i++)
		{
			phonefield = frm[trimString(phonefields[i])]; 
			if(phonefield.length)
			{
				for(var i=0;i<phonefield.length;i++)
					phonenum += phonefield[i].value;
			}
			else
				phonenum = phonefield.value;
			
			phonenum = phonenum.replace(streval.strip, ''); //strip out acceptable non-numeric characters
			if (isNaN(parseInt(phonenum))) 
				error = "The phone number contains illegal characters.";
				
			if (!(phonenum.length == 10)) 
				error = "The phone number is the wrong length.	Make sure you included an area code.\n";

			if(error)
			{
				alert(error);
				var focfield = phonefield.length ? phonefield[0] : phonefield;
				try {focfield.focus();focfield.select();} catch(e){}
				return false; 
			}
		}
		return true; 
	}
	
function trimString(txt) 
{ 
    while (txt.substring(0,1) == ' ') 
        txt = txt.substring(1, txt.length);

    while (txt.substring(txt.length-1,txt.length) == ' ')
        txt = txt.substring(0, txt.length-1);

   return txt;
} 