function validate_order_form()
{
  var tmp;
  var i;
  var res = false;        
  var res2 = false;
  var res3 = (the('inp_email').value > '');
  var res4 = true;

  i = 1;
  while (( tmp = the('radio_s'+(i++)) ))
    res = res || tmp.checked;
  i = 1;
  while (( tmp = the('radio_p'+(i++)) ))
    res2 = res2 || tmp.checked;

  if (the('inp_phone'))
    res3 = res3 || (the('inp_phone').value > '');

  if (the('inp_addr'))
    res4 = (the('inp_addr').value > '');
  
  if (res && res2 && res3 && res4)
    return true;
  if (!res3)
    alert ("Неизвестно как с вами свазаться");
  else if (!res4)
    alert ("Неизвестно куда доставить товар");
  else if (!res2)
    alert ("Не заполнен способ оплаты");
  else 
    alert ("Не заполнен способ доставки");
  return false;
}

function fitPopup(popup)
{
  var plate = the("shadow");
  var po = the(popup);
  var whole = the('whole');
  var basement = the('basement');
  var ex = the('excess');
  var excess = whole.offsetHeight - basement.offsetHeight - po.style.top.substr(0,po.style.top.length-2) - po.offsetHeight;

  //alert ('fitpopup ('+excess+') ' + whole.offsetHeight + ' ' + ex.offsetHeight + ' ! '+ex.style.marginBottom);
  if (excess < 0)
  {
    ex.style.marginBottom = (ex.style.marginBottom.substr(0,ex.style.marginBottom.length-2)-(-65)-excess) + 'px';
    plate.style.height = whole.offsetHeight +'px';
  }
}

function fitPage()
{
  var ex = the('excess');
  var whole = the('whole');
  var basement = the('basement');

  ex.style.marginBottom = '0px';

  var wh = window.innerHeight;
  if (!wh)
    wh = document.body.parentElement.clientHeight==0 ? document.body.clientHeight : document.body.parentElement.clientHeight

  var shortage = wh - whole.offsetHeight;
  
  if (shortage > 0)
  {
    ex.style.marginBottom= shortage+'px';
  }
}

function calcDiscount(sum)
{
  var factor = 0-(-the('personal_discount').value);
  var i;
  var tmp;

  i = 0-(-the('discount_count').value);
  while ( i > 0 )
  {
    if ( sum - the('threshold'+i).value >= 0 )
      if ( factor - the('factor'+i).value > 0 )
        factor = 0-(-the('factor'+i).value);
    i--;
  }

  tmp = 0-(-the('money_spent').value);

  i = 0-(-the('discount_a_count').value);
  while ( i > 0 )
  {
    if ( tmp - the('threshold'+i+'_a').value >= 0 )
      if ( factor - the('factor'+i+'_a').value > 0 )
        factor = 0-(-the('factor'+i+'_a').value);
    i--;
  }

  return factor;
}

function cartRecalc()
{
  var i=0;
  var count = the('cart_count').value;
  var count_t = the('cart_count_tangible').value;
  var tmp=0;
  var total_tangibles=0;
  var total_intangibles=0;
  var factor=1;
  var qty;
  var stock;

  while ( i < count )
  {
    i++;
    qty = the('qty'+i).value;
    stock = the('stock'+i).value;
    if (stock != '')
    {
      if (qty - stock > 0)
      { qty = stock; the('qty'+i).value = stock; the('stock'+i+'_hint').style.display = ''; }
    }
    tmp = Math.round(qty * the('price'+i).value * 100)/100;
    the('subtotal'+i).innerHTML = tmp;
    
    if (i <= count_t) 
      total_tangibles += tmp;
    else
      total_intangibles += tmp;
    
    if (0>=tmp)
      the('cart_row'+i).className += ' disabled';
    else
      unlinkStyle('disabled', the('cart_row'+i));
  }

  if (total_tangibles > 0)
  {
    factor = calcDiscount(total_tangibles);

    the('cart_discount').innerHTML = Math.round(100 - factor*100);
    the('cart_discount_abs').innerHTML = Math.round((100-factor*100)*total_tangibles)/100;
    if (factor != 1)
      the('cart_discount_row').style.display='';
    else
      the('cart_discount_row').style.display='none';
    the('cart_total').innerHTML = Math.round(total_tangibles * factor * 100)/100;
  }

  if (total_intangibles > 0)
  {
    factor = calcDiscount(total_intangibles);

    the('cart_v_discount').innerHTML = Math.round(100 - factor*100);
    the('cart_v_discount_abs').innerHTML = Math.round((100-factor*100)*total_intangibles)/100;
    if (factor != 1)
      the('cart_v_discount_row').style.display='';
    else
      the('cart_v_discount_row').style.display='none';
    the('cart_v_total').innerHTML = Math.round(total_intangibles * factor * 100)/100;
  }
}

function requestSS(cid,typ)
{
  if (the('ss_content'))
    makeRequest('GET','/ajax/smart-search-'+typ+'.php?class_id='+cid, '', acceptPage, 'ss_content', '', '', 'quiet');
}

function add2cart(ware_id)
{
  makeRequest('GET', '/ajax/add2cart.php?ware_id='+(0-(-ware_id)), '', acceptInfo, '', 'the("add-item").className+=" added"; requestQuickCart()', '', 'quiet');
}

