jQuery(document).ready(function($) {
	
	$('input[name="dob1"], input[name="dob2"], input[name="dob3"]').autoTab();
	
	// set up tooltips
	var timeout;
	$("a.tooltip").each(function() {
		var div;
		$(this).hoverIntent(function() {
			offset = $(this).offset();
			div = $('div[@name=\''+$(this).attr('name')+'\']');
			text = div.html();
			div.css({
				opacity: 0,
				visibility: 'visible',
				top: offset.top + $(this).height(),
				left: offset.left
			}).html(text).fadeTo(200, .9).hoverIntent(function() {
				clearTimeout(timeout);
			}, function() {
				$(this).hide();
			});
		}, function() {
			timeout = setTimeout(function() {
				div.hide();
			}, 300);
		});
	});
});

function info_table(table_class) {
	jQuery(document).ready(function($) {
		$('table.'+table_class).find('tbody > tr').each(function() {
			$(this).find('td:first').css({
				textAlign: 'left',
				fontWeight: 'bold'
			});
			$(this).find('td').css('vertical-align', 'top');
		});
	});
}

function save() {
	this.url = '';
	this.save_id = 'save';
	this.form_id = 'form';
	this.src = '';
	this.src_alt = '';
	this.return_false = true;
	this.disable_on_start = false;
	this.validate_before_save = false;
	this._changed = false;
	this.disable_after_save = false;
	this.alert_on_save = true;
	this.alert_text = 'Your quotation has been saved successfully!';
	this.alertHTML = '';
	
	this.s;
	this.f;
	
	this.init = function() {
		var t = this;
		t.s = jQuery('#'+t.save_id);
		t.f = jQuery('form#'+t.form_id);
		if (t.disable_on_start)
			t.disable();
		t.s.click(function() {
			t.save();
			if (t.return_false)
				return false;
		});
		t.f.find('*').change(function() {
			t._changed = true;
			t.enable_disable();
		});
	}
	
	this.enable = function() {
		var t = this;
		t.s.attr({
			'src': t.src,
			'disabled': false
		}).css({
			'cursor': ''
		});
	}
	
	this.disable = function() {
		var t = this;
		t.s.attr({
			'src': t.src_alt,
			'disabled': true
		}).css({
			'cursor': 'default'
		});
	}
	
	this.enable_disable = function() {
		var t = this;
		if (t._changed)
			t.enable();
		else
			t.disable();
	}

	this.save = function() {
		var t = this;
		if (t.validate_before_save) {
			if (t.f.validate().form()) {
				t.submit();
			}
		} else {
			t.submit();
		}
	}
	
	this.submit = function() {
		var t = this;
		t.f.ajaxSubmit({
			url: t.url,
			success: function(data) {
				if (t.disable_after_save) {
					t._changed = false;
					t.disable();
				}
				if (t.alert_on_save) {
					if (t.alertHTML) {
						$.customAlert({
							html: t.alertHTML,
							message: '<p>'+t.alert_text+'</p>',
							messageContainer: '#custom-alert-message',
							buttonContainer: '#custom-alert-buttons',
							customAnimationOpen: function(o) {
								o.fadeIn('fast');
								if ($.browser.msie === true && parseInt($.browser.version) <= 6)
									iepngfix();
							},
							customAnimationClose: function(o) {
								o.fadeOut('fast');
							}
						});
					} else
						alert(t.alert_text);
				}
			}
		});
	}
}
function addCoveredPpl(){
	var row="";	
	$('#nocoverppl').html("");
	row += '<tr><td class="text-left short">Name</td>';
	row += '<td><strong>Title:</strong><br /><select name="covered_ppl[title][]"><option value="Mr">Mr</option><option value="Ms">Ms</option><option value="Mrs">Mrs</option><option value="Miss">Miss</option><option value="Dr">Dr</option><option value="Rev">Rev</option><option value="Sir">Sir</option></select></td>';
	row += '<td><strong>First:</strong><br /><input type="text" name="covered_ppl[name1][]" title="Please enter your first name" value="" /></td>';
	row += '<td><strong>Middle:</strong><br /><input type="text" name="covered_ppl[name2][]" value="" /></td>';
	row += '<td><strong>Last:</strong><br /><input type="text" name="covered_ppl[name3][]" title="Please enter your last name" value="" /></td></tr>';
	row += '<tr><td class="text-left short">Sex:</td><td colspan="4"><select name="covered_ppl[sex][]"><option value="Male">Male</option><option value="Female">Female</option></select></td>	</tr>';
	row += '<tr><td class="text-left short">Date of birth:</td>';
	row += '<td colspan="4">(dd/mm/yyyy) <br /><input type="text" name="covered_ppl[dob1][]" title="dd" style="width: 14px;" maxlength="2" value="" /> /';
	row += '<input type="text" name="covered_ppl[dob2][]" title="mm" style="width: 14px;" maxlength="2" value="" /> /';
	row += '<input type="text" name="covered_ppl[dob3][]" title="yyyy" style="width: 28px;" maxlength="4" value="" /></td></tr>';
	row += '<tr><td>Relationship to applicant</td><td colspan="4"><input type="text" name="covered_ppl[relation][]" value="" /></td></tr>';
	row += '<tr><td>&nbsp;</td></tr>';
	$('#addppl').append(row);
}