/* Check if another customer email already exists. */
jQuery.validator.addMethod("emailUnique", function(email, element) {
	isUnique = true;
	
	jQuery.ajax({cache:false,async:false,type:'GET',url:'/includes/data/checkCustomerEmail.cfm', data:{email: email}, 
		success:function(response){
			if($.trim(response) == 'false')
			{
				isUnique = false;
			}
		}
	});
	
	return isUnique;

}, 'This email address has already been taken.');

jQuery.validator.addMethod("emailExists", function(email, element) {
	exists = true;
	
	jQuery.ajax({cache:false,async:false,type:'GET',url:'/includes/data/checkCustomerEmail.cfm', data:{email: email}, 
		success:function(response){
			if($.trim(response) == 'true')
			{
				exists = false;
			}
		}
	});
	
	return exists;

}, 'This email address does not exist.');


/*
 * matches US phone number format 
 * 
 * where the area code may not start with 1 and the prefix may not start with 1 
 * allows '-' or ' ' as a separator and allows parens around area code 
 * some people may want to put a '1' in front of their number 
 * 
 * 1(212)-999-2345
 * or
 * 212 999 2344
 * or
 * 212-999-0983
 * 
 * but not
 * 111-123-5434
 * and not
 * 212 123 4567
 */
jQuery.validator.addMethod("phone", function(phone_number, element) {
    phone_number = phone_number.replace(/\s+/g, ""); 
	return this.optional(element) || phone_number.length > 9 &&
		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}, "Please specify a valid phone number");

jQuery.validator.addMethod("require_from_group", function(value, element, options) {
	   //From the options array, find out what selector matches
	   //our group of inputs and how many of them should be filled.
	   numberRequired = options[0];
	   selector = options[1];
	   var commonParent = $(element).parents('form');
	   var numberFilled = 0;
	   commonParent.find(selector).each(function(){
	   //Look through fields matching our selector and total up
	   //how many of them have been filled
	     if ($(this).val()) {
	       numberFilled++;
	     }
	   });
	   if (numberFilled >= numberRequired) {
	     //For imputs matching our selector, remove error class
	     //from their text
	     commonParent.find(selector).removeClass('error');
	     //Tell the Validate plugin that this test passed
	     return true;
	   } else {
	     commonParent.find(selector).addClass('error');
	   }
	 //The {0} in the next line is the 0th item in the options array
	 }, jQuery.format("Please fill out at least {0} of these fields."));
