var req;

function loadXMLDoc(url,mode) {
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
        if(mode==2)
        {
          req.onreadystatechange = processReqChange2;
        } else {
           req.onreadystatechange = processReqChange;
          }
        req.open("POST", url, true);
        req.send(null);
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
          if(mode==2)
          {
            req.onreadystatechange = processReqChange2;
          } else {
             req.onreadystatechange = processReqChange;
            }
            req.open("POST", url, true);
            req.send();
        }
    }
}

function create_option(t,v)
{
  var oOption = document.createElement("OPTION");
  document.all.month.options.add(oOption);
  oOption.text = t;
  oOption.value = v;
}

function processReqChange2() {

    ab = window.setTimeout("req.abort();", 5000);

    if (req.readyState == 4)
    {
        clearTimeout(ab);
    }
}

function processReqChange() {

//    document.form1.state.value = stat(req.readyState);

    ab = window.setTimeout("req.abort();", 5000);

    if (req.readyState == 4)
    {
        clearTimeout(ab);

//        document.form1.statusnum.value = req.status;
//        document.form1.status.value = req.statusText;

        // only if "OK"
        if (req.status == 200)
        {
//            document.form1.response.value=req.responseText;
            var str=req.responseText;

            var ss;
            ss = str.split(",");

            var j=0;

//            for(j=0;j<=document.all.month.length;j++)
//            {
//               document.all.month.options.remove(j);
//               document.all.month.options.remove(0);
//            }

            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(0);
            document.all.month.options.remove(1);
            document.all.month.options.remove(2);
            document.all.month.options.remove(3);
            document.all.month.options.remove(4);
            document.all.month.options.remove(5);
            document.all.month.options.remove(6);
            document.all.month.options.remove(7);
            document.all.month.options.remove(8);
            document.all.month.options.remove(9);
            document.all.month.options.remove(10);
            document.all.month.options.remove(11);
            document.all.month.options.remove(12);

            for(j=0;j<ss.length/2;j++)
            {
               create_option(ss[j*2],ss[j*2+1]);
            }

            if(document.all.tempindex.value)
            {
              for(j=0;j<=document.all.month.length;j++)
              {
                if(document.all.month.options[j].text==document.all.tempindex.value)
                {
                   document.all.month.selectedIndex=j;
                   break;
                }
              }
            }

        }
        else
        {
            alert("Не удалось получить данные:\n" + req.statusText);
        }
    }
}

function stat(n)
{
  switch (n) {
    case 0:
      return "не инициализирован";
    break;

    case 1:
      return "загрузка...";
    break;

    case 2:
      return "загружено";
    break;

    case 3:
      return "в процессе...";
    break;

    case 4:
      return "готово";
    break;

    default:
      return "неизвестное состояние";
  }
}

function requestdata(params,mode)
{
  var urlget="";

  if(mode==2)
  {
    urlget='http://www.creativebureau.net';
  } else {
      urlget='http://www.creativebureau.net/ajax_t.php';
    }
  loadXMLDoc(urlget+params,mode);
}
