var REQ;
var MyID;
var MyGID;
var MyCMD;

function MyGETRec(MyFunc,MyURL)
{
  if (window.XMLHttpRequest)
  {
    REQ = new XMLHttpRequest();
    REQ.onreadystatechange = MyFunc;
    REQ.open("GET", MyURL, true);
    REQ.send(null);
  }
  else
  if (window.ActiveXObject)
  {
    REQ = new ActiveXObject("Microsoft.XMLHTTP");
    if (REQ)
    {
      REQ.onreadystatechange = MyFunc;
      REQ.open("GET", MyURL, true);
      REQ.send();
    }
  }
}

function MyPOSTRec(MyFunc,MyURL,QSTR)
{
  if (window.XMLHttpRequest)
  {
    REQ = new XMLHttpRequest();
    REQ.onreadystatechange = MyFunc;
    REQ.open("POST", MyURL, true);
    REQ.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    REQ.send(QSTR);
  }
  else
  if (window.ActiveXObject)
  {
    REQ = new ActiveXObject("Microsoft.XMLHTTP");
    if (REQ)
    {
      REQ.onreadystatechange = MyFunc;
      REQ.open("POST", MyURL, true);
      REQ.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      REQ.send(QSTR);
    }
  }
}

function DrvVendSearch()
{
  var SV;
  SV = document.getElementById('DriversCategory').value;
  if (SV != 0)
  {
    MyGETRec(DrvVendSearchResults,'/bin/first.pl?DGID='+SV);
  }
  else
  {
    document.getElementById('DrvVendors').innerHTML = '';
  }
}

function DrvVendSearchResults()
{
  document.getElementById('DrvVendors').innerHTML = '<img src="/progress.gif" />';
  document.getElementById('DrvFindResult').innerHTML = '';
if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('DrvVendors').innerHTML = REQ.responseText;
    }
    else
    {
      document.getElementById('DrvVendors').innerHTML = "There was a problem retrieving the URL";
    }
  }
}

function DrvSearch()
{
  var SV;
  var SG;
  var SS;
  
  SG = document.getElementById('DriversCategory').value;
  SV = document.getElementById('DriversVendors').value;
  SS = '/bin/first.pl?DGID='+SG+'&DVID='+SV
  if (SV != 0)
  {
    MyGETRec(DrvSearchResults,SS);
  }
  else
  {
    document.getElementById('DrvFindResult').innerHTML = '0';
  }
}

function DrvSearchResults()
{
  document.getElementById('DrvFindResult').innerHTML = '<img src="/progress.gif" />';
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('DrvFindResult').innerHTML = REQ.responseText;
    }
    else
    {
      document.getElementById('DrvFindResult').innerHTML = "There was a problem retrieving the URL";
    }
  }
}

function FindOnEnter(e)
{
  var keynum;
  var keychar;

  if(window.event) // IE
	{
    keynum = e.keyCode;
	}
  else if(e.which) // Netscape/Firefox/Opera
	{
    keynum = e.which;
	}
  if (keynum == 13)
  {
    LoadFindResults(0);
  }
}


var Url =
{
  // public method for url encoding
  encode : function (string)
  {
    return escape(this._utf8_encode(string));
  },

  // public method for url decoding
  decode : function (string)
  {
    return this._utf8_decode(unescape(string));
  },

  // private method for UTF-8 encoding
  _utf8_encode : function (string)
  {
    string = string.replace(/\r\n/g,"\n");
    var utftext = "";

    for (var n = 0; n < string.length; n++)
    {
      var c = string.charCodeAt(n);
      if (c < 128)
      {
        utftext += String.fromCharCode(c);
      }
      else if((c > 127) && (c < 2048))
      {
        utftext += String.fromCharCode((c >> 6) | 192);
        utftext += String.fromCharCode((c & 63) | 128);
      }
      else
      {
        utftext += String.fromCharCode((c >> 12) | 224);
        utftext += String.fromCharCode(((c >> 6) & 63) | 128);
        utftext += String.fromCharCode((c & 63) | 128);
      }
    }
    return utftext;
  },

  // private method for UTF-8 decoding
  _utf8_decode : function (utftext)
  {
    var string = "";
    var i = 0;
    var c = c1 = c2 = 0;

    while ( i < utftext.length )
    {
      c = utftext.charCodeAt(i);

      if (c < 128)
      {
        string += String.fromCharCode(c);
        i++;
      }
      else if((c > 191) && (c < 224))
      {
        c2 = utftext.charCodeAt(i+1);
        string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
        i += 2;
      }
      else
      {
        c2 = utftext.charCodeAt(i+1);
        c3 = utftext.charCodeAt(i+2);
        string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
        i += 3;
      }
    }
    return string;
  }
}

