scriptpath = '/';

function ask(url) {
 if (confirm ('Вы уверены?!'))
  self.location=url;
}
function askform(url) {
 return confirm ('Вы уверены?!');
}

function round2(val) {
  return Math.round(val*100)/100;
}

function win(url,wname,w,h,r,s) {
   var posX = (screen.width - w) / 2;
   var posY = (screen.height - h) / 2;
   var pos = ",top="+posY+",left="+posX;
   newWin = open(url,wname,"toolbar=0,location=0,status=0,menubar=1,scrollbars="+s+",resizable="+r+",width="+w+",height="+h+pos);
}

function payform(method, id_order) {  if (method == '') {    var radios = document.getElementsByTagName('INPUT');
    for (var i=0; i < radios.length; i++) {      if ((radios[i].name == 'paymethod') && (radios[i].checked)) method = radios[i].value;
    }
  }
  open(scriptpath+'user/payform.php?id='+id_order+'&method='+method,'paywindow',"toolbar=1,location=1,status=1,menubar=1,scrollbars=yes,resizable=yes,width="+(screen.width-200)+",height="+(screen.height-200));
}

function toggle_block(eid) {
 if (document.getElementById(eid).style.display == 'none') {
   document.getElementById(eid).style.display='block';
   document.getElementById(eid+'_').style.display='none';
 } else {
   document.getElementById(eid).style.display='none';
   document.getElementById(eid+'_').style.display='block';
 }
}

function toggle_b(eid) {
 if (document.getElementById(eid).style.display == 'none') {
   document.getElementById(eid).style.display='block';
 } else {
   document.getElementById(eid).style.display='none';
 }
}

