function MC_Validation(formObj,msgType){this.form=formObj;this.rules=[];this.badFields=[];this.show_msgs=true;this.CSS_error='';this.msg_area='';if(msgType=='inline'){this.msg_type=msgType;}else{this.msg_type='alert';}}
MC_Validation.prototype.setClass=function(error){this.CSS_error=error;}
MC_Validation.prototype.setMsgArea=function(elm_id){var wrapper=document.getElementById(elm_id);this.msg_area=document.createElement('div');this.msg_area.setAttribute('class','msgs');wrapper.appendChild(this.msg_area);}
MC_Validation.prototype.setShowMsgs=function(value){this.show_msgs=(value)?true:false;}
MC_Validation.prototype.addRules=function(id,rules,msg){if(document.getElementById(id)&&rules){this.rules.push(new Array(document.getElementById(id),rules,msg));}}
MC_Validation.prototype._addClassName=function(element,class_name){var regex=new RegExp(class_name);if(!element.className.match(regex)){var classes=element.className.split(' ');classes.push(class_name);element.className=classes.join(' ');}}
MC_Validation.prototype._removeClassName=function(element,class_name){var regex=new RegExp(class_name);element.className=element.className.replace(regex,'');}
MC_Validation.prototype.clearErrors=function(){for(var i=0;i<this.form.length;i++){this._removeClassName(this.form.elements[i],this.CSS_error);}
this.msg_area.innerHTML='';}
MC_Validation.prototype.displayErrors=function(){var msgs='';for(var x=0;x<this.badFields.length;x++){this._addClassName(this.badFields[x][0],this.CSS_error);if(this.badFields[x][1]!='false'){if(this.msg_type=='inline'){var onClick='onClick="document.getElementById(\''+this.badFields[x][0].id+'\').focus();"';msgs+='<div class="'+this.CSS_error+'" '+onClick+'>'+this.badFields[x][1]+"</div>\n";}else{msgs+=this.badFields[x][1]+'\n';}}}
if(this.show_msgs){switch(this.msg_type){case'inline':if(!this.msg_area)
alert('Your message display type is set to inline, but you have not set a valid message area using setMsgArea().');else
this.msg_area.innerHTML=msgs;break;case'alert':alert(msgs);break;}}
this.badFields[0][0].focus();}
MC_Validation.prototype.validate=function(){this.badFields=[];var element;var mrules;var error;var errorMsgs;var params;var rule_parts;for(var idx=0;idx<this.rules.length;idx++){errorMsgs=[];element=this.rules[idx][0];mrules=this.rules[idx][1].split('|');for(var rule=0;rule<mrules.length;rule++){rule_parts=mrules[rule].match(/^(\w+)(\[(\w+)\])?$/);if(!eval('this.'+rule_parts[1])&&eval(rule_parts[1])){params='element';}else{switch(mrules[rule]){case'optionSelected':params='element.selectedIndex';break;case'radioSelected':params='element.name';break;case'checkboxSelected':params='element.checked';break;default:params='element.value';}
rule_parts[1]='this.'+rule_parts[1];}
if(rule_parts[3]){params+=", '"+rule_parts[3]+"'";}
error=eval(rule_parts[1]+'('+params+')');if(error){errorMsgs.push(error);}}
if(errorMsgs.length>0){if(this.rules[idx][2]){errorMsgs[0]=this.rules[idx][2];}
this.badFields.push(new Array(element,errorMsgs[0]));}}
this.clearErrors();if(this.badFields.length>0){this.displayErrors();return(false);}else{this.msg_area.parentNode.removeChild(this.msg_area);return(true);}}
MC_Validation.prototype.validEmail=function(str){var error="Invalid email address.";if(!str)
return'';if(str.match(/[\x00-\x1F\x7F-\xFF]/))
return error;if(!str.match(/^[^@]{1,64}@[^@]{1,255}$/))
return error;var email_array=str.split('@');var local_array=email_array[0].split('.');for(var i=0;i<local_array.length;i++){if(!local_array[i].match(/^(([A-Za-z0-9!#$%&\'*+\/=?^_`{|}~-]+)|("[^"]+"))$/))
return error;}
if(!email_array[1].match(/^(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}$/)||!email_array[1].match(/^\[(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])(\.(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])){3}\]$/)){var domain_array=email_array[1].split('.');if(domain_array.length<2){return error;}
for(var j=0;j<domain_array.length;j++){if(!domain_array[j].match(/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]))$/))
return error;}}
return'';}
MC_Validation.prototype.validPhone=function(str){if(str&&!str.match(/^(\+?\d{1,3})?(\.|\s|-)?(\(\d{3}\)|\d{3})(\.|\s|-)?\d{3}(\.|\s|-)?\d{4}$/)){return'Invalid phone number.';}
return'';}
MC_Validation.prototype.formatPhone=function(str){var clean=str.replace(/[^0-9]/,'');if(clean.length>10){return clean.replace(/^(\d{1,3})(\d{3})(\d{3})(\d{4})$/,'+$1.$2.$3.$4');}
return clean.replace(/^(\d{3})(\d{3})(\d{4})$/,'$1.$2.$3');}
MC_Validation.prototype.validZip=function(str){if(str&&!str.match(/^\d{5}(-\d{4})?$/)){return'Invalid zip code.';}
return'';}
MC_Validation.prototype.isNotEmpty=function(str){if(str.length===0){return'Field has not been filled in.';}
return'';}
MC_Validation.prototype.isEmpty=function(str){if(str.length!==0){return'Field should not be filled in.';}
return'';}
MC_Validation.prototype.alnum=function(str){if(str&&!str.match(/^\w+$/)){return'Field should contain only alphanumeric characters.';}
return'';}
MC_Validation.prototype.alpha=function(str){if(str&&!str.match(/^[a-zA-Z]+$/)){return'Field should contain only alphabetic characters.';}
return'';}
MC_Validation.prototype.digits=function(str){if(str&&!str.match(/^\d+$/)){return'Field should contain only digits (0-9).';}
return'';}
MC_Validation.prototype.min_length=function(str,len){if(str.length<len){return'Field must be at least '+len+' characters long.';}
return'';}
MC_Validation.prototype.max_length=function(str,len){if(str.length>len){return'Field must be less than '+len+' characters long.';}
return'';}
MC_Validation.prototype.exact_length=function(str,len){if(str.length!=len){return'Field must be '+len+' characters long.';}
return'';}
MC_Validation.prototype.matches=function(str,id){if(str!=document.getElementById(id).value){return'Fields do not match.';}
return'';}
MC_Validation.prototype.optionSelected=function(index){if(!index){return'You must make a selection.';}
return'';}
MC_Validation.prototype.radioSelected=function(grpName){var radios=eval('this.form.'+grpName);for(var n=0;n<radios.length;n++){if(radios[n].checked)
return'';}
return'You must select a radio button.';}
MC_Validation.prototype.checkboxSelected=function(checked){if(!checked){return'Checkbox must be checked.';}
return'';}