/*
This javascript file is to be used in conjunction with
the request a quote html page.  This file controls the dynamic
nature of the form elements on that html file

This file was written by Adam Bantly in March, 2007
*/

//---------------------------------------------------------

// Instance Variables
var HELP_PAGE = "helpmechoosequote.html"
var REQUEST_PAGE = "requestaquote.html"
var PARTS_PAGE = "http://www.rwelectrodes.com"


//---------------------------------------------------------

// Change the class of the element with id=elem to shown
function makeShow(elem)
{
   var y=document.getElementById(elem)
   y.className="shown"
}


//----------------------------------------------------------

// Change the class of the element with id=elem to hidden
// If elem is "all" then hide everything except the machine type
// If elem is "bottom" hide the weld specific options and anything below it
function makeHide(elem)
{
   var y
   if(elem=="all")
   {
      y=document.getElementById("divweld")
      y.className="hidden"
      y=document.getElementById("divmat")
      y.className="hidden"
      y=document.getElementById("divspot")
      y.className="hidden"
      y=document.getElementById("divproject")
      y.className="hidden"
      y=document.getElementById("divcross")
      y.className="hidden"
      y=document.getElementById("divbutt")
      y.className="hidden"
      y=document.getElementById("divseam")
      y.className="hidden"
      y=document.getElementById("divother")
      y.className="hidden"
      y=document.getElementById("divconsult")
      y.className="hidden"
      y=document.getElementById("divtypestainless")
      y.className="hidden"
      y=document.getElementById("divtypealuminum")
      y.className="hidden"
      y=document.getElementById("divbottom")
      y.className="hidden"
      y=document.getElementById("divtypecoated")
      y.className="hidden"
      y=document.getElementById("divcoatedother")
      y.className="hidden"
      y=document.getElementById("divsubmit")
      y.className="hidden"
      y=document.getElementById("formerrormessage")
      y.className="hidden"
      unHighlight("all")
   }
   else if(elem=="bottom")
   {
      y=document.getElementById("divspot")
      y.className="hidden"
      y=document.getElementById("divproject")
      y.className="hidden"
      y=document.getElementById("divcross")
      y.className="hidden"
      y=document.getElementById("divbutt")
      y.className="hidden"
      y=document.getElementById("divseam")
      y.className="hidden"
      y=document.getElementById("divother")
      y.className="hidden"
      y=document.getElementById("divconsult")
      y.className="hidden"
      y=document.getElementById("divbottom")
      y.className="hidden"
      y=document.getElementById("divtypecoated")
      y.className="hidden"
      y=document.getElementById("divcoatedother")
      y.className="hidden"
      y=document.getElementById("divsubmit")
      y.className="hidden"
      y=document.getElementById("formerrormessage")
      y.className="hidden"
      unHighlight("all")
   }
   else
   {
      var y=document.getElementById(elem)
      y.className="hidden"
   }
}

//---------------------------------------------------------

// Remove the highlighting from everything if we step backward
function unHighlight(elem)
{
   if(elem=="all")
   {
      y=document.getElementById("divtypestainless")
      y.className="hidden"
      y=document.getElementById("divtypealuminum")
      y.className="hidden"
      y=document.getElementById("divcoatedother")
      y.className="hidden"
      y=document.getElementById("divother")
      y.className="hidden"
      y=document.getElementById("divspotoption1")
      y.className=""
      y=document.getElementById("divspotoption2")
      y.className=""
      y=document.getElementById("divspotoption3")
      y.className=""
      y=document.getElementById("divspotoption4")
      y.className=""
      y=document.getElementById("divspotoption5")
      y.className=""
      y=document.getElementById("divspotoption6")
      y.className=""
      y=document.getElementById("divspotoption7")
      y.className=""
      y=document.getElementById("divprojectoption1")
      y.className=""
      y=document.getElementById("divstudtype1")
      y.className="hidden"
      y=document.getElementById("divstudtype2")
      y.className="hidden"
      y=document.getElementById("divnuttype1")
      y.className="hidden"
      y=document.getElementById("divnuttype2")
      y.className="hidden"
      y=document.getElementById("divprojectoption4")
      y.className=""
      y=document.getElementById("divprojectoption5")
      y.className=""
      y=document.getElementById("divprojectoption6")
      y.className=""
      y=document.getElementById("divprojectoption7")
      y.className=""
      y=document.getElementById("divcrossoption1")
      y.className=""
      y=document.getElementById("divcrossoption2")
      y.className=""
      y=document.getElementById("divcrossoption3")
      y.className=""
      y=document.getElementById("divcrossoption4")
      y.className=""
      y=document.getElementById("divcrossoption5")
      y.className=""
      y=document.getElementById("divbuttoption1")
      y.className=""
      y=document.getElementById("divbuttoption2")
      y.className=""
      y=document.getElementById("divbuttoption3")
      y.className=""
      y=document.getElementById("divbuttoption4")
      y.className=""
      y=document.getElementById("divseamoption1")
      y.className=""
      y=document.getElementById("divseamoption2")
      y.className=""
      y=document.getElementById("divseamoption3")
      y.className=""
      y=document.getElementById("divseamoption4")
      y.className=""
      y=document.getElementById("divseamoption5")
      y.className=""
      y=document.getElementById("divseamoption6")
      y.className=""
      y=document.getElementById("divvac")
      y.className=""
      y=document.getElementById("divamps")
      y.className=""
      y=document.getElementById("divphase")
      y.className=""
      y=document.getElementById("divhz")
      y.className=""
      y=document.getElementById("divvac2")
      y.className=""
      y=document.getElementById("divamps2")
      y.className=""
      y=document.getElementById("divphase2")
      y.className=""
      y=document.getElementById("divhz2")
      y.className=""
      y=document.getElementById("divbudget")
      y.className=""
      y=document.getElementById("divname")
      y.className=""
      y=document.getElementById("divcompany")
      y.className=""
      y=document.getElementById("divemail")
      y.className=""
      y=document.getElementById("divphone")
      y.className=""
   }
   else
   {
      y=document.getElementById(elem)
      y.className=""
   }
}

