
// Go straight to chosen url from SELECT onchange..
function gotoNewUrl(evt){
	evt.preventDefault();
	var contractor_select = evt.target;
	if(contractor_select.value !== ""){
		if(contractor_select.id == "id_category_filter"){
			var new_url = window.location.protocol + "//" + window.location.host + window.location.pathname + "?category_filter=" + contractor_select.value;
		}else{
			var new_url = window.location.protocol + "//" + window.location.host + contractor_select.value;
		}
		window.location = new_url;
	}
}

function filterCategory(evt){
	evt.preventDefault();
	var contractor_select = evt.target;
	if(contractor_select.value !== ""){
		var new_url = window.location.protocol + "//" + window.location.host + contractor_select.value;
		window.location = new_url;
	}
}
$(document).ready(function(){
	if($("#mediaitemstypeform")){
		// Hide button
		$("#mediaitemstypesubmit").hide();
		$("#mediaItem").change(gotoNewUrl);
	}
	// Hide button
	$("#filterSubmit").hide();
	$("#id_category_filter").change(gotoNewUrl);
});
function initTariff(){
	if($('#tarifPopup')){
		$('#tarifPopup').hide();
	}
	// HIJAX tariff sender form
	if($('mediadoorstuurform')){
		hijax_tariff_form();
	}
	tarifPopUpOpener();
}

function hijax_tariff_form(){
	
	var form = $('#mediadoorstuurform');
	// Get/create AJAX loader img
	loaderIMG = "<img id='ajaxLoader' src='/static/img/tarif_loader.gif' />";
	// Get form submit P (to add loader)
	submitP = $('#tarievensubmit').parent('p');
	submitHTML = submitP.html();
	// Hijax form
	form.submit(function(event){
		// Stop default submit event
		event.preventDefault();
		// Show AJAX loader...
		$('form div.container:first').prepend(loaderIMG);
		
		// submit form data to Django AJAX view
		// .. and replace Form Div with results or validated form
		$.ajax({
			type: "POST",
			url: "/ajax/mediatarieven/send/",
			data: form.serialize(),
			error: function(xhr){form.children('div.container:first').prepend(xhr.statusText);},
			success: function(data){form.children('div.container:first').html(data);},
			complete: function(xhr){$('#ajaxLoader').remove();}
		});
	});
}



function tarifPopUpOpener(){
		var tarifId = location.href.split('#')[1];

		if(location.href.split('#')[ 1] == 'tarieven'){
			$('#tarifPopup').toggle();
		}
	 }
/* **********************************************
 * EVENTS
 * ********************************************* */
$(document).ready(initTariff);
