/*********************************************************************/
/*                                                                   */
/* 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.                         */
/*                                                                   */
/*********************************************************************/

//BeginExternalization
var rules_msg_001="入力された数量は無効です。ご確認の上、0を除く数量を半角数字でご入力ください。"
var rules_msg_002="モノグラムの欄はスペースで始めることはできません。"
var rules_msg_003="モノグラムをご入力されましたが、位置を選択していません。位置をお選びください。";
var rules_msg_004="アルファベットをご入力ください。アルファベットは半角文字でご記入ください。";
var rules_msg_005="お選びになったスタイルはスペースなしで3文字までの入力が可狽ﾅす。アルファベットは半角文字でご記入ください。"
var rules_msg_006="各スタイルに指定された文字数以内で、アルファベットをご入力ください。アルファベットは半角文字でご記入ください。"
var rules_msg_007="モノグラムをご入力されましたが、スタイルを選択していません。スタイルをお選びください。";
var rules_msg_008="モノグラムをご入力されましたが、スタイルを選択していません。スタイルをお選びください。";
var rules_msg_009="シングル仕上げのパンツの股下サイズをお選びください。"
var rules_msg_010="ダブル仕上げのパンツの股下サイズをお選びください。"
var rules_msg_011="The text to be engraved cannot begin with spaces."
var rules_msg_012="Please enter the text for the engraving."
var rules_msg_013="Please enter no more than the maximum number of characters for the selected engraving style(as indicated in parentheses next to the style)." 
var rules_msg_014="You provided text for engraving but did not select a style.\n\nPlease select a style and resubmit."
var rules_msg_015="この商品をご注文いただく際に必要な情報が不足しています。必要情報を選択もしくは入力をされてから、バッグに入れてください。";
var rules_msg_016="お選びの商品からサイズ、カラー、スタイルをお選びください。";
var rules_msg_017="代わりのアイテムを上からお選びください。"
var rules_msg_018="We\'re sorry, more information is needed to email this item to a friend.\n\nPlease complete Step 2 (including all size and color choices) and click the \"Email a friend\" link again."
var rules_msg_019="代わりの商品をお選びください。"
//EndExternalization