function saveCart(act)
{
  var q = 'submit=both';
  var i;
  var count = 0;
  if (the('cart_count'))
    count = 0-(-the('cart_count').value); // this is very total count
  var count_t = 0;
  if (the('cart_count_tangible'))
    count_t = 0-(-the('cart_count_tangible').value); // tangibles enumerated first then intagibles

  i=0;
  while ( i < count )
  {
    i++;
    q += '&ware'+i+'='+the('ware'+i).value+'&qty'+i+'='+ 
          ((('erase'==act && i<=count_t) || ('erase_v'==act && i>count_t)) ? 0 : the('qty'+i).value);
  }

  var nextcall = 'requestQuickCart();';
  if ('issue_order'==act) 
    nextcall = 'requestOrderDetailsForm("")';
  if ('issue_order_v'==act)
    nextcall = 'requestOrderDetailsForm("_v")';

  if (count > 0) 
  {
    makeRequest('POST', '/ajax/cart.php', q, acceptInfo, '', nextcall, '', 'quiet');
  }
}

function closeCart(act)
{
  saveCart(act);
  the('shadow').style.display='none';
  the('cart').style.display='none';
  fitPage();
}

function requestOrderDetailsForm(virt)
{
  makeRequest('GET', '/ajax/order_details.php?virt='+virt, '', acceptPage, 'order_details'+virt, "fitPopup('cart')", '', 'quiet');
}

function requestQuickCart()
{
  if (the('quick-cart'))
    makeRequest('GET', '/ajax/quick_cart.php', '', acceptPage, 'quick-cart', '', '', 'quiet');
}

function requestCart(a)
{
  var q = '';
  if ('recalc' == a || 'recalcv' == a)
  {
    var v = '';
    if ('recalcv' == a)
    {
      v = 'v';
      q = '?submit=virt';
    }
    else
    {
      q = '?submit=norm';
    }
    var n = 1;
    var winp;
    var qinp;
    while (winp = the(v+'ware'+n))
    { 
      if (qinp = the(v+'qty'+n))
      {
        q += '&ware'+n+'='+winp.value+'&qty'+n+'='+qinp.value;
      } 
      n++;
    }
  }
  
  displayPopUp(the('cart'), 897, 50);
  the('cart_content').innerHTML='<div class="ico_progress"></div>';
  makeRequest('GET', '/ajax/cart.php', '', acceptPage, 'cart_content', 'cartRecalc(); fitPopup("cart");', '', 'quiet');
}

function requestCaptcha()
{
  if (the('captcha'))
    makeRequest('GET', '/ajax/getcaptcha.php', '', acceptPage, 'captcha', '', '', 'quiet', '', '');
}

function requestSignup(a, cust)
{
  var q = '';
  var errdiv = '';
  var prgdiv = '';
  var nextcall = 'fitPopup("registration");';
  var errcall = '';
  if ('submit' == a)
  {
    var l = base64(the('reg_login').value);
    var p = base64(the('reg_pass').value);
    var n = base64(the('reg_name').value);
    var e = base64(the('reg_email').value);
    var addr = base64(the('reg_addr').value);
    var phone = base64(the('reg_phone').value);

    q = '?login='+l+'&pass='+p+'&name='+n+'&email='+e+'&addr='+addr+'&phone='+phone;
    errdiv = 'reg_error';
    prgdiv = 'reg_progress';
    nextcall = 'closeSignup(); requestLogin();';

    if (!cust)
    {
      var imark = the('input_mark');
      if (!imark)
      {
        requestCaptcha();
        the('reg_error').innerHTML = 'Введите символы с картинки.';
        the('reg_error').style.display = '';
        return;
      }
      var mark = imark.value;
      var code = the('input_code').value;
      if (!code)
      {
        the('reg_error').innerHTML = 'Введите символы с картинки.';
        the('reg_error').style.display = '';
        return;
      }

      q += '&mark='+mark+'&code='+code;
      errcall = 'requestCaptcha(); the("input_code").value=""';
    }
  }
  else
  {
    the('registration_content').innerHTML='<div class="ico_progress"></div>';
    displayPopUp(the('registration'), 280, 50);
  }
  
  makeRequest('GET', '/ajax/signup.php'+q, '', 
              acceptPage, 'registration_content', nextcall, errcall, 'quiet', errdiv, prgdiv);
}

function closeSignup()
{
  the('shadow').style.display='none';
  the('registration').style.display='none';
  fitPage();
}

function requestLogin(a)
{
  var q='';
  if ('login' == a)
  {
    q='?login='+base64(the('input_login').value)+'&pass='+base64(the('input_pass').value);
  }
  else if ('relogin' == a)
  {
    q='?relogin=1';
  }
  makeRequest('GET', '/ajax/login.php'+q, '', 
              acceptPage, 'login-block', 
              document.location.pathname.substr(1,5)=='order'?'document.location.pathname="/order.php"':'', '', 'quiet', 'login-error', '');
}

function cancelOrder(opass)
{
  if (confirm('Отменить заказ?'))
    document.location.pathname="/order.php?cancel="+opass;
}

//%arguments: obj -- object to change image on
//            waredir 
//            id  -- attachment id
//            ext -- attachment file ext
function change_active_image(obj, waredir, id, ext) 
{
  obj.style.backgroundImage = 'url(' +  waredir + id + '.norm' + ext + ')'; 
  obj.href = waredir + id + '.orig' + ext;     
}

