$(document).ready(function(){
	
	// Bottom form
	$('form.absenceform').submit(function(){
		calcAbsenceForm(this);
		return false;
	});
	
	// Bottom form rollovers
	$('form.absenceform input.submit').hover(function() {
		$(this).css('color', '#f5a200');
	}, function() {
		$(this).css('color', '#ffffff');
	});

	// Contact form
	$('form.contact').submit(function(){
		var b0k = true;
		$('input:text', this).each(function(){
			if ($(this).is(':visible') && $(this).val() == '') {
				$(this).addClass('required');
				b0k = false;
			}
			else {
				$(this).removeClass('required');
			}
		});
		return b0k;
	});

	// Load accordion
	$('#accordion').accordion({
		icons: { 'header': 'arrow-right', 'headerSelected': 'arrow-down' },
		autoHeight: false,
		active: false,
		collapsible: true
	});
	
	// Fix drop down menu in IE6/7
	$('#header .right ul li').hover(function() {
		$(this).addClass('sfhover');
	}, function() {
		if (!$('a', this).hasClass('selected')) {
			$(this).removeClass('sfhover');
		}
	});
	
	// Load notice
	if ($('#notice').length) {
		window.setTimeout('showNotice()', 1000);
	}
});

function showNotice() {
	$('#notice').show("slide", { direction: "up" }, 800);
}

function addCommas(nStr) {
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function isNumeric(strString) {
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	if (strString.length == 0) return false;
	for (i = 0; i < strString.length && blnResult == true; i++) {
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1) {
			blnResult = false;
		}
	}
	return blnResult;
}

function calcAbsenceForm(form) {
	var employees = $("input[name='employees']", form).val();
	var salary = $("input[name='salary']", form).val();
	
	if (!isNumeric(employees)) {
		alert('Please enter the number of employees as a whole number!');
	}
	else if (!isNumeric(salary)) {
		alert('Please enter the average employee salary as a whole number!');
	}
	else {
		var days_off = salary / 365;
		var cost = 8.3 * employees * days_off;
		var results = 'Employee absence is costing your business:<br /><span>&pound;' + addCommas(cost.toFixed(2)) + '</span>';
		results = results + '<p>To calculate the cost of your B&amp;CE Employee Healthcare Plan <a href="calculate-plan.html?employees=' + employees + '">click here</a>.</p>';
		$('#footer .left .results').html(results).fadeIn('slow');
	}
}
