/*********************************************************************/
/*                                                                   */
/* L.L. Bean Confidential                                            */
/*                                                                   */
/*                                                                   */
/* Copright by IBM, May 1998                                         */
/*                                                                   */
/* The source code for this program is not published or otherwise    */
/* divested of its trade secrets, irrespective of what has been      */
/* deposited with the U.S. Copyright Office.                         */
/*                                                                   */
/*********************************************************************/

/*
   Error messages for Order Options selection, including Product Services
*/

// Product Selection: Size Selection
msg_000="We\'re sorry, more information is needed to order this item.  Please select a Size and/or Style."
msg_001="Please select a Style from the list."

// Product Service: Personalized Mug
msg_100="The name on the mug cannot begin with spaces."
msg_102="Please enter the text for your monogram.";
msg_103="The text for your selected monogram style should be 1-3 characters, no spaces."
msg_104="Please enter no more than the maximum number of characters for the specified monogram style (as indicated in parentheses next to the style)."

// Product Service: I'm A Start CD
msg_200="Please enter the name for the CD."
msg_201="Please enter the hometown for the CD."

// default Attributes still selected
msg_200="We\'re sorry, more information is needed to order this item.  Please complete Step 2 then continue your order.";

// no alternative selected
msg_201="Please select an alternate item from the list at the top of the page."

/*
   Determine what kind of checks we want to perform for this page
   and combine them
*/

function checkAll() {
  var retVal = "true"
  if (document.additem.itemnum) {
    var itemnum = document.additem.itemnum.value
    if (itemnum.value == "A123") {
      retVal = monogramChecks()
    }
    if (itemnum.value == "A124") {
      retVal = engraveChecks()
    }
    if (retVal == "false") {
      return false
    }
    else {
      return true
    }
  }
  else {
    return true
  }
}


/*
   This function returns the name of the radio button which was
   selected depending upon the calling "button group"
   This is done cause it is fruitless to check any hardcoded
   values for the radio buttons as there is no static way
   to determine which one of them was selected
*/

function getSelectedButton(buttonGrp) {
  for (var currentItem = 0; currentItem < buttonGrp.length; currentItem++) {
    if (buttonGrp[currentItem].checked) {
      currentItem=buttonGrp[currentItem].value
      return currentItem
    }
  }
  return 0
}


/*
   this function checks whether each digit entered in the
  "How Many" field is numeric and > 0
*/

function checkForNum() {
  var inputStr = document.additem.quantity.value
  if (inputStr == "0" || inputStr == "00") {
    document.additem.quantity.value = "1"
    alert(msg_000)
    return false
  }

  for (i = 0; i < inputStr.length; i++) {
    var singleChar = inputStr.charAt(i)
    if (singleChar < "0" || singleChar > "9") {
      document.additem.quantity.value="1"
      alert(msg_000)
      return false
    }
  }
  return true
}


/*
   Checks for Product Service: Finishing Pants

   This function runs through all the available choices for the monogramming
   and checks whether the text entered for the monogram text matches the
   maximum length for the selected item
*/

function checkCuffValues() {
  var currentItem = getSelectedButton(document.additem.cuffsradio)
  if (currentItem == "NO" && document.additem.inseam_nocuffs.options[document.additem.inseam_nocuffs.selectedIndex].text == "") {
    alert(msg_400)
    return "false"
  }
  else {
    if (currentItem == "YES" && document.additem.inseam_cuffs.options[document.additem.inseam_cuffs.selectedIndex].text == "") {
      alert(msg_401)
      return "false"
    }
  }
  return "true"
}


/*
   This function is invoked via the "onChange" event of the cuffs/no cuffs
   items for the Finishing product service this will cause the respective radio
   button to get automatically selected when the user changes the length of
   either item
*/

function autoSelect(selVar) {
  if (document.additem.cuffsradio.length > 2) {
     if (selVar == "inseam_cuffs") {
       document.additem.cuffsradio[1].checked = true
       document.additem.inseam_nocuffs[0].selected = true
     }
     else {
       document.additem.cuffsradio[0].checked = true
       document.additem.inseam_cuffs[0].selected = true
     }
  } else {
       document.additem.cuffsradio[0].checked = true
  }
}

