$(function() {
	
	$.getScript('scripts/core/assumptions.js');
	
	//session timeout handling
	
	if (typeof $.jqURL.get('blnTimeout') != 'undefined') {
		showModal('Session timeout','Your session has timed out after a period of inactivity. We apologise for any inconvenience.');
	} 
	
	//test session validity every 15 seconds		
	//$.interval(function(){ 
	//				getSession('blnQuoteProcessStarted','handleSessionValidatity');
	//				}, 15000);
	
	//generic handling for dates
	
	$('input[id*=date]').add('input[id*=Date]').click(function() {
		if ($.Trim($.ListLast(this.id,'_')) == $(this).val()) {
			$(this).val('');
		}
	});
	
	$('input[id*=date]').add('input[id*=Date]').blur(function() {
		if ($(this).val() == '') {
			$(this).val($.ListLast(this.id,'_'));
		}
	});
	
	$('input[id*=date]').add('input[id*=Date]').keydown(function(e) {
		return checkChars(e,'1234567890','allow');
	});
	
	$('input[id*=date]').add('input[id*=Date]').keyup(function(e) {
		checkCharsAfter($(this).attr('id'),'1234567890dmy');
	});
	
	//test for invalid text entry items
	$('input[type=text]').keypress(function(e) {
		return !isInValidKeyCodeOperation(e);
	});
	
	
	//bind capitalize function
	
	/*$('.capitalize').blur(function() {
		$(this).val(wordToUpper($(this).val()));
	});*/
	
	//generic binding on help buttons
	
	$('.helpBtn').mouseover(function() {
		var chrFieldId = $(this).attr('id');
		var chrFieldSel = '#' + chrFieldId;
		var chrHelpTextId = $.ReplaceNoCase($(this).attr('id'),'_helpBtn','_helpText');
		var chrHelpTextSel = '#' + 	chrHelpTextId;
		var chrHelpText = $(chrHelpTextSel).html();
		var offset = $(chrFieldSel).offset();
		$('#helpcontent').html(chrHelpText);
		if ($.FindNoCase('classofuse',chrFieldId)){
			//class of use help text requires more space
			$('#helpcontent').css({
				width: 450 + 'px',
				top: (offset.top + 20) + 'px',
				left: (offset.left - 260) + 'px'
			});
		} else {
			$('#helpcontent').css({
				top: (offset.top + 20) + 'px',
				left: (offset.left - 160) + 'px'
			});
		}
		$('#helpcontent').bgiframe();
		$('#helpcontent').fadeIn('slow');
		//alert(offset.top + ' ' + offset.left);
	});
	
	$('.helpBtn').mouseout(function() {
		$('#helpcontent').fadeOut('slow');
	});
	
	//generic bindings used throughout (bind where meta data is defined only): select form elements
	
	$('.metaData+p>select').change(function(event) {
		var chrValue = $(this).val();
		var chrFieldId = $(this).attr('id');
		updateSessionItem(chrFieldId,chrValue);
	});
	
	//generic bindings used throughout (bind where meta data is defined only): text input form elements (on blur for now)
	
	
	$('.metaData+p>input[type=text]').blur(function(event) {
		if (event.keyCode != 9) {
			var chrValue = $(this).val();
			var chrFieldId = $(this).attr('id');
			if ($.Len($.Trim(chrValue))) {
				updateSession(chrFieldId,chrValue);
			} else {
				//if length is zero, delete from session (unless suggest form as handled on key strokes)
				if (!$.FindNoCase('suggest',chrFieldId)) {
					deleteSession(chrFieldId);
				}
			}
		}
	});
	
	//generic bindings used throughout (bind where meta data is defined only): text input form elements (on click)
	
	$('.metaData+p>input[type=radio]').click(function(event) {
		var chrValue = $(this).val();
		var chrFieldId = $(this).attr('id');
		updateSessionItem(chrFieldId,chrValue);
	});
	
	$('.metaData+p>input[type=checkbox]').click(function(event) {
		var chrFieldId = $(this).attr('id');
		var chrValue = ($(this).is(':checked')) ? 'Y' : 'N';
		updateSessionItem(chrFieldId,chrValue);
	});
	
	//field validation (bind where validation defined in meta data only): text input form elements (on keyup)
	
	$('.metaData+p>input[type=text]').keyup(function(event) {
		var chrFieldId = $(this).attr('id');
		validateField(chrFieldId);
	});
	
	//field validation (bind where validation defined in meta data only): select form elements (on change)
	
	$('.metaData+p>select').change(function(event) {
		var chrFieldId = $(this).attr('id');
		validateField(chrFieldId);
	});
	
	//field validation (bind where validation defined in meta data only): checkbox form elements (on click)
	
	$('.metaData+p>input[type=checkbox]').click(function(event) {
		var chrFieldId = $(this).attr('id');
		validateField(chrFieldId);
	});
	
	//focus and onload validation testing
	
	$('.metaData+p>input[type=text]').focus(function(event) {
		var chrFieldId = $(this).attr('id');
		validateField(chrFieldId);
	});
	
	validateFieldsOnLoad();
	
});

function validateFieldsOnLoad() {
	
	$('.metaData+p>input[type=text]').each(function() {
		var chrFieldId = $(this).attr('id');
		var chrValue = $(this).val();
		if ($.Len($.Trim(chrValue))) {
			validateField(chrFieldId);
		}
	});
	
	$('.metaData+p>select').each(function() {
		var chrFieldId = $(this).attr('id');
		var chrValue = $(this).val();
		if ($.Len($.Trim(chrValue))) {
			validateField(chrFieldId);
		}
	});
	
	$('.metaData+p>input[type=checkbox]').each(function(event) {
		var chrFieldId = $(this).attr('id');
		validateField(chrFieldId);
	});
}


 



