var test_timestamp = '2010-05-30 14:02:37';

function changeCheckout () {
  var stayOnTop = false;
  var is_paypal = false;
  var is_amazon = false;
  var list;
  var n;
  var tds;

  is_paypal = (typeof paypal_page != "undefined") && paypal_page;
  is_amazon = (typeof amazon_page != "undefined") && amazon_page;
  var is_cc_page = !(is_paypal || is_amazon);

  if (is_amazon) {
    if (document.forms.checkout && document.forms.checkout.innerHTML.match(/amazon/i)) {
      document.forms.checkout.style.paddingTop = "1.5em";
    }
  }

  var div = document.getElementById("_stVar07CartTitle");
  if (div) {
    //div.style.display = "none";
    div.style.textAlign = "center";
    div.style.display = "inline-block";
    div.style.width = "100%";
    div.innerHTML = "<img alt='Secure Checkout' src='"+_checkout_page_title+"' />";
  }

  //alert("Reset");cc
  try {
    document.body.style.backgroundImage = "url("+_cc_cvv_image+")";
    document.body.style.backgroundImage = "url("+_amex_cvv_image+")";
    document.body.style.backgroundImage = "none";
  } catch(e) {}

  var cart = document.getElementById("_stVar06Table");
  var cartParent = cart;
  if (cart) {
    if (stayOnTop && is_cc_page) {
      list = cart.getElementsByTagName("table");
      if (list) {
        for (n=0;n<list.length;n++) {
          if (list.item(n).getAttribute("width") == "540") {
            list.item(n).setAttribute("width","350");
          }
        }
      }
      list = cart.getElementsByTagName("img");
      if (list) {
        for (n=0;n<list.length;n++) {
          if (list.item(n).getAttribute("width") == "540") {
            list.item(n).setAttribute("width","350");
          }
        }
      }
    }
// AGREGAMOS CODIGO PARCHE No 238467462
/*
    var pote = document.getElementById("_stVar06Table");
    var freddy = pote.getElementById("table");

    for (n=0;n<freddy.length;n++) {
     if (freddy.item(n).getAttribute("width") == "570") {
       freddy.item(n).setAttribute("width","350");
      }
     }
*/
//


    cart = cart.getElementsByTagName("table");

    if (cart && cart.item(0)) {
      cart = cart.item(0);
    }
    else {
      cart = null;
    }
  }

  if (cart) {
    if (!is_cc_page) {
      cart.style.marginTop = "1em";
    }

    list = cart.getElementsByTagName("h3");
    if (list && list.item(0)) {
      list.item(0).style.display = "none";
    }

    list = cart.getElementsByTagName("tr");
    if (list && list.item(0)) {
      list.item(0).style.display = "none";
    }

    var thead = createHeader(_cart_summary,"cart");
    if (thead && cart.firstChild) {
      cart.insertBefore(thead,cart.firstChild);
    }
    else if (thead) {
      cart.appendChild(thead);
    }
  }

  var form = document.getElementById("_stVar07Form");
  var formParent = form;

// AQUI POSIBLEMENTE ESTE EL PEO

  if (form) {
    if (!stayOnTop && is_cc_page) {
      list = form.getElementsByTagName("table");
      if (list) {
        for (n=0;n<list.length;n++) {
          if (list.item(n).getAttribute("width") == "540") {
            list.item(n).setAttribute("width","350");
          }
          else if (!list.item(n).getAttribute("width")) {
            list.item(n).setAttribute("width","350");
          }
        }
      }


/*
      list = form.getElementsByTagName("img");
      if (list) {
       for (n=0;n<list.length;n++) {
	if (list.item(n).getAttribute("width") == "540") {
            list.item(n).setAttribute("width","350");
          }
        }
      }
*/
    }

// FIN DEL CODIGO TENTATIVO
    form = form.getElementsByTagName("table");

    if (form && form.item(0)) {
      form = form.item(0);
    }
    else {
      form = null;
    }
  }

  if (form) {
    list = form.getElementsByTagName("h3");
    if (list && list.item(0)) {
      list.item(0).style.display = "none";
    }

    list = form.getElementsByTagName("b");
    if (list && list.item(0)) {
      list.item(0).style.display = "none";
    }

    list = form.getElementsByTagName("tr");
    if (list && list.item(0)) {
      list.item(0).style.display = "none";
    }


    var images = form.getElementsByTagName("img");
    if (images) {
      for (n=0;n<images.length;n++) {
        if (images.item(n).src.match(/_stPlaceOrder.jpg/i)) {
          images.item(n).src = _end_checkout_btn;
          var link = images.item(n).parentNode;
          if (link && (link.nodeName == "A")) {
            link.href = "javascript:if(ValidateNewCheckoutForm()){document.checkout.submit()}";
          }
        }
      }
    }

    var inner;

    if (is_cc_page) {
    inner = form.getElementsByTagName("form");
    }
    else {
    inner = null;
    }

    sleep(2);

    if (inner && inner.item(0)) {
      inner = inner.item(0);

      inner.style.margin = "0";
      var cvv_field = document.getElementsByName("x_card_code");
      if (cvv_field && cvv_field.item(0)) {
        remove(cvv_field.item(0));
      }

      var trSections = [];

      list = inner.getElementsByTagName("tr");
      if (list) {
        for (n=0;n<list.length;n++) {
          if (list.item(n).innerHTML.match(/email/i)) {
            changeTdContents(list.item(n),/email address:/ig,"Your Email:");
            trSections["email"] = list.item(n);
          }
          else if (list.item(n).innerHTML.match(/first/i)) {
            changeTdContents(list.item(n),/first name:/ig,"Name on Card:");
            trSections["name"] = list.item(n);
          }
          else if (list.item(n).innerHTML.match(/fax number/i)) {
            trSections["fax"] = list.item(n);
            list.item(n).id = "faxOption";
            _using_fax = true;
          }
          else if (list.item(n).innerHTML.match(/country/i)) {
            changeTdContents(list.item(n),/country:/ig,"Your Location:");
            trSections["location"] = list.item(n);
          }
          else if (list.item(n).innerHTML.match(/postal/i)) {
            changeTdContents(list.item(n),/zip\/postal code:/ig,"Zip or Postal Code:");
            trSections["zipcode"] = list.item(n);
          }
          else if (list.item(n).innerHTML.match(/credit card number/i)) {
            changeTdContents(list.item(n),/credit card number:/ig,"Card Number:");
            trSections["card"] = list.item(n);
          }
          else if (list.item(n).innerHTML.match(/expiration/i)) {
            trSections["expiration"] = list.item(n);
          }

          list.item(n).style.display = "none";
        }

        var fieldNames = ["location","zipcode","name","email","card","expiration","fax"];
        for (n=0;n<fieldNames.length;n++) {
          var fieldName = fieldNames[n];
          if (trSections[fieldName]) {
            var parent = trSections[fieldName].parentNode;

            if (fieldName == "card") {
              changeTdContents(trSections[fieldName],/<input/i,"<div id='cc-images' style='text-align:left;margin-bottom:5px;width:225px'>"+
                "<img id='cc-visa' style='margin-right:16px;' src='"+_cc_images_path+"visa.gif' />"+
                "<img id='cc-mastercard' style='margin-right:16px' src='"+_cc_images_path+"mastercard.gif' />"+
                "<img id='cc-amex' style='margin-right:16px' src='"+_cc_images_path+"amex.gif' />"+
                "<img id='cc-discover' src='"+_cc_images_path+"discover.gif' />"+
                "</div>"+
                "<div style='display:none' id='cc-not-supported'>Invalid credit card or not supported</div>"+
                "<input style='width:221px;margin-right:0px' id='cc-field' onchange=\'validateCC()' ");

              tds = trSections[fieldName].getElementsByTagName("td");
              if (tds) {
                for (p=0;p<tds.length;p++) {
                  tds.item(p).style.verticalAlign = "bottom";
                  tds.item(p).style.paddingBottom = "5px";
                }
              }
            }
            else if (fieldName == "fax") {
              changeTdContents(trSections[fieldName],/<input/i,"<br /><input style='width:221px' ");
              changeTdContents(trSections[fieldName],/<select/i,"<select style='width:221px' ");
              trSections[fieldName].id = "faxOption";

              tds = trSections[fieldName].getElementsByTagName("td");
              if (tds) {
                for (p=0;p<tds.length;p++) {
                  tds.item(p).style.verticalAlign = "bottom";
                  tds.item(p).style.paddingBottom = "5px";
                }
              }
            }
            else if (fieldName != "expiration") {
              changeTdContents(trSections[fieldName],/<input/i,"<input style='width:221px' ");
              changeTdContents(trSections[fieldName],/<select/i,"<select style='width:221px' ");
            }

            trSections[fieldName].style.display = "";
            parent.removeChild(trSections[fieldName]);
            parent.appendChild(trSections[fieldName]);
          }
        }

        var newTr = document.createElement("tr");
        var faxOption = document.getElementById("faxOption");
        if (faxOption) {
          faxOption.parentNode.insertBefore(newTr,faxOption);
        }
        else if (list.item(0)) {
          list.item(0).parentNode.appendChild(newTr);
        }
        else {
          newTr = null;
        }

        var tdTitle = document.createElement("td");
        var tdCards = document.createElement("td");
        if (newTr && tdTitle && tdCards) {
          tdTitle.innerHTML = "Security Code:";
          tdCards.innerHTML = "<input type='text' name='x_card_code' id='x_card_code' size='4' maxlength='4' onkeydown='return restrictCharacters(event);' onkeyup='securityCode();'>"+
            "<div id='cvvHover' style='display:inline;position:absolute;z-index:100;font-family:Lucida Grande,Helvetica,Arial,Verdana,sans-serif;font-size:10px' "+
            "onmouseover='javascript:fadeCVV(0)' "+
            "onmouseout='javascript:fadeCVV(1)' "+
            ">"+
            "<img style='position:absolute;z-index:200;margin:3px 6px' src='"+_question_image+"' />"+
            "<div id='cvvDetails' style='z-index:150;display:none;margin-left:2px;margin-top:-0px;background:#fff;border:1px solid #ccc;border-bottom:1px solid #666;border-right:1px solid #666;padding:4px 0 8px 10px;width:392px'>"+
            "<div style='clear:both'>&nbsp;&nbsp;&nbsp;&nbsp;Where&nbsp;to&nbsp;find&nbsp;your&nbsp;security&nbsp;code</div>"+
            "<div style='color:#666;font-size:inherit;float:left;width:190px'>"+
            "Visa/MasterCard/Discover<br>"+
            "<img src='"+_cc_cvv_image+"' />"+
            "</div>"+
            "<div style='color:#666;font-size:inherit;float:left;width:190px'>"+
            "American Express<br>"+
            "<img src='"+_amex_cvv_image+"' />"+
            "</div>"+
            "<div style='clear:both'></div>"+
            "</div>"+
            "</div>";

          newTr.appendChild(tdTitle);
          newTr.appendChild(tdCards);
        }

        // Check web browser
        try {
          var browser = document.createElement("input");
          if (browser &&  document.forms.checkout) {
            browser.type = "hidden";
            browser.name = "x_ilrg_finished";
            browser.value = "true";

            document.forms.checkout.appendChild(browser);
          }
        } catch (e) {}
      }
    }

    var thead = createHeader(is_paypal ? _secure_checkout_paypal : (is_amazon ? _secure_checkout_amazon : _secure_checkout),"lock");
    if (thead && form.firstChild) {
      form.insertBefore(thead,form.firstChild);
    }
    else if (thead) {
      form.appendChild(thead);
    }
  }

  if (!is_cc_page) {
    return;
  }

  var submitButton = document.getElementById("_stVar07Submit");
  if (submitButton) {
    submitButton.style.display = "block";
    submitButton.style.marginTop = "1em";
  }

  if (cartParent && cart) {
    if (document.forms.hiddenPayPal && document.forms.hiddenAmazon) {
      var otherCheckout = "<table style='width:180px;margin-left:0.5em;position:relative;font-family:arial,helvetica,sans-serif,geneva;text-align:center' cellpadding='0' cellspacing='0' id='otherCheckout'>"+
        "<tbody>"+
        "<tr>"+
        "<td style='background:url("+_border_left+") repeat-y'></td>"+
        "<td style='padding:0.5em 0;text-align:left;'>"+
        "<p style='text-align:center;'>PayPal Checkout <a href='javascript:document.forms.hiddenPayPal.submit()'><img style='border:none;vertical-align:-12%' src='"+_arrow_btn+"' alt='Paypal' title='PayPal Checkout' /></a></p>"+
        "<p style='text-align:center'><a href='javascript:document.forms.hiddenPayPal.submit()'><img style='border:none' src='"+_paypal_checkout_2p_btn+"' alt='Paypal' title='PayPal Checkout' /></a></p>"+
        "<p style='text-align:center'>Amazon Checkout <a href='javascript:document.forms.hiddenAmazon.submit()'><img style='border:none;vertical-align:-12%' src='"+_arrow_btn+"' alt='Amazon' title='Amazon Checkout' /></a></p>"+
        "<p style='text-align:center'><a href='javascript:document.forms.hiddenAmazon.submit()'><img style='border:none' src='"+_amazon_checkout_2p_btn+"' alt='Amazon' title='Amazon Checkout' /></a></p>"+
        "</td>"+
        "<td style='background:url("+_border_right+") repeat-y 100% 0'></td>"+
        "</tr>"+
        "<tr style='background:url("+_border_bottom+") repeat-x;'><td style='height:15px;background:url("+_round_corner_bottom_left+") no-repeat'></td><td>&nbsp;</td><td style='height:15px;background:url("+_round_corner_bottom_right+") no-repeat 100% 0'></td></tr>"+
        "</tbody>"+
        "</table>";
    }
    else {
      var otherCheckout = "<table style='width:180px;margin-left:0.5em;position:relative;font-family:arial,helvetica,sans-serif,geneva;text-align:center' cellpadding='0' cellspacing='0' id='otherCheckout'>"+
        "<tbody>"+
        "<tr>"+
        "<td style='background:url("+_border_left+") repeat-y'></td>"+
        "<td style='padding:0.5em 0;text-align:left;'>"+
        "<p style='text-align:center;'>PayPal Checkout <a href='https://www.ilrg.com/checkout.php?paypal=true'><img style='border:none;vertical-align:-12%' src='"+_arrow_btn+"' alt='Paypal' title='PayPal Checkout' /></a></p>"+
        "<p style='text-align:center'><a href='https://www.ilrg.com/checkout.php?paypal=true'><img style='border:none' src='"+_paypal_checkout_2p_btn+"' alt='Paypal' title='PayPal Checkout' /></a></p>"+
        "<p style='text-align:center'>Amazon Checkout <a href='https://www.ilrg.com/checkout.php?amazon=true'><img style='border:none;vertical-align:-12%' src='"+_arrow_btn+"' alt='Amazon' title='Amazon Checkout' /></a></p>"+
        "<p style='text-align:center'><a href='https://www.ilrg.com/checkout.php?amazon=true'><img style='border:none' src='"+_amazon_checkout_2p_btn+"' alt='Amazon' title='Amazon Checkout' /></a></p>"+
        "</td>"+
        "<td style='background:url("+_border_right+") repeat-y 100% 0'></td>"+
        "</tr>"+
        "<tr style='background:url("+_border_bottom+") repeat-x;'><td style='height:15px;background:url("+_round_corner_bottom_left+") no-repeat'></td><td>&nbsp;</td><td style='height:15px;background:url("+_round_corner_bottom_right+") no-repeat 100% 0'></td></tr>"+
        "</tbody>"+
        "</table>";
    }

    remove(formParent);

    cartParent.innerHTML =
      "<table>"+
        "<tr>"+
          "<td"+(stayOnTop ? "" : " colspan='2'")+" style='padding-top:1em'>"+
            cartParent.innerHTML+
          "</td>"+
          (stayOnTop ? "<td style='padding-top:1em;vertical-align:top'>"+otherCheckout+"</td>" : "")+
        "</tr>"+
        "<tr>"+
          "<td"+(stayOnTop ? " colspan='2'" : "")+" style='padding-top:1em"+(_is_ie55 ? ";width:380px;" : "")+"'>"+
            (formParent ? formParent.innerHTML : "")+
          "</td>"+
          (stayOnTop ? "" : "<td style='padding-top:1em;vertical-align:top'>"+otherCheckout+"</td>")+
        "</tr>"+
      "</table>";

    var thead = createHeader(_other_checkout_options);
    var otherCheckout = document.getElementById("otherCheckout");
    if (thead && otherCheckout && otherCheckout.firstChild) {
      otherCheckout.insertBefore(thead,otherCheckout.firstChild);
    }
    else if (thead && otherCheckout) {
      otherCheckout.appendChild(thead);
    }
  }
}

