//## <AJAX>
var working;
//===============================================================================
function getElementByMy(name) {
   curElement = document.getElementById(name);
   if (! curElement) {
      //curElement = document.all[name];
      curElement = document.frm[name];
   }
   return curElement;
}
//========================================================================================
function vacation(url) {
   ajax_scr = 'accounts.php';
   vac_step = 'arv';
   vac_url = script_url + '/' + ajax_scr + '?step=' + vac_step;

   var parr = new Array('country', 'city', 'hotel', 'jetplane', 'lady', 'menu');
   out = '';
   for (var i=0;i<parr.length;i++) {
      cel = getElementByMy(parr[i]);
      if (cel) {
         var val = '';
         if ((cel.type == 'select-one') || (cel.type == 'select')) {
            val = cel.options[cel.selectedIndex].value.toString();
            if (val==1) val = '';
         }
         out = out + '&' + parr[i] + '=' + val;
      }
   }

   //v(vac_url+ out);
   majaxGetUrl(vac_url + out);
   //majaxGetUrl('http://127.0.0.1');
}
//========================================================================================
function ParceResponse(str) {
   //v(str);
   mr = /<(.+?)>(.+?)<\/(.+?)>/gi;
   var res = new Array;
   //#myc !!! certain count
   for (a=0; a<17; a++) {
      arr = mr.exec(str);
      if (arr) {
         if (arr[1].indexOf('|')>-1) {
            atypes = arr[1].split("|");
            arr[1] = atypes[0];
            ctype = atypes[1];
         } else {
            ctype = '';
         }
         //v(arr[1] + ctype);
         curElement = getElementByMy(arr[1]);
         //alert(arr[1] + curElement);
         if ((! ctype) || (ctype=='select')) {
            res[arr[1]] = arr[2].split("|");
            FillSelect(curElement, res[arr[1]]);
         } else if (ctype=='sel_index') {
            curElement.selectedIndex = arr[2];
         } else {
            curElement.innerHTML = arr[2];
         }
      }
   }
}
//========================================================================================
function FillSelect(sel, arr) {
   for (var i = 0; i < sel.options.length; i++) {
      sel.options[i] = null;
   }
   sel.options.count = 0;
   //sel.options =null;
   var tmp = '';
   //c(sel.options.count);
   for (var i = 0; i < arr.length; i=i+2) {
      var a=i/2;
      //c(a);
      sel.options[a] = new Option(arr[i+1], arr[i]);
      tmp = tmp + i/2 + '|';
   }
   c(sel.name);
}
//========================================================================================
function majaxGetUrl(url) {
   var http;

   http = get_http();
   if(! http){
      alert('err XMLHTTP err!')
   }

   // Получаем объект XMLHTTPRequest
   if(!this.http){
      this.http = get_http();
      working = false;
   }
   // Запрос
   //alert(working + '|' +this.http);
   if ((! working) && this.http) {
      var http = this.http;

      //создаём запрос
      this.http.open("GET", url, true);
      //прикрепляем к запросу функцию-обработчик
      //событий
      this.http.onreadystatechange = function() {
         // 4 – данные готовы для обработки
         if (http.readyState == 4) {
            working = false;
            //retrun http.responseText;
            ParceResponse(http.responseText);
         } else {
            // данные в процессе получения,
            // можно повеселить пользовател
            //сообщениями
            // ЖДИТЕ ОТВЕТА
         }
      }
      working = true;
      this.http.send(null);
   }
   if(!this.http){
      alert('err XMLHTTP объекта!')
   }
}
//========================================================================================
function get_http_(){
    var xmlhttp;
    //@cc_on
    if (_jscript_version >= 5)
        try {
            xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (E) {
                xmlhttp = false;
            }
        }
    else
        xmlhttp = false;
    end


    if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
        try {
            xmlhttp = new XMLHttpRequest();
        } catch (e) {
            xmlhttp = false;
        }
    }
    return xmlhttp;
}
//========================================================================================
function get_http() {
  if (window.XMLHttpRequest) {
    //создание объекта для всех браузеров кроме IE
        requestObj = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
      //для IE
        requestObj = new ActiveXObject("Msxml2.XMLHTTP");
        if (!requestObj) {
            requestObj = new ActiveXObject("Microsoft.XMLHTTP");
        };
    };
    return requestObj;
};
//========================================================================================
//## <OTHER FUNC>
//========================================================================================
function CheckFieldsForm(mform, ta) {
   d = ' | ';
   err = 0;
   for (var i=0;i<ta.length;i++) {
      var e = ta[i];
      tInp = mform.elements[e];

      // username not an input skip
      if (! tInp) {continue;}

      //v(e +'|'+  tInp.name +'|'+ typeof(tInp) +'|'+ tInp.type +'|'+ tInp.checked +'|'+ tInp.value);

      if (typeof(tInp) != 'undefined') {
         if (tInp.type == 'checkbox') {
            if (tInp.checked == false) {
               err=1;
            }
         } else {
            if (typeof(tInp.value) != 'undefined') {
               if (tInp.value.length<1) {
                  err=1;
               }
            } else {
               a = '0';
               tn = e + a;
               tInp1 = mform.elements[e + '0'];
               if ((typeof(tInp1) != 'undefined') && (tInp1.type == 'checkbox')) {
                  if (err!=1) {
                     err=1;
                     for (a=0; a<130; a=a+2) {
                        tn = e + a;
                        tInp1 = document.getElementById(tn);
                        if (tInp1) {
                           //v(tn +'|'+  tInp1.name +'|'+ typeof(tInp1) +'|'+ tInp1.type +'|'+ tInp1.checked +'|'+ tInp1.value);
                           if (typeof(tInp1) != 'undefined') {
                              if (tInp1.checked == true) {
                                 err=0;
                                 break;
                              }
                           }
                        }
                     }
                  }
               }
            }
         }
      }
   }

   if (err>0) {
      alert("Please fill all required (*) fields");
      return false;
   } else {
      return true;
   }
}
//========================================================================================
function CheckFields(ta) {
   d = ' | ';
   err = 0;
   for (var i=0;i<ta.length;i++) {
      var e = ta[i];
      //alert(e);
      tInp = document.getElementById(e);
      if (tInp) {
         if (tInp.type == 'checkbox') {
            //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
            tInp1 = document.getElementById(tInp.name + '0');
            if (tInp1) {
               if (err!=1) {
                  err=1;
                  for (a=0; a<100; a=a+2) {
                     tInp1 = document.getElementById(tInp.name + a);
                     if (tInp1) {
                        //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
                        if (tInp1.checked == true) {
                           err=0;
                           break;
                        }
                     }
                  }
               }
            } else {
               if (tInp.checked == false) {
                  err=1;
               }
            }
         } else {
            //alert(tInp.name + d + tInp.type + d + tInp.value + d  + tInp.value.length + d + tInp.checked);
            if (tInp.value.length<1) {
               err=1;
               //alert('err');
               //htmo = document.getElementById(e.toString());
            }
         }
      }
   }

   //alert(err);
   //return false;

   if (err>0) {
      alert("Please fill in all required (*) fields");
      return false;
   } else {
      return true;
   }
}
//========================================================================================
function SubmitAction(act, id) {
   frm.substep.value = act;
   frm.id.value = id;
   frm.submit();
}
//========================================================================================
function QstDel(link) {
   if (window.confirm('Delete item ?') == true) { // + link
      window.location=link;
   }
   return false;
}
//========================================================================================
function SubmitAction1(act, id) { //, id, cpos_null
   frm1.substep.value = act;
   frm1.id.value = id;
   if ((act=='del') && (window.confirm('Delete item ?') != true)) {
      return;
   }
   frm1.submit();
}
//========================================================================================
function SubmitAction2(act, id) { //, id, cpos_null
   frm1.substep.value = act;
   frm1.id.value = id;
   frm1.cpos.value = 0;
   if ((act=='del') && (window.confirm('Delete item ?') != true)) {
      return;
   }
   frm1.submit();
}
//========================================================================================
function CheckAll() {
   frm=frm1;
   for (var i=0;i<frm.elements.length;i++) {
      var e = frm.elements[i];
      if ((e.name != 'allbox') && (e.type=='checkbox')) {
         e.checked = frm.allbox.checked;
      }
   }
}
//========================================================================================
function ChangeContinent() {

   var SelectedContinent = frm.continent.options[frm.continent.selectedIndex].value;
   var oldSelectedCountry = frm.country.options[frm.country.selectedIndex].value;


   // (clear)
   var k = frm.country.options.length;
   for (var i = 0; i < k; i++) {
      frm.country.options[0] = null;
   }

   var result = '';
   var k=0;
   frm.country.options[k] = new Option('All', 'all');
   for (var i in ctr_cnt_h) {
      if ((ctr_cnt_h[i] == SelectedContinent) || (SelectedContinent == 'all')) {
         k++;
         frm.country.options[k] = new Option(i, i);
         if (oldSelectedCountry == i) frm.country.selectedIndex = k;
         result += i;
      }
   }
   //alert(result);
}
//========================================================================================
//## <DEBUG>
//========================================================================================
function v(str) {
   alert(str);
}
//========================================================================================
function c(str) {
   var debug = getElementByMy('debug');
   if (debug) {
     //debug.innerHTML += ' [' + str + '] ';
   }
}
//========================================================================================