/**
	*
	*  Javascript trim, ltrim, rtrim
	*  http://www.webtoolkit.info/
	*
	**/
	 
	function trim(str, chars) {
		return ltrim(rtrim(str, chars), chars);
	}
	 
	function ltrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
	}
	 
	function rtrim(str, chars) {
		chars = chars || "\\s";
		return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
	}
	
$(document).ready(function() {
	$.validator.addMethod(
		"regex", 
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || re.test(value);
		},
		"Please check you input."
	);
	
	$.validator.addMethod(
		"notregex", 
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || !re.test(value);
		},
		"Please check you input."
	);
	
	$.validator.addMethod(
		"regex2", 
		function(value, element, regexp) {
			var check = false;
			var re = new RegExp(regexp);
			return this.optional(element) || re.test(value);
		},
		"Please check you input (2)."
	);
	
	$.validator.addMethod(
		"password",
		function(value, element, param) {
			var numbers = value.match(/[0-9]/g); // regex for numbers
			var letters = value.match(/[a-zA-Z]/g);
			// if the numbers and letters variables are not null then there has been a match
			var result = (numbers != null) && (letters != null);
			return this.optional(element) || result; 
		},
		"The Password must contain, at least, one letter and one number amongst the characters."
	);
	
	$.validator.addMethod(
		"notequalto", 
		function(value, element, param) {
			// this bind unbind functionality was copied from the standard equalTo method
			// bind to the blur event of the target in order to revalidate whenever the target field is updated
			// TODO find a way to bind the event just once, avoiding the unbind-rebind overhead
			var target = $(param).unbind(".validate-notequalto").bind("blur.validate-notequalto", function() {
				$(element).valid();
			});
			return this.optional(element) || value != target.val();
		},
		"They must be different."
	);

	$.validator.addMethod(
			"notireland", 
			function(value, element, param) {
				var uppercase = trim(value.toUpperCase());
				var firstTwo = uppercase.substring(0,2);
				
				return this.optional(element) || (firstTwo!="BT"); 
			},
			"Sorry, we cannot accept registrations from where you are located.  Please contact our Customer Care team at support@challengejackpot.com if you require help or information about this."
		);
	
	$.validator.addMethod(
			"notguernsey", 
			function(value, element, param) {
				var uppercase = trim(value.toUpperCase());
				var firstTwo = uppercase.substring(0,2);
				
				return this.optional(element) || (firstTwo!="GY"); 
			},
			"Sorry, we cannot accept registrations from where you are located.  Please contact our Customer Care team at support@challengejackpot.com if you require help or information about this."
		);
	
	$.validator.addMethod(
			"notjersey", 
			function(value, element, param) {
				var uppercase = trim(value.toUpperCase());
				var firstTwo = uppercase.substring(0,2);
				
				return this.optional(element) || (firstTwo!="JE"); 
			},
			"Sorry, we cannot accept registrations from where you are located.  Please contact our Customer Care team at support@challengejackpot.com if you require help or information about this."
		);
		
	
	$.fn.qtip.styles.regTip = { // Last part is the name of the style
	//	position: { adjust: { x: -163, y: -30 } }//,
		width: 200,
		background: "#ffffff",
		color: "#000000",
		textAlign: "center",
		border: {
		   width: 2,
		   radius: 2,
		   color: "#cccccc"
		},
		tip: "topLeft"
//	   	name: 'dark' // Inherit the rest of the attributes from the preset dark style
	};
});