$(document).ready(function() {
	if ($('.letter').length != 0) {
		$('.letter').hide();
		$('.abc').click(function() {
			var abcLetter = $(this).attr('id').split('_');
			if ($('#abcLetter_'+abcLetter[1]).css('display') == 'none') {
				$('#abcLetter_'+abcLetter[1]).slideDown();
			} else {
				$('#abcLetter_'+abcLetter[1]).slideUp();
			}
		});
	}

	addFormAction();

});

$.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
	$("<img>").attr("src", arguments[i]);
  }
}

function showResultsPerPage(results)
{
	$('#productsPerpage').val(results);
	$('#theFilterForm').submit();
}

function submitFilterForm()
{
	$('#page').val('1');
	$('#theFilterForm').submit();
}

//add action to filterform
function addFormAction()
{
	$('#theFilterForm').submit(function() {
		//display loadingdiv
		$("#loadingFilter").css('opacity', '0.5');
		$("#loadingFilter").css('display','block');
		$("#loadingProducts").css('opacity', '0.5');
		$("#loadingProducts").css('display','block');
		var inputs = $("#theFilterForm :input");
		var filterUrl = '';
		//create filterAjaxurl
		inputs.each(function(i){
			if ($(this).attr('type') == 'checkbox' && $(this).attr('checked') == true) {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			} else if ($(this).attr('type') == 'text' || $(this).attr('type') == 'hidden') {
				if (filterUrl == '') {
					filterUrl =  $(this).attr('name') + '=' + $(this).val()
				} else {
					filterUrl =  filterUrl + '&' + $(this).attr('name') + '=' + $(this).val()
				}
			}
 		});

 		//process ajaxrequest
		$.ajax({
			url: "/filterProducts.php",
			type: "POST",
			data: filterUrl,
			dataType: "html",
			//cache: false,
			success: function(responseHtml) {
				var $holder = $('<div/>').html(responseHtml);
				//update filterDiv
				$('#productFilter').html($('#productFilter', $holder).html());
				//update productresultdiv
				$('.content_bb').html($('#productsFound', $holder).html());

				$("#loadingFilter").css('display','none');
				$("#loadingProducts").css('display','none');
				addFormAction();
			}
		});
		return false;
	});
}

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!

					});
				});
			});
		}

	});
}

function showResultPage(number)
{
	$('#page').val(number);
	$('#theFilterForm').submit();
}

function uncheckFilter(id)
{
	$('#' + id + 'Fieldset > input').attr('checked', false);
	$('#theFilterForm').submit();
}

// check whether fieldvalue is unique or not
function checkFieldValue( structuurid, eigenschapnaam, element ){

		// error span verwijderen
		var id = $(element).attr('id') + '_error';
		var value = element.value;

		if (value.length > 0) {

			filterUrl =  'action=checkduplicate&id='+ structuurid + '&esnaam=' + eigenschapnaam + '&value=' + value;

 			//process ajaxrequest
			$.ajax({
				url: "/ajaxrequests.php",
				type: "POST",
				data: filterUrl,
				dataType: "html",
				cache: false,
				success: function(responseHtml) {


					if (responseHtml != 'false') {

						$("#"+id+"").remove();
						// add span
						$(element).after('<span id="' + id +'" class="error">' + responseHtml + '</span>');
						//for (var x in element ) alert(x);

					}
				}
			});

		}
}

// check length of field value
function checkFieldLength( element, min, max ){

		// error span verwijderen
		var id = $(element).attr('id') + '_error';
		var value = element.value;

		$("#"+id+"").remove();

		if ((value.length < min) || (value.length > max)){
			// add span
			$(element).after('<span id="' + id +'" class="error">Aanbevolen lengte is tussen ' + min + ' en ' + max + '.</span>');
			//for (var x in element ) alert(x);

		}
}


//laden van netto prijzen, in structuurIDPrices zitten 1 of meerdere structuren met hun prijs structuurID:prijs;structuurID2;prijs2
function loadDiscountPrices(structuurIDPrices)
{
	$.ajax({
		url: "/ajaxrequests.php",
		type: "POST",
		data: 'action=loadDiscountPrices&structuurIDPrices='+structuurIDPrices,
		dataType: "html",
		success: function(responseHtml) {
			var $holder = $('<div/>').html(responseHtml);
			var $children = $holder.children('div');
			$.each($children, function(val, i) {
				$('#' + i.id).html(i.innerHTML);
			});
		}
	});

}


