// Fleetcare Core JavaScript

$(document).ready(function() {

	searchSite = $('#searchsite').val();
	$('#searchsite').focus(function() {
		if($(this).val() == searchSite) {
			$(this).val("");
		}
	}).blur(function () {
		if($(this).val() == "") {
			$(this).val(searchSite);
		}
    });
	
	homeRego = $('#hm_registration').val();
	$('#hm_registration').focus(function() {
		if($(this).val() == homeRego) {
			$(this).val("");
		}
	}).blur(function () {
		if($(this).val() == "") {
			$(this).val(homeRego);
		}
    });
	
	
	
	// Drop Down Nav
	/*if (document.all) {
		$("#topnav li").hoverClass("sfHover");
	}*/

	// Vertical Centre RHS Buttons
	$("a.pageoption").each(function() {
		var txtHeight = $("span", this).height();
		var cssObj = {
			height: txtHeight,
			top: "48%",
			marginTop: -(txtHeight/2)
		}
		$("span", this).css(cssObj);		
	});
	
	// Home set heights
	var lnHeight = $(".latest-news").height();
	var qlHeight = $(".quick-links").height();
	if(qlHeight > lnHeight){
		$(".latest-news").height(qlHeight);
	}
	
	// Product Landing Alternate
	$("a.feature:odd").addClass("floatright");
	
	// Product Matrix Hover State
	$(".matrix-row").hover(
		function() { $(this).addClass("hover");  },
		function() { $(this).removeClass("hover"); }
	);
	
	
	// Fleettorque Archive Control
	$(".ftbox").hide();
	$("h4.ft").click(function() {
		$(this).next(".ftbox").slideToggle("normal");
	});

});


// Drop Downs Custom Hover Class Function
$.fn.hoverClass = function(c){
	return this.each(function(){
		$(this).hover(
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};


// Form Validation Errors
function displayErrorsAlert(aErrors) {

	if (aErrors.length) {
		if (aErrors.length > 1) {
			var prefix = 'Some errors occurred:\n\n';
			var suffix = '\nPlease correct these errors and try again.';
		}
		else {
			var prefix = 'An error has occurred:\n\n';
			var suffix = '\nPlease correct this error and try again.';
		}

		var errorMessage = prefix;

		for (var i = 0; i < aErrors.length; i++) {
			errorMessage += aErrors[i] + "\n";
		}

		errorMessage += suffix;

		alert(errorMessage);
	}

}