function getDimensionalPrice(iParentProductID, iGroupID, iWidth, iHeight, strUnitType)
{
  $('#product_add_basket_selection table tr.picked_size').remove();
  $('#width_custom_value,#height_custom_value').remove();

  if(!isNaN(iWidth) && iWidth > 0 && !isNaN(iHeight) && iHeight > 0)
  {
    var iTemplateID = 0;
    var strPersonalisationString = '';
    if( $('#pre_add_template_id').length > 0)
    {
      iTemplateID = $('#pre_add_template_id').val();
      strPersonalisationString = $('form#personalisation_actual_options_wrapper_in_page_' + iTemplateID).serialize(); // the not-entirely-british spelling, booo :(
      strPersonalisationString = strPersonalisationString.replace('%5B','[');
      strPersonalisationString = strPersonalisationString.replace('%5D',']');
      strPersonalisationString = strPersonalisationString + '&template_id=' + iTemplateID;
    }
    var strApplyBasePriceGrid = $('#template_' + iTemplateID + '_skip_base_price_table').val();

    $('#product_buttons_' + iTemplateID).append('<input type="hidden" name="width_custom_value" id="width_custom_value" value="' + iWidth + '" />');
    $('#product_buttons_' + iTemplateID).append('<input type="hidden" name="height_custom_value" id="height_custom_value" value="' + iHeight + '" />');

    $.ajax({
      url: '/dimensions/getprice/' + iParentProductID + '/' + iWidth + '/' + iHeight + '/' + iTemplateID + '/' + strApplyBasePriceGrid + '?' + strPersonalisationString,
      dataType:'json',
      cache:true,
      async:false,
      success:function(strResponse){
        if(strResponse.sale_price != undefined)
        {
          //$('.addtobasket_disabled_link,#addtobasket_disabled').hide();
          //$('#product_price_sale,#addtobasket_enabled').css('visibility', 'visible');
          $('#product_price_' + iTemplateID + ' #product_price_sale').html(strResponse.sale_price);

          if(strResponse.sale_price_cheaper == true)
          {
            $('#product_price_' + iTemplateID + ' #product_price_was').html('WAS: ' + strResponse.price);
            //$('#product_price_was').css('visibility', 'visible');
          }

          iActualWidth = strResponse.actual_width;
          iActualHeight = strResponse.actual_height;

          if(strUnitType != 'cm' && strUnitType != 'CM')
          {
            iActualWidth = iActualWidth/2.54;
            iActualWidth = iActualWidth.toFixed(2);
            iActualHeight = iActualHeight/2.54;
            iActualHeight = iActualHeight.toFixed(2);
            strUnitType = ' ' + strUnitType;
            strUnitType = strUnitType.toLowerCase();
          }

          //$('#product_add_basket_selection,#product_price_sale').css('visibility', 'visible');
          //$('#product_price,#addtobasket_enabled').show();
          //$('#product_add_basket_selection table').append('<tr class="picked_size"><td>Size:</td><td><span>' + (iActualWidth) + strUnitType + ' x ' + (iActualHeight) + strUnitType + '</span></td></tr>');
          $('#customisation_sidebox_' + iTemplateID + ' p.picked_size').remove();
          $('#customisation_sidebox_' + iTemplateID).append('<p class="picked_size picked_size_title"><span>Your Selection</span></p>');
          if(typeof(strResponse.title) != 'undefined')
          {
            $('#customisation_sidebox_' + iTemplateID).append('<p class="picked_size"><span>Item:</span> ' + strResponse.title + '</p>');
          }

          strMethod = $('#customisation_options_' + iTemplateID + ' li.current_customisation_option_tab span').text();
          if(strMethod != '')
          {
            $('#customisation_sidebox_' + iTemplateID).append('<p class="picked_size"><span>Option:</span> ' + strMethod + '</p>');
          }

          var strHTML = '';
          var strMeasurementHTML = '';
          var blnAddedMeasurementHTML = false;

          strMeasurementHTML = strMeasurementHTML + '<p class="picked_size"><span>Window:</span> ' + (iActualWidth) + strUnitType + ' x ' + (iActualHeight) + strUnitType + '</p>';

          if(typeof(strResponse.picked_options) != 'undefined')
          {
            $.each(strResponse.picked_options, function(strOptionName, strPickedValue)
            {
              var strCurrentHTML = '';
              if(strOptionName != 'Units' && !strOptionName.match(/height/gi) && !strOptionName.match(/width/gi) && !strOptionName.match(/drop/gi) && strPickedValue != 'e.g. Kitchen' && strPickedValue != '')
              {
                strCurrentHTML = '<p class="picked_size"><span>' + strOptionName + ':</span> ' + strPickedValue + '</p>';
                if(strOptionName.match(/gather/gi))
                {
                  $('#customisation_sidebox_' + iTemplateID).append(strCurrentHTML);

                  var iWidthScaler = 1;

                  if(typeof(strResponse.numeric_scalers) == 'object')
                  {
                    $.each(strResponse.numeric_scalers, function(strScalerName,iScalerValue)
                    {
                      iWidthScaler = iScalerValue;
                    });
                  }

                  strMeasurementHTML = strMeasurementHTML + '<p class="picked_size"><span>Fabric Width:</span> ' + (iActualWidth*iWidthScaler) + strUnitType + '</p>';

                  $('#customisation_sidebox_' + iTemplateID).append(strMeasurementHTML);
                  blnAddedMeasurementHTML = true
                }
                else
                {
                  strHTML = strHTML + strCurrentHTML;
                }
              }
            });

            if(blnAddedMeasurementHTML == false)
            {
              $('#customisation_sidebox_' + iTemplateID).append(strMeasurementHTML);
            }
            $('#customisation_sidebox_' + iTemplateID).append(strHTML);
          }
        }
        else
        {
          if(strResponse.min_width != undefined)
          {
            var iMaxWidthInches = strResponse.max_width/2.54;
            iMaxWidthInches = iMaxWidthInches.toFixed(1);
            var iMaxHeightInches = strResponse.max_height/2.54;
            iMaxHeightInches = iMaxHeightInches.toFixed(1);
            alert('Please note that this item has maximum sizes of:\n\nWidth: ' + (strResponse.min_width > strResponse.max_width ? (strResponse.min_width + '-' + strResponse.max_width):(strResponse.min_width)) + 'cm (' + (iMaxWidthInches) + ' inches) \nHeight: ' + (strResponse.min_height > strResponse.max_height ? (strResponse.min_height + '-' + strResponse.max_height):(strResponse.min_height)) + 'cm (' + iMaxHeightInches + ' inches)\n\nCheck that the sizes you have chosen fall within these ranges.');
          }
          else
          {
            alert('Please check the sizes you have entered');
          }
        }

        $('#fetching_price').fadeOut('1000', function()
        {
          $('#fetching_price').remove();
          $('#customisation_sidebox_' + iTemplateID).removeClass('loading_prices');
        });

      }
    });
  }
}

