function adtag_place_ad_here(sz)
{
  adtag_place_ad_here_with_url(sz, document.URL);
}
function adtag_place_ad_here_with_url(sz, url) 
{
  var tags = adtag_calculate_tag(sz, url);
  var tagline = '<scr'+'ipt LANGUAGE="JavaScript" SRC="'+tags+'?" type="text/javascript"></scr'+'ipt>';
  document.write(tagline);
}
function adtag_calculate_tag(sz, url)
{
  // alert('url :: ' + url + " :: size :: " + sz);
  var sysparams = adtag_calculate_sysparams(sz);
  // alert("sysparams :: " + sysparams);
  var preprocessed_url = adtag_preprocess_url(url);
  // alert("preprocessed_url :: " + preprocessed_url);
  var tag_with_no_sysparams = adtag_parse_url_to_tag(preprocessed_url);
  // alert("tag_with_no_sysparams :: " + tag_with_no_sysparams);
  var tags = adtag_assemble_tags(tag_with_no_sysparams, sysparams); 
  // alert("tags :: " + tags);
  return tags;
}
function adtag_calculate_sysparams(sz)
{
  if (typeof condenetads_ord == "undefined"){condenetads_ord = Math.round(Math.random()*10000000000000000);}
  if (typeof tile_num == "undefined"){ tile_num=1; } else { tile_num++; }
  var sysparams_template = 'sz=' + sz + ';tile=2;ord=116138112';
  _sysparams = sysparams_template.split(';');
  re_tile = /(tile=)[0-9]*/;
  re_ord = /(ord=)[0-9]*/;
  sysparams = _sysparams[0] + ';' + _sysparams[1].replace(re_tile, '$1') + tile_num + ';' + _sysparams[2].replace(re_ord, '$1') + condenetads_ord;
  return sysparams;
}
function adtag_preprocess_url(myurl)
{
  // get rid of the possible extra space or '/' at the end of the url string
  while ((myurl.charAt(myurl.length -1) == '/') || (myurl.charAt(myurl.length -1) == ' '))
  {
    myurl = myurl.substring(0, myurl.length - 1);
  }
  var pp_pieces = myurl.split("/");
  var num_of_ppp = pp_pieces.length;
  // case-by-case processing
  if ((num_of_ppp == 5) && (pp_pieces[3].toLowerCase() == "wireservice") && (
    (pp_pieces[4].toLowerCase().substring(0, 13) == "headlines.php") || (pp_pieces[4].toLowerCase().substring(0, 9) == "story.php"))) 
  {
    // handle cases for http://www.wired.com/wireservice/headlines.php?...
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + pp_pieces[3];
    var bbb = pp_pieces[4].toLowerCase().indexOf("section=");
    if (bbb > 0)
    {
      var eee = pp_pieces[4].indexOf("&", bbb);
      if (eee < 0)
      {
        eee = pp_pieces[4].length;
      }
      var mytopic = pp_pieces[4].substring(bbb + 8, eee);
      myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + pp_pieces[3] + "/" + mytopic;
    }
  }
  else if ((num_of_ppp == 5) && (pp_pieces[3].toLowerCase() == "services") && (pp_pieces[4].toLowerCase() == "blogs"))
  {
    // handle case for http://www.wired.com/services/blogs
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + pp_pieces[4];
  }
  else if ((num_of_ppp == 5) && (pp_pieces[3].toLowerCase() == "archive") && (pp_pieces[4].toLowerCase() == "index.html"))
  {
    // handle case for http://www.wired.com/archive/index.html
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + pp_pieces[3];
  }
  else if ((num_of_ppp == 5) && (pp_pieces[3].toLowerCase() == "support") && (pp_pieces[4].toLowerCase() == "blogs.html"))
  {
    // handle case for http://www.wired.com/support/blogs.html
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + "blogs";
  }
  else if ((num_of_ppp == 5) && (pp_pieces[4].toLowerCase().indexOf(".htm") > 0))
  {
    // handle case for http://www.wired.com/<zone>/<topic>.htm? - get rid of the suffix
    var mytopic = pp_pieces[4].substring(0, pp_pieces[4].toLowerCase().indexOf(".htm"));
    if ((pp_pieces[3].toLowerCase() == "support") && (mytopic.indexOf("_instructions") > 0))
    {
      var underbar_index = mytopic.indexOf("_instructions");
      mytopic = mytopic.substring(0, underbar_index) + mytopic.substring(underbar_index + 1);
    }
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + pp_pieces[3] + "/" + mytopic;
  }
  else if ((num_of_ppp == 4) && (pp_pieces[3].toLowerCase().indexOf(".htm") > 0))
  {
    // handle case for http://www.wired.com/<zone>.html etc - get rid of the suffix
    var myzone = pp_pieces[3].substring(0, pp_pieces[3].toLowerCase().indexOf(".htm"));
    myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + myzone;
  }
  else if (pp_pieces[2].split(".")[0].indexOf("blog") >= 0)
  {
    // handle cases for http://blog.wired.com/...
    if (num_of_ppp < 4)
    {
        myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + "blogs";
    }
    else
    {
      myurl = pp_pieces[0] + "/" + pp_pieces[1] + "/" + pp_pieces[2] + "/" + "blogs" + "/" + pp_pieces[3];
    }
  }
  return myurl;
}
function adtag_parse_url_to_tag(myurl)
{
  var dc_tag = "http://ad.doubleclick.net/adj/wiredcom.dart/";
  var dc_constant_kw = "abr=!webtv;dcopt=ist";
  var url_pieces = myurl.split("/");
  var num_of_url_pieces = url_pieces.length;
  
  // start to calculate
  if (num_of_url_pieces < 4) 
  {
    dc_tag = dc_tag + "homepage";
  }
  else 
  {
    var myzone = url_pieces[3];
    if (num_of_url_pieces < 5)
    {
      dc_tag = dc_tag + myzone + ";page=index";
    }
    else
    {
      var mysection = url_pieces[4];
      dc_tag = dc_tag + myzone + ";kw=" + mysection;
    }
  }
  dc_tag = dc_tag + ";" + dc_constant_kw;

  return dc_tag;
}
function adtag_assemble_tags(tag_with_no_sysparams, mysysparams)
{
  return tag_with_no_sysparams + ";" + mysysparams;
}

