
var clientRequest;
try
{
  clientRequest = new XMLHttpRequest();
}
catch( e )
{
  try
  {
    clientRequest = new ActiveXObject("Msxml2.XMLHTTP");
  }
  catch( e )
  {
    try
    {
      clientRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch( e )
    {
      alert("This browser does not support Ajax.");
    }
  }
}

var requiredSet = 0x001f;
var activeHelpBlock = "";
var activeHelpSpan = "";
var helpArray = new Array();
initHelpArray();

function helpEntry(ident, helpStr)
{
  this.id = ident;
  this.text = "<span id=\"" + ident + "\" class=\"formHelp\">";
  this.text += helpStr + "</span>";
}

function initHelpArray()
{
  var i = 0;
  helpArray[i++] = new helpEntry("KContactHelp", "Enter the name of person you contacted at Kaydon");
  helpArray[i++] =  new helpEntry("KEmailHelp", "Enter the email of your contact at Kaydon");
  helpArray[i++] =  new helpEntry("CompanyHelp", "Enter your company's name (required)");
  helpArray[i++] =  new helpEntry("DateHelp", "Date of form completion");
  helpArray[i++] =  new helpEntry("Addr1Help", "Enter your company's address");
  helpArray[i++] =  new helpEntry("PhoneHelp",
   "Enter the phone number of the person at your company" +
   " we should contact regarding this return request");
  helpArray[i++] =  new helpEntry("CAddr2Help", "Enter additional line of your company's address");
  helpArray[i++] =  new helpEntry("FaxHelp",
   "Enter the number of the fax machine we should use to" +
   " fax information to your company regarding this request");
  helpArray[i++] =  new helpEntry("CContactHelp",
   "Enter the name of the person at your company we should" +
   " contact regarding this return request (required)");
  helpArray[i++] =  new helpEntry("CEmailHelp",
   "Enter the email address of the person at your company we" +
   " should contact regarding this return request (required)");
  helpArray[i++] =  new helpEntry("OPOHelp", "Enter the number of parts that you want to return (required)");
  helpArray[i++] =  new helpEntry("QtyHelp", "Enter the number of parts in the original order");
  helpArray[i++] =  new helpEntry("RFCHelp", "Enter the estimated freight cost for returning the bearing");
  helpArray[i++] =  new helpEntry("PNHelp", "Enter the Kaydon part number (required)");
  helpArray[i++] =  new helpEntry("DCHelp", "Enter the date code found on the part");
  helpArray[i++] =  new helpEntry("UseMilHelp", "Check if you use the bearing in military applications");
  helpArray[i++] =  new helpEntry("UseComHelp", "Check if you use the bearing in comerical applications");
  helpArray[i++] =  new helpEntry("UseSemHelp",
   "Check if you use the bearing in semiconductor applications");
  helpArray[i++] =  new helpEntry("WhyHelp", "Enter the reason for returning the part");
  helpArray[i++] =  new helpEntry("CondHelp", "Enter the operational conditions during bearing failure");
  helpArray[i++] =  new helpEntry("EquipHelp", "Enter the type of equipment the bearing is used in");
  helpArray[i++] =  new helpEntry("HoursHelp", "Enter the number of hours the bearing was in use");
  helpArray[i++] =  new helpEntry("DebitYesHelp", "Check if you plan on debiting Kaydon for the return");
  helpArray[i++] =  new helpEntry("DebitNoHelp",
   "Check if you are not planing on debiting Kaydon for the return");
  helpArray[i++] =  new helpEntry("NPOHelp", "Enter the new purchase order number (required)");
  helpArray[i++] =  new helpEntry("LubHelp", "Enter the type of lubricant used on the bearing");
  helpArray[i++] =  new helpEntry("SemEquHelp", "Enter the type of equipment the bearing is used in");
  helpArray[i++] =  new helpEntry("CompHelp", "Enter details of the problem");
  helpArray[i++] =  new helpEntry("LifeHelp", "Enter bearing life cycle information");
  helpArray[i++] =  new helpEntry("TempHelp", "Enter temperature information");
  helpArray[i++] =  new helpEntry("PressHelp", "Enter pressure iformation");
  helpArray[i++] =  new helpEntry("GassHelp", "Enter process gasses iformation");
  helpArray[i++] =  new helpEntry("CleanHelp", "Enter cleaning information");
  helpArray[i++] =  new helpEntry("MSDSHelp", "Select file containing MSDS");
}

function setHelp(blkName, spanName)
{
  if( activeHelpBlock != blkName )
  {
    if( activeHelpBlock.length > 0)
    {
      var o = document.getElementById(activeHelpBlock);
      o.innerHTML = "";
      o.style.visibility = "hidden";
    }
    activeHelpBlock = blkName;
    var n = document.getElementById(blkName);
    n.style.visibility = "visible";
  }
  if( activeHelpSpan != spanName )
  {
    activeHelpSpan = spanName;
    for( var i = 0; i < helpArray.length; i++ )
    {
      if( helpArray[i].id == spanName )
      {
        document.getElementById(activeHelpBlock).innerHTML = helpArray[i].text;
        i = helpArray.length;
      }
    }
  }
  try
  {
    clientRequest.open("GET", "/Rma/?a=1", true);
    clientRequest.onreadystatechange = function()
    {
      if( clientRequest.readyState == 4 )
      {
        var xml = clientRequest.responseXML;
        var resp = xml.getElementsByTagName('v')[0].firstChild.data;
        document.cookie = "formagent" + "=" + escape(resp) +
                          "; path=/Rma; domain=.kaydonbearings.com";
      }
    }
    var cStr = document.cookie;
    var cSet = cStr.split(";");
    for( var i = 0; i < cSet.length; i++ )
    {
      clientRequest.setRequestHeader('Cookie', cSet[i]);
    }
    clientRequest.send(null);
  }
  catch (e)
  {
    window.status = "next cookie: " + e.name + " " + e.message + " ";
  }
}

function useDate(df)
{
  var cd = new Date();
  document.getElementById(df).value = cd.toUTCString();
}

function viewSegment(seg, state)
{
  var n = document.getElementById(seg);
  if( state.length == 0 )
  {
    n.innerHTML = "";
  }
  else
  {
    if( document.cookie.length == 0 )
    {
      alert("You need to enable cookies for this site.");
      return;
    }
    try
    {
      clientRequest.open("GET", state, true);
      clientRequest.onreadystatechange = function()
      {
        if( clientRequest.readyState == 4 )
        {
          n.innerHTML = clientRequest.responseText;
        }
      }
      var cStr = document.cookie;
      var cSet = cStr.split(";");
      for( var i = 0; i < cSet.length; i++ )
      {
        clientRequest.setRequestHeader('Cookie', cSet[i]);
      }
      clientRequest.send(null);
    }
    catch (e)
    {
      alert(" " + e.name + ". " + e.message);
    }
  }
}

function requiredBit(bi, yn)
{
  if( yn == true )
  {
    requiredSet |= bi;
    document.rmaInputForm.formSend.disabled = true;
  }
  else
  {
    requiredSet &= (~bi);
    if( requiredSet == 0 )
    {
      document.rmaInputForm.formSend.disabled = false;
    }
  }
}

function flagRequired(fid, bi)
{
  var f = document.getElementById(fid);
  if( f.value.length > 0 )
  {
    requiredBit(bi, false);
  }
  else
  {
    requiredBit(bi, true);
  }
}

function clearForm()
{
  viewSegment("NPODiv", "");
  viewSegment("PartC", "");
  requiredSet = 0x001f;
  document.rmaInputForm.formSend.disabled = true;
}