function replaceLayout () {
  var header = document.getElementById("newHeader");
  var n;
  var list;
  if (header) {
    header.style.background = "transparent url("+_cc_images_path+"Header1px.jpg) scroll repeat-x 0% 0%";
    header.style.height = "53px";
    header.style.padding = "3px 7px 3px 3px";

    list = header.getElementsByTagName("span");
    var logo = null;
    var search = null;
    var phone = null;

    if (list) {
      logo = list.item(0);
      search = list.item(1);
      phone = list.item(2);
    }

    if (logo) {
      if (_is_ie55) {
        logo.innerHTML = "<a href='http://www.ilrg.com/'><span style=\"display:inline-block;width:171px;height:43px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"logo.png', sizingMethod='scale')\"></span></a>";
      }
      else {
        logo.innerHTML = "<a href='http://www.ilrg.com/'><img style='border:none' src='"+_cc_images_path+"logo.png' alt='publiclegalforms.com' title='publiclegalforms.com' /></a>";
      }
    }

    if (search) {
      search = search.getElementsByTagName("input");
    }

    if (search) {
      for (var n=0;n<search.length;n++) {
        if (search.item(n).type == "image") {
          search.item(n).src = _cc_images_path+"search.png";

          if (_is_ie55) {
            search.item(n).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
            search.item(n).parentNode.innerHTML = "<span style=\"display:inline-block;width:112px;height:22px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"search.png', sizingMethod='scale')\">"+search.item(n).parentNode.innerHTML+"</span>";
          }
        }
      }
    }

    if (phone) {
      if (_is_ie55) {
        phone.innerHTML = "<span style=\"display:inline-block;width:171px;height:43px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"phone.png', sizingMethod='scale')\"></span>";
      }
      else {
        phone.innerHTML = "<img src='"+_cc_images_path+"phone.png' alt='Toll-free: 1-800-987-9087' title='Toll-free: 1-800-987-9087' />";
      }
      phone.style.marginTop = "-3px";
    }
  }

  var crumb = null;
  var container = document.getElementsByTagName("table");
  if (container && container.item(0)) {
    container = container.item(0);
  }
  else {
    container = null;
  }

  if (container) {
    container = container.getElementsByTagName("tr");
    if (container && container.item(2) && container.item(2).firstChild) {
      container.item(2).firstChild.style.background = "transparent url("+_cc_images_path+"Header1px.jpg) scroll repeat-x 0px -53px";
      container.item(2).firstChild.style.height = "21px";

      if (container.item(2).firstChild.firstChild) {
        container.item(2).firstChild.firstChild.style.color = "#224271";
        container.item(2).firstChild.firstChild.style.paddingTop = "3px";
      }
    }
    if (container && container.item(3)) {
      container = container.item(3);
    }
    else {
      container = null;
    }
  }

  if (container) {
    if (!_is_ie55 && !document.URL.match(/(cart\.php|checkout\.php)/i) && container.childNodes[1]) {
      container.childNodes[1].style.paddingLeft = "6px";
      container.childNodes[1].style.paddingRight = "6px";

      var tribal = document.getElementById("_stTribal");
      if (tribal) {
        tribal.style.marginLeft = "-6px";
        tribal.style.marginRight = "-6px";
      }
    }
    if (container.childNodes[0]) {
      container.childNodes[0].style.backgroundImage = "url("+_cc_images_path+"leftnav-bgfade.jpg)";
      container.childNodes[0].style.paddingTop = "0px";
    }

    if (container.childNodes[2]) {
      var safe = container.childNodes[2];
      safe.style.paddingTop = "5px";

      var div = document.createElement("div");
      if (div && safe.childNodes.length >= 4) {
        var mcafee = safe.childNodes[0];
        var truste = safe.childNodes[1];
        var verisign = safe.childNodes[2];

        remove(mcafee);
        remove(truste);
        remove(verisign);

        div.appendChild(mcafee);
        div.appendChild(truste);
        div.appendChild(verisign);

        div.style.background = "transparent url("+_cc_images_path+"testimonial-box-head-notitle.png) scroll no-repeat 3px 0%";
        div.style.paddingTop = "5px";
        div.innerHTML = "<div style='padding-bottom:5px;background:transparent url("+_cc_images_path+"testimonial-box-tail.png) scroll no-repeat 3px 100%'><div style='background:transparent url("+_cc_images_path+"testimonial-box-body.png) scroll repeat-y 3px 0%'>"+div.innerHTML+"</div></div>";
        safe.insertBefore(div,safe.firstChild);

        var testimonials = safe.getElementsByTagName("center");
        if (testimonials && testimonials.item(0)) {
          testimonials = testimonials.item(0);
        }
        else {
          testimonials = null;
        }

        if (testimonials) {
          testimonials = testimonials.getElementsByTagName("span");
          if (testimonials && testimonials.item(0)) {
            testimonials = testimonials.item(0);
          }
          else {
            testimonials = null;
          }
        }

        if (testimonials) {
          if (_is_ie55) {
            testimonials.innerHTML = "<a href='http://www.ilrg.com/forms/testimonials.html' style='cursor:pointer'><span style=\"block:inline-block;margin-top:7px;width:120px;height:492px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"testimonial.png',sizingMethod='scale')\"></span></a><a href='http://www.ilrg.com/forms/testimonials.html' style='display:inline-block;width:120px;text-align:center'>More Testimonials</a>";
          }
          else {
            testimonials.innerHTML = "<a href='http://www.ilrg.com/forms/testimonials.html'><img style='border:none;margin-top:4px;' src='"+_cc_images_path+"testimonial.png' alt='Testimonials' title='Testimonials' /></a><a href='http://www.ilrg.com/forms/testimonials.html' style='display:inline-block;margin-top:3px;width:120px;text-align:center'>More Testimonials</a>";
          }
        }
      }
    }

    list = container.getElementsByTagName("strong");
    if (list) {
      if (list.item(2)) {
        crumb = list.item(2).parentNode;
      }
    }
  }

  if (crumb) {
    for (n=0;n<crumb.parentNode.childNodes.length;n++) {
      if (crumb.parentNode.childNodes[n].nodeName.match(/br/i)) {
        crumb.parentNode.childNodes[n].style.display = "none";
        break;
      }
    }

    if (document.URL.match(/(forms\/$|forms\/index.html$|\/searchforms\/)/i)) {
      var tribal = document.getElementById("_stTribal");
      if (tribal) {
        for (n=0;n<tribal.childNodes.length;n++) {
          if (tribal.childNodes[n].nodeName.match(/br/i)) {
            tribal.childNodes[n].style.display = "none";
            break;
          }
        }
      }
    }

    if (crumb.childNodes[0]) {
      remove(crumb.childNodes[0]);
      /*crumb.childNodes[0].style.display = "inline-block";
      crumb.childNodes[0].style.cssFloat = "left";*/
    }
    if (crumb.childNodes[1]) {
      remove(crumb.childNodes[1]);
      /*crumb.childNodes[1].style.display = "inline-block";
      crumb.childNodes[1].style.cssFloat = "left";*/
    }

    var list = crumb.getElementsByTagName("br");
    if (list) {
      for (var n=0;n<list.length;n++) {
        list.item(n).style.display = "none";
      }
    }

    var newCrumb = new Array();
    var list = crumb.getElementsByTagName("a");
    if (list) {
      if (_is_ie55) {
        for (var n=1;n<list.length;n++) {
          newCrumb.push("<a href=\""+list.item(n).href+"\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-body.png',sizingMethod='scale'); display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;\">"+(list.item(n).innerHTML.match(/forms/i) ? "forms menu" : list.item(n).innerHTML)+"</a>");
        }

        if (document.URL.match(/searchforms\/($|\?)/i)) {
          newCrumb.unshift("<a href=\"http://www.ilrg.com/searchforms/\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-body.png',sizingMethod='scale'); display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;\">search</a>");
        }

          newCrumb.unshift("<a href=\"http://www.ilrg.com/forms/\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-body.png',sizingMethod='scale'); display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;\">forms menu</a>");
      }
      else {
        for (var n=1;n<list.length;n++) {
          newCrumb.push('<a href="'+list.item(n).href+'" style="background: transparent url('+_cc_images_path+'crumb-body.png) repeat-x scroll 50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;">'+(list.item(n).innerHTML.match(/forms/i) ? "forms menu" : list.item(n).innerHTML)+'</a>');
        }

        if (document.URL.match(/searchforms\/($|\?)/i)) {
          newCrumb.unshift('<a href="http://www.ilrg.com/searchforms/" style="background: transparent url('+_cc_images_path+'crumb-body.png) repeat-x scroll 50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;">search</a>');
        }

          newCrumb.unshift('<a href="http://www.ilrg.com/forms/" style="background: transparent url('+_cc_images_path+'crumb-body.png) repeat-x scroll 50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;">forms menu</a>');
      }
    }

    if (!newCrumb.length) {
      if (_is_ie55) {
        newCrumb.unshift("<a href=\"http://www.ilrg.com/forms/\" style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-body.png',sizingMethod='scale'); display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;\">forms menu</a>");
      }
      else {
        newCrumb.unshift('<a href="http://www.ilrg.com/forms/" style="background: transparent url('+_cc_images_path+'crumb-body.png) repeat-x scroll 50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 2px; height: 21px;">forms menu</a>');
      }
    }

    if (_is_ie55) {
      crumb.innerHTML = "<span style='display:inline-block;float:left;font-weight:bold;'><img style='margin-left:8px;margin-right:4px;' src='"+(document.location.href.match(/https:/i) ? "https" : "http")+"://www.ilrg.com/sitetuners/breadcrumb.gif' /> you are here:</span><span style=\"background: transparent repeat scroll 0% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 10px; height: 21px;\">&nbsp;</span><span style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-tail.png',sizingMethod='crop'); display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 10px; height: 21px;\">&nbsp;</span>"+newCrumb.join("<span style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-separator.png',sizingMethod='crop'); background-position:50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 20px; height: 21px;\">&nbsp;</span>")+"<span style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_cc_images_path+"crumb-head.png',sizingMethod='crop'); background-position: 100% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 18px; height: 21px;\">&nbsp;</span><div style=\"clear: both; line-height: 0pt; font-size: 0pt; height: 0pt;\"></div>";
    }
    else {
      crumb.innerHTML = "<span style='display:inline-block;float:left;font-weight:bold;'><img style='margin-left:8px;margin-right:4px;' src='"+(document.location.href.match(/https:/i) ? "https" : "http")+"://www.ilrg.com/sitetuners/breadcrumb.gif' /> you are here:</span><span style=\"background: transparent repeat scroll 0% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 10px; height: 21px;\">&nbsp;</span><span style=\"background: transparent url("+_cc_images_path+"crumb-tail.png) repeat scroll 0% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 10px; height: 21px;\">&nbsp;</span>"+newCrumb.join("<span style=\"background: transparent url("+_cc_images_path+"crumb-separator.png) repeat scroll 50% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 20px; height: 21px;\">&nbsp;</span>")+"<span style=\"background: transparent url("+_cc_images_path+"crumb-head.png) repeat scroll 100% 0%; display: inline-block; float: left; margin-top: -2px; padding-top: 0px; width: 18px; height: 21px;\">&nbsp;</span><div style=\"clear: both; line-height: 0pt; font-size: 0pt; height: 0pt;\"></div>";
    }
  }
}