/*
   Checks for Product Service: Monogramming
*/

function monogramChecks() {
  var selectedItem = getSelectedButton(document.additem.psvatrid)
  var checkText1 = document.additem.psatext.value
  var localChar = checkText1.charAt(0)

  // check that the monogram text does not begin with spaces
  if (localChar == " ") {
    alert(msg_100)
    return "false"
  }

  // check whether monogram text has been provided, if a style is selected
  if (selectedItem!="0" && checkText1 == "") {
    alert (msg_102)
    return "false"
  }

  // check whether all the elements for monogramming are present in case
  // the monogramming option has been selected, plus check whether placement is an
  // option for this item, if it is then include it in the checks otherwise
  // exlude it

  if (document.additem.selectbox.value == "1") {
    if (selectedItem != "0" && (document.additem.psvatrid2.options[document.additem.psvatrid2.selectedIndex].text == "")) {
      alert(msg_101)
      return "false"
    }
  }

  // check whether the correct number of characters have been supplied for the
  // monogram based upon their max values
  selectionArray = selectedItem.split("_")
  var selName = selectionArray[0]
  var selSize = selectionArray[1]
  // check diamond style
  if (selSize == "d") {
	if (document.additem.psatext.value.length != "3" || document.additem.psatext.value.indexOf(" ") > -1) {
    		// document.additem.psatext.value=""
    		alert (msg_103)
    		return "false"
	} else {
		return "true"
	}
  }

  // check the maximum length
  if (selectedItem!="0" && document.additem.psatext.value.length > selSize) {
    // document.additem.psatext.value=""
    alert (msg_104)
    return "false"
  }

  // check placement
  if (document.additem.selectbox.value == "1") {
    if (selectedItem == "0" && (document.additem.psvatrid2.options[document.additem.psvatrid2.selectedIndex].text != "")) {
      // document.additem.psatext.value=""
      alert (msg_105)
      return "false"
    }
  }

  // check if text has been entered
  if (selectedItem == "0" && checkText1 != "") {
    // document.additem.psatext.value=""
    alert (msg_106)
    return "false"
  }

  return "true"
}


/*
   Checks for Product Service: Engraving
*/

function engraveChecks() {
  var selectedItem = getSelectedButton(document.additem.psvatrid)
  var checkText2 = document.additem.psatext.value
  var firstChar = checkText2.charAt(0)

  // check whether engraving text begins with spaces
  if (firstChar == " ") {
    alert (msg_500)
    return "false"
  }

  // check whether engraving text has been provided in case the user has opted
  // to get the item engraved
  if (selectedItem != "0" && checkText2 == "") {
    alert (msg_501)
    return "false"
  }

  radioArray = selectedItem.split("_")
  var radioName = radioArray[0]
  var radioSize = radioArray[1]
  if (selectedItem!="0" && document.additem.psatext.value.length > radioSize) {
    // document.additem.psatext.value=""
    alert(msg_502)
    return "false"
  }

  if (selectedItem=="0" && checkText2 != "") {
    // document.additem.psatext.value=""
    alert(msg_503)
    return "false"
  }

  return "true"
}

/********************************************************************/
/* ensures that all of the select attributes on the product page    */
/* are selected                                                     */
/********************************************************************/

function check_attr_selection()
{
  var ret=checkAll();
  var sucess = true;
  if (ret==false) {
    return false;
  }
  for (var i=0;i<= document.additem.length-1;i++)
  {
	if( ((document.additem.elements[i].type == 'select-one') || (document.additem.elements[i].type == 'select-multiple')) && (document.additem.elements[i].selectedIndex == -1))
	{
		alert(msg_201);
		return false;
	}
	if( ((document.additem.elements[i].type == 'select-one') || (document.additem.elements[i].type == 'select-multiple')) && (document.additem.elements[i].selectedIndex != -1) && (document.additem.elements[i].options[document.additem.elements[i].selectedIndex].value == "default") )
	{
		alert(msg_000);
		return false;
	}
  }
  return sucess;
}

