﻿/***********************
*  Thickbox extentions 
***********************/

$(document).ready(function() {
	$('.endorsementThickBox').each(function(i) {
		var el = $(this);
		var orig = el.attr('href');
		orig = orig + '&raw=1';
		el.attr('href', orig);
	})
	tb_init('a.endorsementThickBox');
});

//extend to support auto height size (reduce the white padding at bottom)
var tb_show_orig = tb_show;
var tb_show = function(caption, url, imageGroup) {
	tb_show_orig(caption, url, imageGroup);
	if (url.toString().toLowerCase().indexOf("&height=auto") > 0) {
		$("#TB_ajaxContent").css("height", "auto");
	}
}

/***********************
*  AutoComplete extentions 
***********************/
var oldValue = "";
function AutoCompleteClientPopulated(source, eventArgs) {
	if (source._currentPrefix != null) {
		var list = source.get_completionList();
		var divSuggestions = document.getElementById(source._completionListElementID);
		if (list.childNodes.length == 1 &&
            list.childNodes[0].innerHTML == 'No results.') {
			oldValue = source._element.value;
		}
	}
}

function AutoCompleteItemSelectedHandler(source, eventArgs) {
	if (source._element.value == 'No results.')
		source._element.value = oldValue;
}

/****************************
* DateTime Pickers
****************************/

$(document).ready(function() {
	$('.JQueryDatePicker').datepicker({ dateFormat: 'dd/mm/yy', buttonImage: '/resources/images/calendar-icon.gif', buttonImageOnly: true, showOn: 'both', showAnim: 'fadeIn' });
	$('.JQueryDatePickerRestrictFuture').datepicker({ dateFormat: 'dd/mm/yy', maxDate: '+0d', buttonImage: '/resources/images/calendar-icon.gif', buttonImageOnly: true, showOn: 'both', showAnim: 'fadeIn' });
});


/***************************
* TextBox Length Validation 
****************************/

function ValidateLength(event) {
	var jqueryObj = $(event.target);
	jqueryObj.removeClass("MaxLengthError");
	//length needs to account for line spaces being presented as 1 char but is submitted as 2 chars
	var linefeeds = jqueryObj.val().match(/[^\n]*\n[^\n]*/gi);
	if (linefeeds == null)
		linefeeds = 0
	else
		linefeeds = linefeeds.length;

	var length = jqueryObj.val().length + linefeeds;
	var valid = length <= jqueryObj.attr("MaxLengthValidation");
	if (!valid) {
		jqueryObj.addClass("MaxLengthError");
	}
}
$(document).ready(function() {
	$(':text[MaxLengthValidation], textarea[MaxLengthValidation]').keyup(ValidateLength).each(function(index, domEle) {
		ValidateLength({ target: domEle });
	})
});