function prepareAddToBasket(strAddToBasketClass){
  arrClassNames = strAddToBasketClass.split(' ');
  var iTemplateID = 0;

  for(var i=0; i<arrClassNames.length; i++)
  {
    if(arrClassNames[i].substr(0,4) == 'tpl_')
    {
      iTemplateID = arrClassNames[i].substr(4, arrClassNames[i].length);
    }
  }
  $('#pre_add_template_id').val(iTemplateID);

  if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_singletext_width_wrapper input').length > 0)
  {
    var iWidth = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_singletext_width_wrapper input').val();
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_singletext_hidden input').length > 0)
  {
    var iWidth = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_singletext_hidden input').val();
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_radio_width input:checked').length > 0)
  {
    iWidth = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_radio_width input:checked').val();
    arrWidth = iWidth.split(' ');
    iWidth = arrWidth[0];
    iWidth = iWidth.replace(/cm/gi,'');
    iWidth = iWidth.replace(/mm/gi,'');
  }
  /*else
  {
    var iWidth = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_width input').val();
  }*/

  if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_item_values_select_height select').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_item_values_select_height select').val();
    var blnSelectHeight = true;
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_singletext_height_wrapper input').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_singletext_height_wrapper input').val();
    var blnSelectHeight = true;
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_radio_height input:checked').length > 0)
  {
    iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_option_view_in_page_radio_height input:checked').val();
  }
  /*else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_window-height select').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_window-height select').val();
    var blnSelectHeight = true;
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_window-height input').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_window-height input').val();
    var blnSelectHeight = false;
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_height input').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_height input').val();
    var blnSelectHeight = false;
  }
  else if($('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_height input').length > 0)
  {
    var iHeight = $('.personalisation_actual_options_wrapper_in_page_' + iTemplateID + ' .personalisation_options_values_wrapper_in_page_curtain-height input').val();
    var blnSelectHeight = false;
  }*/

  $('#width_custom_value,#height_custom_value').remove();
  $('#product_buttons_' + iTemplateID).append('<input type="hidden" name="width_custom_value" id="width_custom_value" value="' + iWidth + '" />');
  $('#product_buttons_' + iTemplateID).append('<input type="hidden" name="height_custom_value" id="height_custom_value" value="' + iHeight + '" />');

  addDimensionsToBasket();
}