function checkAll(component) {
  var retVal = "true"
  if (document.data.psvid || eval("document.data.psvid_" + component)) {
    if(component == null){
			var psvid = document.data.psvid.value
    }else{
			var psvid = eval("document.data.psvid_" + component).value    
  	}	

    //check if this is gift card service
    if (psvid.indexOf("163") > "-1") {
      return true; 
    }
   
    if (psvid.indexOf("3") > "-1") {
      retVal = checkCuffValues(component)
    }
    if (psvid.indexOf("1") > "-1") {
      retVal = monogramChecks()
    }
    if (psvid.indexOf("7") > "-1") {
      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(component) {
	
	if(component == null){
		theInputBox = document.data.quantity
	}else{
		theInputBox = eval("document.data.q_" + component)
	}
	
  var inputStr = theInputBox.value
  if (inputStr == "0" || inputStr == "00" || inputStr == "") {
    theInputBox.value = "1"
    alert(rules_msg_001)
    return false 
  }

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

/*
   this function only accepts numeric, Backspace, Delete or Tab keys presses.
   all other keys will be ignored.
*/
function numbersonly(e){
	
	var unicode = e.charCode ? e.charCode : e.keyCode;
	if (unicode != 8 && unicode != 46 && unicode != 9){ // if the key isn't the backspace, delete or tab key (which we should allow)
		if (unicode < 48 || unicode > 57) { // if not a number
			return false; //disable key press
		} 
	}
	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(component) { 
	
	if(component == null){
  	var currentItem = getSelectedButton(document.data.cuffsradio);
  	var theNoCuffsSelect = document.data.inseam_nocuffs;
  	var theCuffsSelect = document.data.inseam_cuffs;
	}else{
		var currentItem = getSelectedButton(eval("document.data.cuffsradio_" + component));
		var theNoCuffsSelect = eval("document.data.inseam_nocuffs_" + component);	
		var theCuffsSelect = eval("document.data.inseam_cuffs_" + component);	
	}
		
  if ((currentItem == "NO" && theNoCuffsSelect.options[theNoCuffsSelect.selectedIndex].value == "") ||
  	(currentItem == "NO" && theNoCuffsSelect.options[theNoCuffsSelect.selectedIndex].text == "")) {
  	alert(rules_msg_009)
    	theNoCuffsSelect.focus();
    	return "false"
  } 
  else {
    if ((currentItem == "YES" && theCuffsSelect.options[theCuffsSelect.selectedIndex].value == "") ||
    	(currentItem == "YES" && theCuffsSelect.options[theCuffsSelect.selectedIndex].text == "")) {
      alert(rules_msg_010)
      theCuffsSelect.focus();
      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, component) {
	if(component == null){
	  cuffsIndex = -1;
	  noCuffsIndex = -1;
	  unfinishedIndex = -1;
	  for (i = 0; i < document.data.cuffsradio.length; i++)
	  {
		  if (document.data.cuffsradio[i].value == "YES")
		  {
		    cuffsIndex = i;	  
		  }
		  else if (document.data.cuffsradio[i].value == "NO")
		  {
		    noCuffsIndex = i;	  
		  }
		  else
		  {
		    unfinishedIndex = i;	  
		  }
	  }
	  if (document.data.cuffsradio.length > 2) { 
	     if (selVar == "inseam_cuffs") {
	       document.data.cuffsradio[cuffsIndex].checked = true;
	       document.data.inseam_nocuffs[0].selected = true;
	     } 
	     else {
	       document.data.cuffsradio[noCuffsIndex].checked = true;
	       document.data.inseam_cuffs[0].selected = true;
	     }
	  } else {
		  singleOptionIndex = 0;
		  if (cuffsIndex >= 0)
		  {
			  singleOptionIndex = cuffsIndex;
		  }
		  else if (noCuffsIndex >= 0)
		  {
			  singleOptionIndex = noCuffsIndex;
		  }
	       document.data.cuffsradio[singleOptionIndex].checked = true;
	  } 		
	}else{
	  if (eval("document.data.cuffsradio_" + component).length > 2) { 
	     if (selVar == ("inseam_cuffs_" + component)) {
	       eval("document.data.cuffsradio_" + component)[0].checked = true
	       eval("document.data.inseam_nocuffs_" + component)[0].selected = true
	     } 
	     else {
	       eval("document.data.cuffsradio_" + component)[1].checked = true
	       eval("document.data.inseam_cuffs_" + component)[0].selected = true
	     }
	  } else {
	       eval("document.data.cuffsradio_" + component)[0].checked = true	
	  } 	
	}
}

/*
   Checks for Product Service: Monogramming
*/

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

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

  // check whether monogram text has been provided, if a style is selected
  if (selectedItem!="0" && checkText1 == "") {
    alert (rules_msg_004)
    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.data.selectbox.value == "1") {
    if (selectedItem != "0" && (document.data.psvatrid2.options[document.data.psvatrid2.selectedIndex].text == "")) {
      alert(rules_msg_003)
      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.data.psatext.value.length != "3" || document.data.psatext.value.indexOf(" ") > -1) {
    		// document.data.psatext.value=""
    		alert (rules_msg_005)
    		return "false"
	} else {
		return "true"
	}
  }

  // check the maximum length
  if (selectedItem!="0" && document.data.psatext.value.length > selSize) {
    // document.data.psatext.value=""
    alert (rules_msg_006)
    return "false"
  }
  
  // check placement
  if (document.data.selectbox.value == "1") {
    if (selectedItem == "0" && (document.data.psvatrid2.options[document.data.psvatrid2.selectedIndex].text != "")) {
      // document.data.psatext.value=""
      alert (rules_msg_007)
      return "false"
    }
  }

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

  return "true"
}


/*
   Checks for Product Service: Engraving
*/

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

  // check whether engraving text begins with spaces
  if (firstChar == " ") {
    alert (rules_msg_011)
    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 (rules_msg_012)
    return "false"
  }

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

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

  return "true"
}

/********************************************************************/
/* ensures that all of the select attributes on the product page    */
/* are selected                                                     */
/********************************************************************/
var emailFriendFlag = false;
function check_attr_selection(component)
{
	var ret=checkAll(component);
  var sucess = true;
  if (ret==false) { 
    return false; 
  }
  for (var i=0;i<= document.data.length-1;i++)
  {
  	theFormObject = document.data.elements[i];
  	
  	if(component == null || (component != null && ((theFormObject.name.substr(4,1) == component && component < 10) || (theFormObject.name.substr(4,2) == component && component >= 10)))){

	  	if( ((theFormObject.type == 'select-one') || (theFormObject.type == 'select-multiple')) && (theFormObject.selectedIndex == -1))
			{
				if (emailFriendFlag == true) {
					alert(rules_msg_018);
				} else {
					if (component == null) {
						alert(rules_msg_017);
					} else {
						alert(rules_msg_019);
					}
				}
				if (theFormObject.offsetWidth ) { // returns true if element is visible
					theFormObject.focus();
				}
				return false;
			}
			if( ((theFormObject.type == 'select-one') || (theFormObject.type == 'select-multiple')) && (theFormObject.selectedIndex != -1) && (theFormObject.options[theFormObject.selectedIndex].value == "default") )
			{
				if(component == null){
					if (emailFriendFlag == true) {
						alert(rules_msg_018);
					} else {
						alert(rules_msg_015);
					}
					if (theFormObject.offsetWidth ) { // returns true if element is visible
						theFormObject.focus();
					}
					return false;						
				}else{
					if (emailFriendFlag == true) {
						alert(rules_msg_018);
					} else {
						alert(rules_msg_016);
					}				
					if (theFormObject.offsetWidth ) { // returns true if element is visible
						theFormObject.focus();
					}
					return false;				   							
				}
			}			  		
  	}
  }

  // check quantity text box for valid value
  if ( document.data.quantity && !checkForNum() ) {
     return false;
  }
	  
  return sucess;  
}