function LoadFindResults(CMD)
{
  var MyURL;
  
  if (document.getElementById('FindArea').value == 2)
  {
    MyURL = '/goods_info/'+document.getElementById('FindWords').value;
    window.location.href=MyURL;
  }
  else if (document.getElementById('FindArea').value == 3)
  {
    MyURL = '/NIX/'+document.getElementById('FindWords').value;
    window.location.href=MyURL;
  }
  else
  {
    if (CMD == 0)
    {
      MyID = Url.encode(document.getElementById('FindWords').value);
      MyCMD = '?q='+MyID+'&cmd=Search%21&np=0&ps=15';
    }
    else
    {
      MyCMD = CMD;
    }
  
    if (window.XMLHttpRequest)
    {
      REQ = new XMLHttpRequest();
      REQ.onreadystatechange = ShowFindResults;
      REQ.open("GET", '/bin/search.cgi'+MyCMD, true);
      REQ.send(null);
    }
    else
    if (window.ActiveXObject)
    {
      REQ = new ActiveXObject("Microsoft.XMLHTTP");
      if (REQ)
      {
        REQ.onreadystatechange = ShowFindResults;
        REQ.open("GET", '/bin/search.cgi'+MyCMD, true);
        REQ.send();
      }
    }
  }
}

function ShowFindResults()
{
  document.getElementById('ContentArea').innerHTML =
  '<table border="0" cellpadding="0" cellspacing="0" width="100%">'+
    '<tr>'+
      '<td width="60" align="center" valign="middle"><img src="/searchanim.gif" alt="" width="48" height="48" /></td>'+
      '<td valign="top">'+
        '<h1 class="Content">ИДЕТ ПОИСК. ПОДОЖДИТЕ</h1>'+
      '</td>'+
    '</tr>'+
  '</table>';
  scroll(0,0);
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('ContentArea').innerHTML = REQ.responseText;
    }
    else
    {
      alert("There was a problem retrieving the URL");
    }
  }
  document.getElementById('FindWords').value = Url.decode(MyID);
}

function LoadGoodsChild(GID)
{
  MyGETRec(function(){OpenGoodsTree(GID);},'/bin/tree.pl?GCID='+GID+'&hash=' + Math.random());
}

function LoadZakazChild(GID)
{
  MyGETRec(function(){OpenZakazTree(GID);},'/bin/zakaztree.pl?GCID='+GID+'&hash=' + Math.random());
}

function OpenGoodsTree(GID)
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('i'+GID).onclick = function() {CloseGoodsTree(GID);}
      document.getElementById('i'+GID).className = 'CloseFolderSign';
      document.getElementById('f'+GID).className = 'OpenFolder';
      document.getElementById('t'+GID).innerHTML = REQ.responseText;
    }
    else
    {
      alert("There was a problem retrieving the URL");
    }
  }
}

function OpenZakazTree(GID)
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('zi'+GID).onclick = function() {CloseZakazTree(GID);}
      document.getElementById('zi'+GID).className = 'CloseFolderSign';
      document.getElementById('zf'+GID).className = 'OpenFolder';
      document.getElementById('zt'+GID).innerHTML = REQ.responseText;
    }
    else
    {
      alert("There was a problem retrieving the URL");
    }
  }
}

function CloseGoodsTree(GID)
{
  document.getElementById('i'+GID).onclick = function() {LoadGoodsChild(GID);}
  document.getElementById('i'+GID).className = 'OpenFolderSign';
  document.getElementById('f'+GID).className = 'ClosedFolder';
  document.getElementById('t'+GID).innerHTML = '';
}

function CloseZakazTree(GID)
{
  document.getElementById('zi'+GID).onclick = function() {LoadZakazChild(GID);}
  document.getElementById('zi'+GID).className = 'OpenFolderSign';
  document.getElementById('zf'+GID).className = 'ClosedFolder';
  document.getElementById('zt'+GID).innerHTML = '';
}

function AddToBasket(GID)
{
  MyID = GID;
  MyGETRec(ShowBasketInfo,'/bin/first.pl?BID='+MyID+'&hash=' + Math.random(), true);
}

function AddToZakazBasket(GID)
{
  MyID = GID;
  MyGETRec(ShowZakazBasketInfo,'/bin/first.pl?ZBID='+MyID+'&hash=' + Math.random(), true);
}

function ShowBasketInfo()
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
			document.getElementById('BasketBlock').innerHTML = REQ.responseText;
		}
		else
		{
		  alert("There was a problem retrieving the URL");
		}
  }
}

function ShowZakazBasketInfo()
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
			document.getElementById('ZBasketBlock').innerHTML = REQ.responseText;
		}
		else
		{
		  alert("There was a problem retrieving the URL");
		}
  }
}

function GetCheckForm()
{
  MyGETRec(ShowCheckForm,'/checkform.htm');
}

function GetZakazCheckForm()
{
  MyGETRec(ShowCheckForm,'/zcheckform.htm');
}

function ShowCheckForm()
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('CheckForm').innerHTML = REQ.responseText;
    }
  }
}

