/*
 * $ Default javascript file
 *
 * Copyright (c) 2007 That's-id Multimedia
 */

/*------------------------------------------------------------------------------
	Methode om CSS background-image cache te forceren voor IE6
------------------------------------------------------------------------------*/

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


/*------------------------------------------------------------------------------
	onLoad handler
------------------------------------------------------------------------------*/

function onLoad(formTokens)
{

	//form validation
	if (typeof $.validator == 'object') {
	    //load tokens if specified
	    if(typeof formTokens == 'object') {
	    	$.validator.tokenEnable(formTokens);
	    } else {
	    	$('form').validatorEnable({autoFocus: false});
	    }
	}	
	
	setSearchSelectHandlers();
	
	if ($.browser.msie)
	{
		$('#navMain ul li').hover(function(){
		  $(this).addClass("hover");
		},function(){
		  $(this).removeClass("hover");
		});
	}
}


/*------------------------------------------------------------------------------
	AJAX handlers
------------------------------------------------------------------------------*/

function setIndex(pub_Id, page, archive)
{
	$.ajax({
		url: '/dynamics/getindex.php',
		data: { pub_Id: pub_Id, page: page, archive: archive },
		success: function(msg) {
			document.getElementById('index_container').innerHTML=msg;
		}
	});
}


/*------------------------------------------------------------------------------
	Various
------------------------------------------------------------------------------*/

function checkCookies(redirect)
{
	var cookieName = 'testCookie' + (new Date().getTime());

	document.cookie = cookieName + '=cookieValue';
	var cookiesEnabled = document.cookie.indexOf(cookieName) != -1;
	if (!cookiesEnabled)
	{
		location.href=redirect;
	}
}

/*------------------------------------------------------------------------------*/

function setRegFormSector(sector, functieVal)
{
	if (typeof functieVal=='undefined') var functieVal = '';

	var organisatieLabel = 'Bedrijfsnaam';
	if (sector=='overheid')
	{
		organisatieLabel = 'Overheidinstelling';
	}
	else if (sector=='onderwijs')
	{
		organisatieLabel = 'Onderwijsinstelling';
	}

	$('#input_ctrl_organisatie label').html(organisatieLabel+'<span class="contTxtAccent">*</span>');

	var ctrl = $('#input_ctrl_functie div.formCtrl');
	if (sector=='onderwijs')
	{
		if ($('.formRadio', ctrl).length <= 0)
		{
			var functie = new Array('Docent PO', 'Docent VO', 'Decaan', 'Directie', 'Anders, namelijk:');
			var checked = false;

			$('input#SFRM_att_functie[type=text]').remove();

			for(var i=0; i < functie.length; i++)
			{
				if (functie[i]==functieVal) checked=true;
				ctrl.append('<div class="formRadio"><input type="radio" name="SFRM_att_functie" class="validator-select" value="'+functie[i]+'" '+(functie[i]==functieVal || (i==4 && !checked && functieVal!='') ? 'checked="checked" ' : '')+'id="SFRM_att_functie'+i+'"><label for="SFRM_att_functie'+i+'">'+functie[i]+'</label>'+(i==4 ? ' <input type="text" name="SFRM_att_functie_other" id="SFRM_att_functie_other" value="'+(!checked ? functieVal : '')+'" maxlength="64">' : '')+'</div>');
			}
		}
	}
	else
	{
		if ($('input#SFRM_att_functie[type=text]').length <= 0)
		{
			$('.formRadio', ctrl).remove();
			ctrl.prepend('<input id="SFRM_att_functie" name="SFRM_att_functie" class="validator-required" maxlength="64" value="'+functieVal+'">');
		}
	}
}

/*------------------------------------------------------------------------------*/

var searchSelectTriggered = false;
var postcodeLookupTriggered = false;
var postcodeLookup;
function setSearchSelectHandlers()
{
	//check current
	if($('#frm_select1 #SFRM_soort').val()) {
		toggleSoortOptions($('#frm_select1 #SFRM_soort').val());
	}
	
	// form select change trigger
	$('#frm_select1 :input:not(:checkbox), #frm_select2 :input:not(:checkbox)').change(function()
	{
		getResult($(this).parents('form'));
	});
	
	// form select click trigger
	$('#frm_select1 input:checkbox, #frm_select2 input:checkbox').click(function()
	{
		getResult($(this).parents('form'));
	});

	// postcode lookup
	$('#frm_select1 #SFRM_postcode').keyup(function()
	{
		if (this.value.length==4)
		{
			if (this.value==postcodeLookup && $('#resultLocation').html()!='') return;
			if (!postcodeLookupTriggered)
			{
				postcodeLookupTriggered = true;
				postcodeLookup = this.value;
				$('#resultLocation').load('/dynamics/search.php?action=postcode', {value: this.value}, function(){ postcodeLookupTriggered = false; });
				$('#frm_select1 #SFRM_straal').attr('disabled', '');
			}
		}
		else
		{
			$('#resultLocation').html('');
			$('#frm_select1 #SFRM_straal').attr('disabled', 'disabled');
		}
	});
}