function apply_recipe () {
  try {replaceLayout();}catch(e){}

  if (document.URL.match(/checkout/i)) {
      changeCheckout();

    var cartHover = document.getElementById("cartHover");
    if (cartHover) {
      cartHover.onmouseover = mOver;
      cartHover.onmouseout = mOut;
    }
  }
  else if (document.URL.match(/cart/i)) {
    var tbody = document.getElementsByTagName("body");
    if (tbody && tbody.item(0)) {
      tbody = tbody.item(0).innerHTML;
    }
    else {
      tbody = "Your cart is currently empty";
    }

    if (tbody.match(/Your cart is currently empty/i)) {
      changeEmptyCart();
    }
    else {
      changeCart();
    }
  }
  else {
    changeAddToCart();
    
    /*if (!timestamp || (timestamp != test_timestamp)) {
      return;
    }
    
    if (!recipe || (recipe != 'b')) {
      return;
    }*/
    
    var cc = document.getElementById('_stFormName');
    if (!cc) {
      return;
    }
    
    var wrong = true;
    do {
      cc = cc.parentNode;
      if (cc.tagName.match(/body/i)) {
        return;
      }
      if (cc.tagName.match(/td/i)) {
        wrong = false;
      }
    } while (cc && wrong);
    
    cc.style.backgroundImage = "url(/graphics/layout/ilrg-100517-free-preview-65.jpg)";
  }
}