//---------------------------------------------------------

// function to highlight a section if the validation fails
function makeHighlight(elem)
{
   y=document.getElementById(elem)
   y.className="highlight"
   makeShow("formerrormessage")
}


//---------------------------------------------------------

// Set the default machine type if it was set by the
// help me choose page
function setMachine(machine)
{
   //DEBUGGING
   //alert(machine)

   var y=document.getElementById("machine")
   var k=y.length
   var j=0
   while(j!=k)
   {
      if(machine==y.options[j].value)
      {
         y.options[j].selected=true
	 chooseWeld(machine)
	 break
      }
      j+=1
   }
}

//---------------------------------------------------------

// Check the URL for parameters passed from the help me choose page
function checkURL()
{
   var machine = decodeURIComponent(window.location.search.substring(9)) || ""
   machine = machine.replace(/</g, "&lt;").replace(/>/g, "&gt;")

   //DEBUGGING
   //alert("Onload is loaded")

   if(machine.length>0)
   {
      setMachine(machine)
   }
}

//---------------------------------------------------------

// Once the machine type has been chosen we show the 
// weld choices specific to the machine type
function chooseWeld(machine)
{
   // Get the weld element
   var x=document.getElementById("weld")

   // Hide everything in case we're changing something
   makeHide("all")

   // Decide which weld types to show based on machine type
   switch (machine)
   {
   case "rocker":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Cross Wire Weld", "cross")
      makeShow("divweld")
      break
   case "press":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Projection Weld", "projection")
      x[3]=new Option("Cross Wire Weld", "cross")
      makeShow("divweld")
      break
   case "bench":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Projection Weld", "projection")
      x[3]=new Option("Cross Wire Weld", "cross")
      x[4]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "seam":
      x.options.length=1
      x[1]=new Option("Cross Wire Weld", "cross")
      x[2]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "butt":
      x.options.length=1
      x[1]=new Option("Butt Weld", "butt", true, true)
      makeShow("divweld")
      chooseMat("butt")
      break
   case "inverter":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Projection Weld", "projection")
      x[3]=new Option("Cross Wire Weld", "cross")
      x[4]=new Option("Butt Weld", "butt")
      x[5]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "multi":
      makeShow("divconsult")
      break
   case "gun":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Projection Weld", "projection")
      x[3]=new Option("Cross Wire Weld", "cross")
      x[4]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "mini":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "special":
      makeShow("divconsult")
      break
   case "reconditioned":
      x.options.length=1
      x[1]=new Option("Spot Weld", "spot")
      x[2]=new Option("Projection Weld", "projection")
      x[3]=new Option("Cross Wire Weld", "cross")
      x[4]=new Option("Butt Weld", "butt")
      x[5]=new Option("Seam Weld", "seam")
      makeShow("divweld")
      break
   case "help":
      window.location=HELP_PAGE
      break
   case "part":
      window.location=PARTS_PAGE
      break
   default:
   }
}

//---------------------------------------------------------

