
function FieldExists(frm, fieldName)
{
  for(i=0; i<frm.elements.length; i++) {
    if (frm.elements[i].name == fieldName) { 
      return true;
    }
  }
  return false;
}

// ContactInfo Object

function SetCookie(sName, sValue)
{
  var expired = new Date();
  expired.setTime(expired.getTime() + ( 2*31*24*60*60*1000 ) ); // expire in 2 months
  document.cookie = sName + "=" + sValue + "; expires=" + expired.toGMTString() + ";";
}

// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }

  // a cookie with the requested name does not exist
  return null;
}

// Delete the cookie with the specified name.
// Not used
function DelCookie(sName)
{
  document.cookie = sName + "=" + escape(sValue) + "; expires=Thu, 31 Dec 1991 23:59:59 GMT;";
}

function FillFields()
{
 try {
  theString = this.GetCookie(this.COOKIE_NAME);
  if (theString!=null && theString+""!="") {
    aInfo = theString.split(this.DELIMITER);    
    (aInfo[0]+""=="undefined" ? document.frm1.Name.value = "" : document.frm1.Name.value     = aInfo[0]);
    (aInfo[1]+""=="undefined" ? document.frm1.Company.value = "" : document.frm1.Company.value    = aInfo[1]);    
    (aInfo[2]+""=="undefined" ? document.frm1.address.value = "" : document.frm1.address.value    = aInfo[2]);
    (aInfo[3]+""=="undefined" ? document.frm1.address2.value = "" : document.frm1.address2.value    = aInfo[3]);
    (aInfo[4]+""=="undefined" ? document.frm1.address3.value = "" : document.frm1.address3.value    = aInfo[4]);
    (aInfo[5]+""=="undefined" ? document.frm1.city.value = "" : document.frm1.city.value    = aInfo[5]);
    (aInfo[6]+""=="undefined" ? document.frm1.state.value = "" : document.frm1.state.value    = aInfo[6]);
    (aInfo[7]+""=="undefined" ? document.frm1.post.value = "" : document.frm1.post.value    = aInfo[7]);
    (aInfo[8]+""=="undefined" ? document.frm1.country.value = "" : document.frm1.country.value    = aInfo[8]);
    (aInfo[9]+""=="undefined" ? document.frm1.phone.value = "" : document.frm1.phone.value    = aInfo[9]);
    (aInfo[10]+""=="undefined" ? document.frm1.fax.value  = "" : document.frm1.fax.value     = aInfo[10]);
    (aInfo[11]+""=="undefined" ? document.frm1.url.value  = "" : document.frm1.url.value     = aInfo[11]);
    (aInfo[12]+""=="undefined" ? document.frm1.email.value = "" : document.frm1.email.value    = aInfo[12]);
    // where there is no selectperson ("OEM Excess")
    if (FieldExists(document.frm1, "salesperson")) {
      (aInfo[13]+""=="undefined" ? document.frm1.salesperson.selectedIndex = 0 : document.frm1.salesperson.selectedIndex = parseInt(aInfo[13]));
    } else {
      (aInfo[13]+""=="undefined" ? this.salesperson = 0 : this.salesperson = parseInt(aInfo[13])); 
    }
    // for "Sell OEM Excess"
    if (FieldExists(document.frm1, "Title")) {
      (aInfo[14]+""=="undefined" ? document.frm1.Title.value = "" : document.frm1.Title.value = aInfo[14]);
    } else {
      (aInfo[14]+""=="undefined" ? this.title = "" : this.title = aInfo[14]); 
    }
  }
 } catch (e) { }
}

function StoreFields()
{
   theString = escape(document.frm1.Name.value) + this.DELIMITER +
               escape(document.frm1.Company.value) + this.DELIMITER +
               escape(document.frm1.address.value) + this.DELIMITER +
               escape(document.frm1.address2.value) + this.DELIMITER +
               escape(document.frm1.address3.value) + this.DELIMITER +
               escape(document.frm1.city.value) + this.DELIMITER +
               escape(document.frm1.state.value) + this.DELIMITER +
               escape(document.frm1.post.value) + this.DELIMITER +
               escape(document.frm1.country.value) + this.DELIMITER +
               escape(document.frm1.phone.value) + this.DELIMITER +
               escape(document.frm1.fax.value) + this.DELIMITER +
               escape(document.frm1.url.value) + this.DELIMITER +
               escape(document.frm1.email.value);
   // where there is no selectperson ("OEM Excess")
   if (FieldExists(document.frm1, "salesperson")) {
      theString += this.DELIMITER + escape(document.frm1.salesperson.selectedIndex);
   } else {
      theString += this.DELIMITER + this.salesperson;
   }
    // for "Sell OEM Excess"
   if (FieldExists(document.frm1, "Title")) {
      theString += this.DELIMITER + escape(document.frm1.Title.value);
   } else {
      theString += this.DELIMITER + this.title;
   }
   this.SetCookie(this.COOKIE_NAME, theString);
}

function CheckReqFields()
{
   if (document.frm1.Name.value+""=="" ||
       document.frm1.Company.value+""=="" ||
       document.frm1.address.value+""=="" ||
       document.frm1.phone.value+""=="" ||
       document.frm1.fax.value+""=="" ||
     //  document.frm1.salesperson.value+""=="" ||
       document.frm1.email.value+""=="")
         return false;
   else  return true;
}

function toStringFunc()
{
   return "D.R. Components ContactInfo. Cookie '"+ this.COOKIE_NAME + "'";
}

// JS Object
function ContactInfo()
{
   this.DELIMITER   = "\\\\ ";
   this.COOKIE_NAME = "ContactInfo";
   this.salesperson = 0;
   this.title       = "";

   this.toString    = toStringFunc;
   this.SetCookie   = SetCookie;
   this.GetCookie   = GetCookie;
   this.DelCookie   = DelCookie;
   this.FillFields  = FillFields;
   this.StoreFields = StoreFields;
   this.CheckReqFields = CheckReqFields;
}

