{include file="header.tpl"}

<script type="text/javascript">
discounts = [
{section loop=$discounts name=i}
{literal}{{/literal}"from":{$discounts[i].from}, "to":{$discounts[i].to}, "discount":{$discounts[i].discount}{literal}}{/literal}{if !$smarty.section.i.last},{/if}
{/section}
];
{literal}
function chng_type(id) {
  mnu = document.getElementById('menu');
  for (i = 1; i < 200; i++) {
    obj = document.getElementById('at_li_'+i);
    if (obj) obj.className = '';
    obj = document.getElementById('form_'+i);
    if (obj) obj.style.display = 'none';
  }
  obj = document.getElementById('at_li_'+id);
  obj.className = 'active';
  obj = document.getElementById('form_'+id);
  obj.style.display = '';

  calc_price();
}

function chng_price(price, tid) {
  mnu = document.getElementById('menu_'+tid);
  for (i = 0; i < mnu.children.length; i++) {
    mnu.children[i].className = '';
  }
  obj = document.getElementById('pr_li_'+tid+'_'+price);
  obj.className = 'active';

  calc_price();
}

function calc_price() {
  type = _get_type();
  price = _calc_price(type);
  weeks = parseInt(document.getElementById('weeks_'+type).value);
  discount = _get_discount(weeks);
  if (discount > 0) price = price * ((100-discount)/100);

  obj = document.getElementById('tprice_'+type);
  if (obj) obj.innerHTML = price < 0 ? 0 : price;

  obj = document.getElementById('discount_'+type);
  if (obj) obj.innerHTML = discount;
}

function _get_type() {
  type = 0;
  mnu = document.getElementById('menu');
  inpts = mnu.getElementsByTagName('input');
  for (i = 0; i < inpts.length; i++) {
    if (inpts[i].checked) {
      type = inpts[i].value; break;
    }
  }
  return type;
}
function _calc_price(type) {
  price = 0;

  mnu = document.getElementById('menu_'+type);
  if (!mnu) {
    obj = document.getElementById('price_'+type);
    if (obj) price = obj.innerHTML;
  } else {
    inpts = mnu.getElementsByTagName('input');
    for (i = 0; i < inpts.length; i++) {
      if (inpts[i].checked) {
        price = inpts[i].value; break;
      }
    }
  }
  if (price == 0) return -1;

  weeks = parseInt(document.getElementById('weeks_'+type).value);
  if (isNaN(weeks) || weeks < 1) return -2;

  return weeks * price;
}

function _get_discount(weeks) {
  discount = 0;
  for (i in discounts) {
    if (discounts[i].from <= weeks && (discounts[i].to >= weeks || discounts[i].to == 0)) {
      discount = discounts[i].discount;
    }
  }
  return discount;
}

