
	
function attachFormValidation() {
	var loForms = document.getElementsByTagName("form");
	for ( var lnI = 0; lnI < loForms.length; lnI++ ) {
		if (loForms[lnI].className.match(/xval/gi) ) {
			}
		else {
			var loInputs = loForms[lnI].getElementsByTagName("INPUT");
			var lnJ = 0;
			var lnContinue = 1;
			while (lnJ < loInputs.length && lnContinue==1) {
				if ( loInputs[lnJ].className.match(/m(in|ax)val/gi) ||
					loInputs[lnJ].className.match(/required/gi) ||
					loInputs[lnJ].className.match(/email/gi) 
					) {
					CMNaddEvent(loForms[lnI], 'submit', checkFormInputs );	
					lnContinue = 0;
					} /* if( ..match()..) */
				lnJ++;
				} /* while(..) */
			} /* if( .match(/xval/gi) ) */
		} /* for(lnI) */	
	} /* attachFormValidation()  */
	
function checkFormInputs(e) {
	var llRV = true;
	
	var laTags = ["input","select"];
	for (lcTag in laTags) {
		if (llRV) {
			if (e.srcElement) {
				var loInputs = e.srcElement.getElementsByTagName(laTags[lcTag]);
				}
			else {
				var loInputs = this.getElementsByTagName(laTags[lcTag]);
				}

			var lnJ = 0;
			while (lnJ < loInputs.length && llRV ) {
				var loI = loInputs[lnJ];
				if (loI.className != "") {
					var laClasses = loI.className.split(" ");
					for (lnK = 0; lnK < laClasses.length; lnK++){
						if (llRV) {
							var laCsp = laClasses[lnK].split(":")
							var lcName = laCsp[0];
							var lcValue = laCsp[1];
							var lcIfDep = laCsp[2];
							var loDep = document.getElementById(lcIfDep);
							var lcIfDepValue = laCsp[3];
							if( !loDep 
								|| (loDep 
									&& (
										(loDep.type=="radio" && loDep.checked)
									||	(loDep.type=="checkbox" && loDep.checked)
									||	(loDep.type=="text" && loDep.value == lcIfDepValue)
										)
									)
								){
								
								switch (lcName.toLowerCase() ) {
									case "minval" :
										if (loI.value=="" || parseInt(loI.value) < parseInt(lcValue) ){
											alert("You must enter at least " + lcValue) ;
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											} /* if(..) */
										break;
									case "maxval" :
										if (loI.value=="" || parseInt(loI.value) > parseInt(lcValue) ){
											alert("You must enter less than " + lcValue) ;
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											} /* if(..) */
										break;
									case "required" :
										if (	loI.value.replace( /\s/g, "" ) ==""
											||	(loI.type=="radio" && !loI.checked)
											||	(loI.type=="checkbox" && !loI.checked)
											){
											switch( loI.type ){
												case "checkbox" :
													alert("You must check required boxes");
													break;
												case "radio" :
													alert("You must select a value");
													break;
												default :
													alert("You must enter a value for required fields");
													break;
												}
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											}
										break;
									case "match" : 
										var vfy = document.getElementById( lcValue );
										if( vfy && loI.value != vfy.value ) {
											alert("Values must match");
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											}
										break;
									case "cardtype" :
											
										if (loI.value=="" || 
												!isValidCardNumber (loI.value) ) {
											alert("Invalid credit card number");
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											}
										else {
											var loCT = document.getElementById(lcValue);
											if( loCT && !isCardTypeCorrect (loI.value, loCT.value) ){
												if (loCT.value != "") {
													alert("Invalid " + loCT.options[loCT.selectedIndex].text + " card number");
													if(loI.select){ loI.select(); }
													if(loI.focus){ loI.focus(); }
													llRV = false;
													}
												else {
													alert("Please select a card type");
													if(loCT.select){ loCT.select(); }
													if(loCT.focus){ loCT.focus(); }
													llRV = false;
													}
												}
											}
										break;
									case "ccexpirationyear" :
										var loM = document.getElementById(lcValue);
										if(loM){
											var today = new Date();
											var expire = new Date();
											expire.setMonth( parseInt(loM.value)-1 );
											expire.setYear( loI.value );
											
											var dpm = [31,28,31,30,31,30,31,31,30,31,30,31];
											expire.setDate( dpm[ expire.getMonth() ] );
											
											if (expire <= today) {
												alert("Invalid expiration date");
												if(loM.select){ loM.select(); }
												if(loM.focus){ loM.focus(); }
												llRV = false;
												}
											}								
										break;
									case "email" :
										if (loI.value.indexOf("@") < 0 || 
												loI.value.indexOf(".") < 0 ) {
											alert("Invalid Email Address");
											if(loI.select){ loI.select(); }
											if(loI.focus){ loI.focus(); }
											llRV = false;
											}
										else if ( lcValue != "undefined" && lcValue != "" ){
											var vfy = document.getElementById( lcValue );
											if( vfy && loI.value != vfy.value ) {
												alert("Email verification does not match");
												if(loI.select){ loI.select(); }
												if(loI.focus){ loI.focus(); }
												llRV = false;
												}
											}
										break ;
									} /* switch (lcName) */
								} /* if loDep ... */	
							} /* if (llRV) */
						} /* for(lnK) */
					} /* if (loI.className!="") */
				lnJ++
				} /* while(..) */
			} /* if(llRV) */	
		} /* for(lcTag in laTags) */
	if (e && e.preventDefault && llRV==false) { e.preventDefault(); } // DOM style		
	return llRV;
	} /* checkformInputs() */


