function showLoginError(elementName){


	var element = document.getElementById(elementName);

	$.modal($(element),{

    	minHeight:17,
		containerCss:{backgroundColor:"#fff",height:130,padding:0,width:322},

		onOpen: function(dialog)
		{
			dialog.overlay.fadeIn('fast', function () {
				dialog.container.slideDown('normal', function () {
					dialog.data.fadeIn('normal');

					element.style.visibility = 'visible';


				});
			});
		},
		onClose: function(dialog)
		{
			dialog.overlay.fadeOut('fast', function () {
				dialog.container.fadeOut('fast', function () {
					dialog.data.fadeOut('fast', function () {

						$.modal.close(); // must call this!
						$("#klantLoginFoutMeldingen").remove(); // and this too!

						});
					});
				});

		}

	});
}

$(document).ready(function()	{

	//table color rows
	$("table.producten tr:nth-child(even)").removeClass("product_oneven").addClass("product_even");

	//autofocus op input veld
	$("input#searchquery").focus();

	//controle op ingelogd
	checkUserLoggedIn();

	//controle op activatie van favosystem
	checkActiveFavo();

	//handle the select change function
	handleGroupChange();

	//handle super user actions
	handleSuperUser();
})

function checkUserLoggedIn()
{
	$.ajax({
		url: "/javascript/ajaxRequests/checkActive.php",
		type: "GET",
		dataType: "html",
		//cache: false,
		success: function(data) {
			if (data == 'li')	{
				handleLoginActions();
			}else	{
				handleNotLoginActions();
			}
		}
	});
}

function handleLoginActions()
{
	//controleer het tonen van prijzen
	$.ajax({
		url: "/javascript/ajaxRequests/checkPriceShow.php",
		type: "GET",
		dataType: "html",
		//cache: false,
		success: function(data) {
			if (data == 'nS')	{
				//hide prices
				//remove the unused tabel koloms
				$("table th.prijs, table td.productprijs").remove();
				//set the other koloms on a acepteble width
				$("table th.artikelnaam, table td.productartikelnaam").css('width','402px');
				//favo overview, tonen prijs
				if ($("table.favoTable").length != 0)	{
					$("table.favoTable td.topFavoPrice, table.favoTable td.prijs").remove();
					$("table.favoTable td.tekst").css('width','396px');
				}
			}else	{
				//get the price
				getProductPrices();
			}
		}
	});
}

function getProductPrices()
{
	var curPagePrice = '';
	$("td.productprijs").each(function()	{
		curPagePrice = curPagePrice + "/" + $(this).attr("id");
	})

	if (curPagePrice != '')	{
		//request voor status van veld
		$.ajax({
			url: "/javascript/ajaxRequests/getProductPrices.php",
			type: "GET",
			data: "clRow="+curPagePrice,
			dataType: "html",
			//cache: false,
			success: function(data) {
				//data vullen
				if (data != '0')	{
					var $holder = $('<div/>').html(data);
					var $children = $holder.children('div');
					$.each($children, function(val, i) {

						$("td.productprijs[id=pr_"+i.id+"]").html(i.innerHTML).show();
					});
					$holder.remove();
				}
			}
		});
	}
}

function handleSuperUser()
{
	//controle voor resetten van superuser
	if ($("a.resSpUser").length != 0)	{

		$("a.resSpUser").click(function()	{
			//request
			$.ajax({
				url: "/javascript/ajaxRequests/resSupUser.php",
				type: "GET",
				dataType: "html"
			});
		})
	}
}

function handleNotLoginActions()
{
	//remove the unused tabel koloms
	$("table th.aantal, table th.add, table th.prijs, table td.productaantal, table td.productadd, table td.productprijs").remove();
	//set the other koloms on a acepteble width
	//$("table th.artikelnaam, table td.productartikelnaam").css('width','465px');
	$("table th.artikelnaam, table td.productartikelnaam").css('width','519px');
}

function handleGroupChange()
{
	$("select.altena_groepen").change(function()	{
		var selected = $("select.altena_groepen option:selected").val();
		if (selected != '' && selected != 'undefined')	{
			//send the request for the htmlname
			$.ajax({
			    url: "/javascript/ajaxRequests/getHtmlName.php",
			    type: "GET",
			    data: "stId="+selected,
			    dataType: "html",
			    //cache: false,
				success: function(data)	{
					if (data != 'fail')	{
						//locate the window
						window.location = data;
					}
				}
			});
		}
	})
}

function focusBarcode()
{
	$("input.barcode").focus();
}

function handleBarSubmit()
{

	$("form#barForm").submit(function()	{
		$("div.barError").hide();
		$("div.add_icon_load").show();

		if ($("input.barcodeShopcart").length > 0)	{
			var barCode = $("input.barcodeShopcart").val();
		}else	{
			var barCode = $("input.barcode").val();
		}

		if (barCode != '' && barCode != 'undefined')	{
			//send a request for some info
			$.ajax({
			    url: "/javascript/ajaxRequests/getPdInfo.php",
			    type: "GET",
			    data: "bC="+barCode,
			    dataType: "html",
				success: function(data)	{
					if (data != 'fail')	{
						//add the order
						postedData="structuurId="+data;
						postedData=postedData+"&language=nl";
						postedData=postedData+"&action=add";
						postedData=postedData+"&quantity=1";
						postedData=postedData+"&sid="+Math.random();
						$.ajax({
						    url: "/modules/sc/modules/order.php",
						    type: "GET",
						    data: postedData,
						    dataType: "html",
						    //cache: false,
							complete: function(XMLHttpRequest, textStatus) {
								//$("div.add_icon_load").hide();
								ShowOrderOverview('cartoverview','nl','vieworder');
								if($("input[type=hidden][name=reloaded]").length > 0)	{
									window.location.reload();
								}
								//$("input.barcode").focus();
							}
						});
					}else	{
						$("div.add_icon_load").hide();
						$("div.barError").show();

						if ($("input.barcodeShopcart").length > 0)	{
							$("input.barcodeShopcart").val('').focus();
						}else	{
							$("input.barcode").val('').focus();
						}

					}
				}
			});
		}
		$("div.add_icon_load").hide();
		return false;
	})
}

