function checkamount () {
  $.ajax({  type: 'POST',
            url: '/index.php?do=order&method=AJAX',
            data: 'action=wares_count',
            success: function(output) {
              if(output == '' || output == '0') {
                gebi('tnum').innerText = ''
                gebi('tnum_d').innerText = '';
              } else {
                $("#tnum").text(output);
                gebi('tnum_d').innerText = '';
              }
            }
         });
}

function getbalance () {
  $.ajax({  type: 'POST',
            url: '/index.php?do=order&method=AJAX',
            data: 'action=balance',
            success: function(output) {
              if(gebi("hv_balance") != undefined) gebi('hv_balance').style.display = '';
              gebi('h_balance').innerText = output + ' руб';
            }
         });
}

function add2basket (posid, obj) {
  $.ajax({  type: 'POST',
            url: '/index.php?do=order&method=AJAX',
            data: 'action=add_ware&ware_id='+posid,
            success: function(output) {
              var amount = ($("#tnum").text()*1)+1;
              //gebi('tnum_d').innerText = ' ';
              $("#tnum").text(amount);
              $("#tnum_d").text("");
              
              if(gebi("reg_order") != undefined)
                gebi('reg_order').style.display = '';

              if(gebi("o_but_"+posid) != undefined)
                gebi("o_but_"+posid).style.display = 'none';
              if(gebi("o_price_"+posid) != undefined)
                gebi("o_price_"+posid).style.display = 'none';
              if(gebi("d_but_"+posid) != undefined)
                gebi("d_but_"+posid).style.display = 'none';
              if(gebi("d_price_"+posid) != undefined)
                gebi("d_price_"+posid).style.display = 'none';
                
              if(gebi("added_"+posid) != undefined)
                gebi("added_"+posid).style.display = '';
              if(gebi("orders_"+posid) != undefined)
                gebi("orders_"+posid).style.display = '';
            }
         });
  return false;
}
/*************************************************************************************************************************/

function gebi( obj ) {
  return document.getElementById(obj);
}

function str_replace ( search, replace, subject ) {
	if(!(replace instanceof Array)){
		replace=new Array(replace);
		if(search instanceof Array){
			while(search.length>replace.length){
				replace[replace.length]=replace[0];
			}
		}
	}
	if(!(search instanceof Array))search=new Array(search);
	while(search.length>replace.length){
		replace[replace.length]='';
	}
	if(subject instanceof Array){
		for(k in subject){
			subject[k]=str_replace(search,replace,subject[k]);
		}
		return subject;
	}
	for(var k=0; k<search.length; k++){
		var i = subject.indexOf(search[k]);
		while(i>-1){
			subject = subject.replace(search[k], replace[k]);
			i = subject.indexOf(search[k],i);
		}
	}
	return subject;
}

function intval ( mixed_var, base ) {
  var tmp;
  if( typeof( mixed_var ) == 'string' ) {
    tmp = parseInt(mixed_var);
    if(isNaN(tmp)) {
      return 0;
    } else {
      return tmp.toString(base || 10);
    }
  } else if( typeof( mixed_var ) == 'number' ) {
    return Math.floor(mixed_var);
  } else {
    return 0;
  }
}

function change_amount (obj, price, id) {
var value = obj.value;
obj.style.border = '1px solid black';
if(value == '') {
  return false;  
}
if(value < 1) {
  delete_ware(id);
  return false;
}
if(value > 10) {
  obj.style.border = '1px solid red';
  obj.value = '10';
}
//alert(gebi("names['1']").value);return 1;
  $.ajax({ async: false, 
           type: 'POST', 
           url: '/index.php?do=order&method=AJAX', 
           data: 'action=change_amount&ware_id='+gebi('names['+id+']').value+'&ware_amount='+obj.value });
  gebi('amounts['+id+']').value = obj.value;
  recalculate();
}