function isValidCardNumber (strNum) {
   var nCheck = 0;
   var nDigit = 0;
   var bEven  = false;
   
   for (n = strNum.length - 1; n >= 0; n--) 
   {
      var cDigit = strNum.charAt (n);
      if (isDigit (cDigit))
      {
         var nDigit = parseInt(cDigit, 10);
         if (bEven)
         {
            if ((nDigit *= 2) > 9)
               nDigit -= 9;
         }
         nCheck += nDigit;
         bEven = ! bEven;
      }
      else if (cDigit != ' ' && cDigit != '.' && cDigit != '-')
      {
         return false;
      }
   }
   return (nCheck % 10) == 0;
}
function isDigit (c) {
   var strAllowed = "1234567890";
   return (strAllowed.indexOf (c) != -1);
}
function isCardTypeCorrect (strNum, type) {
	var cardtype = type.toUpperCase().replace(/ -/g,"")
	var nLen = 0;
	for (n = 0; n < strNum.length; n++) {
		if (isDigit (strNum.substring (n,n+1))) {
			++nLen;
			} // if
		} // for

	switch(cardtype) {
		case '3' :
		case 'VISA' :
			return	((strNum.substring(0,1) == '4') 
					&& 
					(nLen == 13 
						|| 
						nLen == 16));
			break
		case '5' :	
		case 'AMEX' :
		   return	((strNum.substring(0,2) == '34' 
						|| 
						strNum.substring(0,2) == '37') 
					&&
					(nLen == 15));
			break
		case '4' :	
		case 'MASTERCARD' :
			return	((strNum.substring(0,2) == '51' 
						|| 
						strNum.substring(0,2) == '52'
						||
						strNum.substring(0,2) == '53'
						|| 
						strNum.substring(0,2) == '54'
						||
						strNum.substring(0,2) == '55')
					&& 
					(nLen == 16));
			break
		case '6' :	
		case 'DISCOVER' :
			return	((strNum.substring(0,4) == '6011')
					&&
					(nLen == 16));
			break
			
		default :
			return false
		} // switch
	} // function

	
	
	
function CMNaddEvent(obj, evType, fn){ 
	if (obj.addEventListener){ 
		obj.addEventListener(evType, fn, false); 
		return true; 
	} else if (obj.attachEvent){ 
		var r = obj.attachEvent("on"+evType, fn); 
		return r; 
	} else { 
		return false; 
	} 
}	

CMNaddEvent(window, 'load', attachFormValidation);