function checkAds() {
  type = _get_type();
  if (!type) {
    alert('Please, select ads zone.');
    return false;
  }
  price = _calc_price(type);
  if (price == -1) {
    alert('Please, choose ad place');
    return false;
  }
  if (price == -2) {
    alert('Please, enter weeks period');
    return false;
  }

  f = document.forms['ads_'+type];                                                     
  if (f.url && !f.url.value.match(/^https?:\/\//)) {
     alert('Please, enter full destination URL');
     return false;
  }

  if (f.banner_url && f.banner_url.value != '' && !f.banner_url.value.match(/^https?:\/\//)) {
     alert('Please, enter your banner full URL (starts with http://)');
     return false;
  }

  if (f.banner_url && f.banner_url.value != '' && !f.banner_url.value.match(/^https?:\/\//)) {
     alert('Please, enter your banner full URL (starts with http://)');
     return false;
  }

  if (f.email && f.email.value != '') {
    status = checkEmail(f.email.value);

    if (status > 0) {
      if (status == 1)
  	    alert('Please, enter your E-Mail address!');
      if (status == 2)
  	    alert('Please, enter a valid E-Mail address!');
      return false;
    }
  }

  return true;
}
</script>
{/literal}

<h3>Advertising</h3>
<div class="buy_ads">

<div class="menu_wrap clearfix">
<img src="{"`$theme`images/buy_ads.png"|encurl}" width="150">
<ul class="menu" id="menu">
{foreach from=$bads item=atype key=tid}
 <li id="at_li_{$tid}"><input type="radio" name="tid" value="{$tid}" id="at_rd_{$tid}" onclick="chng_type('{$tid}')"><label for="at_rd_{$tid}"><b>{$tid}</b>. {$atype.name}</label></li>
{/foreach}
</ul>
</div>

{if $errors}
<ul class="error">
{if $errors.invalid_type}<li> Please, choose correct ads type</li>{/if}
{if $errors.no_available_places}<li> No avaliable places for this ads type</li>{/if}
{if $errors.invalid_period}<li> Please, choose correct period</li>{/if}
{if $errors.invalid_listing}<li> Please, select Program</li>{/if}
</ul>
{/if}

{foreach from=$bads item=atype key=tid}
<div class="{$atype.type}" id="form_{$tid}" style="display: none;">
<h3 id="title_{$tid}">Buy {$atype.name}</h3>
{if $atype.exp_date}
<div class="message">Sorry, all ad places are currently busy.<br>The closest one will be avaliable on <b>{$atype.exp_date}</b> for <b>${$atype.exp_price}</b>/week.</div>
{else} {* exp_date *}
<form name="ads_{$tid}" method="post" onsubmit="return checkAds()" action="">
<input type="hidden" name="a" value="buy_ads">
<input type="hidden" name="action" value="preview">
<input type="hidden" name="tid" value="{$tid}">
<table class="form">
<tr>
 <th>Available:</th>
 <td>{if $atype.max_ads > 0}<b>{$atype.available|intval}</b> of <b>{$atype.max_ads}</b>{else}<b>Unlimited</b>{/if} ad places</td>
</tr>
{if $atype.type == 'rotating'}
<tr>
 <th>Price:</th>
 <td><b>$<span id="price_{$tid}">{$atype.price}</span></b>/week</td>
</tr>
{else}
<tr>
 <th>Ad Place:</th>
 <td>
<ul class="menu" id="menu_{$tid}">
{foreach from=$atype.ads item=p key=price}
 <li id="pr_li_{$tid}_{$price}">
{if $p.type == 'default'}
  <input type="radio" name="price" value="{$price}" id="pr_rd_{$tid}_{$price}" {if $price == $frm.price}checked{/if} onclick="chng_price('{$price}', '{$tid}')">
  <label for="pr_rd_{$tid}_{$price}">
   Available now for <b>${$p.price}</b>/week
  </label>
{else}
  <label for="pr_rd_{$tid}_{$price}">
   Available on <b>{$p.exp_date}</b> for <b>${$p.price}</b>/week
  </label>
{/if}
 </li>
{/foreach}
</ul>
 </td>
</tr>
{/if}

<tr>
 <th>Period:</th>
 <td><input type="text" name="period" value="{$frm.period|intval|default:1}" style="width: 50px;" id="weeks_{$tid}" onkeyup="calc_price()"> weeks</td>
</tr>
{if $discounts}
<tr>
 <th>Discount:</th>
 <td><b id="discount_{$tid}">0</b>%</td>
</tr>
{/if}


{if $atype.type == 'listings'}

<tr>
 <th>Program:</th>
 <td>
  <select name="listing_id">
{foreach from=$blistings item=l}
<option value="{$l.id}">{$l.name|escape:html}</option>
{/foreach}
  </select>
 </td>
</tr>

{else} {* type *}

<tr>
 <th>Destination URL:</th>
 <td><input type="text" name="url" value="{$frm.url|escape:html}"></td>
</tr>
<tr>
 <th>Banner URL:</th>
 <td><input type="text" name="banner_url" value="{$frm.banner_url|escape:html}"></td>
</tr>
<tr>
 <th>Titile:</th>
 <td><input type="text" name="title" maxlength=80 value="{$frm.title|escape:html}"></td>
</tr>
<tr>
 <th>Text:</th>
 <td><input type="text" name="text" maxlength=120 value="{$frm.text|escape:html}"></td>
</tr>

{/if}  {* type *}

<tr>
 <th>Contact E-Mail:</th>
 <td><input type="text" name="email" value="{$frm.email|escape:html}"></td>
</tr>
<tr>
 <th>Total Price:</th>
 <td><b>$<span id="tprice_{$tid}"></span></b></td>
</tr>
<tr>
 <th></th>
 <td><input type=submit value="Preview"></td>
</tr>
</table>
</form>
{/if} {* exp_date *}
</div>

{/foreach}

</div> {* buy_ads *}

{if $frm.tid}
<script type="text/javascript">
tid = parseInt('{$frm.tid|intval}');
{literal}
if (obj = document.getElementById('at_rd_'+tid)) {
 obj.checked = true;
 chng_type(tid);
}
{/literal}
</script>
{/if}
{include file="footer.tpl"}