// Make the material type select show up after we select
// a weld type, if they select a weld that can only do 
// certain types of materials we change accordingly
function chooseMat(weld)
{
   // Hide the bottom in case we're changing options here
   makeHide("bottom")

   // Hide the material selection in case we're changing options
   makeHide("divmat")

   // grab that select box
   var x = document.getElementById("material")

   // make sure we're on the default selection
   // in case things changed
   x.selectedIndex = 0

   // Add all the options in now
   x.options.length=1
   x[1] = new Option("Low Carbon Steel", "low")
   x[2] = new Option("High Carbon Steel", "high")
   x[3] = new Option("Stainless Steel", "stainless")
   x[4] = new Option("Aluminum", "aluminum")
   x[5] = new Option("Coated Sheet Metal", "coated")
   x[6] = new Option("Other", "other")
   
   // If we have butt weld or cross wire weld selected it
   // it should say "Coated Wire" instead of "Coated Sheet Metal"
   if(weld=="butt" || weld=="cross") 
   {
      x[5] = new Option("Coated Wire", "coatedwire")
   }

   // If we selected no weld type (the first option) don't show
   // the material list, otherwise...
   if(weld!="none")
   {
      // Show that puppy!
      makeShow("divmat")
   }
}

//---------------------------------------------------------

// Verify the material type in case we got something that 
// requires more explaination
function checkMat(material)
{
   // Hide the bottom parts in case we're changing options here
   makeHide("bottom")
   makeHide("divtypestainless")
   makeHide("divtypealuminum")

   if(material=="other")
   {
      makeShow("divother")
   }
   else if(material=="stainless")
   {
      makeShow("divtypestainless")
   }
   else if(material=="aluminum")
   {
      makeShow("divtypealuminum")
   }
   else if(material=="coated" || material=="coatedwire")
   {
      makeShow("divtypecoated")
   }
   
   // Put the rest of the stuff up now too
   showTheRest()
}

//---------------------------------------------------------

// When we select the material we can show the rest depending
// on which weld type was selected
function showTheRest()
{
   var y = document.getElementById("weld")
   var index = y.selectedIndex
   var weld = y.options[index].value

   switch (weld)
   {
   case "spot":
      makeShow("divspot")
      break
   case "projection":
      makeShow("divproject")
      break
   case "cross":
      makeShow("divcross")
      break
   case "butt":
      makeShow("divbutt")
      break
   case "seam":
      makeShow("divseam")
      break
   default:
      makeHide("divmat")
   }
   makeShow("divbottom")
   makeShow("divsubmit")
}

//---------------------------------------------------------

// When we select the type of projection weld we need a type
// for stud and nut
function projecttype(type)
{
   // Hide them in case they are already up there, we don't want both
   makeHide("divstudtype1")
   makeHide("divstudtype2")
   makeHide("divnuttype1")
   makeHide("divnuttype2")

   if(type=="stud")
   {
      makeShow("divstudtype1")
      makeShow("divstudtype2")
   }
   else if(type=="nut")
   {
      makeShow("divnuttype1")
      makeShow("divnuttype2")
   }
}

//----------------------------------------------------------

// When we select coated sheet metal, if we pick other for type
// we need to specify what that is
function coatedType(coatedtype)
{
   makeHide("bottom")
   
   makeShow("divtypecoated")

   if(coatedtype=="other")
   {
      makeShow("divcoatedother")
   }
   showTheRest()
}

//---------------------------------------------------------