function fast_buy (ware_id) {
  $.ajax({ async: false,
           type: 'POST',
           url: '/index.php?do=order&method=AJAX',
           data: 'action=fast_buy&ware_id='+ware_id,
           success: function(output) {
            if(output == '') {
              document.location.href="/?do=order&wareid="+ware_id;
              return true;
            }
            if(gebi('dl_content_'+ware_id) != undefined) {
              gebi('dl_content_'+ware_id).innerHTML = str_replace('Download', 'Скачать', output);
            } else {
              gebi('dl_content').innerHTML = str_replace('Download', 'Скачать', output);
            }
            getbalance();
            return false;
           }
         });
}

function delete_ware (ware_id) {
  $.ajax( { async: false, 
            type: 'POST', 
            url: '/index.php?do=order&method=AJAX', 
            data: 'action=del_ware&ware_id='+gebi("names["+ware_id+"]").value } );
  gebi("tr_"+ware_id).style.display = 'none';
  gebi("names["+ware_id+"]").value = '';
  gebi("amounts["+ware_id+"]").value = '';
  gebi("prices["+ware_id+"]").value = '';
  recalculate();
}

function show_intbill () {
  recalculate();
}

function recalculate () {
  var payment_summ = 0;
  var discount_summ = 0;
  var intbill_summ = 0;
  var total_summ = 0;
  var counter = 1;
  var all_amount = 0;
  while(true) {
    if(gebi('prices['+counter+']') != undefined) {
      var ware_price = ((gebi('prices['+counter+']').value * 1) * (gebi('amounts['+counter+']').value * 1) * 1);
      gebi('total_price_'+counter).innerHTML = intval(ware_price);//ware_price.toFixed(2);
      payment_summ += ware_price;
      all_amount += gebi('amounts['+counter+']').value * 1;
      counter++;
    } else break;
  }
  if(payment_summ == 0) {
    document.location.href="/?do=order";
    return false;
  }
  $.ajax( { async: false,
            type: 'POST',
            url: '/index.php?do=order&method=AJAX',
            data: 'calcdiscount='+payment_summ,
            success: function(output){ discount_summ = output; } } ); 

  intbill_summ = gebi('balance').value * 1;
  if(intbill_summ > discount_summ) intbill_summ = discount_summ;
  total_summ = discount_summ;
  discount_summ = payment_summ - discount_summ;
  if( all_amount == 0 ) {
    gebi('tnum').innerText = '';
    gebi('tnum_d').innerText = '';
    if(gebi("reg_order") != undefined) gebi('reg_order').style.display = 'none';
  } else {
    gebi('tnum').innerText = all_amount;
    gebi('tnum_d').innerText = '';
    if(gebi("reg_order") != undefined) gebi('reg_order').style.display = '';
  }
  if (discount_summ < 0) discount_summ = 0;
  gebi('payment_summ').innerHTML = intval(payment_summ);//payment_summ.toFixed(2);
  if(discount_summ > 0) {
    gebi('discount_1').style.display = '';
    gebi('discount_2').style.display = '';
    gebi('discount_summ').innerHTML = intval(discount_summ);//discount_summ.toFixed(2);
  } else {
    gebi('discount_1').style.display = 'none';
    gebi('discount_2').style.display = 'none';
  }
  if (gebi('sub_intbill') != undefined)
    gebi('sub_intbill').innerHTML = gebi('balance').value;
  if (gebi('intbill_summ') != undefined)
    gebi('intbill_summ').innerHTML = intbill_summ; 

  if(gebi('cb1') != undefined && gebi('cb1').checked == false)
    gebi('intbill_summ').innerHTML = '0';

  if(intbill_summ != 0 && (gebi('cb1') != undefined  && gebi('cb1').checked == true)) {
    intbill_summ = intbill_summ * 1;
    total_summ = total_summ - intval(intbill_summ);//intbill_summ.toFixed(2);
  }

  gebi('total_summ').innerHTML = total_summ;
}
