/*
  22.08.2006 SK:
    A field separator sign for composing and splitting
    shipping_info cookies.
*/
var strFieldSeparator = '##';

/*
  22.08.2006 SK:
    Shipping info cookie name.
*/
var strShippingCookie = 'shipping_info';

/**
 * @description Saves the shipping and billing information to a cookie.
 *
 * @author Sami Keskinen
 * @since 22.08.2006
 */
function shippingInfoSave() {
  /*
    22.08.2006 SK:
      Get all form fields.
  */
  var objForm = document.forms['order-shipping-info'];
  var lstFields = objForm.getElementsByTagName('input');

  /*
    22.08.2006 SK:
      Construct a delimited string from text input form element values.
  */
  var strValues = '';
  var intValuesInList = 0;
  for (iItem = 0; iItem < lstFields.length; iItem++) {
    /*
      22.08.2006 SK:
        Skip non-text type input fields.
    */
    if (lstFields[iItem].type != 'text') {
      continue;
    }

    if (intValuesInList > 0) {
      strValues += strFieldSeparator;
    }

    strValues += lstFields[iItem].value;
    intValuesInList++;
  }

  /*
    22.08.2006 SK:
      Add the additional info field to the cookie.
      Only first 2kB!
  */
  var lstFields = objForm.getElementsByTagName('textarea');
  for (iItem = 0; iItem < lstFields.length; iItem++) {
    var strValue = lstFields[iItem].value;
    var intLength = strValue.length;
    if (2093 < intLength) {
      intLength = 2093;
    }

    strValues += strFieldSeparator + strValue.substring(0, intLength);
  }

  /*
    27.08.2006 SK:
      Convert plus signs to numbered character notations to store them
      correctly.
  */
  strValues = strValues.replace('\+', '&#043;');

  /*
    22.08.2006 SK:
      Store the constructed cookie.
  */
  setCookie(strShippingCookie, strValues);
}

/**
 * @description Clears the form of all input.
 *
 * @author Sami Keskinen
 * @since 22.08.2006
 */
function shippingInfoReset() {
  /*
    22.08.2006 SK:
      Ask the user if he/she really wants to clear all input.
  */
  if (!(confirm('Tyhjennetäänkö yhteystiedot?'))) {
    return;
  }

  /*
    22.08.2006 SK:
      Repeat through all input fields and clear 'em on the way.
  */
  var objForm = document.forms['order-shipping-info'];
  var lstFields = objForm.getElementsByTagName('input');

  for (iItem = 0; iItem < lstFields.length; iItem++) {
    if (!(lstFields[iItem].type == 'text')) {
      continue;
    }

    lstFields[iItem].value = '';
  }

  /*
    22.08.2006 SK:
      Clear the large textarea block too.
  */
  var lstFields = objForm.getElementsByTagName('textarea');
  lstFields[0].value = '';

  /*
    22.08.2006 SK:
      Yet, discard the shipping info cookie.
  */
  shippingInfoDiscard();
}

/**
 * @description Discards the shipping information cookie.
 *
 * @author Sami Keskinen
 * @since 22.08.2006
 */
function shippingInfoDiscard() {
  deleteCookie(strShippingCookie);
}