// Validation of the form elements to see if we're ready to submit
// THIS REALLY SHOULD HAVE BEEN CODED BETTER, NEEDS FUNCTIONS
// TO SIMPLIFY IT
function validate_form(thisform)
{
   // unhighlight everything in case things have changed
   unHighlight("all")
   
   // get the weld type and the material type for later
   w=document.getElementById("weld")
   weld=w.value
   w=document.getElementById("material")
   material=w.value

   // success flag to be set to false if we find errors
   var success_flag=true
   
   // if...else to validate all different material options
   if(material=="stainless")
   {
      y=document.getElementById("stainlesstype")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divtypestainless")
         success_flag=false
      }
   }
   else if(material=="aluminum")
   { 
      y=document.getElementById("aluminumtype")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divtypealuminum")
         success_flag=false
      }
   }
   else if(material=="coated" || material=="coatedwire")
   { 
      y=document.getElementById("coatedtype")
      if(y.value=="other")
      {
         y=document.getElementById("coatedothertype")
         if(y.value==null || y.value=="")
         {
            makeHighlight("divcoatedother")
            success_flag=false
         }
      }
   }
   else if(material=="other")
   { 
      y=document.getElementById("othertype")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divother")
         success_flag=false
      }
   }

   // if...else to validate spot weld options
   if(weld=="spot")
   {      
      y=document.getElementById("spotoption1")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divspotoption1")
         success_flag=false
      }
      y=document.getElementById("spotoption2")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divspotoption2")
         success_flag=false
      }
      y=document.getElementById("spotoption3")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divspotoption3")
         success_flag=false
      }
      y=document.getElementById("spotoption4")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divspotoption4")
         success_flag=false
      }
      y=thisform.spotoption5
      if((y[0].checked==false) && (y[1].checked==false))
      {
         makeHighlight("divspotoption5")
         success_flag=false
      }        
      y=thisform.spotoption6
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false)
      {
         makeHighlight("divspotoption6")
         success_flag=false
      }
      y=thisform.spotoption7
      if(y[0].checked==false && y[1].checked==false)
      {
         makeHighlight("divspotoption7")
         success_flag=false
      }
   }
   else if(weld=="projection")  // validate projection weld options
   {
      
      y=document.getElementById("projectoption1")
      if(y.value=="stud")
      {
         y=document.getElementById("projectoption2")
         if(y.value=="" || y.value==null)
         {
            makeHighlight("divstudtype1")
	    makeHighlight("divstudtype2")
            success_flag=false
         }
      }
      else if(y.value=="nut")
      {
          y=document.getElementById("projectoption3")
          if(y.value==null || y.value=="")
          {
            makeHighlight("divnuttype1")
	    makeHighlight("divnuttype2")
            success_flag=false
          }
      }      
      y=document.getElementById("projectoption4")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divprojectoption4")
         success_flag=false
      }
      y=document.getElementById("projectoption5")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divprojectoption5")
         success_flag=false
      }
      y=document.getElementById("projectoption6")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divprojectoption6")
         success_flag=false
      }
      y=thisform.projectoption7
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false)
      {
         makeHighlight("divprojectoption7")
         success_flag=false
      }
   }
   else if(weld=="cross")  // validate cross wire weld options
   {
      y=document.getElementById("crossoption1")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divcrossoption1")
         success_flag=false
      }      
      y=document.getElementById("crossoption2")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divcrossoption2")
         success_flag=false
      }
      y=document.getElementById("crossoption3")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divcrossoption3")
         success_flag=false
      }
      y=document.getElementById("crossoption4")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divcrossoption4")
         success_flag=false
      }
      y=thisform.crossoption5
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false)
      {
         makeHighlight("divcrossoption5")
         success_flag=false
      }
   }
   else if(weld=="butt")  // validate butt weld options
   {
      y=document.getElementById("buttoption1")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divbuttoption1")
         success_flag=false
      }      
      y=thisform.buttoption2
      if(y[0].checked==false && y[1].checked==false)
      {
         makeHighlight("divbuttoption2")
         success_flag=false
      }
      y=document.getElementById("buttoption3")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divbuttoption3")
         success_flag=false
      }
      y=thisform.buttoption4
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false)
      {
         makeHighlight("divbuttoption4")
         success_flag=false
      }
   }
   else if(weld=="seam")  // validate seam weld options
   {  
      y=thisform.seamoption1
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false && y[3].checked==false)
      {
         makeHighlight("divseamoption1")
         success_flag=false
      }      
      y=document.getElementById("seamoption2")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divseamoption2")
         success_flag=false
      }
      y=document.getElementById("seamoption3")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divseamoption3")
         success_flag=false
      }
      y=document.getElementById("seamoption4")
      if(y.value==null || y.value=="")
      {
         makeHighlight("divseamoption4")
         success_flag=false
      }
      y=thisform.seamoption5
      if(y[0].checked==false && y[1].checked==false && y[2].checked==false)
      {
         makeHighlight("divseamoption5")
         success_flag=false
      }
      y=thisform.seamoption6
      if(y[0].checked==false && y[1].checked==false)
      {
         makeHighlight("divseamoption6")
         success_flag=false
      }
   }

   // validate the voltage options
   y=document.getElementById("vac")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divvac")
      makeHighlight("divvac2")
      success_flag=false
   } 
   y=document.getElementById("amps")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divamps")
      makeHighlight("divamps2")
      success_flag=false
   }
   y=document.getElementById("phase")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divphase")
      makeHighlight("divphase2")
      success_flag=false
   }
   y=document.getElementById("hz")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divhz")
      makeHighlight("divhz2")
      success_flag=false
   }
   
   // validate the contact info
   y=document.getElementById("name")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divname")
      success_flag=false
   } 
   y=document.getElementById("phone")
   if(y.value==null || y.value=="")
   {
      makeHighlight("divphone")
      success_flag=false
   }

   return success_flag
}

//---------------------------------------------------------
///////////////////////////////////////////////////////////
