var timerid = null;
Ajax.Responders.register({ onCreate: function() { Ajax.activeRequestCount=1; }, onComplete: function() { Ajax.activeRequestCount=0; }});
function showLoader(div) { var timerid = window.setTimeout("startloader('" + div + "')", 1000); return timerid; }
function startloader(divloader) { if(!Ajax.activeRequestCount) { return false; } }
function stopLoader(divloader, timerid) { Ajax.activeRequestCount=0; window.clearTimeout(timerid); }

function sendRequest(functioncall, div) {
	if(Ajax.activeRequestCount || !div) { return false; }
	timerid = showLoader(div);
	var url = basepath + functioncall;
	new Ajax.Request(url, {
		method: 'post',
		parameters: { requestitem: functioncall },
		onSuccess: function(transport, json) {
			stopLoader(div, timerid);
			if(!json) { json = eval('(' + transport.responseText + ')'); }
			if(json.error) { errorMessage(json.error,json.type,div);
			} else {
				if(json.notice) { errorMessage(json.notice,json.type,div); }
				if(json.reload) { location.reload(); }
				if(json.tinymce) { tinyMCE.activeEditor.setContent(json.tinymce); }
				if(json.html) { $(div).innerHTML = json.html; }
				if(json.execute) { eval(json.execute + '()'); }
				if(json.redirect) { redirect(json); }
			}
		},
		onFailure: function(transport) {
			stopLoader(div, timerid);
			alert('method ' + methode + ' failed');
		}
	});
	return false;
}

function postRequest(functioncall, div, div2) {
	if(Ajax.activeRequestCount || !div2) { return false; }
	timerid = showLoader(div2);
	var url = basepath + functioncall;
	var params = $(div).serialize();
	new Ajax.Request(url, {
		method: 'post',
		parameters: params,		
		onSuccess: function(transport, json) {
			stopLoader(div2, timerid);			
			if(!json) { json = eval('(' + transport.responseText + ')'); }	
			if(json.error) { errorMessage(json.error, json.type, div); }
			else {
				if(json.notice){ errorMessage(json.notice, json.ntype, div); }
				if(div2 && json.html){ $(div2).innerHTML = json.html; }	
				if(json.tinymce) { tinyMCE.activeEditor.setContent(json.tinymce); }
				if(json.reload) { location.reload(); }
				if(json.redirect) { redirect(json); }
				if(json.execute) { eval(json.execute + '()'); }		
			}
		},
		onFailure: function(transport) {
			stopLoader(div2, timerid);
			alert('method ' + methode + ' failed');
		}
	});
	return false;
}

function updateOrder(functioncall, div) {
	if(Ajax.activeRequestCount || !div) { return false; }
	timerid = showLoader(div);	
	var url = basepath + functioncall + div;
	var params = Sortable.serialize(div);
	new Ajax.Request(url, {
		method: 'post',
		parameters: params,		
		onSuccess: function(transport, json) {
			stopLoader(div, timerid);
			if(!json) { json = eval('(' + transport.responseText + ')'); }	
			if(json.error) { errorMessage(json.error, json.type, div); }
			else {
				if(json.notice) { errorMessage(json.notice, json.ntype, div); }
				if(div && json.html) { $(div).innerHTML = json.html; }	
				if(json.tinymce) { tinyMCE.activeEditor.setContent(json.tinymce); }				
				if(json.reload) { location.reload(); }	
				if(json.execute) { eval(json.execute + '()'); }
				if(json.redirect) { redirect(json); }
			}
		},
		onFailure: function(transport) {
			stopLoader(div, timerid);
			alert('method ' + methode + ' failed');
		}
	});
	return false;
}

function imageRequest(functioncall, imagename, div) {
	if(Ajax.activeRequestCount || !div) { return false; }
	timerid = showLoader(div);
	var url = basepath + functioncall;
	new Ajax.Request(url, {
		method: 'post',
		parameters: { image: imagename },
		onCreate: function() {
			showLoader(div, timerid);
		},		
		onSuccess: function(transport, json) {
			stopLoader(div, timerid);
			if(!json) { json = eval('(' + transport.responseText + ')'); }	
			if(json.error) { errorMessage(json.error, json.type, div); }
			else {
				if(json.notice) { errorMessage(json.notice, json.type, div); }
				if(json.execute) { eval(json.execute + '()'); }
				if(json.redirect) { redirect(json); }
				$(div).innerHTML = json.html;
			}
		},
		onFailure: function(transport) {
			stopLoader(div, timerid);
			alert('method ' + methode + ' failed');
		}
	});
	return false;
}

function redirect(json) { if(!json.redirect) { return false; } window.location = basepath + json.redirect; }
function errorMessage(message, type, div) { if(type) { return alert(message); } $('error').innerHTML=message; }
function confirmation(functioncall, div) { var test = confirm("Wilt u dit item verwijderen?"); if(test) { sendRequest(functioncall, div); return true; } return false; }
function startUpload(form, div) { Ajax.activeRequestCount = 1; $(form).submit(); showLoader(div); }
function reloadDiv(functioncall, div, timerid) { Ajax.activeRequestCount = 0; stopLoader(div, timerid); sendRequest(functioncall, div); }
function showDiv(name) { $(name).show(); }
function hideDiv(name) { $(name).hide(); }
function toggleDiv(name) { $(name).toggle(); }
function uploadedImages() { Sortable.create('uploadorder',{tag:'div', ghosting:false, constraint:false, onUpdate:updateuploadedImages}); }
function updateuploadedImages() { updateOrder('fileupload/sortlist/', 'uploadorder'); }

function postPrices(functioncall, id, div2) {
	if(Ajax.activeRequestCount || !div2) { return false; }
	timerid = showLoader(div2);
	var url = basepath + functioncall + id;
	var params = {
		"form[price_normal]":$('price_normal_' + id).value,
		"form[price_discount]":$('price_discount_' + id).value,	
		"form[price_dealer]":$('price_dealer_' + id).value,
		"form[price_margin]":$('price_margin_' + id).value
	};
	new Ajax.Request(url, {
		method: 'post',
		parameters: params,		
		onSuccess: function(transport, json) {
			stopLoader(div2, timerid);			

			if(!json) { json = eval('(' + transport.responseText + ')'); }	
			if(json.error) { errorMessage(json.error, json.type, div2); }
			else {
				if(json.notice){ errorMessage(json.notice, json.ntype, div2); }
				if(div2 && json.html){ $(div2).innerHTML = json.html; }	
				if(json.tinymce) { tinyMCE.activeEditor.setContent(json.tinymce); }
				if(json.reload) { location.reload(); }
				if(json.redirect) { redirect(json); }
				if(json.execute) { eval(json.execute + '()'); }		
			}
		},
		onFailure: function(transport) {
			stopLoader(div2, timerid);
			alert('method ' + methode + ' failed');
		}
	});
	return false;
}