function getResult(form)
{
	//hide postcode options
	toggleSoortOptions($('#frm_select1 #SFRM_soort').val());
	
	if (searchSelectTriggered) return;
	searchSelectTriggered = true;
	//var form = $(this).parents('form');
	$('#resultQty', form).html('Aantal resultaten: <b>berekenen</b>');
	$.ajax({
		type: 'POST',
		dataType: 'json',
		url: '/dynamics/search.php?action=count',
		data: form.find(':input').serialize(),
		success: function(msg){
			$('#resultQty', form).html('Aantal resultaten: <b>'+msg.total+'</b>');
			//$('#resultLocation', form).html($('#SFRM_postcode', form).val()!='' ? msg.location : '');
			searchSelectTriggered = false;
		},
		error: function()
		{
			$('#resultQty', form).html('Aantal resultaten: <b>berekenen mislukt</b>');
			searchSelectTriggered = false;
		}
	});
	return false;
}


/*------------------------------------------------------------------------------*/

var form_focus = '';
function setSearchSelectForm(bool)
{
	if (bool && form_focus!='select')
	{
		$('#frm_select1,#frm_select2').removeClass('contTxtGhost').siblings(':submit').attr('disabled', '');;
		$('#frm_term1,#frm_term2').addClass('contTxtGhost').siblings(':submit').attr('disabled', 'disabled');
		$('#frm_term1 :input:visible,#frm_term2 :input:visible').not(':submit').val('');
		form_focus='select';
	}
	else if (!bool && form_focus!='term')
	{
		$('#frm_term1,#frm_term2').removeClass('contTxtGhost').siblings(':submit').attr('disabled', '');;
		$('#frm_select1,#frm_select2').addClass('contTxtGhost').siblings(':submit').attr('disabled', 'disabled');;
		$('#frm_select1 :input:visible,#frm_select2 :input:visible').not(':submit').val('');
		$('#resultQty').html();
		form_focus='term';
	}

}

function toggleSoortOptions(soort)
{
	if(soort == 5) {
		$('#SFRM_postcode').val('');
		$('#SFRM_straal').val(0);
		$('#resultLocation').html('');
		$('.ctrl_postcode').hide();
		$('.ctrl_straal').hide();
		$('.ctrl_regio').show();
	}
	else {
		$('.ctrl_postcode').show();
		$('.ctrl_straal').show();
		$('.ctrl_regio').hide();
	}
}


var lastMail = null;
function setEmail(mail)
{
	lastMail = mail;
	$('#SFRM_email_hidden').val(lastMail);
}

function setSubscribeForm(type)
{
	if($('#SFRM_email').val()) {
		setEmail($('#SFRM_email').val());
	}
	$('#SFRM_email').blur(function() {
		
		if(!this.value || this.value == lastMail) return null;
		setEmail(this.value);
		
		$.ajax({
			type: 'POST',
			dataType: 'json',
			url: '/dynamics/subscribtions.php',
			data: {mail: this.value},
			success: function(data){
								
				if(data.subscribtions) {
					$('.formNotice2').show();
					$('input').attr('disabled',true);
					window.location.href = "/publicaties/"+data.subscribtions+"&nieuwsbrieven=aanpassen";
					return true;
				}
			},
			error: function()
			{
				//reset on different e-mail
				$('input:checkbox').attr('checked',false);
				$('.formNotice').hide();
				$('.formNotice2').hide();
				$('.completeUnsubscribe').hide();
				setAllMandatory();
			}
		});
	}
	);
}

/*
 * Zet regio verplicht als thema gezet is en andersom
 */
function setMandatorySubscribe() 
{
		var manRegio = false;
		var manThema = false;
		
		//bepaal verplichte opties
		$("form :checkbox").each(function(){
			if(this.name == 'SFRM_list[]' && this.checked) {
				//betaknooppunt has no region
				if(this.value != 3) {
					manRegio = true; 
				}	
			}
			if(this.name == 'SFRM_regio[]' && this.checked) {
				manThema = true;
			}
		});
		
		//if new theme is required
		if($('#submit').val() == 'Aanmelden') {
			manThema = true;
		}
		
		//zet alle validatie classes
		$("form :checkbox").each(function(){
			if(this.name == 'SFRM_list[]') {
				if(manThema) {
					$(this).addClass('validator-select');
					$('#SFRM_list_Mandatory').show();
				} else {
					$(this).removeClass('validator-select');
					$('#SFRM_list_Mandatory').hide();
				}
			}
			if(this.name == 'SFRM_regio[]') {
				if(manRegio) {
					$(this).addClass('validator-select');
					$('#SFRM_regio_Mandatory').show();
				} else {
					$(this).removeClass('validator-select');
					$('#SFRM_regio_Mandatory').hide();
				}
			}
		});		
}

//kan niet afmelden dus alles verplicht:
function setAllMandatory()
{
	//zet alle validatie classes
	$("form :checkbox").each(function(){
		if(this.name == 'SFRM_list[]') {
			if(!$(this).hasClass('validator-select')) {
				$(this).addClass('validator-select');
			}
		}
		if(this.name == 'SFRM_regio[]') {
			if(!$(this).hasClass('validator-select')) {
				$(this).addClass('validator-select');
			} 
		}
	});	
}

function setUnSubscribe()
{
	$('input:checkbox').attr('checked',false);
	$('#SFRM_list_Mandatory').hide();
	$('#SFRM_regio_Mandatory').hide();
	$("form :checkbox").each(function() {
		if($(this).hasClass('validator-select'))$(this).removeClass('validator-select');
	});	
}

/*------------------------------------------------------------------------------*/