function addDimensionsToBasket()
{

  var iParentProductID = $('#parent_product_id').val();
  var iProductID = $('#product_id').val();
  var iWidth = $('#width_custom_value').val();
  var iHeight = $('#height_custom_value').val();
  var strPersonalisationString = '';

  if(typeof(iWidth) == 'undefined' || typeof(iHeight) == 'undefined')
  {
    alert('Please enter a width and height');
    return false;
  }
  else
  {
    strUnitType = $('div.personalisation_options_units input:checked').val();
    strUnitType = strUnitType.toLowerCase();

    if(strUnitType != 'cm')
    {
      iWidth = iWidth*2.54;
      iHeight = iHeight*2.54;
    }
  }

  if($('#fetching_price').length == 0)
  {
    $('#product_buttons').append('<div id="fetching_price"><span>Adding to Basket...</span></div>');
  }

  var iQuantity = 1;
  var iTemplateID = 0;
  if( $('#pre_add_template_id').length > 0)
  {
    iTemplateID = $('#pre_add_template_id').val();
    strPersonalisationString = $('form#personalisation_actual_options_wrapper_in_page_' + iTemplateID).serialize(); // the not-entirely-british spelling, booo :(
    strPersonalisationString = strPersonalisationString.replace('%5B','[');
    strPersonalisationString = strPersonalisationString.replace('%5D',']');
  }

  if($('div.personalisation_item_values_select_quantity select').length > 0)
  {
    iQuantity = $('div.personalisation_item_values_select_quantity select').val();
  }

  var strURL = '/dimensions/add/' + iParentProductID + '/' + iProductID + '/' + iWidth + '/' + iHeight + '/' + iQuantity + '/' + iTemplateID;

  $.ajax({
    url: strURL,
    dataType:'json',
    cache:false,
    type:'GET',
    data: strPersonalisationString,
    success:function(strResponse)
    {
      /*if($('#personalisation_template_id').val() > 0)
      {
        $('#fetching_price span').text('Loading Product Options....');
        tb_remove();
        arrPageSize = tb_getPageSize();
        var iPageHeight = parseInt(arrPageSize[1]/1.25);
        if($('#personalisation_template_width').length > 0)
        {
          iPageWidth = $('#personalisation_template_width').val();
          iPageWidth = 739; // temp for Nets2Go
        }
        else
        {
          iPageWidth = 908;
        }

        $('#personalisation_link').attr('href', '/product_personalisation/start/' + $('#personalisation_template_id').val() + '/1/' + strResponse + '/true?keepThis=true&TB_iframe=true&modal=true&height=' + iPageHeight + '&width=' + iPageWidth);
        $('#personalisation_link').click();
      }
      else
      {*/
        document.location.href = '/basket';
      //}
    }
  });
}


