function fetchVariationOptions(object, productId, variationPrices) {
	if ($('#selectedVariationImage').length){
		$('#selectedVariationImage').hide();
	}
	var parameterId = $(object).attr('id');
	var parametersOption = '';
	
	var nextParameterId = null;
	var isAfterCurrent= false;
	
	$buyInput = $('#buy');
	$buyInput.after("<div></div>").next().css({
		'width': $buyInput.outerWidth(),
		'height': $buyInput.outerHeight(),
		'position': 'absolute',
		'background': 'url("/js/progressbar.gif") no-repeat 50% rgba(255, 255, 255, 0.5)',
		'top': $buyInput.position().top+parseInt($buyInput.css('margin-top').replace('px', '')),
		'left': $buyInput.position().left
	});
	
	var parameters = $('.parameters_select');
	parameters.each(function(i){
		if (isAfterCurrent && nextParameterId == null) {
			nextParameterId = this.id;
		}
		
		if (isAfterCurrent) {
			$('#variation_id').val('');
			$('#' + this.id).val('none');
			$('#' + this.id).attr('disabled', 'disabled');
		}
		
		if (this.id == parameterId) {
			isAfterCurrent = true;
		}
		parametersOption += this.id + '-';
		parametersOption += $('#' + this.id).val() + ';';
	});
	
	if ($(object).val() != 'none') {
		jQuery.ajax({
			type: "POST",
		   	url: "/ajax/fetchvariationoptions/",
		   	data: "nextParameterId=" + nextParameterId + "&parametersOption=" + parametersOption + "&productId=" + productId,
		   	dataType: "json",
			success: function(obj) {
				//if the returned value is number (variationId)
				if (obj.product_variation_id != null && isNumber(obj.product_variation_id)) {
					if (obj.img){
						if ($('#selectedVariationImage').length){
							$('#selectedVariationImage').attr('src', obj.img);
							$('#selectedVariationImage').show();
						}
						else{
							$buyInput.before('<div><img src="'+obj.img+'" id="selectedVariationImage" /></div>');
						}
					}
					$('#variation_id').val(obj.product_variation_id);
					if (variationPrices == 1) {
						$('#price').show();
						$('#main_price').html(obj.prices.main.price);
						$('#main_price_abrv').html(obj.prices.main.abrv);
						if (obj.prices.other.price != null && obj.prices.other.price != '') {
							$('#other_price_all').show();
							$('#other_price').html(obj.prices.other.price);
							$('#other_price_abrv').html(obj.prices.other.abrv);
						} else {
							$('#other_price_all').hide();
						}
					}
				} else {
					$('#price').hide();
					$('#' + nextParameterId).removeAttr('disabled');
					$('#' + nextParameterId).html(obj.options);
				}
				$buyInput.next().remove();
			}
		});
	} else {
		$buyInput.next().remove();
	}
}

function isNumber(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = false;
      }
   }
   return IsNumber;
}

