var selbox, prodbox, orderbox, namebox, emailbox, schoolbox, textbox;
var choice = "", optn = "@", prod = "@";
var emailvalid, LNamevalid, OrderNumvalid, sendTo, theMessage, outMsg;

function setOptions(choice) {
// Step 1 has been changed, modify options
   selbox.options.length = 0;
   prodbox.options.length = 0;

   disable(prodbox);
   disable(namebox);
   disable(textbox);
   disable(orderbox);
   disable(schoolbox);
   disable(emailbox);

   optn = "@"; prod = "@"; theMessage = ""; sendTo.value = "0"; theMessage = ""; outMsg = "";

   if (choice == 0) {
      resetForm();
   } else if (choice == 1) {
      selbox.options[selbox.options.length] = new Option('--Choose an area--','@');
      selbox.options[selbox.options.length] = new Option('Mail-In Production','Status_Reg');
      selbox.options[selbox.options.length] = new Option('Internet Production','Status_IP');
      selbox.options[selbox.options.length] = new Option('Online Kit Order','Status_Order');
      prodbox.options[prodbox.options.length] = new Option('-(Not Applicable)-','@');

      selbox.disabled = false;
   } else if (choice == 2) {
      selbox.options[selbox.options.length] = new Option('--Choose an area--','@');
      selbox.options[selbox.options.length] = new Option('Internet Production','Tech_IP');
      selbox.options[selbox.options.length] = new Option('General Website','Tech_Gen');
      prodbox.options[prodbox.options.length] = new Option('-(Not Applicable)-','@');

      selbox.disabled = false;
   } else if (choice == 3) {
      selbox.options[selbox.options.length] = new Option('--Choose an area--','@');
      selbox.options[selbox.options.length] = new Option('Order Status','School_Stat');
      selbox.options[selbox.options.length] = new Option('Product Question','School_PQ');
      selbox.options[selbox.options.length] = new Option('Errors/Corrections','School_Redo');
      selbox.options[selbox.options.length] = new Option('Repurchasing','School_Repur');
      selbox.options[selbox.options.length] = new Option('How to Order','School_Order');
      prodbox.options[prodbox.options.length] = new Option('-(Not Applicable)-','@');

      selbox.disabled = false;
   } else if (choice == 4) {
      selbox.options[selbox.options.length] = new Option('--Choose an area--','@');
      selbox.options[selbox.options.length] = new Option('Problem with finished item','Gen_Item');
      selbox.options[selbox.options.length] = new Option('Repurchasing','Gen_Repur');
      selbox.options[selbox.options.length] = new Option('Kit Questions','Gen_Kit');
      selbox.options[selbox.options.length] = new Option('Ordering Questions','Gen_OQ');
      selbox.options[selbox.options.length] = new Option('Suggestions and Feedback','Gen_Feed');
      prodbox.options[prodbox.options.length] = new Option('-(Not Applicable)-','@');

      selbox.disabled = false;
   } else {
      alert("Error: S1.  Value: " + choice + ".\nPlease contact technicalsupport@creationsbyyou.com.");
   }
}

function setVars() {
// Step 2 has been changed, modify options

   optn = document.custform.step2.options[document.custform.step2.selectedIndex].value;
   prodbox.options.length = 0;

   prod = "@"; theMessage = ""; sendTo.value = "0"; theMessage = ""; outMsg = "";

   if (optn == "@") {
      disable(prodbox);
      disable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      disable(textbox);
      sendTo.value = "0";
   } else if (optn == "Status_Reg") {
      enableP(prodbox, "Reg");
      enable(emailbox);
      enable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "1";
   } else if (optn == "Status_IP") {
      enableP(prodbox, "Inet");
      enable(emailbox);
      enable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "1";
   } else if (optn == "Status_Order") {
      disable(prodbox);
      enable(emailbox);
      enable(orderbox);
      disable(namebox);
      disable(schoolbox);
      enable(textbox);
      sendTo.value = "2";
   } else if (optn == "Tech_IP") {
      enableP(prodbox, "Inet");
      enable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "3";
   } else if (optn == "Tech_Gen") {
      disable(prodbox);
      enable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      textbox.value = "What area of the site were you in?\nWhat was the error message?"
      sendTo.value = "3";
   } else if ((optn == "School_Stat") || (optn == "School_PQ") || (optn == "School_Redo") ||
        (optn == "School_Repur") || (optn == "School_Order")) {
      enableP(prodbox, "School");
      enable(emailbox);
      enable(namebox);
      enable(schoolbox);
      disable(orderbox);
      enable(textbox);
      namebox.value = "Type Teacher's Last Name Here"
      sendTo.value = "4";
   } else if ((optn == "Gen_Item") || (optn == "Gen_Repur")) {
      enableP(prodbox, "Reg");
      enable(emailbox);
      enable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "1";
   } else if (optn == "Gen_Kit") {
      enableP(prodbox, "Reg");
      enable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "1";
   } else if (optn == "Gen_OQ") {
      disable(prodbox);
      enable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "5";
   } else if (optn == "Gen_Feed") {
      disable(prodbox);
      enable(emailbox);
      disable(namebox);
      disable(schoolbox);
      disable(orderbox);
      enable(textbox);
      sendTo.value = "1";
   } else {
      alert("Error: S2.  Value: " + optn + ".\nPlease contact technicalsupport@creationsbyyou.com.");
   }

   //textbox.value = "Category: " + optn;
}

