
  var xmlHttp;
  var xmlHttpPr;
  var preload = "<center><img id='preloader' src='i/preloader.gif' /></center>"; 
  var typeOrd;

// COMMON FUNCS

  function GetXmlHttpObject(handler) { 
    var objXmlHttp=null;
    if ( (window.XMLHttpRequest) && (navigator.appVersion.indexOf("MSIE 7.0") == -1) ) {
      objXmlHttp = new XMLHttpRequest();
      objXmlHttp.onload=handler;
      objXmlHttp.onerror=handler; 
    } else if (window.ActiveXObject) {
      try { objXmlHttp = new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) {
        try { objXmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); }
        catch (e) {}
      }
      objXmlHttp.onreadystatechange=handler;
    }
    return objXmlHttp;
  } 



  function getCode(url, func, method, params) {
    if (method == null) { method = "GET"; params = null; } 
    xmlHttp=GetXmlHttpObject(func);
    xmlHttp.open(method, url , true);
    if (method == "POST"){  
      xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
      xmlHttp.setRequestHeader("Content-length", params.length);
      xmlHttp.setRequestHeader("Connection", "close");
    }
    xmlHttp.send(params);
  } 


  function readData() {
    xmlDoc = xmlHttp.responseText;
    xmlDoc = removeNewLines (xmlDoc);
    return xmlDoc;
  }


  function removeNewLines(str){
    while (str.substring(0,1) == "\r") str = str.substring (2, str.length);
    return str;
  }
      
  function get_firstchild(n){
    x=n.firstChild;
    while (x.nodeType!=1) x=x.nextSibling;
    return x;
  }

// ORDER functions
  function loadPrices(delivery, zone, payment){
    $("deliveryPrice").innerHTML = preload; 
    $("paymentPrice").innerHTML = preload; 
    if (!zone) zone=0;
    params = 'delivery=' + delivery + '&zone=' + zone + '&payment=' + payment + '&dummy=' + new Date().getTime();
    url = 'ajax/get_prices.php';
    getCode (url, stateChangedLoadPrice, "POST", params);
  }

  function stateChangedLoadPrice() { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
      txt = readData ();
      arr = txt.split ("|")
      $('deliveryPrice').innerHTML = arr[0];
      $('paymentPrice').innerHTML = arr[1];
    } 
  }




// USER functions

  function savePrivate (id) {
    full_name = $('full_name').value;
    city = $('city').value;
    pk = $('pk').value;
    address = $('address').value;
    phone = $('phone').value;
    params = 'id=' + id + '&full_name=' + full_name + '&city=' + city + '&pk=' + pk + '&address=' + address + '&phone=' + phone + '&type=0';
    url = 'ajax/user_update.php';
    getCode (url, stateChangedSaveUser, "POST", params);
  } 

  function saveFkt (id) {
    company = $('company').value;
    ino = $('ino').value;
    ino_dds = $('ino_dds').value;
    city_fkt = $('city_fkt').value;
    pk_fkt = $('pk_fkt').value;
    address_fkt = $('address_fkt').value;
    mol = $('mol').value;
    params = 'id=' + id + '&company=' + company + '&ino=' + ino + '&ino_dds=' + ino_dds + '&city_fkt=' + city_fkt + '&pk_fkt=' + pk_fkt + '&address_fkt=' + address_fkt + '&mol=' + mol + '&type=1';
    url = 'ajax/user_update.php';
    getCode (url, stateChangedSaveUser, "POST", params);
  } 

  function saveSettings (id) {
    username = $('username').value;
    password = $('password').value;
    password_re = $('password_re').value;
    mail = $('mail').value;

    params = 'id=' + id + '&username=' + username + '&password=' + password + '&password_re=' + password_re + '&mail=' + mail + '&type=2';
    url = 'ajax/user_update.php';
    getCode (url, stateChangedSaveUser, "POST", params);
  } 
  function stateChangedSaveUser() { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") { 
      xmlDoc = xmlHttp.responseText;
      arr = xmlDoc.split("|");
      $('systemMsg').innerHTML = arr[0];
      $('systemMsg').className = (arr[1] == "err")?"red":"gray";
    } 
  }



