// some common functions
function hideAllSubmenus() {}

function trim(value) {
  value = value.replace(/^\s+/,'');
  value = value.replace(/\s+$/,'');
  return value;
}

function validateEmail(email) {
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test( email ) ) {
		return false;
	} else {
		return true;
	}
}

function validatePhone(phone) {
	return true;
}


var hideheader;


$(document).ready(function(){
	
	init_hideheader();
	
	init_strech();
	
	init_submenus();
	
	$('.headertoggle').click(function(e){
		e.preventDefault();
		toggle_header();
	});
	
	
	$('.catlisttoggle').click(function(e){
		e.preventDefault();
		toggle_catlist($(this).closest('.catlist'));
	});
	
	$('form').submit(function(){
		var ok = true;
		$(this).find('.form_required').parent().each(function() {
			var input = $(this).find('input,textarea');
			if (input.val()=='') {
				ok = false;
				$(this).addClass('error');
			} else if( $(input).hasClass('validate-email') ) {
				if (!validateEmail(input.val())) {				
					ok = false;
					$(this).addClass('error');
				} else {
					$(this).removeClass('error');
				}
			}  else if( $(input).hasClass('validate-phone') ) {
				if (!validatePhone(input.val())) {				
					ok = false;
					$(this).addClass('error');
				} else {
					$(this).removeClass('error');
				}
			} else {
				$(this).removeClass('error');
			}
		});
		
		if (!ok) {
			$(this).find('p.form_error').show();
		} else {
			$(this).find('p.form_error').hide();
		}
		return ok;
	});
	
	$('table.qheader.benchmarking td:last-child,table.question.benchmarking td:last-child').css('border-right','none');
	
	$('.bm_showmodal').click(function(e){
		e.preventDefault();
		
		var url = $(this).attr('href');
		
		$.modal('<iframe src="' + url + '" height="560" width="935" frameborder="no" scrolling="no" style="border:0"></iframe>', {
		
			containerCss:{
				backgroundColor:"#fff",
				borderColor:"#fff",
				height:566,
				padding:0,
				width:935
			}
		});	
		
	});
	
	$(".tooltip").tipTip({defaultPosition: "top",delay:0});
	
});

function init_strech() {
	var colheight = 0;
	$('.stretch').each(function(){
		colheight = Math.max(colheight,$(this).height());		
	});
	$('.stretch:not(.box)').css('height',colheight);
	$('.box.stretch').css('height',colheight+20);
}

function init_hideheader() {
	var hide = $.cookie('_bm_hideheader');
	if(hide == null ) {
		hide = 0;
		$.cookie('_bm_hideheader', 0, { expires: 365, path: '/'});
	}
	hide = parseInt(hide);
	hideheader = hide;
	if(hide) {
		$('.headerimage').hide();
		$('.headertoggle img').attr('src', '/images/ico_headertoggle_down.gif');
	}
}


function init_submenus() {

	$('.submenu li a span').each(function(){
		var top = Math.floor( (33-$(this).height())/2);
		$(this).css('top',top);
		$(this).css('visibility','visible');
	});

}

function toggle_header() {
	if(hideheader) {
		hideheader = 0;
		$('.headerimage').show('blind');
		$('.headertoggle img').attr('src', '/images/ico_headertoggle_up.gif');
	} else {
		hideheader = 1;
		$('.headerimage').hide('blind');
		$('.headertoggle img').attr('src', '/images/ico_headertoggle_down.gif');
	}
	
	$.cookie('_bm_hideheader', hideheader, { path: '/'});
}

function toggle_catlist (jqoCatlist) {
	var img = jqoCatlist.find('.catlisttoggle img');
	if(img.attr('src').indexOf('down')>0) {
		jqoCatlist.find('tbody.content').show();
		img.attr('src','/images/ico_headertoggle_up.gif');
	} else {
		jqoCatlist.find('tbody.content').hide();
		img.attr('src','/images/ico_headertoggle_down.gif');
	}
}

function getdutchnumber(s) {
	s += '';
	var x = s.split('.');
	var x1 = x[0]; 
	var x2 = x.length > 1 ? ',' + x[1] : '';	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return '' + x1 + x2;
}
