//  Java Script for Validating the form on the Prod (product) page
//  Copyright Baked Just For You, Inc.
//  v0.01 05/19/06
//
//  This function returns true or false
//     True = no error on form
//     False = error on form
//
function checkProd(form) {
//
var nameFCQ
var nameFCQvalue
//
   if (form.Product_Code.value == 'FNC01') {   // this edit is only for the product 'FNC01' Fortune Cookies
        return validateFNC01(form);
   }  // end of check for FNC01
   return true;
//
}  // end of function checkProd
//
//  this function does validation for the product 'FNC01' Fortune Cookies
//
function validateFNC01(form) {
//      find the FCQ Attribute (Quanitity)
   for (var i = 0; i < form.elements.length; i++) {   // loop through each element of the form
      if (form.elements[i].value == "FCQ") {          // we are looking for the FCQ Attribute (Quanitity)
          nameFCQ = form.elements[i].name;
          nameFCQvalue = nameFCQ.substring(0, nameFCQ.indexOf("template_code")) + "value";
//         find the FCQ Attribute Value
          for (var j = 0; i < form.elements.length; j++){  // loop through each element of the form again
             if (form.elements[j].name == nameFCQvalue) {  // we are looking for the FCQ Value element
                if (form.elements[j].value == "Twelve") {   // now check if 12 was selected
                   return checkMsgs()
                }
                break
             }
          }  // end for j
          break
      }
   }  // end for i
      return true;
//
function checkMsgs() {
//  this function is called if the quanitity is twelve
//  it checks to see if anything has been entered in messages 13 through 24
//  then it alerts the user that they need to remove the messages or change Quanitity to 24
//
//  find the element#'s for the values for the text fields msg13 to msg24
var attName = new Array(12);
var attNum = new Array(12);   // array to hold the attribute number for msg13 to msg24
var elmNum = new Array(12);   // array to hold the element number for msg13 to msg24
var attTitle = new Array(12);  // array to hold the title shown on the screen for the attribute
var strStart
var strEnd
var elmName
var alertMsg

// setup attName Array
   attName[1]  = "MSG13";
   attTitle[1] = "Fortune #13"  
   attName[2] = "MSG14";
   attTitle[2] = "Fortune #14"  
   attName[3] = "MSG15";
   attTitle[3] = "Fortune #15"  
   attName[4] = "MSG16";
   attTitle[4] = "Fortune #16"  
   attName[5] = "MSG17";
   attTitle[5] = "Fortune #17"  
   attName[6] = "MSG18";
   attTitle[6] = "Fortune #18"  
   attName[7] = "MSG19";
   attTitle[7] = "Fortune #19"  
   attName[8] = "MSG20";
   attTitle[8] = "Fortune #20"  
   attName[9] = "MSG21";
   attTitle[9] = "Fortune #21"  
   attName[10] = "MSG22";
   attTitle[10] = "Fortune #22"  
   attName[11] = "MSG23";
   attTitle[11] = "Fortune #23"  
   attName[12] = "MSG24";
   attTitle[12] = "Fortune #24"  
//
   for (var i = 0; i < form.elements.length; i++) {   // loop through each element of the form
      if (form.elements[i].name.indexOf("]:template_code") != -1) {
         for (var j = 1; j < 13; j++) { 
            if (form.elements[i].value == attName[j]) {
               elmName = form.elements[i].name
               strStart = elmName.indexOf("Product_Attributes[");
               strEnd = elmName.indexOf("]:template_code");
               if (strStart != -1 && strEnd != -1) {
                  strStart = strStart + 19;
                  attNum[j] = elmName.substring(strStart, strEnd);
               }
               break
            }  // end of if
         }  // end for j
      }  // end of if 
   }  // end for i
//
//  Now that we have the the attribute numbers for MSG13 to MSG24
//  We need to get the element numbers that have the values for those attributes
   for (var i = 0; i < form.elements.length; i++) {   // loop through each element of the form
      for (var j = 1; j < 13; j++) { 
         if (form.elements[i].name == "Product_Attributes[" + attNum[j] + "]:value") {
            elmNum[j] = i;
         }
      }  // end for j
   }  // end for i 
//
//  Now we have the element number for MSG13 to MSG24
//
//  So we need to check each element to make sure it is empty
//
   for (var j = 1; j < 13; j++) {   //  loop through each attribute element
      if (form.elements[elmNum[j]].value.length > 0) {
         alertMsg = 'You have selected the option for 12 Fortune Cookies, ';
         alertMsg += 'so please either select the option for 24 Fortune Cookies';
//         alertMsg += %0d
         alertMsg += ' or only input 12 personalized messages.';
         alert(alertMsg);
         return false;
      }  // end of if
   }  //  end for j  
   return true
}  // end of function checkMsgs
//
}  // end of function validateFNC01
//

