
var gBasketPrice = 0;
var djConfig = {
    isDebug: false
};

//dojo.require("dojo.io.IframeIO");
function search_swap() {
  code = document.getElementById('code-input');
  input2 = document.getElementById('s-input2');
  if (input2.value == "") {
    input2.value = code.value;
    code.value = "";
  }
  else {
    code.value = input2.value;
    input2.value = ""
  }
  error_code_search(code);
}


function shopProductToBasket(pId, amount) {
  if (document.getElementById("addamount"+pId).value == '') {
      document.getElementById("addamount"+pId).value = amount;
  }
  comands = '';
  if (document.getElementById('add2basket'+pId).style.display == '') {
    comands = 'none';
  }
  document.getElementById('add2basket'+pId).style.display = comands;
}

function errorchiki(obj, amount) {
  if (obj.value.match(/^[0-9]+$/) && (obj.value % amount == 0)) {
    obj.style.background = "none";
  }
  else {
    obj.style.background = "#FFDDDD";
  }
}

function error_code_search(obj) {
  if (obj.value.match(/^[0-9]+$/) || (obj.value == "")) {
    obj.style.background = "none";
  }
  else {
    obj.style.background = "#FFDDDD";
  }
}

function shopProductToBasket2(pId, norma, add) {
    //cp.call("shop_be.php", "shop_be_basket_add", shopHighProductCont, pId);
    document.getElementById('add2basket'+pId).style.display='none';
    amount = document.getElementById('addamount'+pId).value;
    amount = parseInt(amount);

    if (amount<=0 || isNaN(amount)) {
        alert('Нужно ввести число');
        return false;
    }
    
    if (norma > 1) {
       if (amount % norma) {
          amount = Math.ceil(amount / norma) * norma;
          if (add < 0)
            alert('Удаляемое количество округлено до '+amount);
          else
            alert('Добавляемое количество округлено до '+amount);
       }
    }
    
    if (add < 0)
      amount = -amount;

    
    dojo.io.bind({
        url: "/shop_be2.php",
        load: function(type, data, evt){
            if (type == "load") {

                result = data;
                if (result=='error')    
                    return false;
                p_td_el = document.getElementById('p_pic_td'+pId);
                if (p_td_el)
                    p_td_el.innerHTML = result;
                old_amount = parseInt(document.getElementById(pId+'amount').innerHTML);
                res = old_amount + amount;
                if (res < 0)
                  res = 0;
                document.getElementById(pId+'amount').innerHTML = res;
                
                    dojo.io.bind({
                        sync: true,
                        url: "/shop_be2.php",
                        load: function(type, data, evt){
                           if (type == "load") {
//                          alert('data2 = '+data);
                                document.getElementById('product_all_count').innerHTML = data;
                           } else
                           if(type == "error") {} 
                           else {}
                        },
                        postContent: "method=shop_be_basket_price",
                        method: "POST",
                        mimetype: "text/plain",
                        transport: "XMLHTTPTransport"
                    });
                
            } else
            if(type == "error") {
            } else {
            }
        },
        postContent: "method=shop_be_basket_add&pid="+pId+"&amount="+amount,
        method: "POST",
        mimetype: "text/plain",
        transport: "XMLHTTPTransport"
    });
/*    
    dojo.io.bind({
        sync: true,
        url: "/shop_be2.php",
        load: function(type, data, evt){
            if (type == "load") {
  //              alert('data2 = '+data);
                document.getElementById('product_all_count').innerHTML = data;
            } else
            if(type == "error") {
            } else {
            }
        },
        postContent: "method=shop_be_basket_price",
        method: "POST",
        mimetype: "text/plain",
        transport: "XMLHTTPTransport"
    });*/
    return false;
}
function shopProductFromBasket(pId) {
    document.getElementById('p_tr'+pId).style.backgroundColor='#ebebeb';
    if (!confirm('удалить продукт из корзины?')) {
        document.getElementById('p_tr'+pId).style.backgroundColor='#ffffff';
        return false;
    } else {
        document.location = 'basket.php?delete_product=1&id=' + pId;
    }
    /*dojo.io.bind({
        url: "/shop_be2.php",
        load: function(type, data, evt){
            if (type == "load") {
                result = data;
                if (result=='error')    
                    return false;
                document.getElementById('p_tr'+pId).style.display='none';

            } else
            if(type == "error") {
            } else {
            }
        },
        postContent: "method=shop_be_basket_del&pid="+pId,
        method: "POST",
        mimetype: "text/plain",
        transport: "XMLHTTPTransport"
    });

    return false;*/
}

function shopBasketDel(result) {
    if (result=='error') return false;
    tr_el = document.getElementById('p_tr'+result);
    if (!tr_el) return false;
    tr_el.style.backgroundColor = '#eeeeee';
    tr_el.style.display='none';
}

function shopHighProductCont(result) {
    tp = document.getElementById('p'+result);
    if (!tp) return false;
    if (tp.style)
        tp.style.backgroundColor = '#eeeeee';
    pa = document.getElementById('pa'+result);
    if (!pa) return false;
    pa.value = parseInt(pa.value)+1;
}

function shopProdEmail(pId) {
    window.open("shop_email.php?pid="+pId, "pemail", "width=500, height=500, resizable=1, scrollbars=1");
}

function replace_string(txt,cut_str,paste_str){ 
  var f=0;
  var ht='';
  ht = ht + txt;
  f=ht.indexOf(cut_str);
  while (f!=-1){ 
    //цикл для вырезания всех имеющихся подстрок 
    f=ht.indexOf(cut_str);
    if (f>0){
      ht = ht.substr(0,f) + paste_str + ht.substr(f+cut_str.length);
    };
  };
return ht
};


function voron_email(id, prefix) {
    if  (document.getElementById(id)) {
      str   = document.getElementById(id).innerHTML;
      pesik = String.fromCharCode(30+34);
      dot   = String.fromCharCode(30+16);
      g     = String.fromCharCode(30+73);
      str   = replace_string(str, "unknown", prefix+dot+'voron'+pesik+g+'mail'+dot+'com');
      document.getElementById(id).innerHTML = str;
    } 
}

