function setStyleByClass(t,c,p,v)
{
var elements;
  if(t == '*')
  {
    // '*' not supported by IE/Win 5.5 and below
    elements = (ie) ? document.all : document.getElementsByTagName('*');
  }
  else
  {
    elements = document.getElementsByTagName(t);
  }
  for(var i = 0; i < elements.length; i++)
  {
  var node = elements.item(i);
    for(var j = 0; j < node.attributes.length; j++)
    {
      if(node.attributes.item(j).nodeName == 'class')
      {
        if(node.attributes.item(j).nodeValue == c)
        {
          eval('node.style.' + p + " = '" +v + "'");
        }
      }
    }
  }
}

function toMoney(val)
{
var sCheck = new String(val);
var sAdd = "";

  var i = sCheck.length;
  if (i == 0)
  {
    return("0.00");
  }
  
  var j = sCheck.indexOf(".");
  if (j == -1) // Easy no decimals at all
  {
    sAdd = ".00";
  } 
  else
  {
    i = i - j;
    if (i == 2)
    {
      sAdd = "0";
    }
  }
  return(sCheck + sAdd);
}

function Update()
{
var TheForm = document.Registration;
var Total   = 0.00;

  TheForm.exVAT.disabled = true;
  TheForm.VAT.disabled   = true;
  TheForm.Total.disabled = true;

  var exVAT = prices[TheForm.Service.value];
  
  if (TheForm.Service.value == "1")
  {
    if (TheForm.M_IDM[1].checked)
      exVAT = exVAT + DMS;
  }
  var VAT = Math.round(exVAT * VATRate) / 100;
  
  Total = toMoney(exVAT + VAT);
  TheForm.exVAT.value = "£" + toMoney(exVAT);
  TheForm.VAT.value   = "£" + toMoney(VAT);
  TheForm.Total.value = "£" + Total;

/*
  WorldPay Stuff
*/
  TheForm.amount.value = Total;
  TheForm.M_Service.value = SvLevel[TheForm.Service.value]
  TheForm.desc.value   = "ITAccounting " + SvLevel[TheForm.Service.value] + " Service";
  if (TheForm.Service.value == "1" && TheForm.M_IDM[1].checked)
  {
    TheForm.desc.value = TheForm.desc.value + " with Internet Document Management";
  }
  TheForm.cartId.value = TheForm.postcode.value + SvLevel[TheForm.Service.value];
/*
  End Of WorldPay Specifics...
*/
    
  if (TheForm.Service.value == "1")
  {
    setStyleByClass("SPAN", "hidable", "display", "inline");    
    setStyleByClass("SPAN", "showable", "display", "none");    
  }
  else
  {
    setStyleByClass("SPAN", "hidable", "display", "none");    
    setStyleByClass("SPAN", "showable", "display", "inline");    
  }
}

function validate(theForm)
{
  if (theForm.M_sFirstName.value == "")
  {
    alert("Please enter a value for the \"First Name\" field.");
    theForm.M_sFirstName.focus();
    return (false);
  }

  if (!isAllAlpha(theForm.M_sFirstName.value))
  {
    alert("Please enter only letter and whitespace characters in the \"First Name\" field.");
    theForm.M_sFirstName.focus();
    return (false);
  }

  if (theForm.M_sSurname.value == "")
  {
    alert("Please enter a value for the \"Surname\" field.");
    theForm.M_sSurname.focus();
    return (false);
  }

  if (!isAllAlpha(theForm.M_sSurname.value))
  {
    alert("Please enter only letter and whitespace characters in the \"Surname\" field.");
    theForm.M_sSurname.focus();
    return (false);
  }

  if (!isAllAlpha(theForm.M_Contact_Title.value))
  {
    alert("Please enter only letter and whitespace characters in the \"Contact_Title\" field.");
    theForm.M_Contact_Title.focus();
    return (false);
  }

  if (!isAllAlpha(theForm.M_Contact_Organization.value))
  {
    alert("Please enter only letter and whitespace characters in the \"Contact_Organization\" field.");
    theForm.M_Contact_Organization.focus();
    return (false);
  }

  if (theForm.M_Street.value == "")
  {
    alert("Please enter a value for the \"Street\" field.");
    theForm.M_Street.focus();
    return (false);
  }

  if (!isAllAlphaNumeric(theForm.M_Street.value))
  {
    alert("Please enter only letter, digit and whitespace characters in the \"Street\" field.");
    theForm.M_Street.focus();
    return (false);
  }

  if (theForm.M_City.value == "")
  {
    alert("Please enter a value for the \"City\" field.");
    theForm.M_City.focus();
    return (false);
  }

  if (!isAllAlpha(theForm.M_City.value))
  {
    alert("Please enter only letter and whitespace characters in the \"City\" field.");
    theForm.M_City.focus();
    return (false);
  }

  if (theForm.postcode.value == "")
  {
    alert("Please enter a value for the \"Post Code\" field.");
    theForm.postcode.focus();
    return (false);
  }

  if (theForm.postcode.value.length > 12)
  {
    alert("Please enter at most 12 characters in the \"Post Code\" field.");
    theForm.postcode.focus();
    return (false);
  }

  if (!isAllAlphaNumeric(theForm.postcode.value))
  {
    alert("Please enter only letter, digit and whitespace characters in the \"Post Code\" field.");
    theForm.postcode.focus();
    return (false);
  }

  if (theForm.tel.value == "")
  {
    alert("Please enter a value for the \"Daytime Phone\" field.");
    theForm.tel.focus();
    return (false);
  }

  if (theForm.tel.value.length > 25)
  {
    alert("Please enter at most 25 characters in the \"Daytime Phone\" field.");
    theForm.tel.focus();
    return (false);
  }

  theForm.tel.value = StripLeading(StripTrailing(theForm.tel.value, " "), " ");
  var checkStr = theForm.tel.value;
  checkStr = ReplaceChar(checkStr, " ", "");

  if (isNaN(checkStr))
  {
    alert("Please enter only digit and whitespace characters in the \"Daytime Phone\" field.");
    theForm.tel.focus();
    return (false);
  }
  
  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"e-mail\" field.");
    theForm.email.focus();
    return (false);
  }

  theForm.address.value = theForm.M_Street.value;  
  if (theForm.M_Address2.value != "")
  {
    theForm.address.value += "\r" + theForm.M_Address2.value;
  }
  theForm.address.value += "\r" + theForm.M_City.value;
  theForm.address.value += "\r" + theForm.M_County.value;

  theForm.name.value = theForm.M_sFirstName.value + " " + theForm.M_sSurname.value;

  return (true);
}
