function Pop(num) {
  document.getElementById('pop1').style.display='none';
  document.getElementById('pop2').style.display='none';
  document.getElementById('pop'+num).style.display='';

  document.getElementById('PL1').style.display='none';
  document.getElementById('PL2').style.display='none';
  document.getElementById('Pl'+num).style.display='';
  return false;
}

//Установка кукеса
function setCookie (name, value) {
  var expDate=new Date();
  expDate.setTime(expDate.getTime()+2592000000); //на месяц
  document.cookie=name+"="+escape(value)+"; expires="+expDate.toGMTString()+"; path=/";
}

var highlight = true;
function ChShow(formobj) {
  var sp = document.getElementsByTagName('SPAN');

  if (highlight==true) {
    for (var i=0; i<sp.length; i++)
      if (sp[i].className=='R')
        sp[i].className='B';

    document.getElementById('SHCH').src='/img/ch1.gif';
    setCookie('h', 0);
    highlight=false;
  } else {
    for (var i=0; i<sp.length; i++)
      if (sp[i].className=='B')
        sp[i].className='R';

    document.getElementById('SHCH').src='/img/ch0.gif';
    setCookie('h', 1);
    highlight=true;
  }

  document.SForm.s.focus();
  return false;
}


//Прячет/показывает окно с логином
function SLogin(type) {
  if (document.getElementById('LoginLayer'))
    if (type==0)
      document.getElementById('LoginLayer').style.display = 'none';
    else
      document.getElementById('LoginLayer').style.display = '';
  return false;
}


//Проверка поиска
function Test(obj) {
  obj.s.value = obj.s.value.replace(/^\s+/, '').replace(/\s+$/, '');
  obj.s.value = obj.s.value.replace('  ', ' ');

  if (obj.s.value.length < 3) {
    alert('Строка запроса должна состоять минимум из 3-х символов!');
    obj.s.focus();
    return false;
  }

  return true;
}


function pWin(hr) {
  pw = window.open(hr, 'popw', 'menubar=no, scrollbars=yes, width=400, height=400, top='+(screen.height/2-400/2-100)+', left='+(screen.width/2-500/2));
  pw.focus();
  return false;
}



var searchInputIsActive = false;
var CtrlUp = false;

function Init() {
  if (document.getElementById) {
    if (document.forms['SForm'].s) {
      document.SForm.s.onfocus = function () {
        searchInputIsActive = true;
        if (document.SForm.s.select && CtrlUp) { document.SForm.s.select(); }
      };

      document.SForm.s.onblur = function () { searchInputIsActive = false; CtrlUp = false; };
      document.onkeydown = KeyHook;
    }
  }
}

function KeyHook(e) {
  var code;
  if (!e) var e = window.event;
  if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

  if ((code == 13) && (e.ctrlKey == true)) document.SForm.submit();

  if (!searchInputIsActive) {
    if ((code == 37) && (e.ctrlKey == true)) {
      var dest = document.getElementById('prev');
      if (dest) location.href = dest.href;
    }
    if ((code == 39) && (e.ctrlKey == true)) {
      var dest = document.getElementById('next');
      if (dest) location.href = dest.href;
    }
  }

  if ((code == 38) && (e.ctrlKey == true) && document.SForm.s) {
    CtrlUp = true;
    document.SForm.s.focus();
  }
}



//AJAX
function loadDoc(url) {
  req = false;
  try {
    req=new ActiveXObject('Msxml2.XMLHTTP');
  } catch (e) {
    try {
      req=new ActiveXObject('Microsoft.XMLHTTP'); //Internet Explorer
    } catch (e) {
      if(window.XMLHttpRequest) //Mozilla & Safari
        req=new XMLHttpRequest();
    }
  }

  if (req) {
    req.onreadystatechange = processReq;
    req.open("GET", url, true);
    req.send(null);
    return true;
  }
  return false;
}

function processReq() {
  if (req.readyState == 4 && req.status == 200 && req.responseText != '')
    eval(req.responseText);
}

function Show(num) {
  document.getElementById(num).style.display = '';
}

function Element(num) {
  return document.getElementById(num);
}

function LoadShop(part) {
  loadDoc('/shop.php?part='+encodeURIComponent(part));
}

function tn(event) {
  if (((event.keyCode<48) || (event.keyCode>57)) && event.keyCode!=13)
    event.returnValue=false;
}

function ch(obj, maxval, minval) {
  var x = parseInt(obj.value);
  if (obj.value == '' || x <= 0 || x == NaN) {
     obj.value = '0';
  } else if (minval>0 && x<minval) {
    alert('Минимальное количество заказа для этого компонента составляет '+minval+' шт.');
    obj.value = minval;
    obj.focus();
  } else if (maxval>0 && x>maxval) {
    alert('На складе в наличии есть только '+maxval+' шт. Пожалуйста, обратитесь к нашим менеджерам, возможно они смогут помочь.');
    obj.value = maxval;
    obj.focus();
  }
}
       
function fo(obj) {
  if (obj.value == '0') obj.value = '';
}
