var oneshow;

function unescape_opera(text)
{
        if(typeof(RegExp) == 'function') {
                re = /quot;/g;
                newstr=text.replace(re, '');
                re = /&/g;
                return newstr.replace(re, '"');
        }
        else return text;
}
function check_edit_Form() {
        formErrors = false;
        if (document.search.query.value.length < 3) {formErrors = "Строка поиска должна содержать не менее 3 символов!";}
        if (formErrors)         {
                                alert(formErrors);
                                return false;
                                }
                                else
                                {
                                return true;
                                }
}

function show(objname, hrefname, quantity) {
  var obj = document.getElementById(objname);
  var href = document.getElementById(hrefname);
  var offsetTop = 0;
  if (oneshow) hide(oneshow);
  oneshow = objname;
  formname = hrefname.replace(/link/i,'form');
  document.getElementById(formname).quant.value=quantity;
  calculate_price(formname);
  obj.style.display = 'none';
  obj.style.display = '';
  document.getElementById(formname).quant.focus();
  while (href) {
  offsetTop += href.offsetTop;
  href = href.offsetParent;
  }
  if (obj.htimer) {
    clearInterval(obj.htimer);
    } else {
  obj.style.top = offsetTop-20;
  obj.stimer=setInterval("showT('" + objname + "', '" + hrefname + "')"  , 10); }
}

function hide(objname) {
  obj = document.getElementById(objname);
  obj.style.display = 'none';
}


function showT(objname, hrefname) {
  var obj = document.getElementById(objname);
  var href = document.getElementById(hrefname);
  var offsetTop = 0;
  while (href) {
  offsetTop += href.offsetTop;
  href = href.offsetParent;
  }
  if (parseInt(obj.style.top) < offsetTop-10) {
    obj.style.top = parseInt(obj.style.top)+2;
  } else {
    obj.style.top = offsetTop-10;
    clearInterval(obj.stimer);
  }
}

function validate_quantity(evt, formname) {
  var charCode = (evt.which) ? evt.which : event.keyCode
  if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
    return false;
    }
    else
    {
    return true;
    }
}

function calculate_price(formname) {
  var formw = document.getElementById(formname);
  var quanw = formw.quant.value;
  var table = formw.quantity_array.value;
  var xmpq = 1;
  pricename = formname.replace(/form/i,'price');
  if (quanw.replace(/^\d+$/,"").length < 1)
      {
      table_array = table.split("#");
      for (i=0; i < table_array.length; i++)
           {
           table_array_item = table_array[i];
           table_array_item_array = table_array_item.split(":");
           if ((quanw < parseInt(table_array_item_array[0])) && (i < 1))
               {
               document.getElementById(pricename).innerHTML = table_array_item_array[1];
               xmpq = parseInt(table_array_item_array[0]);
               }
               else
               {
               if (quanw >= parseInt(table_array_item_array[0]))
                   {
                   document.getElementById(pricename).innerHTML = table_array_item_array[1];
                   xmpq = parseInt(table_array_item_array[0]);
                   }
               }
           }
      input_hash_string = formw.xname_array.value + ":::" + formw.xmantext_array.value + ":::" + xmpq + ":::" + document.getElementById(pricename).innerHTML + ":::" + formw.stock_id.value;
      encode_hash = base64_encode(utf8_encode(input_hash_string));
      formw.id.value = encode_hash;
      }
}

function base64_encode(a) {
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
    var i, n = a.length, tmp = 0, s = Array();
    i = j = 0;
    while (i < n) {
        tmp = (tmp << 8) | a[i];
        if (i % 3 == 2) {
            s[s.length] = chars[(tmp >> 18) & 0x3f];
            s[s.length] = chars[(tmp >> 12) & 0x3f];
            s[s.length] = chars[(tmp >> 6) & 0x3f];
            s[s.length] = chars[tmp & 0x3f];
            j += 4;
            if (j % 76 == 0) s[s.length] = '\r\n';
            tmp = 0;
        }
        i++;
    }
    switch (i % 3) {
    case 1 :
        s[s.length] = chars[(tmp >> 2) & 0x3f];
        s[s.length] = chars[(tmp << 4) & 0x3f];
        s[s.length] = '=';
        s[s.length] = '=';
        break;
    case 2 :
        s[s.length] = chars[(tmp >> 10) & 0x3f];
        s[s.length] = chars[(tmp >> 4) & 0x3f];
        s[s.length] = chars[(tmp << 2) & 0x3f];
        s[s.length] = '=';
        break;
    }
    return s.join('');
}

