var updateCartAswell = false;

function AjaxConnector()
{
	this.getRequestObject = function()
	{
		var req = null;
		
		// Mozilla/Safari
		if (typeof XMLHttpRequest != "undefined")
		{
			req = new XMLHttpRequest();
		}
		if (!req && typeof ActiveXObject != "undefined")
		{
			try
			{
				req = new ActiveXObject("Msxml2.XMLHTTP");
			}
			catch (e)
			{
				// IE
				try
				{
					req = new ActiveXObject("Microsoft.XMLHTTP");
				}
				catch (e2)
				{
					req=null;
				}
			}
		}
		if(!req && window.createRequest)
		{
			req = window.createRequest();
		}

		return req;
	}
	
	this.doCall = function(url, data, processingFunction)
	{
		
		var req;
		var me = this; // refer to 'this' inside anonymous function 
		
		req = this.getRequestObject();
		if (!req)
		{
			return false;
		}
		
		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				if (req.status == 200)
				{
					var response = req.responseText;
					
					if (processingFunction instanceof Function)
					{
						processingFunction(response);
					}
				}
			}
			else
			{
				if (updateCartAswell)
				{
					cartObject = document.getElementById('shoppingcartMenu');
					cartObject.innerHTML = "<p>De winkelwagen word bijgewerkt...</p>";
				}
			}
		};
		
		req.open('GET', url, true);
		req.send(data);
	};

	this.doPOSTCall = function(url, data, processingFunction)
	{
		
		var req;
		var me = this; // refer to 'this' inside anonymous function 
		
		req = this.getRequestObject();
		if (!req)
		{
			return false;
		}
		
		req.onreadystatechange = function()
		{
			if (req.readyState == 4)
			{
				if (req.status == 200)
				{
					var response = req.responseText;
					
					if (processingFunction instanceof Function)
					{
						processingFunction(response);
					}
				}
			}
		};
		
		req.open('POST', url, true);
		req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		req.setRequestHeader("Content-length", data.length);
		req.setRequestHeader("Connection", "close");
		req.send(data);
	};
}

function trim(value)
{
	value = value.replace(/^\s+/,''); 
	value = value.replace(/\s+$/,'');
	return value;
}

function doSaveTransmitMethode(transmitMethod)
{
	updateCartAswell = false;

	try
	{
		url = "javascriptverzendmethode.asp?methode=" + transmitMethod;
		connection = new AjaxConnector();
		connection.doCall(url, null, 
			function(returnData)
			{
				if (trim(returnData) != "ERROR")
				{
					priceObject = document.getElementById('totalPriceText');
					priceObject.innerHTML = '<b>&euro; ' + returnData + '</b>';
				}
				else
				{
					alert("Er ging iets niet goed. Gegevens zijn niet aangepast.");
				}
			}
		);
	}
	catch(err)
	{
		alert("Er is iets misgegaan, uw browser ondersteund de bestelmogelijkheid niet!");
	}
}

function doUpdateRowAmount(amountObjId, artNr, amount)
{
	updateCartAswell = false;

	try
	{
		url = "javascriptUpdateRowAmount.asp?artnr=" + artNr + "&amount=" + amount;
		connection = new AjaxConnector();
		connection.doCall(url, null, 
			function(returnData)
			{
				if (trim(returnData) != "ERROR")
				{
					document.getElementById(amountObjId).innerHTML = '<img src="template/images/updating.gif" border="0" width="14" height="14">';
					setTimeout("location = 'overzichtwinkelwagen.asp'", 1000);
				}
				else
				{
					alert("Er ging iets niet goed, de verzendkosten konden niet verwerkt worden.");
				}
			}
		);
	}
	catch(err)
	{
		alert("Er is iets misgegaan, uw browser ondersteund de bestelmogelijkheid niet!");
	}
}


