function reorderPersonalisationOptions()
{
  var iPosition = 0;

  $('.personalisation_actual_options_wrapper .personalisation_options_wrapper:visible').each(function(i,e)
  {
    var blnRequirementsMet = true;
    if($(e).hasClass('item_requires_options'))
    {
      blnRequirementsMet = false;

      strClassNames = $(e).attr('className');
      arrClasses = strClassNames.split(' ');
      for(i=0; i<arrClasses.length; i++)
      {
        if(arrClasses[i].substr(0,4) == 'req_')
        {
          iValueID = arrClasses[i].substr(4, arrClasses[i].length);
          if($('input.value_' + iValueID).length > 0)
          {
            $('input.value_' + iValueID).each(function(j, e2)
            {
              if( $(e2).attr('checked') == true )
              {
                blnRequirementsMet = true;
              }
            });
          }
        }
      }
    }

    if($(e).attr('id') != '' && blnRequirementsMet && $('#' + $(e).attr('id') + ' .personalisation_options_title:visible').length > 0)
    {
      strClassNames = $(e).attr('className');
      arrClasses = strClassNames.split(' ');
      for(i=0; i<arrClasses.length; i++)
      {
        if(arrClasses[i].substr(0,41) == 'personalisation_options_wrapper_position_')
        {
          $(e).removeClass(arrClasses[i]);
        }
      }

      if( $(e).is(':visible') && !$(e).hasClass('special_personalisation_headers') )
      {
        iPosition++;
        $(e).addClass('personalisation_options_wrapper_position_' + iPosition + ' personalisation_options_wrapper_positioned');
        $(e).removeClass('personalisation_options_wrapper_unpositioned');
      }
      else
      {
        $(e).removeClass('personalisation_options_wrapper_positioned');
        $(e).addClass('personalisation_options_wrapper_unpositioned');
      }
    }
  });
  //checkForLiveFontPreviews();
  clientPostReorderPersonalisationOptions();
}

function checkForRequiredPersonalisationFields(iItemID, iValueID, blnDisable)
{
  //alert('checkForRequiredPersonalisationFields');
  if($('#item_' + iItemID + '_' + iValueID + ':checked').length == 0)
  {
    $('div.req_' + iValueID).hide();
  }
  else
  {
    //alert('input.req_' + iValueID + ': ' + $('input.req_' + iValueID).length);
    if($('input.req_' + iValueID).length > 0)
    {
      $('input.req_' + iValueID).each(function(i, e)
      {
        strID = $(e).attr('id');
        arrBits = strID.split('_');

        $('.item_' + arrBits[1]).hide();
        if(blnDisable)
        {
          //$('input.item_' + arrBits[1]).attr('disabled', true);
          //$('input.item_' + arrBits[1]).attr('checked', false);
        }

        $('.req_' + iValueID + ', ul.personalisation_item_values_step_' + iItemID + ' .no_requirements').show();
        $('.req_' + iValueID + ', ul.personalisation_item_values_step_' + iItemID + ' .no_requirements input').attr('disabled', false);
      });
    }
    else
    {
      //$('ul.personalisation_item_values_step_' + iItemID + ' .no_requirements').show();
      //$('ul.personalisation_item_values_step_' + iItemID + ' .no_requirements input').attr('disabled', false);
      //$('ul.personalisation_item_values_step_' + iItemID + ' .has_requirements').hide();
      //$('ul.personalisation_item_values_step_' + iItemID + ' .has_requirements input').attr('disabled', true);
      //$('ul.personalisation_item_values_step_' + iItemID + ' .has_requirements input').attr('checked', false);
    }

    //alert('li.value_of_item_' + iItemID + ': ' + $('li.value_of_item_' + iItemID).length);
    $('li.value_of_item_' + iItemID).each(function(i,e)
    {
      strClasses = $(e).attr('className');
      arrBits = strClasses.split(' ');

      for(var i=0; i<arrBits.length; i++)
      {
        strTemp = 'item_' + iItemID + '_value_';
        iLength = strTemp.length;
        if(arrBits[i].substr(0,iLength) == 'item_' + iItemID + '_value_')
        {
          strID = arrBits[i].substr(iLength, arrBits[i].length);

          if($('input.value_' + strID + ':checked').length == 0)
          {
            $('div.req_' + strID).hide();
          }
        }
      }
    });


    arrGreenLit = new Array();
    iGreenLitPointer = 0;
    //alert('div.req_' + iValueID + ': ' + $('div.req_' + iValueID).length);
    $('div.req_' + iValueID).each(function(i, e)
    {
      $(e).show();
      arrGreenLit[$(e).attr('id')] = $(e).attr('id');
      iGreenLitPointer++;
    });

    //alert('div.item_requires_options: ' + $('div.item_requires_options').length);
    $('div.req_' + iValueID).each(function(i, e)
    {
      strClasses = $(e).attr('className');
      arrBits = strClasses.split(' ');
      for(var i=0; i<arrBits.length; i++)
      {
        if(arrBits[i].substr(0,4) == 'req_')
        {
          strValueID = arrBits[i].substr(4, arrBits[i].length);
          if($('.value_' + strValueID).length == 0 && arrGreenLit[$(e).attr('id')] == undefined && !$(e).hasClass('req_' + strValueID))
          {
            strElementID = $(e).attr('id');
            $('#' + strElementID).hide();
            if(blnDisable)
            {
              $('#' + strElementID + ' input, #' + strElementID + ' select, #' + strElementID + ' textarea').val('');
            }
          }
        }
      }
    });
  }
  reorderPersonalisationOptions();
}