function createRequest() {
  var request;
  try {
    request = new XMLHttpRequest();
  } catch (trymicrosoft) {
    try {
      request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (othermicrosoft) {
      try {
        request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (failed) {
        request = false;
      }
    }
  }
  return request;
}

function counttime(id, count) {  var ctitle = 'секунд';
  if (count == 1) ctitle = 'секунду';
  else if ((count == 2) || (count == 3) || (count == 4)) ctitle = 'секунды';  document.getElementById("id_cart_span_"+id).innerHTML = "Это сообщение закроется через " + count + " " + ctitle + ".";
  if (count > 0) window.setTimeout('counttime('+id+', '+(count-1)+')', 1000);
    else document.body.removeChild(document.getElementById('id_cart_'+id));
}

function addtocart(id, name, price, count) {
  var request = createRequest();
  var url = scriptpath + "include/ajax.php?addtocart=" + encodeURIComponent(id) + "&price=" + encodeURIComponent(price) + "&count=" + encodeURIComponent(count);

  var dw = 260, dh = 110;
  var divcart = document.createElement("div");
  divcart.id = "id_cart_" + id;
  divcart.className = 'cartp';
  divcart.style.width = dw + "px";
  divcart.style.height = dh + "px";
  divcart.style.marginTop = -(Math.round(dh/2) - document.documentElement.scrollTop) + "px";
  divcart.style.marginLeft = "-" + Math.round(dw/2) + "px";
  divcart.innerHTML = "<table border='0'><tr><td></td><td><span id='id_cart_h2_"+id+"'>Добавление...</span></td></tr><tr><td></td><td><b>" + name + "</b><p><span id='id_cart_span_"+id+"'></span></p></td></tr><tr><td></td><td><a href='#'  onclick=\"document.body.removeChild(document.getElementById('id_cart_"+id+"')); request.abort(); return false;\">закрыть</a></td></tr></table>";
  document.body.appendChild(divcart);


  request.open("GET", url, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      if (request.status == 200) {
        var resp = request.responseText;
        if (resp.substr(0, 2) == 'OK') {           document.getElementById("id_cart_h2_"+id).innerHTML = "Добавлено";
           var curcount = parseInt(document.getElementById("cart_count").innerHTML);
           var curprice = parseFloat(document.getElementById("cart_price").innerHTML);
           document.getElementById("cart_count").innerHTML = (curcount + count);
           document.getElementById("cart_price").innerHTML = round2(curprice + (price*count));
           counttime(id, 2);
        } else {
           counttime(id, 0);           alert(resp);
        }
      }
    }
  }
  request.send(null);
}

function delitem(id, num) {
  if (window.confirm("Вы уверены?")) {
    var request = createRequest();
    var url = scriptpath + "include/ajax.php?delfromcart=" + (id) + "&price=" + (num*prices[id]) + "&num=" + (num);
    request.open("GET", url, true);
    request.onreadystatechange = function() {
      if (request.readyState == 4) {
        if (request.status == 200) {
          var resp = request.responseText;
          if (resp.substr(0, 2) == "OK") {
            document.getElementById("f_cnt_"+id).value = "0";
            document.getElementById("tr_"+id).style.display = "none";
            settotal();
          } else {            alert(resp);
          }
        }
      }
    }
    request.send(null);
  }
}

function getRadioGroupValue(radioGroupObj)
{
  if (radioGroupObj.value) return radioGroupObj.value;
  for (var i=0; i < radioGroupObj.length; i++)
    if (radioGroupObj[i].checked) return radioGroupObj[i].value;
  return null;
}

function settotal () {
  var postmethod = getRadioGroupValue(document.order_form.postmethod);
  var inputs = document.getElementsByTagName("input");
  var totalcnt=0, totalprice=0, totalweight=0, curid, curcnt;
  for (var i = 0; i < inputs.length; i++) {
    if (inputs[i].name.substring(0, 6) == "f_cnt_") {
       curid = inputs[i].id.substring(6);
       curcnt = parseInt(document.getElementById("f_cnt_"+curid).value);
       if (!isNaN(curcnt)) {
         if (curcnt > maxcounts[curid]) {           document.getElementById("f_cnt_"+curid).value = maxcounts[curid];
           curcnt = maxcounts[curid];
         }
         document.getElementById("f_price_"+curid).innerHTML = round2(prices[curid] * curcnt);
         totalprice += prices[curid] * curcnt;
         totalweight += curcnt*weights[curid];
         totalcnt += curcnt;
       } else {
         document.getElementById("f_price_"+curid).innerHTML = "0";
       }
    }
  }
  if (totalprice >= discountprice) {     totalprice = totalprice*(1-discount);
     document.getElementById("discounttext").innerHTML = ' (со скидкой '+ (discount*100) +'%)';
  }  else {     document.getElementById("discounttext").innerHTML = '';
  }
  if (postmethod == "post") {
    var weightprice = (totalprice ? getweightprice(totalweight) : 0);
    var insprrice = (totalprice+weightprice)*insurance;
  } else if (postmethod == "self") {
    var weightprice = 0;
    var insprrice = 0;
  } else if (postmethod == "negr") {
    var weightprice = negrprice;
    var insprrice = 0;
  }
  document.getElementById("f_weight_price").innerHTML = round2(weightprice + insprrice);
  document.getElementById("f_total_price").innerHTML = round2(totalprice+weightprice+insprrice);
  document.getElementById("f_total_cnt").innerHTML = totalcnt;
  return true;
}

function getweightprice(weight) {
  var init0 = packprice + 105.02;
  var init1500 = packprice + 112.95;
  var weightstep = Math.ceil(weight/20);
  if (weight <= 1500) {
    return init0 + 2.83*weightstep;
  } else {
    return init1500 + 2.83*weightstep;
  }
}


function savevote(id, num) {
  document.getElementById('yourvote'+id).innerHTML = 'Сохранение...';
  document.getElementById('ratestars'+id).style.visibility = 'hidden';
  var errstr = '&nbsp;';
  var request = createRequest();
  var url = scriptpath + "include/ajax.php?rate=1&id=" + id + "&value=" + num;
  request.open("GET", url, true);
  request.onreadystatechange = function() {
    if (request.readyState == 4) {
      if (request.status == 200) {
        var response = request.responseText;
        if (response.substr(0, 2) == 'OK') {
          document.getElementById('yourvote'+id).innerHTML = 'Ваша оценка: '+num+'/5';
          var valarr = response.split(' ');
          document.getElementById('numvotes'+id).innerHTML = valarr[1];
          document.getElementById('currrating'+id).innerHTML = Math.round(valarr[2]*5) / 5;
          var i;
          for (i=1; i <= 5; i++){
            document.getElementById('rateimg'+i+"-"+id).className='star' + ((i <= Math.round(valarr[2])) ? '1' : '2') + '';
            document.getElementById('rateimg'+i+"-"+id).onmouseover = null;
            document.getElementById('rateimg'+i+"-"+id).onmouseout = null;
            document.getElementById('rateimg'+i+"-"+id).onclick = null;
          }
        } else {
          document.getElementById('yourvote'+id).innerHTML = response;
        }
        document.getElementById('ratestars'+id).style.visibility = 'visible';
      } else {
        errstr = 'Ошибка';
      }
    }
  }
  request.send(null);
}

function selectrate(num, ansid, cr) {
  var i;
  for (i=1; i <= 5; i++) {
     if (i <= num) document.getElementById('rateimg'+i+"-"+ansid).className='star0';
     else if (i <= cr) document.getElementById('rateimg'+i+"-"+ansid).className='star1';
     else document.getElementById('rateimg'+i+"-"+ansid).className='star2';
  }
  if (num) document.getElementById('yourvote'+ansid).innerHTML = 'Ваша оценка: '+num+'/5';
      else document.getElementById('yourvote'+ansid).innerHTML = '&nbsp;';
}