// ------------------- CATEGORY FUNCS -----------------------

  var getXmlCatsSel = 0;
  var getXmlSubCatsSel = -1;
  var getXmlCatsSelName = "";
  var getXmlSubCatsSelName = "";
  
  function getCatsXML (selCat, subselCat) {
      $("categories").innerHTML = preload; 
      urlXml = 'xml/categories.xml?&dummy=' + new Date().getTime();
      xmlHttp=GetXmlHttpObject(stateChangedCatsXML);
      xmlHttp.open("GET", urlXml , true);
      xmlHttp.setRequestHeader("Content-Type","application/xml; charset=utf-8");
      xmlHttp.send(null);    
      getXmlCatsSel = selCat;
      getXmlSubCatsSel = subselCat;
  } 
  function stateChangedCatsXML() { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
      sel = ""; 
      html = '';

      try {
        res = xmlHttp.responseXML.documentElement.childNodes;
      } catch (e) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xmlHttp.responseText);
        res = xmlDoc.documentElement.childNodes;
      }
      for (i=0;i<res.length;i++){
        if (res.item(i).nodeType != 1) continue;
        sel = ((getXmlCatsSel == res.item(i).attributes[0].value) && (getXmlSubCatsSel == -1))?"sel":"";

        if (res[i].nodeName == "category") {
          html += '<ul><p class="' + sel + '"><a href="?nav=9&cat=' + res.item(i).attributes[0].value + '">' + get_firstchild(res.item(i)).childNodes[0].nodeValue + '</a></p>';
          subs = res[i].childNodes;
          for (j=0;j<subs.length;j++){
            if (subs.item(j).nodeType != 1) continue;
            if (subs[j].nodeName == "subcategory"){
              sel = (getXmlSubCatsSel == subs.item(j).attributes[0].value)?"sel":"";
              html += '<li class="' + sel + '"><a href="?nav=12&cat=' + res.item(i).attributes[0].value + '&subcat=' + subs.item(j).attributes[0].value + '">' + get_firstchild(subs.item(j)).childNodes[0].nodeValue + '</a></li>';
            }
          }
          html += '</ul>';
        }
      }
      $("categories").innerHTML = html; 
    } 
  }


  function getCatName (sCat, subsCat, link) {
      urlXml = 'xml/categories.xml?&dummy=' + new Date().getTime();
      xmlHttpA=GetXmlHttpObject(stateChangedCatName);
      xmlHttpA.open("GET", urlXml , true);
      xmlHttpA.setRequestHeader("Content-Type","application/xml; charset=utf-8");
      xmlHttpA.send(null);    
      cat = sCat;
      subcat = subsCat;
      l = link;
  } 
  function stateChangedCatName() { 
    if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
      try {
        res = xmlHttpA.responseXML.documentElement.childNodes;
      } catch (e) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xmlHttp.responseText);
        res = xmlDoc.documentElement.childNodes;
      }
      for (i=0;i<res.length;i++){
        if (res.item(i).nodeType != 1) continue;

        if (res[i].nodeName == "category") {
          if (cat == res.item(i).attributes[0].value) getXmlCatsSelName = get_firstchild(res.item(i)).childNodes[0].nodeValue;
          subs = res[i].childNodes;
          for (j=0;j<subs.length;j++){
            if (subs.item(j).nodeType != 1) continue;
            if (subs[j].nodeName == "subcategory"){
              if (subcat == subs.item(j).attributes[0].value) getXmlSubCatsSelName = get_firstchild(subs.item(j)).childNodes[0].nodeValue;
            }
          }
        }
      }
      
      if (l){
        $("catIn").innerHTML = '<a href="?nav=9&cat=' + cat + '">' + getXmlCatsSelName + '</a>';
        if (getXmlSubCatsSelName != "") $("catIn").innerHTML += '&nbsp; <strong>&raquo;</strong> &nbsp;<a href="?nav=12&cat=' + cat + '&subcat=' + subcat + '">' + getXmlSubCatsSelName + '</a>';
      } else {
        $("caption").innerHTML = getXmlCatsSelName;
        document.title = "Микроко :: " + getXmlCatsSelName;
        if (getXmlSubCatsSelName != "") $("caption").innerHTML += " &nbsp; <strong>&raquo;</strong> &nbsp; " + getXmlSubCatsSelName;
      }
    } 
  }



// ------------------- PRINTERS FUNCS -----------------------

  var makerg = "";
  var seriesg = "";
  
  function getPrintersXML (maker, series) {
      $("sseries").innerHTML = preload; 
      urlXml = 'xml/printers.xml?&dummy=' + new Date().getTime();
      xmlHttpPr=GetXmlHttpObject(stateChangedPrintersXML);
      xmlHttpPr.open("GET", urlXml , true);
      xmlHttpPr.setRequestHeader("Content-Type","application/xml; charset=utf-8");
      xmlHttpPr.send(null);    
      makerg = maker;
      seriesg = series;
  } 
  function stateChangedPrintersXML() { 
    if (xmlHttpPr.readyState==4 || xmlHttpPr.readyState=="complete") {
      sel = ""; 
      html = '<select id="maker" name="maker" onchange="getPrintersXML(this.value, $(\'series\').value)"><option value="">Марка...</option>';
      subhtml = '<select id="series" name="series"><option value="-1">Модел...</option>';

      try {
        res = xmlHttpPr.responseXML.documentElement.childNodes;
      } catch (e) {
        xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
        xmlDoc.loadXML(xmlHttpPr.responseText);
        res = xmlDoc.documentElement.childNodes;
      }
      for (i=0;i<res.length;i++){
        if (res.item(i).nodeType != 1) continue;
        if (res[i].nodeName == "printer") {
          brandName = get_firstchild(res.item(i)).childNodes[0].nodeValue;
          sel = (makerg == brandName)?"selected":"";
          html += '<option ' + sel + ' value="' + brandName + '">' + brandName + '</option>';
          
          if (sel != ""){
            subs = res[i].childNodes;
            for (j=0;j<subs.length;j++){
              if (subs.item(j).nodeType != 1) continue;

              if (subs[j].nodeName == "model"){
                modelName = get_firstchild(subs.item(j)).childNodes[0].nodeValue;
                sel = (seriesg == modelName)?"selected":"";
                subhtml += '<option ' + sel + ' value="' + modelName + '">' + modelName + '</option>';
              }
            }
          }
        
        }
      }

      html += '</select>';
      subhtml += '</select>';

      $("smaker").innerHTML = html; 
      $("sseries").innerHTML = subhtml; 
    } 
  }












