function isAjaxEnabled() {
    try {
        jQuery.getJSON('/sneladvies/jsonTest', function(data) {
            if (!data.result) {
                $("#ajaxDisabled").css({display: 'block'});
                return false;
            } else {
                return true;
            }
        });
    } catch (ex) {
        $("#ajaxDisabled").css({display: 'block'});
        return false;
    }
}
//function logoTopMargin() {
//	var tp = ($(window).height() / 4) - $('td.content').position().top;
//	$('#TableLogo').css({ marginTop: tp });
//}

function validatePostcode(fieldName, callback) {
	var sel = ":input[name='" + fieldName + "']";			
	var pc = jQuery.trim($(sel).val());
	if (pc == '') {
		alert('Voer svp een geldige postcode in. Deze is nodig voor de afstandsberekening naar de voor u geselecteerde autobedrijven.');
		$(sel).trigger('focus');
		return;
	}
	var re = new RegExp("^[0-9]{4} ?[A-z]{2}$");
	if (!re.test(pc)) {
		alert('De door u ingevoerde postcode is niet in het juiste formaat, b.v. "1012 NP". Voer een geldige postcode in svp.\nDeze is nodig voor de afstandsberekening naar de voor u geselecteerde autobedrijven.');
		$(sel).trigger('focus');
		return;
	}
	jQuery.getJSON('/sneladvies/postcode?postcode=' + pc, function(data) {
		if (!data.result) {
			alert('De door u ingevoerde postcode komt niet voor in de database met alle bestaande Nederlandse postcode. Voer een geldige postcode in s.v.p. .\nDeze is nodig voor de afstandsberekening naar de voor u geselecteerde autobedrijven.');
			$(sel).trigger('focus');
		}
		else // postcode valid
			callback();
	});
}

var _thProps = ['paddingBottom', 'paddingTop', 'borderBottomWidth', 'borderTopWidth', 'marginTop', 'marginBottom'];
var _twProps = ['paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth', 'marginLeft', 'marginRight'];

function totalHeight(elem) {
	var d = $(elem).height();
	for (i = 0; i < _thProps.length; i++) {
		var val = parseInt($(elem).css(_thProps[i]));
		if (!isNaN(val))
			d += val;
	}
	return d;
}
function totalWidth(elem) {
	var d = $(elem).width();
	for (i = 0; i < _twProps.length; i++) {
		var val = parseInt($(elem).css(_twProps[i]));
		if (!isNaN(val))
			d += val;
	}
	return d;
}



// ------------------------------------------------------------ class SubMenu
function SubMenu(button, panel) {
	
	//------------------------------------------------------------- variables
	this.button = $(button);
	// hereby binding SubMenu object to button
	// to find SubMenu object without context, just by button id
	this.button.data('SubMenuObj', this); 
	this.panel = $(panel);
	this.timer = null;
	this.height = null;
	this.visible = false;
	this.interval = 500;
			
	//--------------------------------------------------------------- methods
	this.show = function() {
		if (this.height == null) {
			this.panel.show();
			this.height = this.panel.outerHeight();
			this.panel.hide();
		}
		var x = this.button.offset().left;
		//var y = this.button.offset().top - this.height;
        var y = this.button.offset().top + 17;
		this.panel.css({ top: y, left: x });
		this.visible = true;
		this.panel.fadeIn("slow");		
	};
	
	this.hide = function() {
		this.visible = false;
		this.panel.fadeOut("slow");		
	};
	
	this.mouseIn = function() {
		clearTimeout(this.timer);
		this.timer = null;
	};
	
	this.mouseOut = function() {
		if (this.visible) {
			var cmd = 'SubMenu_hide($("#' + this.button.attr('id') + '"));';
			this.timer = setTimeout(cmd, this.interval);
		}
	};

	//-------------------------------------------------------- event handlers
	// alias for this (as "this" is overridden in event handlers)
	var th = this; 	
	this.button.mouseover(function() {
		if (th.visible)
			th.mouseIn();
		else 
			th.show(); 
	});
	this.panel.mouseout(function() { th.mouseOut(); });
	this.panel.mouseover(function() { th.mouseIn(); });
	this.button.mouseout(function() { th.mouseOut(); });
}

// SubMenu_hide calls SubMenu.hide() without context
function SubMenu_hide(elem) {
	var sm = elem.data('SubMenuObj');
	sm.hide();
}

// extend jQuery
jQuery.fn.extend({
	attachMenu: function(panel) {
		sm = new SubMenu(this, panel);
	}
});

