$(document).ready(function(){
    /* Start custom JQuery validation rules*/
    //custom function to check Name field for unwanted characters
    $.validator.addMethod('validChars', function (value) { // check for unwanted string field characters
       var result = true;
       var iChars = "!@#$%^&*()+=[]\\\;,/{}|\":<>?"; // unwanted characters
    
       for (var i = 0; i < value.length; i++) {
           if (iChars.indexOf(value.charAt(i)) != -1) {
                  return false;
              }
          }
          return result;
   }, '');
   
   //function to check phone number for unwanted characters
    $.validator.addMethod('validPhone', function (value) { // check for unwanted numeric field characters
       var result = true;
       //strip out parentheses space and hyphen from phone number
       var rawPhone = value.replace("(","");
       rawPhone = rawPhone.replace(")","");
       rawPhone = rawPhone.replace("-","");
       rawPhone = rawPhone.replace(" ","");
       
       if(isNaN(rawPhone)){ //non-numeric digits in phone field
            return false;
       }
       return result;
   }, '');
   
   //function to check Name and Message fields against blacklist
   $.validator.addMethod('NoBlackListWords', function (value) { // check for unwanted words in string field
       var result = true;
       var badWordCount = 0;
       var toCheck = value.toLowerCase();
       var arrBlackListWords = [ "viagra", "cialis", "extenze", "<script", "<embed", "<codebase", "applet", "<style", "fuck", "shit", "cunt", "asshole", "pussy", "cock", "dickhead", "sex" ]; //blacklist words array
    	$.each(arrBlackListWords, function( i, bval ){ //loop through array 
 			if(toCheck.indexOf(bval) >= 0) { 
 				badWordCount++;
 			}
 		});
 		
 		if(badWordCount > 0){ //at least one match against blacklist words found
			return false;
 		}
 		
 		return result;
 		   
   }, '');  
   /* End custom Jquery validation rules*/
    
    $("#theForm").validate({
    rules: {
        Name: {
        	required: true,
            minlength: 2,
            maxlength: 50,
            validChars: true,
            NoBlackListWords: true
    	},
		Phone: {
        	required: true,
            minlength: 14,
            maxlength: 14,
            validPhone: true
    	},
		Email: {
        	required: true,
            email: true,
            minlength:6,
            maxlength:50
    	},
		Message: {
        	required: true,
        	minlength: 30,
        	maxlength: 500,
        	NoBlackListWords: true
    	}
  	},
  	messages: {
     Name: {
        required: "Please enter your Name",
        minlength: "The Name entered is too short",
        maxlength: "The Name entered is too long",
        validChars: "Invalid character(s) in Name",
        NoBlackListWords: "Name contains a banned word"
     },
     Phone: {
       required: "Please enter your Phone Number",
       minlength: "Please enter a 10-digit phone number",
       maxlength: "Please enter a 10-digit phone number",
       validPhone: "Invalid character(s) in Phone" 
     },
     Email: {
       required: "Please enter your Email address",
       email: "Please enter a valid Email address",
	   minlength: "Email address is too short"
     },
     Message: {
        required: "Please enter a Message",
        minlength: "Please enter a longer Message",
        maxlength: "Message exceeds the character limit",
        NoBlackListWords: "Message contains a banned word" 
     }
   } 	
  }
);

$("#Phone").mask("(999) 999-9999",{placeholder:" "});
	
});