/*****************************************************************
  Just declare the vars and function in case of non Control Group
 ****************************************************************/
var _using_fax = false;
var _add_to_cart = "";
var _is_opera = navigator.appName.indexOf("Opera") != -1;
var _is_ie55 = true;
try {_is_ie55 = (parseFloat(navigator.appVersion.split("MSIE")[1]) >= 5.5) && (document.body.filters);}catch(e){}

var _cc_images_path = "://www.ilrg.com/graphics/layout/";
try {_cc_images_path = (document.location.href.match(/https:/i) ? "https" : "http")+_cc_images_path;}
catch (e) {_cc_images_path = "https"+_cc_images_path;}

//var _credit_cards_btn = _cc_images_path.replace("/layout/","/")+"visamcamexdiscpp.jpg";
var _credit_cards_btn = _cc_images_path+"creditcards-new.jpg";

//var _add_to_cart_btn = _cc_images_path+"ilrg-0310-add-to-cart.png";
var _add_to_cart_btn = _cc_images_path+"ilrg-0310-addtocart.jpg";

var _continue_shopping_btn = _cc_images_path+"ilrg-0310-continue-shopping.png";
var _cart_page_title = _cc_images_path+"ilrg-0310-youraccount.gif";
var _checkout_page_title = _cc_images_path+"ilrg-0310-billing-safe.gif";

//var _checkout_btn = _cc_images_path+"ilrg-0310-proceed-to-checkout.gif";
var _checkout_btn = _cc_images_path+"ilrg-0310-btn_checkout.gif";

var _paypal_checkout_2p_btn = _cc_images_path+"paypal-p2.gif";
var _amazon_checkout_2p_btn = _cc_images_path+"amazon-p2.gif";

//var _end_checkout_btn = _cc_images_path+"ilrg-0310-place-order.gif";
var _end_checkout_btn = _cc_images_path+"ilrg-0310-submit_btn.gif";

var _lock_cart_btn = _cc_images_path+"ilrg-0310-lock.png";
var _edit_cart_btn = _cc_images_path+"ilrg-0310-edit-cart.png";
var _arrow_btn = _cc_images_path+"ilrg-0310-arrow.png";
var _cc_cvv_image = _cc_images_path+"ilrg-0310-card-visa-cvv.jpg";
var _amex_cvv_image = _cc_images_path+"ilrg-0310-card-amex-cvv.jpg";
var _question_image = _cc_images_path+"ilrg-0310-help.png";
var _spacer_btn = _cc_images_path+"140.png";

var _cart_summary_bg = _cc_images_path+"_bg-cart-summary.png";
var _recommended_bg = _cc_images_path+"bg-recommended.png";

var _cart_summary = "Items in Your Cart";
var _recommended_string = "_RECOMMENDED_STRING";
var _recommended_singular = "Those who bought your selection also bought...";
var _recommended_plural = "Those who bought your selections also bought...";
var _checkout_table_title = "Cart Summary";

var _round_corner_top_left_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-top-left-corner.gif/ig;
var _round_corner_top_right_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-top-right-corner.gif/ig;
var _round_corner_bottom_left_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-bottom-left-corner.gif/ig;
var _round_corner_bottom_right_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-bottom-right-corner.gif/ig;

var _secure_checkout = "Secure Checkout: <span style='font-weight:normal!important'>Pay now with</span> Credit <span style='font-weight:normal!important'>or</span> Debit Card";
var _secure_checkout_paypal = "PayPal Checkout";
var _secure_checkout_amazon = "Amazon Checkout";
var _other_checkout_options = "Other Payment Options";

var _credit_cards_map = "<map name='credit_cards_map'>"+
  "<area shape='rect' coords='1,1,41,25' href='https://www.ilrg.com/checkout.php' alt='Visa' title='Visa' />"+
  "<area shape='rect' coords='47,1,87,25' href='https://www.ilrg.com/checkout.php' alt='Master Card' title='Master Card' />"+
  "<area shape='rect' coords='92,1,132,25' href='https://www.ilrg.com/checkout.php' alt='AmEx' title='American Express' />"+
  "<area shape='rect' coords='138,1,178,25' href='https://www.ilrg.com/checkout.php' alt='Discover' title='Discover' />"+
  "<area shape='rect' coords='184,1,221,25' href='https://www.ilrg.com/checkout.php?paypal=true' alt='PayPal' title='PayPal Checkout' />"+
  "<area shape='rect' coords='227,1,267,25' href='https://www.ilrg.com/checkout.php?amazon=true' alt='Amazon' title='Amazon Checkout' />"+
  "</map>";

/*
var _round_top_left = _cc_images_path+"graybox-top-left.png";
var _round_top_right = _cc_images_path+"graybox-top-right.png";

var _round_corner_top_left = _cc_images_path+"graybox-top-left.gif";
var _round_corner_top_right = _cc_images_path+"graybox-top-right.gif";
var _round_corner_bottom_left = _cc_images_path+"graybox-bottom-left.gif";
var _round_corner_bottom_right = _cc_images_path+"graybox-bottom-right.gif";
*/

// http://www.ilrg.com/sitetuners/bluebox-bottom-left-corner.gif
var _round_top_left = _cc_images_path+"graybox-top-left.png";
var _round_top_right = _cc_images_path+"graybox-top-right.png";

var _round_corner_top_left = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-top-left-corner.gif";
var _round_corner_top_right = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-top-right-corner.gif";
var _round_corner_bottom_left = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-bottom-left-corner.gif";
var _round_corner_bottom_right = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-bottom-right-corner.gif";


var _border_left_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-left.gif/ig;
var _border_right_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-right.gif/ig;
var _border_top_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-top.gif/ig;
var _border_bottom_old = /https?:\/\/www.ilrg.com\/sitetuners\/bluebox-bottom.gif/ig;

/*
var _border_left = _cc_images_path+"graybox-left.gif";
var _border_right = _cc_images_path+"graybox-right.gif";
var _border_top = _cc_images_path+"graybox-top.gif";
var _border_bottom = _cc_images_path+"graybox-bottom.gif";
*/
var _border_left = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-left.gif";
var _border_right = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-right.gif";
var _border_top = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-top.gif";
var _border_bottom = _cc_images_path.replace("/graphics/layout/","/sitetuners/")+"bluebox-bottom.gif";


