{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 {
    custrd = document.getElementById('pr_rd_'+type+'_cust');
    if (custrd && custrd.checked) {
      pr = document.getElementById('pr_inp_'+type+'_cust');
      if (pr && !isNaN(pr.value)) {
        price = pr.value;
      }
    } 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 check_cust_price(type) {
   pr = document.getElementById('pr_inp_'+type+'_cust');
   price = pr.value;
   parr = pr.getAttribute('rel').split(/\|/);
   price_min = parr[0];
   price_inc = parr[1];

   if (isNaN(price)) {
     pr.value = price_min;
     pr.focus();
     return false;
   }
   if (price < price_min) {
     alert('Price can not be less $'+price_min);
     pr.value = price_min;
     pr.focus();
     calc_price();
     return false;
   }
   if (price_inc > 0 && (price - price_min)%price_inc != 0) {
     alert('Price should multiple $'+price_inc);
     pr.value = parseFloat(price_min) + Math.round((price - price_min)/price_inc)*price_inc;
     pr.focus();
     calc_price();
     return false;
   }

   return true;
}

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;
  }

  custrd = document.getElementById('pr_rd_'+type+'_cust');
  if (custrd && custrd.checked) {
   check_cust_price(type);
  }

  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>{t}Advertising{/t}</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.place_busy}<li> {t}Place is busy{/t}</li>{/if}
{if $errors.invalid_type}<li> {t}Please, choose correct ads type{/t}</li>{/if}
{if $errors.no_available_places}<li> {t}No available places for this ads type{/t}</li>{/if}
{if $errors.invalid_period}<li> {t}Please, choose correct period{/t}</li>{/if}
{if $errors.invalid_listing}<li> {t}Please, select Program{/t}</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}">{t name=$atype.name}Buy %name{/t}</h3>
{if $atype.exp_date}
<div class="message">{t exp_date=$atype.exp_date price=$atype.exp_price}Sorry, all ad places are currently busy.<br>The closest one will be available on <b>%exp_date</b> for <b>$%price</b>/week.{/t}</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>{t}Available:{/t}</th>
 <td>{if $atype.max_ads > 0}{t available=$atype.available|intval max_ads=$atype.max_ads}<b>%available</b> of <b>%max_ads</b>{/t}{else}<b>{t}Unlimited{/t}</b>{/if} {t}ad places{/t}</td>
</tr>
{if $atype.type == 'rotating'}
<tr>
 <th>{t}Price:{/t}</th>
 <td><b>$<span id="price_{$tid}">{$atype.price}</span></b>/{t}week{/t}</td>
</tr>
{else}
<tr>
 <th>{t}Ad Place:{/t}</th>
 <td>
<ul class="menu" id="menu_{$tid}">
{if $atype.max_ads == 0}
 <li id="pr_li_{$tid}_cust">
  <input type="radio" name="price" value="cust" id="pr_rd_{$tid}_cust" {if 'cust' == $frm.price}checked{/if} onclick="chng_price('cust', '{$tid}')">
  <label for="pr_rd_{$tid}_cust" class="nosize">
   {t}Offer Price:{/t}
  </label>
  <input type="text" name="cprice" value="{$frm.cprice|default:$atype.price}" id="pr_inp_{$tid}_cust" onblur="check_cust_price('{$tid}')" class="nosize" rel="{$atype.price}|{$atype.price_inc}">
 </li>
{/if}
{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}">
   {t price=$p.price}Available now for <b>$%price</b>/week{/t}
  </label>
{else}
  <label for="pr_rd_{$tid}_{$price}">
   {t price=$p.price exp_date=$p.exp_date}Available on <b>%exp_date</b> for <b>$%price</b>/week{/t}
  </label>
{/if}
 </li>
{/foreach}
</ul>
 </td>
</tr>
{/if}

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


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

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

{/if}  {* type *}

<tr>
 <th>{t}Contact E-mail:{/t}</th>
 <td><input type="text" name="email" value="{$frm.email|escape:html}"></td>
</tr>
<tr>
 <th>{t}Total Price:{/t}</th>
 <td><b>$<span id="tprice_{$tid}"></span></b></td>
</tr>
<tr>
 <th></th>
 <td><input type=submit value="{t}Preview{/t}"></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"}