function CheckMySubmit()
{
  var UserName;
  var PayMethod;
  var PayMethods;
  var ContactPhone;
  var UserAddress;
  var UserComment;
  var URL;
  if (document.getElementById('UserName').value == '')
  {
    alert('Необходимо заполнить Ваше Ф.И.О.');
  }else if (document.getElementById('ContactPhone').value == '')
  {
    alert('Необходимо заполнить Ваш контактный телефон');
  }else if (document.getElementById('GoHome').checked && document.getElementById('UserAddress').value == '')
  {
    alert('Необходимо заполнить адрес доставки');
  }
  else
  {
    UserName = document.getElementById('UserName').value;
    PayMethods = document.getElementsByName('PayMethod');
    if(PayMethods != null)
    {
      for(var i = 0; i < PayMethods.length; i++)
      {
        if(PayMethods[i].checked == true){ PayMethod = PayMethods[i].value}
      }
    }
    ContactPhone = document.getElementById('ContactPhone').value;
    UserAddress = document.getElementById('UserAddress').value;
    UserComment = document.getElementById('UserComment').value;
    URL = '/bin/first.pl?SBID=2'+
          '&UserName='+ Url.encode(UserName)+
          '&PayMethod='+PayMethod+
          '&ContactPhone='+Url.encode(ContactPhone)+
          '&UserAddress='+Url.encode(UserAddress)+
          '&UserComment='+Url.encode(UserComment);
    MyGETRec(ShowPayCheck,URL);
  } 
}

function CheckMyZakazSubmit()
{
  var UserName;
  var PayMethod;
  var PayMethods;
  var ContactPhone;
  var UserAddress;
  var UserComment;
  var URL;
  if (document.getElementById('UserName').value == '')
  {
    alert('Необходимо заполнить Ваше Ф.И.О.');
  }else if (document.getElementById('ContactPhone').value == '')
  {
    alert('Необходимо заполнить Ваш контактный телефон');
  }
  else
  {
    UserName = document.getElementById('UserName').value;
    ContactPhone = document.getElementById('ContactPhone').value;
    UserComment = document.getElementById('UserComment').value;
    URL = '/bin/first.pl?ZSBID=2'+
          '&UserName='+ Url.encode(UserName)+
          '&ContactPhone='+Url.encode(ContactPhone)+
          '&UserComment='+Url.encode(UserComment);
    MyGETRec(ShowPayCheck,URL);
  } 
}

function ShowPayCheck()
{
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('ContentArea').innerHTML = REQ.responseText;
    }
  }
}

function CheckAddress()
{
  document.getElementById('UserAddress').disabled = ! document.getElementById('GoHome').checked;
}

function ShowInetStat()
{
  var UserName;
  var PayMethod;
  var PayMethods;
  var UserPassword;
  var QSTR;
  if (document.getElementById('UserName').value == '')
  {
    alert('Необходимо заполнить Ваш логин');
  }else if (document.getElementById('UserPassword').value == '')
  {
    alert('Необходимо заполнить Ваш пароль');
  }
  else
  {
    UserName = document.getElementById('UserName').value;
    PayMethods = document.getElementsByName('PayMethod');
    if(PayMethods != null)
    {
      for(var i = 0; i < PayMethods.length; i++)
      {
        if(PayMethods[i].checked == true){ PayMethod = PayMethods[i].value}
      }
    }
    UserPassword = document.getElementById('UserPassword').value;
    QSTR = 'IID=2'+
          '&UserName='+ UserName+
          '&PayMethod='+PayMethod+
          '&UserPassword='+UserPassword;
    MyPOSTRec(ShowInetStat2,'/bin/first.pl',QSTR);
  } 
}

function ShowInetStat2()
{
  document.getElementById('InetStatShowArea').innerHTML = '<table border="0" cellpadding="0" cellspacing="0" width="100%">'+
    '<tr>'+
      '<td width="60" align="center" valign="middle"><img src="/searchanim.gif" alt="" width="48" height="48" /></td>'+
      '<td valign="top">'+
        '<h1 class="Content">ИДЕТ ПОИСК. ПОДОЖДИТЕ</h1>'+
      '</td>'+
    '</tr>'+
  '</table>';
  if (REQ.readyState == 4)
  {
    if (REQ.status == 200)
    {
      document.getElementById('InetStatShowArea').innerHTML = REQ.responseText;
    }
  }
}

function ShowTab(TabNum)
{
	if (TabNum == 0)
	{
		document.getElementById('NaSklade').className = 'MenuShow';
		document.getElementById('PodZakaz').className = 'MenuHide';
		document.getElementById('TabNaSklade').className = 'TabOn';
		document.getElementById('TabPodZakaz').className = 'TabOff';
	}
	if (TabNum == 1)
	{
		document.getElementById('NaSklade').className = 'MenuHide';
		document.getElementById('PodZakaz').className = 'MenuShow';
		document.getElementById('TabNaSklade').className = 'TabOff';
		document.getElementById('TabPodZakaz').className = 'TabOn';
	}
}