function doBestel(artNr)
{
	updateCartAswell = true;

	try
	{
		url = "javascriptbestel.asp?id=" + artNr;
		connection = new AjaxConnector();
		connection.doCall(url, null, 
			function(returnData)
			{
				if (trim(returnData) != "ERROR")
				{
					cartObject = document.getElementById('shoppingcartMenu');
					cartObject.innerHTML = returnData;

					showDialogMessage("Artikel toegevoegd aan winkelwagen.", 220, true, "overzichtwinkelwagen.asp");
				}
				else
				{
					alert("Er ging iets niet goed, het artikel is niet aan de winkelwagen toegevoegd.");
				}
			}
		);
	}
	catch(err)
	{
		alert("Er is iets misgegaan, uw browser ondersteund de bestelmogelijkheid niet!");
	}
}

function doReport(typeOfBug, artNr, comments)
{
	try
	{
		url = "javascriptmail.asp?bug=" + typeOfBug + "&artNr=" + artNr;
		connection = new AjaxConnector();
		connection.doPOSTCall(url, "comments=" + encodeURIComponent(comments), 
			function(returnData)
			{
				if (trim(returnData) != "ERROR")
				{
					var report = document.getElementById('tab-report');
					report.innerHTML = returnData;

					setTimeout('closeReport()', 2000);

				}
				else
				{
					alert("Er ging iets fout waardoor de mail niet verzonden kon worden.");
				}
			}
		);
	}
	catch(err)
	{
		alert("Er is iets misgegaan, uw browser ondersteund de verzendmogelijkheid niet!");
	}
}

function doQueryPrinterType(comments)
{
	try
	{
		url = "javascriptmail.asp?bug=QueryPrinterType";
		connection = new AjaxConnector();
		connection.doPOSTCall(url, "comments=" + encodeURIComponent(comments), 
			function(returnData)
			{
				if (trim(returnData) != "ERROR")
				{
					var notification = document.getElementById('tab-notification');
					notification.innerHTML = returnData;

					setTimeout('displayPrinterNotOnListNotification(false)', 2000);
				}
				else
				{
					alert("Er ging iets fout waardoor de mail niet verzonden kon worden.");
				}
			}
		);
	}
	catch(err)
	{
		alert("Er is iets misgegaan, uw browser ondersteund de verzendmogelijkheid niet!");
	}
}

var timeCounter = 0
function showDialogMessage(text, extraTime, clickable, url)
{
	dialog = document.getElementById('informationdialog');
	dialog.style.display = "block";
	dialog.style.top = (10 + document.documentElement['scrollTop']) + "px";
	dialog.style.filter = "alpha(opacity=100)";
	dialog.style.MozOpacity = 1;
	setBackgroundColor(dialog,'#ffffff');
	setTimeout("setBackgroundColor(dialog,'#95ffb0')", 75);
	dialog.innerHTML = "<p>" + text + "</p>";
	if (clickable)
	{
		dialog.style.cursor = "pointer";
		dialog.onclick = function ()
		{
			location = url;
		};
	}
	else
	{
		dialog.style.cursor = "default";
		dialog.onclick = null;
	}

	timeCounter = extraTime;
	var scrollOffsetY = 0;
	var dialogInterval = window.setInterval(
		function ()
		{
			timeCounter--;
			dialog = document.getElementById('informationdialog');

			dialog.style.top = (12 + scrollOffsetY) + "px";
			dialog.style.display = "block";
			if ( timeCounter < 20 )
			{
				document.getElementById('informationdialog').style.filter = "alpha(opacity=" + (timeCounter+80) + ")";
				document.getElementById('informationdialog').style.MozOpacity = (timeCounter+80)/100;
				document.getElementById('informationdialog').style.opacity = (timeCounter+80)/10;
			}

			if ( timeCounter <= 0 )
			{
				document.getElementById('informationdialog').style.display = "none";
				clearInterval( dialogInterval );
			}
		},
		9
	);
}

function setBackgroundColor(obj, color)
{
	obj.style.backgroundColor = color;
}

// Basic functions
// ===============================================================================

function right(str, n)
{
	if (n <= 0)
	{
		return "";
	}
	else if (n > String(str).length)
	{
		return str;
	}
	else
	{
		var iLen = String(str).length;
		return String(str).substring(iLen, iLen - n);
	}
}
