// VALIDATION

//Removes Error Messages
jQuery.fn.removeErrorMessage = function() {
	$('#err_'+this.attr('id')).remove();
}

//Adds Inline Error Message
jQuery.fn.addErrorMessage = function(msg) {
	msg = msg?msg:'You must complete this';
	$('.error:first').focus();
	this.after('<div class="error-message" id="err_'+this.attr('id')+'">'+msg+'</div>');
}

//Checks if textfield is blank
jQuery.fn.validateNotBlank = function(msg) {
	msg = msg?msg:'This cannot be left blank';
	if(jQuery.trim(this.val())=='') {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if select is not blank or null
jQuery.fn.validateIsSelected = function(msg) {
	msg = msg?msg:'You must make a selection';
	if(this.val()==''||this.val()==null) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if a radio button has been selected
jQuery.fn.validateRadioIsChecked = function(msg) {
	msg = msg?msg:'You must make a selection';
	if(this.find('input:radio:checked').length==0) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if a 1 out of 1 tick box has been ticked
jQuery.fn.validateCheckboxIsChecked = function(msg) {
	msg = msg?msg:'You must tick this box';
	if(!this.next().attr('id')) this.next().attr('id','r_'+this.attr('id'));
	if(this.parent().find(':checked').length==0) {
		if (!$('#err_'+this.next().attr('id')).length>0) {
			this.next().addErrorMessage(msg);
		} return false; // Fail
	} else this.next().removeErrorMessage();
	return true;
}

//Checks if a 1 out of many tick box has been ticked
jQuery.fn.validateCheckboxIsCheckedFromMany = function(msg) {
	msg = msg?msg:'You must tick at least one box';
	if(this.find(':checked').length==0) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if email is valid
jQuery.fn.validateEmail = function(msg) {
	msg = msg?msg:'This email address is not valid';
	if(!/^[!-'*+=?{-~\/-9A-Z^-z-]+(\.[!-'*+=?{-~\/-9A-Z^-z-]+)*@[!-'*+=?{-~\/-9A-Z^-z-]+\.[!-'*+=?{-~\/-9A-Z^-z-]{2,}/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if password is valid
jQuery.fn.validatePassword = function(msg) {
	msg = msg?msg:'This password is not valid';
	if(!/^[a-zA-Z0-9]{4,20}$/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if digits only
jQuery.fn.validateNumeric = function(msg) {
	msg = msg?msg:'The number is not valid, it should be written in numbers only, no spaces';
	if(!/^[0-9]+$/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks min value
jQuery.fn.validateMinimumNumeric = function(min_value,msg) {
	msg = msg?msg:'The number is not valid, it should be written in numbers only and be greater than '+min_value;
	if(!/^[0-9]+$/.test(this.val())||this.val()<min_value) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if sortcode is valid
jQuery.fn.validateSortCode = function(msg) {
	msg = msg?msg:'The sort code is not valid, it should be written 98-76-54 (numbers seperated with dashes)';
	if(!/^[0-9]{2}-[0-9]{2}-[0-9]{2}$/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if account number is valid
jQuery.fn.validateAccountNumber = function(msg) {
	msg = msg?msg:'The account number is not valid, it should be written 87654321 (numbers only, no spaces)';
	if(!/^[0-9]{7,10}$/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if url slug is valid
jQuery.fn.validateUrlSlug = function(msg) {
	msg = msg?msg:'The url slug is not valid. It must only contain numbers, letters and dashes (-)';
	if(!/^[0-9a-z\-]{1,200}$/.test(this.val())) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Checks if alias is valid in realtime
jQuery.fn.validateDuplicateAliases = function(site_url,msg_valid,msg_duplicated) {
	msg_valid = msg_valid?msg_valid:'Aliases must be between 2 and 20 characters long, only containing numbers and letters'; 
	msg_duplicated = msg_duplicated?msg_duplicated:'This alias is already in use';
	if (!/^[a-zA-Z0-9-]{2,20}$/.test(this.val())) {
		$('#alias_status').attr('class','state-bad');
		$('#alias').removeErrorMessage();
		$('#alias').addErrorMessage(msg_valid);
		return false;
	} else {
		$.ajax({
			type:'POST',
			url: site_url+'ajaxcalls/check-alias',
			data:'alias='+$(this).val(),
			beforeSend: function() {
				if (!($('#alias_status').length>0)) $('#alias').before('<span id="alias_status"></span>');
				$('#alias_status').attr('class','state-wait');
				$('#alias').removeErrorMessage();
			},
			success: function(data) {
				if(data=='invalid-format') {
					$('#alias_status').attr('class','state-bad');
					$('#alias').addErrorMessage(msg_valid);
				} else if(data=='exists') {
					$('#alias_status').attr('class','state-bad');
					$('#alias').addErrorMessage(msg_duplicated);
				} else {
					$('#alias_status').attr('class','state-good');
					$('#alias').removeErrorMessage();
				}
			}
		});
	}
	if($('#alias_status.state-bad').length>0) return false;
	else return true;
}

//Validates two elements against each other
jQuery.fn.validateMatch = function(compare_to_this,msg) {
	msg = msg?msg:"Your passwords don't match";
	if(this.val()!=compare_to_this.val()) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}

//Validates if date is (today or) in the future
jQuery.fn.validateFutureDay = function(today_is_acceptable,msg) {
	msg = msg?msg:"This date is not valid";
	if(!checkDateIsAfterNow(this.val(),$('#'+this.attr('id').replace('day','month')).val(),$('#'+this.attr('id').replace('day','year')).val(),today_is_acceptable)) {
		if (!$('#err_'+this.attr('id').replace('day','year')).length>0) {
			$('#'+this.attr('id').replace('day','year')).addErrorMessage(msg);
		} return false; // Fail
	} else $('#'+this.attr('id').replace('day','year')).removeErrorMessage();
	return true;
}

//Validates if text input date is presented as dd/mm/yyyy
jQuery.fn.validateDate_ddmmyyyy = function(msg,allow_blank) {
	msg = msg?msg:"The date must be writen as dd/mm/yyyy e.g. 31/05/1985";
	if(!/^([0-9]{2})(\/)([0-9]{2})(\/)([0-9]{4})$/.test(this.val())&&!(allow_blank||this.val()=='')) {
		if (!$('#err_'+this.attr('id')).length>0) {
			this.addErrorMessage(msg);
		} return false; // Fail
	} else this.removeErrorMessage();
	return true;
}