function setProd() {
// Product has been selected, update product code
   optn = document.custform.step2.options[document.custform.step2.selectedIndex].value;
   prod = document.custform.product.options[document.custform.product.selectedIndex].value;
   //textbox.value = "Category: " + optn + "  Product: " + prod;
}

function addProducts(prodbox) {
   prodbox.options[prodbox.options.length] = new Option('--Select your product--','@');
   prodbox.options[prodbox.options.length] = new Option('IlluStory','IKR');
   prodbox.options[prodbox.options.length] = new Option('IlluStory Christian Ed.','IKRC');
   prodbox.options[prodbox.options.length] = new Option('Baby Story','BS');
   prodbox.options[prodbox.options.length] = new Option('Baby Calendar','BC');
   prodbox.options[prodbox.options.length] = new Option('Kid Clock','KC');
   prodbox.options[prodbox.options.length] = new Option('Make-A-Story','MAS');
   prodbox.options[prodbox.options.length] = new Option('Make-A-PhotoStory','MAPS');
   prodbox.options[prodbox.options.length] = new Option('My Calendar','MC');
   prodbox.options[prodbox.options.length] = new Option('My Masterpiece','MST');
   prodbox.options[prodbox.options.length] = new Option('My Mug','MM');
   prodbox.options[prodbox.options.length] = new Option('My Watch','MW');
   prodbox.options[prodbox.options.length] = new Option('Pet Story','PTS');
   prodbox.options[prodbox.options.length] = new Option('Pet Calendar','PTC');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Jr.','PSJ');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Sr.','PSS');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Classroom Ed.','PSCE');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Team Ed.','PSTE');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Wedding Ed.','PSWE');
   prodbox.options[prodbox.options.length] = new Option('Photo Clock','PC');
   prodbox.options[prodbox.options.length] = new Option('Photo Watch','PW');
   prodbox.options[prodbox.options.length] = new Option('Photo Mug','PM');
   prodbox.options[prodbox.options.length] = new Option('Photo Calendar','PC');
   prodbox.options[prodbox.options.length] = new Option('Plate Works','PLT');
}

function addInetProducts(prodbox) {
   prodbox.options[prodbox.options.length] = new Option('--Select your product--','@');
   prodbox.options[prodbox.options.length] = new Option('Internet StoryBook','ISB');
   prodbox.options[prodbox.options.length] = new Option('Internet Make-A-Story','ISB-MAS');
   prodbox.options[prodbox.options.length] = new Option('Internet Make-A-PhotoStory','ISB-MAPS');
   prodbox.options[prodbox.options.length] = new Option('Internet PhotoBook','IB7');
   prodbox.options[prodbox.options.length] = new Option('Internet PhotoBook (Full-Sized)','IB8');
   prodbox.options[prodbox.options.length] = new Option('Internet Calendar','ICL');
   prodbox.options[prodbox.options.length] = new Option('Internet Wall Art','IMST');
   prodbox.options[prodbox.options.length] = new Option('Internet Watch','IPW');
   prodbox.options[prodbox.options.length] = new Option('Internet PhotoClock','IPC');
   prodbox.options[prodbox.options.length] = new Option('Internet Tableware','IPL');
   prodbox.options[prodbox.options.length] = new Option('Internet Mug','IMG');
}

function addSchoolProducts(prodbox) {
   prodbox.options[prodbox.options.length] = new Option('--Select your product--','@');
   prodbox.options[prodbox.options.length] = new Option('IlluStory','IKR');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Sr. Classroom Ed.','PSCE');
   prodbox.options[prodbox.options.length] = new Option('PhotoStory Jr. Classroom Ed.','PSJC');
   prodbox.options[prodbox.options.length] = new Option('Plate Works','PW');
   prodbox.options[prodbox.options.length] = new Option('Kid Clock','KC');
}

