function tooltipScore(scorediv, tooltipdiv)
{
	$('#' + scorediv).hover(function(e) {
		$('#' + tooltipdiv).show();
	}, function() {
	    $('#' + tooltipdiv).hide();
	});
}

function confirmpopup(Message, url) { 

    var is_confirmed = confirm(Message);
    if (is_confirmed) {
       location = url;
    }
}

function showtab(tabnr, totaltabs)
{
	for(i=1;i<=totaltabs;i++)
	{
		if (document.getElementById('tab' + i)) document.getElementById('tab' + i).style.display = 'none';
		if (document.getElementById('tabbutton' + i)) document.getElementById('tabbutton' + i).className = '';
	}
	document.getElementById('tabbutton' + tabnr).className = 'selected';
	document.getElementById('tab' + tabnr).style.display = 'block';
}
function hashesToArray() {
	var hash = window.location.hash;
	var hashes = hash.substr(2).split('&');
	$.hashParameters = [];
	for (hash in hashes) {
		if (hashes[hash].indexOf('=') != -1) {
			var hoi = hashes[hash].split('=');
			$.hashParameters[hoi[0]] = hoi[1];
		}
		else if (hashes[hash] != '') {
			$.hashParameters[hashes[hash]] = true;
		}
	}
}
function generateCompareUrl(aSettings)
{
	if ($.site_base_url) {
		url = $.site_base_url;
	}
	else {
		url = 'http://www.verzekeraar.nl/';
	}
	urlOptions = '';

	if (aSettings['insurance']) {
	console.log(aSettings['insurance']);
		url += aSettings['insurance'] + '-vergelijken/';
	}
	else if ($.insuranceName) {
		url += $.insuranceName + '-vergelijken/';
	}
	else {
		url += 'verzekeringen-vergelijken/';
	}
	for (i in aSettings) {
		urlOptions += '&' + i + '=' + aSettings[i];
	}
	if (urlOptions != '') url += '#!' + urlOptions.substr(1);
	return url;
}

function validateEmail(form_id,email) 
{
 
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   var address = document.forms[form_id].elements[email].value;
   if(reg.test(address) == false) 
   {
	  return false;
   }
}  

function submitEmail(FieldId, FormId)
{
	if(document.getElementById(FieldId).value=='' || validateEmail( FormId, FieldId) === false  )
	{
		window.alert('U bent vergeten een (geldig) e-mailadres in te vullen. Vul deze nog even in!');
		return false;
	}
	else
	{
		document.forms[FormId].submit();
	}
}

 function redirectOverview(iInsuranceId, iSortId)
 {
	//Set url beginning
	$sUrl = '/verzekeraars/';
	
	//Check on value
	if(document.getElementById(iInsuranceId).value != '')
	{
		$sUrl = $sUrl + document.getElementById(iInsuranceId).value + '/';
	}
	
	//Check on value
	if(document.getElementById(iSortId).value != '')
	{
		$sUrl = $sUrl + document.getElementById(iSortId).value + '/';
	}
	
	//Redirect
	document.location.href= $sUrl;
 }
 
//Function for loading image
function Loader(fieldid)
{
 if(document.getElementById(fieldid).style.display == 'none')
 {
   document.getElementById(fieldid).style.display = ''; 
   document.getElementById('pagecontent').style.display = 'none';
 }
 else
 {
   document.getElementById(fieldid).style.display = 'none';
   document.getElementById('pagecontent').style.display = '';
 }
}


//This function generates the url for compare detail
function generateCompareDetail(Id, productId, totalProducts, ButtonId, ButtonId2, iMaxSelect, sUri, alertmessage)
{
	var sUrl = '/verzekeringen-vergelijken/detail/';
	var iCounter = 0;
	var iChecks  = 0;
	
	if( iMaxSelect === undefined )
	{
		var iMaxSelect = 3;	
	}
	
	if( sUri === undefined )
	{
		var sUri = '';	
		var iMinProducts = 2;
	}
	else
	{
		var iMinProducts = 1;
	}
	
	if( alertmessage === undefined )
	{
		var alertmessage = 'U kunt maximaal ' + iMaxSelect + ' verzekeringen tegelijk vergelijken.';	
	}
	
	//Loop
	for($i = 1; $i<=totalProducts; $i++)
	{
		if (document.getElementById(productId+$i))
		{
			if( document.getElementById(productId+$i).checked )
			{			
				//Update counter
				iCounter++;
			
				if(iCounter <= iMaxSelect)
				{
					//Put ids in url devided by _
					sUri = sUri + document.getElementById(productId+$i).value + '_';
				
					//Update checked
					iChecks++;
				}
			}
		}
	}
	
	//No more then 3
	if(iCounter > iMaxSelect)
	{
		window.alert(alertmessage);
		document.getElementById(Id).checked = false;
	}
	//Remove disabled
	else if(iCounter >= iMinProducts)
	{
		//Remove disabled
		$("#"+ButtonId).removeAttr('disabled');
		
		//Update class
		//document.getElementById(ButtonId).setAttribute("class", "button thickbox");
		$("#"+ButtonId).attr('class', 'button thickbox');
		
		//Remove on second button
		if(ButtonId2)
		{
			$("#"+ButtonId2).removeAttr('disabled');
			
			//Update class
			//document.getElementById(ButtonId2).setAttribute("class", "button thickbox");
			$("#"+ButtonId2).attr('class', 'button thickbox');
		}
	}
	//Add disabled again
	else if(iCounter < iMaxSelect-1)
	{
		//add disabled
		$("#"+ButtonId).attr('disabled','true');
		
		//Update class
		//document.getElementById(ButtonId).setAttribute("class", "button_dis thickbox");
		$("#"+ButtonId).attr('class', 'button_dis thickbox');
		
		//add on second button
		if(ButtonId2)
		{
			$("#"+ButtonId2).attr('disabled','true');
			
			//Update class
			//document.getElementById(ButtonId2).setAttribute("class", "button_dis thickbox");
			$("#"+ButtonId2).attr('class', 'button_dis thickbox');
		}
	}
	
	//Calculate width
	iWidthDef 	= 300;
	iWidth 		= 190 * iChecks;
	iWidth		= iWidth + iWidthDef;
	
	//Create url
	sUrl = sUrl + sUri.substring(0, sUri.length-1) + '/';
	
	//Add tb
	sUrl = sUrl + '?TB_iframe=true&height=550&width=' + iWidth;

	//Give to html
	document.getElementById(ButtonId).setAttribute("alt", sUrl);
	
	//Also to button 2?
	if(ButtonId2)
	{
		document.getElementById(ButtonId2).setAttribute("alt", sUrl);
	}
}