function changeCart () {
  remove("cartRow1");
  var list;
  var n;

  var div = document.getElementById("_stVar06");
  if (div) {
    list = div.getElementsByTagName("table");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (list.item(n).getAttribute("width") == "582") {
          list.item(n).setAttribute("width","");
        }
      }
    }
  }

  var title = document.getElementById("_stVar06Title");
  if (title) {
    //title.style.display = "none";
    title.style.textAlign = "center";
    title.style.display = "inline-block";
    title.style.width = "100%";
    title.innerHTML = "<img alt='Your Cart' src='"+_cart_page_title+"' style='margin-bottom:0.5em' />";
    /*title.innerHTML = "<table width='100%' cellpadding='0' cellspacing='0' bordercollapse='0'><col width='15'><col width='*'><col width='15'><tbody><tr><td></td><td></td><td></td></tr>";
    var shoppingTitle = createHeader("Shopping Cart","title");
    if (shoppingTitle && title.firstChild && title.firstChild.firstChild) {
      title.firstChild.insertBefore(shoppingTitle,title.firstChild.firstChild);
    }*/

    list = title.getElementsByTagName("ul");
    if (list) {
      if (list.item(0)) {
        list.item(0).parentNode.removeChild(list.item(0));
      }
    }

    list = title.getElementsByTagName("h2");
    if (list) {
      if (list.item(0)) {
        list.item(0).style.display = "none";
      }

      if (list.item(1)) {
        list.item(1).style.display = "none";
      }
    }

    list = title.getElementsByTagName("img");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (list.item(n).src.match(/visamcamexdiscpp/)) {
          list.item(n).src = _credit_cards_btn;
        }
      }
    }
  }

  var _fax_holder = document.getElementById("_stFax");
  if (_fax_holder && _fax_holder.innerHTML.match(/in "fine" resolution/i)) {
    _fax_holder.innerHTML = _fax_holder.innerHTML.replace(/in "fine" resolution/i,"");
  }

  var table = document.getElementById("_stVar06Table");
  if (table) {
    //var paypal = copyForm(document.getElementById("paypal"));
    //var amazon = copyForm(document.getElementById("amazon"));
    var paypal = null;
    var amazon = null;
    var checkout = null;
    var _continue = null;
    var _subtitles = 0;
    var _recommended_title = null;
    var _plural = false;
    var subtotal = "";
//    var list;


    list = table.getElementsByTagName("div");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (list.item(n).innerHTML.match(/paypal/i)) {
          paypal = copyForm(list.item(n));
        }
        else if (list.item(n).innerHTML.match(/amazon/i)) {
          amazon = copyForm(list.item(n));
        }
      }
    }

    list = table.getElementsByTagName("span");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (list.item(n).innerHTML.match(/in "fine" resolution/i)) {
          list.item(n).innerHTML = list.item(n).innerHTML.replace(/in "fine" resolution/i,"");
          break;
        }
      }
    }

    list = table.getElementsByTagName("table");
    if (list) {
      for (n=0;n<list.length;n++) {
        list.item(n).setAttribute("width","100%");
      }
    }
    list = table.getElementsByTagName("img");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (list.item(n).width == "540") {
          list.item(n).setAttribute("width","100%");
        }
      }
    }

    list = table.getElementsByTagName("table");
    if (list && list.item(0)) {
      var trs = list.item(0).getElementsByTagName("tr");
      if (trs && trs.item(0)) {
        trs.item(0).style.display = "none";
      }

      //list.item(0).style.marginTop = "1em";

      var thead = createHeader(_cart_summary);
      if (thead && list.item(0).firstChild) {
        list.item(0).insertBefore(thead,list.item(0).firstChild);
      }
      else if (thead) {
        list.item(0).appendChild(thead);
      }
    }

    list = table.getElementsByTagName("h3");
    if (list) {
      for (n=0;n<list.length;n++) {
        // Having more than 1 means we need to create the extra table
        list.item(n).style.display = "none";
        _subtitles++;
      }
    }

    var images = table.getElementsByTagName("img");
    if (images) {
      for (n=0;n<images.length;n++) {
        if (images.item(n).src.match(/_stContinue.jpg/i) || images.item(n).src.match(/btn_cont_shopping.gif/i)) {
          images.item(n).src = _continue_shopping_btn;
          _continue = images.item(n).parentNode;

          if (_is_ie55) {
            // 99 x 23
            images.item(n).style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity=0)";
            _continue.innerHTML = "<span style=\"display:inline-block;width:99px;height:23px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_continue_shopping_btn+"', sizingMethod='scale')\">"+_continue.innerHTML+"</span>";
          }
        }
      }
    }

    var inputs = table.getElementsByTagName("input");
    if (inputs) {
      for (n=0;n<inputs.length;n++) {
        if (inputs.item(n).src.match(/_stProceed/i)) {
          inputs.item(n).src = _checkout_btn;
          checkout = inputs.item(n).parentNode;
        }
      }
    }

    list = table.getElementsByTagName("div");
    if (list) {
      for (n=0;n<list.length;n++) {
        if (!list.item(n).style.display.match(/!important/i)) {
          list.item(n).style.display = "none";
        }
      }
    }

    list = table.getElementsByTagName("br");
    if (list) {
      for (n=0;n<list.length;n++) {
        list.item(n).style.display = "none";
      }
    }

    list = table.getElementsByTagName("table");
    if (list) {
      if (list.item(3)) {
        list.item(3).style.display = "none";
      }

      if (list.item(1)) {
        var tds = list.item(1).getElementsByTagName("td");
        if (tds) {
          for (n=0;n<tds.length;n+=3) {
            if (tds.item(n)) {
              tds.item(n).style.width = "500px";
            }

            if (tds.item(n+1)) {
              tds.item(n+1).style.textAlign = "right";

              if (tds.item(n).innerHTML.match(/subtotal/i)) {
                subtotal = tds.item(n+1).innerHTML;
              }
            }

            if (tds.item(n+2)) {
              tds.item(n+2).style.textAlign = "right";
              tds.item(n+2).width = "60";
            }
          }
        }
      }
    }

    var tablebottom = null;
    if (_subtitles > 1) {
      tablebottom = table.cloneNode(true);
    }

    if (tablebottom) {
      tablebottom.style.marginTop = "1em";

      _plural = document.getElementsByTagName("body");
      if (_plural && _plural.item(0)) {
        var _above_form = _plural.item(0).innerHTML.match(/Fax Delivery of Above/i) && 1;
        _plural = _plural.item(0).innerHTML.match(/('|")remove('|")/ig);
        if (_plural) {
          _plural = (_plural.length - _above_form) > 1;
        }
        else {
          _plural = false;
        }
      }
      else {
        _plural = false;
      }

      list = tablebottom.getElementsByTagName("th");
      if (list && list.item(1)) {
        list.item(1).parentNode.style.backgroundImage = "url("+_recommended_bg+")";
        list.item(1).innerHTML = (_plural) ? _recommended_plural : _recommended_singular;
      }

      list = tablebottom.getElementsByTagName("table");
      if (list) {
        if (list.item(1)) {
          list.item(1).style.display = "none";
        }

        if (list.item(3)) {
          list.item(3).style.display = "block";

          tds = list.item(3).getElementsByTagName("td");
          if (tds) {
            for (n=0;n<tds.length;n+=3) {
              if (tds.item(n)) {
                tds.item(n).width = "436";
              }

              if (tds.item(n+1)) {
                tds.item(n+1).style.textAlign = "right";
                tds.item(n+1).width = "40";
              }

              if (tds.item(n+2)) {
                tds.item(n+2).style.textAlign = "right";
                tds.item(n+2).width = "140";
                if (_is_ie55) {
                  tds.item(n+2).innerHTML = "<div style='float:right'>"+tds.item(n+2).innerHTML+"</div><span style=\"display:inline-block;width=140px;height:1px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_spacer_btn+"', sizingMethod='scale');clear:both;line-height:0;font-size:0;margin:0;padding:0\"></span>";
                }
                else {
                  tds.item(n+2).innerHTML = "<div style='float:right'>"+tds.item(n+2).innerHTML+"</div><img src='"+_spacer_btn+"' style='clear:both' />";
                }
              }
            }
          }
        }
      }

      list = tablebottom.getElementsByTagName("span");
      if (list) {
        for (n=0;n<list.length;n++) {
          if (list.item(n).className == "small") {
            list.item(n).style.display = "none";
          }
        }
      }

      table.parentNode.appendChild(tablebottom);
    }

    var top = document.createElement("table");
    var topTbody = document.createElement("tbody");
    var topTr = document.createElement("tr");
    var topTdL = document.createElement("td");
    var topTdR = document.createElement("td");
    if (top && topTbody && topTr && topTdL && topTdR) {
      top.cellpadding = "0";
      top.cellspacing = "0";
      top.style.marginBottom = "1em";
      top.style.width = "100%";
      top.style.textAlign = "left";

      topTdR.style.textAlign = "right";
      topTdR.style.paddingRight = "176px";

      if (paypal) {
        paypal.style.display = "block";
        list = paypal.getElementsByTagName("input");
        if (list) {
          for (n=0;n<list.length;n++) {
            list.item(n).style.marginLeft = "-1em";
            list.item(n).style.marginRight = "-1em";
          }
        }
      }
      if (amazon) {
        amazon.style.display = "block";
      }
      if (_continue) {
        topTdL.appendChild(_continue.cloneNode(true));
      }
      if (checkout) {
        //margin-right:176px;
        //width:268px;
        topTdR.innerHTML = topTdR.innerHTML+"<img style='border:none;display:inline-block;float:right;' src='"+_credit_cards_btn+"' usemap='#credit_cards_map' />"+_credit_cards_map+"<div style='clear:both;line-height:0;font-size:0;height:0;'></div>";
      }

      topTr.appendChild(topTdL);
      topTr.appendChild(topTdR);
      topTbody.appendChild(topTr);
      top.appendChild(topTbody);

      if (tablebottom) {
        /*var topClone = top.cloneNode(true);
        if (topClone && 0) {
          tablebottom.parentNode.appendChild(topClone);
        }*/

        if (paypal) {
          tablebottom.parentNode.appendChild(paypal);
        }
        if (amazon) {
          tablebottom.parentNode.appendChild(amazon);
        }
      }
      else {
        /*var topClone = top.cloneNode(true);
        if (topClone && 0) {
          topClone.style.marginTop = "1em";
          table.parentNode.appendChild(topClone);
        }*/

        if (paypal) {
          table.parentNode.appendChild(paypal);
        }
        if (amazon) {
          table.parentNode.appendChild(amazon);
        }
      }
    }

    var newTable = document.createElement("table");
    var newTbody = document.createElement("tbody");
    var newTr = document.createElement("tr");
    var newTd = document.createElement("td");
    var insertCart = document.createElement("td");
    var insertCheckout = document.createElement("div");
    var checkoutWrapper = document.createElement("div");

    if (newTable && newTr && newTd && insertCart && insertCheckout && checkoutWrapper) {
      newTable.width = "100%";

      newTd.style.verticalAlign = "top";
      insertCart.style.verticalAlign = "top";

      if (paypal) {
        paypal.parentNode.removeChild(paypal);
        paypal.innerHTML = paypal.innerHTML.replace(/<td><b>(&nbsp;)+<\/b><\/td>/igm,"");
        paypal.innerHTML = paypal.innerHTML.replace(/<\/?table[^>]*>|<\/?thead[^>]*>|<\/?tbody[^>]*>|<\/?tr[^>]*>|<\/?th[^>]*>|<\/?td[^>]*>/igm,"");
        insertCheckout.appendChild(paypal);
      }

      if (amazon) {
        amazon.parentNode.removeChild(amazon);
        amazon.innerHTML = amazon.innerHTML.replace(/<td><b>(&nbsp;)+<\/b><\/td>/igm,"");
        amazon.innerHTML = amazon.innerHTML.replace(/<\/?table[^>]*>|<\/?thead[^>]*>|<\/?tbody[^>]*>|<\/?tr[^>]*>|<\/?th[^>]*>|<\/?td[^>]*>/igm,"");
        insertCheckout.appendChild(amazon);
      }

      if (1) {
        //newTd.style.position = "fixed";
      }
      else {
        newTd.style.position = "relative";
        insertCheckout.style.position = "fixed";
      }

      if (checkout) {
        checkout.style.marginTop = "0.5em";
        checkout.style.marginBottom = "0.2em";
        checkoutWrapper.appendChild(checkout);
      }
      checkout = checkoutWrapper.innerHTML+"<span style='display:inline-block;width:100%;text-align:center;font-size:11px;font-family:Arial,Helvetica,sans-serif;'><a href='javascript:window.open(\"/safe-shopping.html\",\"safeShopping\",\"toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=yes, width=644, height=600, top=85, left=140\")'>Safe Shopping Guarantee</a></span>";

      newTd.style.verticalAlign = "top";
      newTd.style.width = "170px";
      newTd.innerHTML = "<table style='width:160px;margin-left:0.5em;position:relative' cellpadding='0' cellspacing='0'>"+
        "<col width='15'><col width='*'><col width='15'>"+
        "<tbody>"+
        "<tr>"+
        "<td style='background:url("+_border_left+") repeat-y'></td>"+
        "<td style='padding:0.5em 0;'>"+
        "<p style='color:#999;font-weight:bold'>Total</br>\n"+
        "<span style='color:#000;font-size:larger;font-weight:bold'>"+subtotal+"</span></p>\n"+
        "<p style='color:#0085cf;font-size:7pt'>No hidden fees. No monthly memberships. This is a one-time, nonrecurring charge.</p>\n"+
        "<p style='color:#f00;font-size:8pt'>Instant Download</p>\n"+
        "</td>"+
        "<td style='background:url("+_border_right+") repeat-y 100% 0'></td>"+
        "</tr>"+
        "<tr>"+
        "<td style='background:url("+_border_left+") repeat-y'></td>"+
        "<td style='text-align:center;border-top:1px solid #999;border-jbottom:1px solid #999;'>"+
        checkout+
        "</td>"+
        "<td style='background:url("+_border_right+") repeat-y 100% 0'></td>"+
        "</tr>"+
        /*"<tr>"+
        "<td style='background:url("+_border_left+") repeat-y'></td>"+
        "<td style='text-align:center;padding-top:0.5em;'>"+
        (paypal && amazon ? "<p style='font-weight:bold;color:#666;margin-left:-0.5em;margin-right:-0.5em;'>Other Payment Options</p>" : "&nbsp;")+
        (paypal ? paypal.innerHTML : "")+
        (amazon ? amazon.innerHTML : "")+
        "</td>"+
        "<td style='background:url("+_border_right+") repeat-y 100% 0'></td>"+
        "</tr>"+*/
        "<tr style='background:url("+_border_bottom+") repeat-x;'><td style='height:15px;background:url("+_round_corner_bottom_left+") no-repeat'></td><td>&nbsp;</td><td style='height:15px;background:url("+_round_corner_bottom_right+") no-repeat 100% 0'></td></tr>"+
        "</tbody>"+
        "</table>";

      var thead = createHeader(_checkout_table_title);
      if (thead && newTd.firstChild && newTd.firstChild.firstChild) {
        newTd.firstChild.insertBefore(thead,newTd.firstChild.firstChild);
      }
      else if (thead && newTd.firstChild) {
        newTd.firstChild.appendChild(thead);
      }

      var tableParent = table.parentNode;
      tableParent.removeChild(table);
      insertCart.appendChild(table);

      if (tablebottom) {
        tablebottom.style.display = "block";
        tablebottom.style.marginTop = "1em";
        tablebottom.style.marginBottom = "1em";
        tablebottom.parentNode.removeChild(tablebottom);
        insertCart.appendChild(tablebottom);

        if (_continue) {
          insertCart.appendChild(_continue);
        }
      }
      else {
        table.style.display = "block";
        table.style.marginBottom = "1em";

        if (_continue) {
          insertCart.appendChild(_continue);
        }
      }

      insertCart.style.width = "500px";

      newTr.appendChild(insertCart);
      newTr.appendChild(newTd);
      newTbody.appendChild(newTr);
      newTable.appendChild(newTbody);

      tableParent.insertBefore(newTable,tableParent.firstChild);
      //tableParent.insertBefore(top,newTable);
    }
  }
}

