var Cartutil = {};

//************************************************************************
// Name   : Cartutil.updateSwatchNum
//  this is called from swatchSelect.php when a user clicks a swatch image
//
//  (string) formName - the name of the form. Used to access html elements
//                      belonging to the form
//  (string) swatchNum - the # of the swatch ex - "8"
//  (string) swatchTxt - the text of the full swatch name ex - "Happy Days (blue)"
//  (string) swatchImg - full path to selected swatch image ex "gtde/images/swatches/sw8_hp_b.jpg"
//
// Returns : (nothing)
//************************************************************************
Cartutil.updateSwatchNum = function (formName,swatchNum,swatchTxt,swatchImg)
{
  var form; // the form object
  var rawCartIdObj = $(formName + "_rawCartId");
  var rawCartDescObj = $(formName + "_rawCartDesc");
  var ecartIdObj   = $(formName + "_gtEcartId");
  var ecartDescObj = $(formName + "_gtEcartDesc");
  var fabricName;

  // update the swatch image
  $(formName +"Img").setProperty("src",swatchImg);

  // get the form object
  form = document.forms[formName +"Form"];

  // update gtEcartId
  ecartIdObj.setProperty("value",rawCartIdObj.getProperty("value") + "~s" + swatchNum);

  // get the fabric name
  fabricName = rawCartDescObj.getProperty("value");
  cartDesc = "#" + swatchNum + " " + swatchTxt;

  // if there is a fabric name, prefix the cart desc with it
  if (fabricName != "") {
    cartDesc = fabricName + ", " + cartDesc;
  } // endif

  // update gtEcartDesc
  ecartDescObj.setProperty("value",cartDesc);

  // update the display of the swatch name and number
  $(formName + "NumCell").innerHTML = "#" + swatchNum;
  $(formName + "TxtCell").innerHTML = swatchTxt;

}  // end of Cartutil.updateSwatchNum

//************************************************************************
// Name   : Cartutil.updateGiftRegistrySwatchNum
//  this is called from swatchSelect.php when a user clicks a swatch image
//
//  (string) cartId - a unique id prefix. Used to access html elements
//                      belonging to the form
//  (string) swatchNum - the # of the swatch ex - "8"
//  (string) swatchTxt - the text of the full swatch name ex - "Happy Days (blue)"
//  (string) swatchImg - full path to selected swatch image ex "gtde/images/swatches/sw8_hp_b.jpg"
//
// Returns : (nothing)
//************************************************************************
Cartutil.updateGiftRegistrySwatchNum = function(cartId,swatchNum,swatchTxt,swatchImg)
{
  // update the displayed swatch num
  $(cartId + "_displayedSwatch").set("html","#" + swatchNum + " " + swatchTxt);

  // update the hidden form field
  $(cartId + "_formSwatch").setProperty("value","#" + swatchNum + " " + swatchTxt);
}  // end of mCartutil.updateGiftRegistrySwatchNum

//************************************************************************
// Name   : Cartutil.preProcess
//  This adjusts the product price for the number of letters selected,
//  checks to ensure a swatch was chosen if required and informs the user
//  if a swatch still needs to be selected
//
// Returns : true  - form is ready to be submitted
//           false - form is not ready to be submitted
//************************************************************************
Cartutil.preProcess = function(formName)
{
  // get the number price
  var priceObj = $(formName + "_gtEcartPrice");
  var price = parseFloat(priceObj.getProperty("value"));
  var cartIdObj = $(formName + "_gtEcartId");
  var cartId = cartIdObj.getProperty("value");
  var lettersObj = $(formName + "_letters");
  var letters;

  // if this form has a personalized letters field
  if (lettersObj) {

    // get the letter, remove any spaces (spaces don't count)
    letters = lettersObj.getProperty("value");
    letters = letters.replace(/[ ]+/g,"");
    numLetters = letters.length;

    // if there is at least one letter
    if ( numLetters > 0 ) {
      // get the letter price
      letterPrice = parseFloat($(formName + "_letterPrice").getProperty("value"));

      // adjust the item price
      price += numLetters * letterPrice;
      priceObj.setProperty("value",price);

      // adjust the item id
      id = cartIdObj.getProperty("value");
      id += " (" + lettersObj.getProperty("value") + ")";
      cartIdObj.setProperty("value",id);
    } // endif

  } // endif

  // if no pattern was chosen, remind the user
  if ($(formName + "NumCell") && ($(formName + "NumCell").innerHTML == "")) {
    alert("Please select a fabric pattern");
    return false;
  } // endif

  return true;

} // end of Cartutil.preProcess