//Function to set a class on one id and resets the classes on other ids in the group
function setClassGroup( iId, sGroupId, iCount, sClassName )
{
	//Reset class
	if (document.getElementById(sGroupId+iId).className== sClassName )
	{
		document.getElementById(sGroupId+iId).className= '';
	}
	//Set class
	else
	{
		document.getElementById(sGroupId+iId).className= sClassName;
	}
	
	//Get class of all others
	for($i = 0; $i < iCount; $i++)
	{
		if($i != iId)
		{
			document.getElementById(sGroupId+$i).className= '';
		}
	}
}

function changeLoadingSource()
{
	document.getElementById('loadingimage').src='/system/view/images/misc/loadBar.gif';
}

function searchLoading()
{
	document.getElementById('body').style.backgroundImage = "url(/system/view/images/misc/bg.jpg)";
	document.getElementById('body').style.backgroundRepeat = 'repeat-x';
	document.getElementById('contentCntr').style.display = 'none';
	document.getElementById('loading').style.display = '';	
	
	window.setTimeout("changeLoadingSource()", 100);
}

function reviewFormSubmit()
{
	//Replace button by loader
	document.getElementById('review_button_submit').innerHTML = '<img src="/system/view/images/misc/loadBar.gif" /><br />Verwerken..';
	
	//Wait 2 secondes then hide form and show other content
	var t=setTimeout("reviewShowSuccess('review_form_holder')",2000);
}

function reviewShowSuccess(idForm)
{
	document.getElementById(idForm).innerHTML = '<br/><br/><br/><div class="errorblock green"><b class="head_title">De ervaring is ingestuurd</b><p>Bedankt voor het insturen van uw ervaring. Wij zullen de ervaring z.s.m. controleren. Indien u een toelichting heeft ingevuld zullen wij u op de hoogte brengen zodra deze gecontroleerd is.<br/><br/><a href="/" title="Verzekeraar.nl">Naar de homepage van Verzekeraar.nl</a></p></div>';
	
	//Scroll to top
	scroll(0,100);
}

$(document).ready(function() {
	window.onhashchange = function() {
		hashesToArray();
		if ($.hashParameters['deelervaring']) {
			showtab(4,4);
		}
		else if($.hashParameters['kostenendekking']) {
			showtab(1,4);
		}
		else if($.hashParameters['verzekeringen']) {
			showtab(1,4);
		}
		else if($.hashParameters['meerinformatie']) {
			showtab(2,4);
		}
		else if($.hashParameters['ervaringen']) {
			showtab(3,4);
		}
	}
	hashesToArray();
	if ($.hashParameters['deelervaring']) {
		showtab(4,4);
	}
	else if($.hashParameters['kostenendekking']) {
		showtab(1,4);
	}
	else if($.hashParameters['verzekeringen']) {
		showtab(1,4);
	}
	else if($.hashParameters['meerinformatie']) {
		showtab(2,4);
	}
	else if($.hashParameters['ervaringen']) {
		showtab(3,4);
	}
	$('#tocompareform').change(function () {
		settings = [];
		settings['maxownrisk'] = $('#tocompare_risk').val();
		settings['coverage'] = $('#tocompare_coverage').val();
		settings['maxmaincost'] = $('#tocompare_monthly').val();
		settings['insurance'] = $('#tocompare_insurance').val();
		var url = generateCompareUrl(settings);
		$(this).attr('action', url);
	});
});

function goToByScroll(id)
{
     	$('html,body').animate({scrollTop: $("#"+id).offset().top},'slow');
}