function changeAddToCart () {
  var div = document.getElementById("_stVar05");
  if (!div) {
    return;
  }

  /*var images = div.getElementsByTagName("img");
  if (images) {
    for (var n=0;n<images.length;n++) {
      if (images.item(n).src.match(/creditcards/)) {
        images.item(n).src = _credit_cards_btn;
      }
    }
  }*/

  var links = div.getElementsByTagName("a");
  if (links) {
    for (var n=0;n<links.length;n++) {
      if (links.item(n).innerHTML.match(/add to cart/i)) {
        links.item(n).innerHTML = "<img style='border:none;margin-bottom:10px' src='"+_add_to_cart_btn+"' />";

        _add_to_cart = links.item(n).href;

        // 5 level deep element
        var thisNode = links.item(n).parentNode.parentNode.parentNode.parentNode.parentNode;
        if (thisNode.addEventListener) {
          thisNode.addEventListener("click",function(){document.location.href=_add_to_cart;},false);
        }
        else if (thisNode.attachEvent) {
          thisNode.attachEvent("onclick",function(){document.location.href=_add_to_cart;});
        }
        else {
          thisNode.setAttribute("onclick","javascript:document.location.href=_add_to_cart;");
        }

        thisNode.style.cursor = "pointer";
      }
    }
  }
}

function changeEmptyCart () {
  var n;
  remove("cartRow1");
  var tbody = document.getElementsByTagName("body");

  if (tbody && tbody.item(0)) {
    tbody = tbody.item(0);

    var images = tbody.getElementsByTagName("img");
    if (images) {
      for (n=0;n<images.length;n++) {
        if (images.item(n).src.match(/_stContinue.jpg/i) || images.item(n).src.match(/btn_cont_shopping.gif/i)) {
          images.item(n).width = "141";
          images.item(n).height = "23";
          images.item(n).src = _continue_shopping_btn;
        }
      }
    }
  }

  tbody = document.getElementById("_stVar06Title");
  if (tbody) {
    var list = tbody.getElementsByTagName("ul");
    if (list) {
      for (n=0;n<list.length;n++) {
        list.item(n).style.display = "none";
      }
    }
  }

  var list = document.getElementsByTagName("img");
  if (list) {
    for (n=0;n<list.length;n++) {
      if (list.item(n).src.match(/visamcamexdiscpp/)) {
        list.item(n).src = _credit_cards_btn;
      }
    }
  }
}

function fadeCVV (decrease, recurrent, id) {
  if (typeof fadeCVV.value == "undefined") {
    fadeCVV.value = -0.5;
    fadeCVV.min = -0.5;
    fadeCVV.max = 1.5;
    fadeCVV.delta = 1/8;
    fadeCVV.delay = 1000/30;
    fadeCVV.domElem = null;
    fadeCVV.decrease = false;
    fadeCVV.st = false;
    fadeCVV.id = 1;
  }

  if (recurrent && (id != fadeCVV.id)) {
    return;
  }

  if (!fadeCVV.domElem) {
    fadeCVV.domElem = document.getElementById("cvvDetails");
  }

  if (!fadeCVV.domElem) {
    return;
  }

  if (recurrent) {
    if (!fadeCVV.decrease && (fadeCVV.value < fadeCVV.max)) {
      fadeCVV.value += fadeCVV.delta;
    }
    else if (fadeCVV.decrease && (fadeCVV.value > fadeCVV.min)) {
      fadeCVV.value -= fadeCVV.delta;
    }

    if ((fadeCVV.value < fadeCVV.max) && (fadeCVV.value > fadeCVV.min)) {
      fadeCVV.domElem.style.display = "block";

      if (fadeCVV.value <= 0) {
        fadeCVV.domElem.style.opacity = 0;
        fadeCVV.domElem.style.filter = 'alpha(opacity=0)';
      }
      else if (fadeCVV.value >= 1) {
        fadeCVV.domElem.style.opacity = 1;
        fadeCVV.domElem.style.filter = 'alpha(opacity=100)';
      }
      else {
        fadeCVV.domElem.style.opacity = fadeCVV.value;
        fadeCVV.domElem.style.filter = 'alpha(opacity='+fadeCVV.value*100+')';
      }

      fadeCVV.id = Math.random();
      fadeCVV.st = setTimeout("fadeCVV(0,1,"+fadeCVV.id+")",fadeCVV.delay);
    }
    else {
      if (fadeCVV.value <= fadeCVV.min) {
        fadeCVV.domElem.style.display = "none";
      }

      clearTimeout(fadeCVV.st);
    }
  }
  else {
    fadeCVV.decrease = decrease;
    fadeCVV.id = Math.random();
    fadeCVV.st = setTimeout("fadeCVV(0,1,"+fadeCVV.id+")",fadeCVV.delay);
  }
}