function checkForDimensionalPrice(iParentProductID)
{
  //$('#product_price_sale,#addtobasket_enabled,#product_price_was').css('visibility', 'hidden');
  //$('.addtobasket_disabled_link,#addtobasket_disabled').show();
  //$('#product_add_basket_selection').css('visibility', 'hidden');
  //$('#product_add_basket_selection table tr').remove();

  $.ajax({
    url: '/dimensions/check/' + iParentProductID,
    dataType:'json',
    cache:true,
    success:function(strResponse){

      if(strResponse != 'false' && strResponse != false)
      {
        $('#product_options').css('visibility','hidden');



        // look for in-page select elements, that are entitled Height to add in the values
        $('.personalisation_options_values_wrapper_in_page_height select, .personalisation_options_values_wrapper_in_page_window-height select').each(function(i,e)
        {
          var strID = $(e).attr('id');
          $('#' + strID).html('<option value="0" selected="selected">-Choose Height-</option>');

          $.each(strResponse.heights, function(j,iHeight)
          {
            var iInches = (iHeight/2.54);
            iInches = iInches.toFixed(2);
            $('#' + strID).append('<option value="' + iHeight + '">' + iHeight + 'cm  (' + iInches + 'in)</option>');
          });
        });

        //default any "Hidden" Widths as wide as possible
        $('div.personalisation_singletext_hidden_wrapper input').val( strResponse.max_width );

        $('#product_dimensional_price_group_id').remove();
        $('#product_form_container').append('<input id="product_dimensional_price_group_id" name="product_dimensional_price_group_id" value="' + strResponse.products_price_dimension_group_id + '" type="hidden" />');

        $('.personalisation_options_units input:checked').each(function(i,e)
        {
          strUnitType = $(this).val();
          strUnitTypeLower = strUnitType.toLowerCase();

          if(strUnitType != 'CM' && strUnitType.toUpperCase != 'CM')
          {
            iScaler = 2.54;
          }
          else
          {
            iScaler = 1;
          }

          iMinPossibleHeight = parseInt(strResponse.min_height/iScaler);
          iMaxPossibleHeight = parseInt(strResponse.max_height/iScaler);
          if(iMinPossibleHeight < iMaxPossibleHeight)
          {
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_height .measurements').remove();
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_height').append(' <span class="measurements">(<span class="unit_value">' + iMinPossibleHeight + '</span><span class="unit_type">' + strUnitType + '</span>-<span class="unit_value">' + iMaxPossibleHeight + '</span><span class="unit_type">' + strUnitType +  '</span>)</span>');
          }
          else if(iMinPossibleHeight == iMaxPossibleHeight)
          {
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_height .measurements').remove();
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_height').append(' <span class="measurements">(up to <span class="unit_value">' + iMaxPossibleHeight + '</span><span class="unit_type">' + strUnitType +  '</span>)</span>');
          }

          iMinPossibleWidth = parseInt(strResponse.min_width/iScaler);
          iMaxPossibleWidth = parseInt(strResponse.max_width/iScaler);
          if(iMinPossibleWidth < iMaxPossibleWidth)
          {
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_width .measurements').remove();
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_width').append(' <span class="measurements">(<span class="unit_value">' + iMinPossibleWidth + '</span>' + strUnitType + '-<span class="unit_value">' + iMaxPossibleWidth + '</span><span class="unit_type">' + strUnitType +  '</span>)</span>');
          }
          else if(iMinPossibleWidth == iMaxPossibleWidth)
          {
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_width .measurements').remove();
            $('.personalisation_option_type_in_page_text .personalisation_option_view_in_page_singletext_width').append(' <span class="measurements">(up to <span class="unit_value">' + iMaxPossibleWidth + '</span><span class="unit_type">' + strUnitType +  '</span>)</span>');
          }
        });

        // reset/remove
        $('.picked_size').remove();

        // Max height/widths into the form
        //var strHTML = '<p class="picked_size"><span>Widths:</span> ' + iMinPossibleWidth + '-' + iMaxPossibleWidth + ' (' + strUnitType + ')</p>';
        //strHTML+= '<p class="picked_size"><span>Heights:</span> ' + iMinPossibleHeight + '-' + iMaxPossibleHeight + ' (' + strUnitType + ')</p>';
        //$('div.personalisation_actual_options_wrapper_in_page form').append(strHTML);

        // max width and height into the title of each box
        //var strHTML = '<span>(Widths between ' + strResponse.min_width + '-' + strResponse.max_width + strUnitType + ' and Heights from ' + strResponse.min_height + '-' + strResponse.max_height + strUnitType + '</span>';
        //$('li.customisation_option_tab a span').append(strHTML);
        //var strHTML = '<span>(' + strResponse.min_width + '-' + strResponse.max_width + 'cm)</span>';
        //$('div.personalisation_options_window-width div.personalisation_text, div.personalisation_options_width div.personalisation_text').append(strHTML);

        // max width for each form element
        $('.max_widths_text').remove();
        var strHTML = '<span class="max_widths_text max_widths_text_cm">(' + strResponse.min_width + 'cm to ' + strResponse.max_width + 'cm)</span>';
        iMinWidthInches = strResponse.min_width/2.54;
        iMinWidthInches = iMinWidthInches.toFixed(0);
        iMaxWidthInches = strResponse.max_width/2.54;
        iMaxWidthInches = iMaxWidthInches.toFixed(0);
        strHTML+= '<span class="max_widths_text max_widths_text_inches">(' + iMinWidthInches + '&quot; to ' + iMaxWidthInches + '&quot;)</span>';
        $('div.personalisation_options_window-width div.personalisation_text, div.personalisation_options_width div.personalisation_text').append(strHTML);

        // max height for each form element
        $('.max_heights_text').remove();
        var strHTML = '<span class="max_heights_text max_heights_text_cm">(' + strResponse.min_height + 'cm to ' + strResponse.max_height + 'cm)</span>';
        iMinHeightInches = strResponse.min_height/2.54;
        iMinHeightInches = iMinHeightInches.toFixed(0);
        iMaxHeightInches = strResponse.max_height/2.54;
        iMaxHeightInches = iMaxHeightInches.toFixed(0);
        strHTML+= '<span class="max_heights_text max_heights_text_inches">(' + iMinHeightInches + '&quot; to ' + iMaxHeightInches + '&quot;)</span>';
        $('div.personalisation_options_window-height div.personalisation_text, div.personalisation_options_height div.personalisation_text').append(strHTML);

        strUnitType = strUnitType.toLowerCase();

        if(strUnitType != 'inches')
        {
          $('.max_heights_text_inches, .max_widths_text_inches').hide();
          $('.max_heights_text_cm, .max_widths_text_cm').show();
        }
        else
        {
          $('.max_heights_text_cm, .max_widths_text_cm').hide();
          $('.max_heights_text_inches, .max_widths_text_inches').show();
        }

        $('#add_to_basket_form').unbind('submit');
        $('#add_to_basket_form').bind('submit', function()
        {

          //calculateDimensionPrice( $('#pre_add_template_id').val(), false );
          //addDimensionsToBasket();
          return false;
        });

        $('input.addtobasket_enabled').unbind('click');
        $('input.addtobasket_enabled').bind('click', function()
        {
          strClassName = $(this).attr('className');
          arrClassName = strClassName.split(' ');
          for(var i=0; i<arrClassName.length; i++)
          {
            if( arrClassName[i].substr(0,4) == 'tpl_' )
            {
              iTemplateID = arrClassName[i].substr(4, arrClassName[i].length);
            }
          }
          $('#addtobasket_template_id').remove();
          $('#add_to_basket_form').append('<input type="hidden" name="addtobasket_template_id" id="addtobasket_template_id" value="' + iTemplateID + '" />');

          prepareAddToBasket( $(this).attr('className') );
          $('#add_to_basket_form').submit();
        });
      }
      else
      {
        $('.product_define_size_custom').hide();
      }
    }
  });

}