function checkForLiveFontPreviews()
{
  arrTextOptions = new Array();
  arrTextOptions['scaling'] = 1;
  var iNumSet = 0;

  $('.personalisation_item_value_font_preview').each(function()
  {
    var strElementID = $(this).attr('id');
    $('.personalisation_text_wrapper textarea:enabled').each(function(i,e)
    {
      if($(e).val() != '' && typeof($(e).val()) != 'undefined')
      {
        arrTextOptions['text'] = $(e).val();
        iNumSet++;
      }
    });

    if($('.personalisation_option_type_font input:enabled').length > 0)
    {
      $('.personalisation_option_type_font input:enabled').each(function(i,e)
      {
        if($(e).val() != '' && typeof($(e).val()) != 'undefined')
        {
          strFont = $(e).val();
          strFontReworked = strFont.toLowerCase();
          strFontReworked = strFontReworked.replace(' ', '_');
          iScaling = 1;
          if($('.font_' + strFontReworked).length > 0)
          {
            strFont = $('.font_' + strFontReworked).text();
          }
          if($('.scaling_' + strFontReworked).length > 0)
          {
            iScaling = $('.scaling_' + strFontReworked).text();
          }
          arrTextOptions['font'] = strFont;
          arrTextOptions['scaling'] = iScaling;
          iNumSet++;
        }
      });
    }
    if($('.personalisation_option_type_font select:enabled').length > 0)
    {
      $('.personalisation_option_type_font select:enabled').each(function(i,e)
      {
        if($(e).val() != '' && typeof($(e).val()) != 'undefined')
        {
          strFont = $(e).val();
          strFontReworked = strFont.toLowerCase();
          strFontReworked = strFontReworked.replace(' ', '_');
          iScaling = 1;
          if($('.font_' + strFontReworked).length > 0)
          {
            strFont = $('.font_' + strFontReworked).text();
          }
          if($('.scaling_' + strFontReworked).length > 0)
          {
            iScaling = $('.scaling_' + strFontReworked).text();
          }
          arrTextOptions['font'] = strFont;
          arrTextOptions['scaling'] = iScaling;
          iNumSet++;
        }
      });
    }

    $('.personalisation_option_type_colourswatch input:enabled').each(function(i,e)
    {
      if($(e).val() != '' && typeof($(e).val()) != 'undefined')
      {
        arrTextOptions['colour'] = $(e).val();
        iNumSet++;
      }
    });

    if(iNumSet >= 3)
    {
      $(this).addClass('personalisation_item_value_font_preview_loading');
      iNumSet = 0;
      $(this).html('<img src="/product_personalisation/text/' + arrTextOptions['font'] + '_' + arrTextOptions['colour'] + '_' + arrTextOptions['text'] + '_' + arrTextOptions['scaling'] + '.png" id="text_preview_' + strElementID + '" alt="' + arrTextOptions['text'] + '" />');
      $('#text_preview_' + strElementID).load(function()
      {
        $('#' + strElementID).removeClass('personalisation_item_value_font_preview_loading');
      });
    }


  });
}

function maxLength(strElementID, iMaxLength)
{
  strValue = $('.input_item_' + strElementID).val();
  iLength = strValue.length;
  iRemaining = iMaxLength-iLength;
  if(iRemaining > 0)
  {
    $('#max_size_item_' + strElementID + ' .num').show();
    $('#max_size_item_' + strElementID + ' .message').show();
    $('#max_size_item_' + strElementID + ' .num').text(iRemaining);
    if(iRemaining > 1)
    {
      $('#max_size_item_' + strElementID + ' .message').text('characters remaining');
    }
    else
    {
      $('#max_size_item_' + strElementID + ' .message').text('character remaining');
    }
  }
  else
  {
    $('#max_size_item_' + strElementID + ' .num').hide();
    $('#max_size_item_' + strElementID + ' .message').text('Maximum of ' + iMaxLength + ' characters!');
  }
}// client personalisation functions
function clientPostReorderPersonalisationOptions(){

}
function clientSubmitPersonalisationStep()
{
  return true; // default
}