function ValidateNewCheckoutForm() {
  var cc = document.checkout.x_card_num.value.checkCreditCard();

  if(document.checkout.x_country.selectedIndex < 1) { alert('Please select your country.'); return 0; }
  if(document.checkout.x_zip.value=='') { alert('Please enter your zip code. Enter n/a for international addresses with no zip or postal code.'); return 0; }
  if(document.checkout.x_first_name.value=='') { alert('Please enter your name as appears at your card.'); return 0; }
  if(document.checkout.x_email.value=='') { alert('Please enter your email address.'); return 0; }
  if(document.checkout.x_email.value.indexOf('@') < 1) { alert('Please enter a valid email address in the format address@yourdomain.com.'); return 0; }
  if(document.checkout.x_email.value.indexOf('.') < 1) { alert('Please enter a valid email address in the format address@yourdomain.com.'); return 0; }
  if(document.checkout.x_card_num.value=='') { alert('Please enter your credit card number.'); return 0; }
  if(cc=='') { alert('Please enter a valid credit card number.'); return 0; }
  if(document.checkout.expmonth.selectedIndex < 1) { alert('Please select your credit card expiration month.'); return 0; }
  if(document.checkout.expyear.selectedIndex < 1) { alert('Please select your credit card expiration year.'); return 0; }
  document.checkout.x_exp_date.value = document.checkout.expmonth.options[document.checkout.expmonth.selectedIndex].value + '/' + document.checkout.expyear.options[document.checkout.expyear.selectedIndex].value;
  if(document.checkout.x_card_code.value=='') { alert('Please enter your card security code.'); return 0; }
  if(document.checkout.x_card_code.value.match(/[^\d]/i)) { alert('Please enter only numeric digits for your card security code.'); return 0; }

  switch (cc) {
    case "":
      alert('Please enter a valid credit card number');return 0;
      break;
    case "amex":
      if(document.checkout.x_card_code.value.length != 4){alert('Please enter the 4 digits security code for your American Express card');return 0;}
      break;
    case "visa":
      if(document.checkout.x_card_code.value.length != 3){alert('Please enter the 3 digits security code for your Visa card');return 0;}
      break;
    case "mastercard":
      if(document.checkout.x_card_code.value.length != 3){alert('Please enter the 3 digits security code for your MasterCard card');return 0;}
      break;
    case "discover":
      if(document.checkout.x_card_code.value.length != 3){alert('Please enter the 3 digits security code for your Discover card');return 0;}
      break;
  }
  if((document.checkout.x_card_code.value.length < 3) || (document.checkout.x_card_code.value.length > 4)) { alert('Please enter a 3- or 4-digit number for your card verification code.'); return 0; }

  if (_using_fax) {
    fax_number = document.checkout.x_fax
    fax_country = document.checkout.x_faxcountry
    fax_number.value = fax_number.value.replace(/[^0-9]/ig,'');  if(fax_number.value == '') { alert('Please enter your fax number.'); return 0; }
    country_index = fax_country.selectedIndex;
    country_code = fax_country.options[country_index].value;
    if ((country_code == 1) && (fax_number.value.length != 10)) { alert('Please enter a valid 10 digit fax number in the format xxx-yyy-zzzz'); return 0; };
    if ((country_code != 1) && (fax_number.value.length < 6)) { alert('Please enter a valid fax number'); return 0; };
  }

  if (document.checkout.submitorder) {
    document.checkout.submitorder.disabled = 1;
  }
  return 1;
}

function createHeader (title,kind) {
  var header = document.createElement("thead");
  var row = document.createElement("tr");
  var left = document.createElement("th");
  var middle = document.createElement("th");
  var right = document.createElement("th");

  if (_is_ie55) {
    var limage = document.createElement("span");
    var rimage = document.createElement("span");
  }
  else {
    var limage = document.createElement("img");
    var rimage = document.createElement("img");
  }

  if (header && row && left && middle && right && limage && rimage) {
    if (kind == "cart") {
      middle.innerHTML = "<div style='float:left;width:55%;'>"+title+"</div>";

      if (_is_ie55) {
        middle.innerHTML += "<div style='float:right;text-align:right;width:40%'>"+
        "<a id='cartHover' style='lfloat:right;text-decoration:none;color:#fff!important' href='https://www.ilrg.com/cart.php'>"+
        "<small style='padding-bottom:5px'>Edit Cart</small>"+
        "</a>"+
        "<a style='lfloat:right;text-decoration:none;color:#fff!important' href='https://www.ilrg.com/cart.php'>"+
        "<span style=\"display:inline-block;width=19px;height:14px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_edit_cart_btn+"', sizingMethod='scale');\" title='Edit Cart'></span>"+
        "</a>"+
        "</div>";
      }
      else {
        middle.innerHTML += "<div style='float:right;text-align:right;width:40%'>"+
        "<a id='cartHover' style='lfloat:right;text-decoration:none;color:#fff!important' href='https://www.ilrg.com/cart.php'>"+
        "<small style='padding-bottom:5px'>Edit Cart</small>"+
        "</a>"+
        "<a style='lfloat:right;text-decoration:none;color:#fff!important' href='https://www.ilrg.com/cart.php'>"+
        "<img style='border:none' src='"+_edit_cart_btn+"' alt='Edit Cart' title='Edit Cart' />"+
        "</a>"+
        "</div>";
      }
    }
    else if (kind == "lock") {
      if (_is_ie55) {
        middle.style.overflow = "visible";
        middle.innerHTML = "<div style='float:left;text-align:left;width:16px;margin-left:-0px;'>"+
        "<span style=\"display:inline-block;width=13px;height:13px;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_lock_cart_btn+"', sizingMethod='crop');\" title='Edit Cart'></span>"+
        "</div>";
      }
      else {
        middle.innerHTML = "<div style='float:left;text-align:left;width:16px;margin-left:-5px;'>"+
        "<img style='border:none' src='"+_lock_cart_btn+"' alt='Edit Cart' title='Edit Cart' />"+
        "</div>";
      }

      middle.innerHTML += "<div style='float:left;width:95%;'>"+title+"</div>";
    }
    else if (kind == "title") {
      middle.innerHTML = "<img src='http://www.magentaderek.com/sandbox/youraccount.gif' alt='Your Cart' />";
    }
    else {
      middle.innerHTML = title;
    }

    //row.setAttribute("style","font-family:'Lucida Grande',Arial,Helvetica,sans-serif;color:#fff;font-size:smaller;background:url("+_cart_summary_bg+");text-align:left");
    row.style.fontFamily = "'Lucida Grande',Arial,Helvetica,sans-serif";
    row.style.color = "#fff";

    if (kind == "title") {
      row.style.textAlign = "center";
      row.style.backgroundImage = "url("+_cart_summary_bg+")";
    }
    else {
      row.style.fontSize = "smaller";
      row.style.textAlign = "left";
      row.style.backgroundImage = "url("+_cart_summary_bg+")";
    }

    //left.setAttribute("style","background:url("+_round_top_left+") no-repeat");
    //right.setAttribute("style","background:url("+_round_top_right+") no-repeat");
    //left.style.backgroundImage = "url("+_round_top_left+")";
    //left.style.backgroundRepeat = "no-repeat";
    //left.style.backgroundColor = "#224272";
    //right.style.backgroundImage = "url("+_round_top_right+")";
    //right.style.backgroundRepeat = "no-repeat";
    //right.style.backgroundColor = "#224272";
    left.style.margin = "0";
    left.style.padding = "0";
    left.setAttribute("valign","top");
    left.style.verticalAlign = "top";
    right.style.margin = "0";
    right.style.padding = "0";
    right.setAttribute("valign","top");
    right.setAttribute("align","right");
    right.style.verticalAlign = "top";

    //middle.setAttribute("style","padding:0.5em 0");
    middle.style.padding = "0.5em 0";

    if (_is_ie55) {
      limage.style.display = "inline-block";
      limage.style.width = "15px";
      limage.style.height = "15px";
      limage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_round_top_left+"', sizingMethod='scale')";
      rimage.style.display = "inline-block";
      rimage.style.width = "15px";
      rimage.style.height = "15px";
      rimage.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+_round_top_right+"', sizingMethod='scale')";
    }
    else {
      limage.src = _round_top_left;
      limage.style.verticalAlign = "top";
      rimage.src = _round_top_right;
      rimage.style.verticalAlign = "top";
    }

    left.appendChild(limage);
    right.appendChild(rimage);
    row.appendChild(left);
    row.appendChild(middle);
    row.appendChild(right);
    header.appendChild(row);

    return header;
  }

  return null;
}

function copyForm (form) {
  if (!form) {
    return null;
  }

  var newForm = null;
  form = form.getElementsByTagName("form");
  if (form && form.item(0)) {
    form = form.item(0);
    newForm = form.cloneNode(true);
  }


  if (newForm && form) {
    newForm.innerHTML = "";

    if (form.innerHTML.match(/amazon/i)) {
      newForm.style.paddingTop = "1em";
    }

    var list = form.getElementsByTagName("input");
    if (!list) {
      return newForm;
    }

    for (var n=0;n<list.length;n++) {
      newForm.appendChild(list.item(n).cloneNode(true));
    }

    var div = document.createElement("div");
    if (div) {
      div.appendChild(newForm);
      return div;
    }

    return newForm;
  }

  return null;
}

function changeTdContents (tr,match,replacement) {
  if (!tr) {
    return;
  }

  var tds = tr.getElementsByTagName("td");
  if (!tds) {
    return;
  }

  for (var n=0;n<tds.length;n++) {
    if (tds.item(n).innerHTML.match(match)) {
      tds.item(n).innerHTML = tds.item(n).innerHTML.replace(match,replacement);
    }
  }
}

