var myCallback = function (data)
{
	eval("var data = " +data);
	
	if(data.id != 'null'){
		
		if(data.type != 'change')
		{ 
			document.getElementById('quantity_'+data.id).value = data.quantity;
			if(document.getElementById('insurance_'+data.id))
            {
                document.getElementById('insurance_'+data.id).value = data.quantity;
            }
            if (document.getElementById('total_product_'+data.id))
            {
			    document.getElementById('total_product_'+data.id).value = FormatPrice(data.price);
            }
			if(document.getElementById('insurance_'+data.id))
            {
                document.getElementById('total_insurance_'+data.id).value = FormatPrice(document.getElementById('insurance_price_'+data.id).value * data.quantity);
            }
			if(document.getElementById('installation_'+data.id))
            {
                document.getElementById('installation_'+data.id).value = data.quantity;
            }
			if(document.getElementById('installation_'+data.id))
            {
                document.getElementById('total_installation_'+data.id).value = FormatPrice(document.getElementById('installation_price_'+data.id).value * data.quantity);
            }
			if(document.getElementById('removal_'+data.id))
            {
                document.getElementById('removal_'+data.id).value = data.quantity;
            }
			if(document.getElementById('removal_'+data.id))
            {
                document.getElementById('total_removal_'+data.id).value = FormatPrice(document.getElementById('removal_price_'+data.id).value * data.quantity);
            }
		}
		if ( $('#del_2:checked').val() )
		{
			$('#delcost').val( FormatPrice(data.delivery) );
		}			
		
		$('.pickup_price').html( FormatPrice( data.pickup_price ) );
		$('#total_products').val( FormatPrice(data.total) );

		total = data.total;
		total = total.toString();
		total = total.replace(',','.');
		total = new Number(total);
		
		vat = ((total / 119) * data.vat);
		
		$('#inc_vat').val( FormatPrice(vat) + ' BTW' );
		
		var art = ' Artikelen ';
		if(data.all_products == 1){
			art = ' Artikel ';
		}

		document.getElementById('header_cart').value = data.all_products + art + ' ' + ' / ' + FormatPrice(data.total);
		
		if ( 'undefined' != typeof data.is_binnenbrengen )
		{		
			if ( data.is_binnenbrengen )
			{
				$('#kies_aflever_verdieping').show();
			}
			else
			{
				$('#kies_aflever_verdieping').hide();
			}			
		}
		if (    'undefined' != typeof data.kan_topservice 
			 && 'undefined' != typeof data.gratis_topservice )
		{		
			$('#topservice').removeClass('disabled');
			$('#topservice').removeClass('enabled');
			$('#topservice').removeClass('free');
			$('#topservice').removeClass('notfree');				
			
			if ( data.kan_topservice )
			{
				$('#topservice').addClass('enabled');				
				if ( data.gratis_topservice )
				{
					$('#topservice').addClass('free');				
					$('#checkbox_topservice').attr('disabled','disabled');
					$('#checkbox_topservice').attr('checked','checked');				
				}
				else
				{
					$('#topservice').addClass('notfree');				
					$('#checkbox_topservice').removeAttr('disabled');
				}
			}
			else // topservice kan niet
			{
				$('#topservice').addClass('disabled');				
				$('#checkbox_topservice').removeAttr('checked');
			}
			updateFloor();
		}

        if (    'undefined' != typeof data.id
             && 'undefined' != typeof data.delivery_id )
        {
            $('.deliveryprice_' + data.delivery_id).text( FormatPrice(data.delivery) );
        }
	}
}


function FormatPrice(price)
{
	price = price.toString();
	price = price.replace(',','.');
	price = new Number(price);
	price = Math.round (price * 100) / 100;
	price = price.toString();
	price = new Number(price);
	price = number_format(price, 2, ",", ".");
	price = price.replace(",00", ",-");

	return ' € ' + price;
}

// threadsafe asynchronous XMLHTTPRequest code

function ajaxSend(url, callback, returnFormat, pr_id, type){
        // we use a javascript feature here called "inner functions"
        // using these means the local variables retain their values after the outer function
        // has returned. this is useful for thread safety, so 
        // reassigning the onreadystatechange function doesn't stomp over earlier requests.

        if(type == '') return;
        
        href = url + "?type=" + escape(type) + "&pr_id=" + escape(pr_id);
              
        function ajaxBindCallback(){
                if (ajaxRequest.readyState == 4) {
                        if (ajaxRequest.status == 200) {
                                if (ajaxCallback){
                                        returnFormat == "XML" ? ajaxCallback(ajaxRequest.responseXML) : ajaxCallback(ajaxRequest.responseText);
                                } else {
                                        alert('no callback defined');
                                }
                        } else {
                                alert("Er is een fout opgetreden bij het ophalen van de XML data:\n" + ajaxRequest.status + ":\t" + ajaxRequest.statusText + "\n" + ajaxRequest.responseText);
                        }
                }
        }

        // use a local variable to hold our request and callback until the inner function is called...
        var ajaxRequest = null;
        var ajaxCallback = callback;
		
        // bind our callback then hit the server...
        if (window.XMLHttpRequest) {
                // moz et al
                ajaxRequest = new XMLHttpRequest();
                ajaxRequest.onreadystatechange = ajaxBindCallback;
	            ajaxRequest.open("GET", href, true);
                ajaxRequest.send(null);
        } else if (window.ActiveXObject) {
               // ie
                ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
                if (ajaxRequest) {
                        ajaxRequest.onreadystatechange = ajaxBindCallback;
                        ajaxRequest.open("GET", href, true);
                        ajaxRequest.send();
                }
        }
}


function number_format (number, decimals, dec_point, thousands_sep)
{
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1)
  {
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null)
  {
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0)
  {
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != "")
  {
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function handleSubmit(form, element) {
    //Overwrite onclick to prevent double submit
    element.attr('onclick', 'return false;');
    //Submit form   
    document.forms[form].submit();
}

function showDeliveryPrice(el)
{
	var bezorgwijze_id = 1;	
	if ( $(el).length )
	{
		bezorgwijze_id = $(el).val();
	}		
	$('[id^=delivery_]').hide();
	$('#delivery_'+bezorgwijze_id).show();	
}

function updateFloor()
{
	$('#span_checkboxlift').hide();
	$('.floorwarning').hide();
	
	if ( '0' != $('#selectfloor').val() )
	{
		$('#span_checkboxlift').show();
		if (    ! $('#checkbox_topservice:checked').val()
			 && ! $('#checkboxlift:checked').val()
			 && $('#topservice').hasClass('enabled') )
		{
			$('.floorwarning').show();
		}
	}				
}

function updateRetour()
{
	if ( $('#checkbox_oud_apparaat_retour:checked').val() )
	{
		$('#span_apparaat_retour').show();
	}
	else
	{
		$('#span_apparaat_retour').hide();		
	}
}

$(document).ready( function() {
	$('#selectfloor').bind('click change', updateFloor);
	$('#checkbox_topservice').bind('click change', updateFloor);
	$('#checkboxlift').bind('click change', updateFloor);
	updateFloor();
	
	$('#checkbox_oud_apparaat_retour').bind('click change', updateRetour);
	updateRetour();
} );

