// DEV NOTES:	These are standard for validation functions that HBI use a lot

// NOTE: standard values across all sites 
var ValidateUtils		= {
	nUKID : "207",
	nUSID : "208"
}

// checks that a string only has only alpha numeric characters
jQuery.validator.addMethod('alphanumeric', function (value) {
	// password regex
	var rePassword					= /^[\da-z]+$/i;
	
	if (value === '') {
		return true;
	}
	
	return rePassword.test(value);
}, 'This should comprise of letters and numbers.');

// checks that a string is an integer - optional
jQuery.validator.addMethod('integer', function (value) {
	// integer regex
	var reInteger					= /^\d+$/i;
	
	if (value != '') {
		return reInteger.test(value);
	}
	else {
		return true;
	}
}, 'This needs be an integer');

// validates UK postcode format
jQuery.validator.addMethod('postcode', function (value) {
	var rePostCode					= /^\w{1,2}[\dR][\d\w]? \d\w{2}$/i;
	var sPostCode					= value.replace(/^(\s)*/, '').replace(/(\s)*$/, '');
	
	// only validate it is in the correct format - not if its filled in
	if (sPostCode == '') {
		return true;
	}
	
	// if we have a country - check that it is set to UK
	if ($("#nCountryID").val()) {
		if ($("#nCountryID").val() == ValidateUtils.nUKID) {
			return rePostCode.test(sPostCode);
		} else {
			return true;
		}
	} else {
		return rePostCode.test(sPostCode);
	}
}, 'Post Code is not in the correct format.');

// validates US zipcode format
jQuery.validator.addMethod('zipcode', function (value) {
	var reZipCode					= /^(\d{5}-\d{4})|(\d{5})$/i;
	var sZipCode					= value.replace(/^(\s)*/, '').replace(/(\s)*$/, '');
	
	// only validate it is in the correct format - not if its filled in
	if (sZipCode == '') {
		return true;
	}
	
	// if we have a country - check that it is set to US
	if ($("#nCountryID").val()) {
		if ($("#nCountryID").val() == ValidateUtils.nUSID) {
			return reZipCode.test(sZipCode);
		} else {
			return true;
		}
	} else {
		return reZipCode.test(sZipCode);
	}
}, 'Zip Code is not in the correct format.');

// validates DART Zone Names
jQuery.validator.addMethod('nowhitespace', function (value) {
	// zonename regex
	var reZoneName					= /\s/;
	var bTestForSpace				= reZoneName.test(value);
	return !bTestForSpace;
}, 'Name cannot contain a whitespace.');


//comparison of dates validation
jQuery.validator.addMethod('dateGreaterThan', function(value, element, param) {
	//value is the field being checked
	//param is the field to check against
	var param = jQuery(param).val();
	if (param.length == 0 || value.length == 0) {
		return 1;
	} else {
		var date1 = stringToDateUKFormat(value);
		var date2 = stringToDateUKFormat(param);
		
		if (date1 > date2 ){
			return 1;
		}else{
			return 0;
		}
	}
}, '' );

//comparison of dates validation
jQuery.validator.addMethod('dateGreaterThanEqualTo', function(value, element, param) {
	//value is the field being checked
	//param is the field to check against
	var param = jQuery(param).val();
	if (param.length == 0 || value.length == 0) {
		return 1;
	} else {
		var date1 = stringToDateUKFormat(value);
		var date2 = stringToDateUKFormat(param);
		
		if (date1 >= date2 ){
			return 1;
		}else{
			return 0;
		}
	}
}, '' );

jQuery.validator.addMethod('dateLessThan', function(value, element, param) {
	//value is the field being checked
	//param is the field to check against
	var param = jQuery(param).val();
	
	if (param.length == 0 || value.length == 0) {
		return 1;
	} else {
		var date1 = stringToDateUKFormat(value);
		var date2 = stringToDateUKFormat(param);
		
		if (date1 < date2 ){
			return 1;
		}else{
			return 0;
		}
	}
}, '' );

function stringToDateUKFormat(dateString){
	//new Date(year, month, day, hours, minutes, seconds, milliseconds)
	//months run 0-11 
	var aStr = dateString.split("/");
	if (aStr.length == 3){
		var dateUKFormat = new Date(aStr[2],aStr[1]-1,aStr[0]);
		return dateUKFormat;
	}		
}