function disable(theObj) {
   if (theObj == prodbox) {
      prodbox.options[prodbox.options.length] = new Option('-(Not Applicable)-','@');
      prodbox.disabled = true;
   } else if (theObj == textbox) {
      theObj.disabled = true;
      theObj.value = "";
   } else {
      theObj.disabled = true;
      theObj.value = "-(Not Applicable)-";
   }
}

function enable(theObj) {
   theObj.disabled = false;
   theObj.value = "";
}

function enableP(theObj, list) {
// Use for product box
   theObj.disabled = false;
   if (list == "Reg") addProducts(theObj);
   if (list == "Inet") addInetProducts(theObj);
   if (list == "School") addSchoolProducts(theObj);
}

function resetForm() {
   selbox = document.custform.step2;
   prodbox = document.custform.product;
   orderbox = document.custform.ordernum;
   namebox = document.custform.name;
   emailbox = document.custform.email;
   schoolbox = document.custform.school;
   textbox = document.custform.question;
   sendTo = document.custform.recipient;
   document.custform.go.disabled = false;

   selbox.options.length = 0;
   prodbox.options.length = 0;
   selbox.options[selbox.options.length] = new Option('--Select a category above--','@');
   selbox.disabled = true;

   disable(prodbox);
   disable(emailbox);
   disable(namebox);
   disable(schoolbox);
   disable(orderbox);
   disable(textbox);

   textbox.value = "";
   document.custform.step1.selectedIndex = 0;
}

function submitForm() {
   if(isSubmitted)
     return false;
   // outputbox = document.custform.output;
   document.custform.flag.value = "";
   outMsg = document.custform.Message;

   emailvalid = 0;
   validateEmail();

   LNamevalid = 0;
   validateLName();

   // Chip doesn't want the Order Confirmation Number to be validated
   // OrderNumvalid = 0;
   // validateOrderNum();

   if ((optn == "@") || (optn == "") || (optn == "undefined")) {
      alert("Please select a specific area in Step 2.");
      return false;
   } else if (((prod == "@") || (prod == "") || (prod == "undefined")) && (prodbox.disabled == false)) {
      alert("Please select the appropriate product.");
      return false;
   } else if ((emailvalid == 0) && (emailbox.disabled == false)) {
      alert("Please provide a valid email address so that we may answer your question.");
      return false;
   } else if ((LNamevalid == 0) && (namebox.disabled == false)) {
      alert("Please fill in a valid Last Name.");
      return false;
   } else if ((OrderNumvalid == 0) && (orderbox.disabled == false)) {
      alert("Please type in a valid Order Number.");
      return false;
   } else {
      document.custform.flag.value = "filled";
      //document.custform.go.disabled = true;

      theMessage  =   "   Category: " + optn;

      if (prodbox.disabled == false) {
         theMessage += "\n    Product: " + prod;
      }

      theMessage += "\n      Email: " + emailbox.value;

      if (namebox.disabled == false) {
         theMessage += "\n  Last Name: " + namebox.value;
      }

      if (schoolbox.disabled == false) {
         theMessage += "\nSchool Name: " + schoolbox.value;
      }

      if (orderbox.disabled == false) {
         theMessage += "\nKit Order #: " + orderbox.value;
      }

      theMessage += "\n\nQuestion/Comment/Message:\n" + textbox.value;

      // outputbox.value = theMessage;
      outMsg.value = theMessage;
      isSubmitted = true;
      return true;
   }
}

function validateEmail() {
   if (emailbox.value.length > 0) {
      at    = emailbox.value.indexOf("@")
      prd   = emailbox.value.indexOf(".",at)
      comma = emailbox.value.indexOf(",")
      space = emailbox.value.indexOf(" ")
      prd2  = emailbox.value.lastIndexOf(".") + 1
      len   = emailbox.value.length

      if ((at > 0) && (prd > (1 + 1)) && (comma == -1) && (space == -1) &&
          (len - prd2 >= 2) && (len - prd2 <= 3)) {
         emailvalid = 1
         //alert("Valid.");
      } else {
         emailvalid = 0
         //alert("Invalid.");
      }
   }
}

function validateLName() {
   if (namebox.value.indexOf("Type T") != -1) {
      LNamevalid = 0;
   } else if (namebox.value == "") {
      LNamevalid = 0;
   } else {
      LNamevalid = 1;
   }
}

function validateOrderNum() {
   if (orderbox.value.indexOf("DS") == -1) {
      OrderNumvalid = 0;
   } else if (orderbox.value.length < 6) {
      OrderNumvalid = 0;
   } else if (orderbox.value.length > 7) {
      OrderNumvalid = 0;
   } else {
      OrderNumvalid = 1;
   }
}