function base64_decode(s) {
    s = s.split('');
    var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
    var i, j, codes = Object(), a = Array(), n = s.length, tmp = 0;
    for (i = 0; i < 64; i++) codes[chars[i]] = i; // формируем таблицу кодов
    i = j = 0;
    while (i < n) {
        while (i < n && isNaN(codes[s[i]]) && s[i] != '=') i++; // пропускаем неверные символы
        if (i == n || s[i] == '=') break;
        tmp = (tmp << 6) | codes[s[i]];
        if (j % 4 == 3) {
            a[a.length] = (tmp >> 16) & 0xff;
            a[a.length] = (tmp >> 8) & 0xff;
            a[a.length] = tmp & 0xff;
            tmp = 0;
        }
        j++;
        i++;
    }
    switch (j % 4) {
    case 2 :
        a[a.length] = (tmp >> 4) & 0xff;
        break;
    case 3 :
        a[a.length] = (tmp >> 10) & 0xff;
        a[a.length] = (tmp >> 2) & 0xff;
        break;
    }
    return a;
}

function utf8_encode(s) {
    var n = s.length, i, a = Array(), ch;
    for (i = 0; i < n; i++) {
        ch = s.charCodeAt(i);
        if (!(ch >> 7)) a[a.length] = ch; // один байт
        else if (!(ch >> 11)) {
            // два байта
            a[a.length] = 0xc0 | (ch >> 6);
            a[a.length] = 0x80 | (ch & 0x3f);
        } else if (!(ch >> 16)) {
            // три байта
            a[a.length] = 0xe0 | (ch >> 12);
            a[a.length] = 0x80 | ((ch >> 6) & 0x3f);
            a[a.length] = 0x80 | (ch & 0x3f);
        } else {
            // четыре байта
            a[a.length] = 0xf0 | (ch >> 18);
            a[a.length] = 0x80 | ((ch >> 12) & 0x3f);
            a[a.length] = 0x80 | ((ch >> 6) & 0x3f);
            a[a.length] = 0x80 | (ch & 0x3f);
        }
    }
    return a;
}

function utf8_decode(a) {
    var n = a.length, i, s = '', ch;
    i = 0;
    while (i < n) {
        if (!(a[i] & 0x80)) ch = a[i]; // один байт
        else if (i + 1 < n && !((a[i + 1] & 0xc0) ^ 0x80)) {
            if (!((a[i] & 0xe0) ^ 0xc0)) {
                ch = ((a[i] & 0x1f) << 6) | (a[i + 1] & 0x3f); // два байта
                i++;
            }
            else if (i + 2 < n && !((a[i + 2] & 0xc0) ^ 0x80)) {
                if (!((a[i] & 0xf0) ^ 0xe0)) {
                    ch = ((a[i] & 0x0f) << 12) | ((a[i + 1] & 0x3f) << 6) | (a[i + 2] & 0x3f); // три байта
                    i += 2;
                }
                else if (i + 3 < n && !((a[i + 3] & 0xc0) ^ 0x80)) {
                    if (!((a[i] & 0xf8) ^ 0xf0)) {
                        ch = ((a[i] & 0x07) << 18) | ((a[i + 1] & 0x3f) << 12) | ((a[i + 2] & 0x3f) << 6) | (a[i + 3] & 0x3f); // четыре байта
                        i += 3;
                    }
                    else {
                        // неверный формат символа с индексом i
                    }
                } else {
                    // неверный формат символа с индексом i + 3
                }
            } else {
                // неверный формат символа с индексом i + 2
            }
        } else {
            // неверный формат символа с индексом i + 1
        }
        s += String.fromCharCode(ch);
        i++;
    }
    return s;
}
