$(function(){

	var baseUrl = $('#baseUrl').val();
	var productId = parseInt($('#productId').val());
	var selectedPrice = pprices[$('#detailform input:radio:checked').val()];

	// Quick fix:  This part triggered a Javascript error in Internet Explorer 6. Fixed by Josse Zwols (concept7) on the 23rd of December 2009.
	var selectedQ = '';
	var tmp = '';

	if (tmp = $('#detailform input:radio:checked').attr('id'))
		selectedQ = parseInt(tmp.substring(6));
	// End of quick fix

	var regionA = $("OPTION:selected", $('#destination')).attr('class');


	$('#act').live('click',function(){

		if ( $(this).attr('class') == 'btnGrey' ){

			return false;
		}
	});

    /*$('#destination').live('change',function(){

		regionA =   $("OPTION:selected", this).attr('class');

		alert('opasss');return ;

	    $('#zoneDelivery').val(regionA);
		getShipingPrices();
	})*/

	 $('#destination').change(function(){

		 	regionA =   $("OPTION:selected", this).val();

		    $('#zoneDelivery').val(regionA);
			getShipingPrices();

	 });


	$('.prc').live('click',function(){

		//Highlight Selected
		$('.ul_buttons_p .price').find('span').removeClass('active');
		$(this).parent().next('.price').find('span').addClass('active');


		var qty =  parseInt($(this).attr('id').substring(6));
		$('#qty').val(qty);

		selectedPrice = pprices[$(this).val()];
		selectedQ = qty;

		getShipingPrices();
	});


    function getShipingPrices (){

		$.ajax({
			   type: "POST",
			   url: baseUrl + "/products/update-shippingprices/format/json",
			   data: 'prodId=' + productId + '&qty=' + selectedQ ,
			   dataType: 'json',
			   beforeSend : function(){
			   },
			   success: function(data){

				   var sPrice ;
				   var obj = data.shippingPrices;



				   //alert(region.length);

				   //if (typeof region != "undefined"){
				   if (regionA.length > 0){


					   if (regionA == 'nl'){
						   sPrice = obj.nl;
					   } else if (regionA == 'eur1')  {
						   sPrice = obj.eur1;
					   } else if   (regionA == 'eur2'){
						   sPrice = obj.eur2;
					   }  else if  (regionA == 'world1'){
						   sPrice = obj.world1;
					   } else if  (regionA == 'world2'){
						   sPrice = obj.world2;
					   }   else {
						   sPrice = null;
					   }

					   	if (sPrice){


					   		   var retval = (parseFloat( sPrice.replace(',','.')) + parseFloat(selectedPrice.replace(',','.'))).toFixed(2);
					   		   $('#ship').text('€ ' + sPrice);
					   		   $('#act').removeClass().addClass('btnGreen');
					   		   $('#error-msg').hide();
					   		   $('#priceDelivery').val(sPrice);





					   	} else{
					   		  var retval = selectedPrice.replace('.',',');
					   		$('#ship').empty();
					   		$('#act').removeClass().addClass('btnGrey');
					   		$('#error-msg').show();

					   	}


				   } else {
					   var retval = selectedPrice.replace('.',',');
					   $('#ship').empty();
					   $('#act').removeClass().addClass('btnGrey');
					   $('#error-msg').show();

				   }
				   //$('#hiddenPrice').val(parseFloat(currValue) + parseFloat(nextVal));
				   $('#totalFinish').empty().text('€ ' + retval);
			   }

			 });

	}




})