function sleep (naptime){
  naptime = naptime * 1000;
  var sleeping = true;
  var now = new Date();
  var alarm;
  var startingMSeconds = now.getTime();

  while(sleeping){
    alarm = new Date();
    alarmMSeconds = alarm.getTime();
    if(alarmMSeconds - startingMSeconds > naptime){ sleeping = false; }
  }
}

/* code from qodo.co.uk */
// create as many regular expressions here as you need:
function restrictCharacters(e) {
	if (!e) var e = window.event
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	var character = String.fromCharCode(code);

	// if they pressed esc... remove focus from field...
	if (code==27) { this.blur(); return false; }

	// ignore if they are press other keys
	// strange because code: 39 is the down key AND ' key...
	// and DEL also equals .
  
  if ((code>95) && (code<106)) { return true; }
	if (!e.ctrlKey && code!=9 && code!=8 && code!=36 && code!=37 && code!=38 && code!=39 && code!=40) {
		if (character.match(/[^\d]/i)) {
			return false;
		} else {
			return true;
		}
	}
}

function securityCode () {
  var cc = document.getElementById('x_card_code');
  if (cc) {
    if (cc.value.match(/^\d*$/ig)) { return true; }
    cc.value = cc.value.replace(/[^\d]/ig,'');
  }
}

function replaceBorders () {
  var list = document.getElementsByTagName("img");
  if (list) {
    for (var n=0;n<list.length;n++) {
      if (list.item(n).src.match(_round_corner_top_left_old)) {
        list.item(n).src = _round_corner_top_left;
      }
      else if (list.item(n).src.match(_round_corner_top_right_old)) {
        list.item(n).src = _round_corner_top_right;
      }
      if (list.item(n).src.match(_round_corner_bottom_left_old)) {
        list.item(n).src = _round_corner_bottom_left;
      }
      else if (list.item(n).src.match(_round_corner_bottom_right_old)) {
        list.item(n).src = _round_corner_bottom_right;
      }
    }
  }

  var list = document.getElementsByTagName("td");
  if (list) {
    for (var n=0;n<list.length;n++) {
      var bg = list.item(n).getAttribute("background");
      if (!bg) {
        continue;
      }

      if (bg.match(_border_left_old)) {
        list.item(n).setAttribute("background",_border_left);
      }
      else if (bg.match(_border_right_old)) {
        list.item(n).setAttribute("background",_border_right);
      }
      if (bg.match(_border_top_old)) {
        list.item(n).setAttribute("background",_border_top);
      }
      else if (bg.match(_border_bottom_old)) {
        list.item(n).setAttribute("background",_border_bottom);
      }
    }
  }
}

function mOver (e){
  e = e||window.event;
  var trgSrc = e.target||e.srcElement;
  trgSrc.style.textDecoration = "underline";
};

function mOut (e){
  e = e||window.event;
  var trgSrc = e.target||e.srcElement;
  trgSrc.style.textDecoration = "none";
};

/*

Author:     John Gardner
Date:       1st November 2003
Updated:    26th Feb. 2005      Additional cards added by request
Updated:    27th Nov. 2006      Additional cards added from Wikipedia
Updated:    18th Jan. 2008      Additional cards added from Wikipedia
Updated:    26th Nov. 2008      Maestro cards extended
Updated:    19th Jun. 2009      Laser cards extended from Wikipedia

*/

var ccErrorNo = 0;
var ccErrors = new Array ()

ccErrors [0] = "Unknown card type";
ccErrors [1] = "No card number provided";
ccErrors [2] = "Credit card number is in invalid format";
ccErrors [3] = "Credit card number is invalid";
ccErrors [4] = "Credit card number has an inappropriate number of digits";

// Array to hold the permitted card characteristics
var ccNames = new Array();

// Define the cards we support. You may add addtional card types.

//  Name:      As in the selection box of the form - must be same as user's
//  Length:    List of possible valid lengths of the card number for the card
//  prefixes:  List of possible prefixes for the card
//  checkdigit Boolean to say whether there is a check digit

ccNames.push({name: "visa",
  length: new Array(13,16),
  prefixes: new Array([4]),
  checkdigit: true});
ccNames.push({name: "mastercard",
  length: new Array(16,0),
  prefixes: new Array(51,52,53,54,55),
  checkdigit: true});
ccNames.push({name: "amex",
  length: new Array(15,0),
  prefixes: new Array(34,37),
  checkdigit: true});
ccNames.push({name: "discover",
  length: new Array(16,0),
  prefixes: new Array(6011,622,64,65),
  checkdigit: true});

/*
ccNames.push({name: "diners",
  length: new Array(14,16),
  prefixes: new Array(305, 36, 38, 54,55),
  checkdigit: true});
ccNames.push({name: "CarteBlanche",
  length: new Array(14,0),
  prefixes: new Array(300,301,302,303,304,305),
  checkdigit: true});
ccNames.push({name: "jcb",
  length: new Array(16,0),
  prefixes: new Array([35]),
  checkdigit: true});
ccNames.push({name: "enRoute",
  length: new Array(15,0),
  prefixes: new Array(2014,2149),
  checkdigit: true});
ccNames.push({name: "Solo",
  length: new Array(16,18,19),
  prefixes: new Array(6334, 6767),
  checkdigit: true});
ccNames.push({name: "Switch",
  length: new Array(16,18,19),
  prefixes: new Array(4903,4905,4911,4936,564182,633110,6333,6759),
  checkdigit: true});
ccNames.push({name: "Maestro",
  length: new Array(12,13,14,15,16,18,19),
  prefixes: new Array(5018,5020,5038,6304,6759,6761),
  checkdigit: true});
ccNames.push({name: "VisaElectron",
  length: new Array(16,0),
  prefixes: new Array(417500,4917,4913,4508,4844),
  checkdigit: true});
ccNames.push({name: "LaserCard",
  length: new Array(16,17,18,19),
  prefixes: new Array(6304,6706,6771,6709),
  checkdigit: true});
*/

function cardValid (cardNo) {
  var checksum = 0;                                  // running checksum total
  var mychar = "";                                   // next char to process
  var j = 1;                                         // takes value of 1 or 2

  // Process each digit one by one starting at the right
  var calc;
  for (i = cardNo.length - 1; i >= 0; i--) {
    // Extract the next digit and multiply by 1 or 2 on alternative digits.
    calc = Number(cardNo.charAt(i)) * j;

    // If the result is in two digits add 1 to the checksum total
    if (calc > 9) {
      checksum = checksum + 1;
      calc = calc - 10;
    }

    // Add the units element to the checksum total
    checksum = checksum + calc;

    // Switch the value of j
    if (j ==1) {j = 2} else {j = 1};
  }

  // All done - if checksum is divisible by 10, it is a valid modulus 10.
  // If not, report an error.
  if (checksum % 10 != 0)  {
    return false;
  }

  return true;
};

Array.prototype.find = function(value){for(var n in this){if(this[n]==value){return n}}return -1};

String.prototype.checkCreditCard = function () {
  var cardNumber = this.replace(/[\s\-]/g,"");

  if (!cardNumber.length) {
    return "";
  }

  if (!cardValid(cardNumber)) {
    return "";
  }

  // Establish card type
  var candidates = new Array();
  var cardLength = cardNumber.length;

  for (var i=0;i<ccNames.length;i++) {
    // See if it is this card (ignoring the case of the string)
    if (-1!=ccNames[i].length.find(cardLength)) {
      candidates.push(i);
    }
  }

  // If card type not found, report an error
  if (candidates.length < 1) {
    return "";
  }

  var winner = ccNames[0];
  // Check if prefix matches
  for (i in candidates) {
    winner = ccNames[candidates[i]];

    if(!winner){continue}

    for (var n in winner.prefixes) {
      var rx = new RegExp("^"+winner.prefixes[n]);

      if (rx.test(cardNumber)) {
        return winner.name;
      }
    }
  }

  // The credit card is in the required format.
  return "";
}

function validateCC (elem) {
  if (!elem) {
    elem = "cc-field";
  }

  elem = document.getElementById(elem);

  if(!elem){return}

  for (var n in ccNames) {
    var image = document.getElementById("cc-"+ccNames[n].name);
    if(!image){continue}
    image.src = _cc_images_path+ccNames[n].name+"_gray.gif";
  }

  var cc = elem.value.checkCreditCard();
  image = document.getElementById("cc-"+cc);

  if (!image) {
    elem.style.color = "#dc6666";

    elem = document.getElementById("cc-images");
    if (elem) {
      elem.style.display = "none";
    }

    elem = document.getElementById("cc-not-supported");
    if (elem) {
      elem.style.display = "block";
    }

    return;
  }

  var oldNumber = elem.value.replace(/[^\d]/g,"");
  var newNumber = "";

  if (cc == "amex") {
    newNumber = oldNumber.substr(0,4)+"-"+oldNumber.substr(4,6)+"-"+oldNumber.substr(10,5);
  }
  else {
    do {
      newNumber += oldNumber.substr(0,4);
      oldNumber = oldNumber.substr(4);

      if (oldNumber) {newNumber += "-";}
    } while (oldNumber);
  }

  elem.value = newNumber;

  image.src = _cc_images_path+cc+".gif";
  //elem.style.color = "inherit";
  elem.style.color = "#000";

  elem = document.getElementById("cc-images");
  if (elem) {
    elem.style.display = "block";
  }

  elem = document.getElementById("cc-not-supported");
  if (elem) {
    elem.style.display = "none";
  }
}
/*****************************************************************
  Just declare the vars and function in case of non Control Group
 ****************************************************************/

