$.extend($.ford, {validation: {}});
$.extend($.ford.validation, { isCalZip: function(zip) {
	// Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
	// ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
	if(zip) {
		var regx = new RegExp('^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$');
		return regx.test(zip.toUpperCase());		
	} else {
		return false;
	}
}});
$.extend($.ford.validation, {isPostal: function(string) {
	// Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
	// ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
	var regx = new RegExp('^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$');
	return regx.test(string.toUpperCase());	
}});



Ford.Validation = {
	types: ['required', 'postal', 'email', 'email2', 'phone', 'year', 'number'],
	validate: {
		form: function(form, markInvalid) {
			var valid = true;
			$.each($(form).find(":input[data-validation]"), function(key, obj) {
				if($(obj).data('validation').indexOf('visible') > -1) {
					if(Ford.Validation.check.visible(obj) == false) {
						return true;
					}
				}
				$.each(Ford.Validation.types, function(key, type) {
					if($(obj).data('validation').indexOf(type) == -1) return;
					if(eval('Ford.Validation.check.' + type)(obj)) {
						if(markInvalid) {
							$(obj).addClass('error');
						}
						valid = false;
					}
				});
			});
			return valid;
		},
		input: function(obj) {
			$(obj).removeClass('error');
			if($(obj).attr('data-validation').indexOf('visible') > -1) {
				if(Ford.Validation.check.visible(obj) == false) {
					return true;
				}
			}			
			$.each(Ford.Validation.types, function(key, type) {
				if($(obj).attr('data-validation').indexOf(type) == -1) return;
				if(eval('Ford.Validation.check.' + type)(obj)) {
					$(obj).addClass('error');
					return false;
				}
			});
			return true;
		}
	},
	check: {
		visible: function(obj) {
			return $(obj).is(':visible');
		},
		required: function(obj) {
			return Ford.Validation.Input.isEmpty(obj);
		},
		postal: function(obj) {
			return !Ford.Validation.Input.isCaZip(obj);
		},
		email: function(obj) {
			return !Ford.Validation.Input.isEmail(obj);
		},
		email2: function(obj) {
			return !Ford.Validation.Input.isEmail2(obj);
		},
		phone: function(obj) {
			return !Ford.Validation.Input.isPhone(obj);
		},
		number: function(obj) {
			return !Ford.Validation.isNumber($(obj).val());
		},
		year: function(obj) {
			var length = $(obj).val().length;
			if(length == 0) {
				return false;
			} else if(length == 4) {
				var regx = new RegExp('^\\d\{4}$');
				return !regx.test($(obj).val());
			}
			return true;
		}
	},
	hasLocation: function() {
		if(_F.location.province == 'UN' && $.cookie('zip_ignore') == null) {
			return false;
		}	
		return true;
	},
	isCalZip: function(zip) {
		// Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
		// ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
		var regx = new RegExp('^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$');
		return regx.test(zip.toUpperCase());		
	},
	isNumber: function(val) {
		if(val.length == 0) {
			return true;
		}
		var regx = new RegExp('^-?(\\d?,?\\d)+(?:\\.\\d+)?$');
		return regx.test(val);	
	},
	Input: {
		isCaZip: function(obj) {
			// Matches Canadian PostalCode formats with or without spaces (e.g., "T2X 1V4" or "T2X1V4")
			// ^[ABCEGHJKLMNPRSTVXY]{1}\d{1}[A-Z]{1} *\d{1}[A-Z]{1}\d{1}$
			var regx = new RegExp('^[ABCEGHJKLMNPRSTVXY]{1}\\d{1}[A-Z]{1} *\\d{1}[A-Z]{1}\\d{1}$');
			return regx.test($(obj).val().toUpperCase());
		},
		isEmpty: function(obj) {
			var val;
			if($(obj).attr('type') == "select-one") {
				val = $(obj).children('option:selected').val();
			} else if($(obj).attr('type') == 'checkbox') {
				return !$(obj).is(':checked');
			} else {
				val = $(obj).val();
			}
			if(val == "") {
				return true;
			}
			return false;
		},
		isEmail: function(obj) {
			var regx = new RegExp('^([0-9a-zA-Z]([-\\.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})$');
			return regx.test($(obj).val());	
		},
		isEmail2: function(obj) {
			if($("#email2").val() == $("#email").val()){
				return $("#email2").val();
			}
		},
		isPhone: function(obj) {
			var regx = new RegExp('^(\\+?1[- ]?)?\\(?(\\d{3})\\)?[\\s-]?(\\d{3})[\\s-]?(\\d{4})$');
			return regx.test($(obj).val());
		}
	}
};

$(function() {
	$.each($('form[data-validate="true"] :input[data-validation]'), function(key, item) {
		var validation = $(item).attr('data-validation');
		
		$(item).bind('change', function() {
			return Ford.Validation.validate.input(item);
		});

		$(item).bind('keyup, focusout', function() {
			return Ford.Validation.validate.input(item);
		});
	});
});
