//ajax
var req;
try {
  req = new XMLHttpRequest();
} catch (trymicrosoft) {
  try {
    req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch (othermicrosoft) {
    try {
      req = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (failed) {
      req = false;
    }
  }
}

function setOperation(option, operator, task)
{
 document.userForm.option.value    = option;
 document.userForm.operator.value  = operator;
 document.userForm.task.value      = task;
 document.userForm.submit();
}

function correctFields(mas, message)
{
 err = 0;
 for (i = 0; i < mas.length; i ++)
   if (trim(document.getElementById(mas[i]).value) == '') err = 1;
 if (err == 1)
     {
      alertBox(1, message);
      return false;
     }
 else return true;
}

function correctEmail(mess)
{
 patrn = /@/;
 if (patrn.exec(document.userForm.loginn.value) == null)
     {
       alertBox(1, mess);
       return false;
     }
 else return true;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

function typeWithPlanning(parentSelect, url, baseURL)
{
  ajaxPostRequestMult(['types_div', 'planning_div'], url, 'kind=' + parentSelect.value, '', baseURL);
}

function regionOptions(parentSelect, url, baseURL)
{
  document.getElementById('city_div').innerHTML = "<select name='city'><option></option></select>";
  ajaxPostRequest('region_div', url, 'country=' + parentSelect.value, '', baseURL, 1);
}

function cityOptions(parentSelect, url, baseURL)
{
  ajaxPostRequest('city_div', url, 'region=' + parentSelect.value, '', baseURL, 1);
}

//if with district
function regionOptions2(parentSelect, url, baseURL)
{
  ajaxPostRequest('region_div', url, 'country=' + parentSelect.value + '&dist=1', '', baseURL, 1);
}

function regionOptionsReFb(parentSelect, url, baseURL)
{
  document.getElementById('district_div').innerHTML = "<select name=\"district\"><option></option></select>";
  ajaxPostRequest('region_div', url, 'country=' + parentSelect.value, '', baseURL);
}
function districtOptions(countryVal, region, url, baseURL)
{
  ajaxPostRequest('district_div', url, 'country_ref=' + countryVal + '&region=' + region.value, '', baseURL);
}

function agencyBlockShow(id, url, baseURL)
{
 ajaxPostRequest('main_find_conteiner', url, 'find_mode=' + id, '', baseURL, 2);
}


/*
 * id for target request object,
 * url to php-file,
 * request variables & their values for php-file,
 * del objects in this id
 */
function ajaxPostRequest(targetId, url, sendTxt, showObj, baseURL, imageType)
{
 if (imageType == 'undefined') imageType = 1;
 
 targ = document.getElementById(targetId);
 if(req)
  {
    try
    { 
      targ.innerHTML = "<img src='" + baseURL + "images/"+((imageType == 1)?"spinner.gif":"indicator.gif")+"'>";

      req.open('POST', url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
      req.send(sendTxt);
      req.onreadystatechange = function()
      {
        if(req.readyState == 4)
        {
          if(req.responseText.length > 0)
            {
             setTimeout(function(){
              targ.innerHTML = req.responseText;
             }, 150);
            }
          else if (showObj.length > 0) for (i = 0; i < showObj.length; i ++) $(showObj[i]).innerHTML = ''; 
        }
      }
    }
    catch(err) {alert(err);}
    //req.send(null);
  }
}

function ajaxPostRequestMult(targetsId, url, sendTxt, showObj, baseURL)
{
 if(req)
  {
    try
    {
      for (i = 0; i < targetsId.length; i ++)
       {
         targ = document.getElementById(targetsId[i]);
         targ.innerHTML = "<img src='" + baseURL + "/images/spinner.gif'>";
       }
 
      req.open('POST', url, true);
      req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
      req.send(sendTxt);
      req.onreadystatechange = function()
      {
        if(req.readyState == 4)
        {
          if(req.responseText.length > 0)
            {
             setTimeout(function(){
              
              textResult = req.responseText.split('||||||');
              for (i = 0; i < targetsId.length; i ++)
               {
                 targ = document.getElementById(targetsId[i]);
                 targ.innerHTML = textResult[i];
               }
              
             }, 300);
            }
          else if (showObj.length > 0) for (i = 0; i < showObj.length; i ++) $(showObj[i]).innerHTML = '';
        }
      }
    }
    catch(err) {alert(err);}
    //req.send(null);
  }
}

//photos operations
function toDel(t_d, c_b)
{
   table_td =document.getElementById(t_d);
   checkbx  = document.getElementById(c_b);

   if (table_td.style.backgroundColor != "rgb(255, 136, 136)"
   && table_td.style.backgroundColor != "#ff8888"
   && table_td.style.backgroundColor != "rgb(255,136,136)")
   table_td.style.backgroundColor = "rgb(255, 136, 136)";

   else if (table_td.style.backgroundColor == "rgb(255, 136, 136)"
   || table_td.style.backgroundColor == "#ff8888"
   || table_td.style.backgroundColor == "rgb(255,136,136)")
   table_td.style.backgroundColor = "rgb(255, 255, 255)";

   if (checkbx.checked == true) checkbx.checked = false;
   else checkbx.checked = true;
}
function mouseIn(id)
{
 imag  = document.getElementById(id);
 imag.style.border = "4px outset #fb2c2c";
}
function mouseOut(id)
{
 imag  = document.getElementById(id);
 imag.style.border = "4px solid white";
}
//add fields for photos
function add_photo_admin(fileName, comment)
{
  if (fieldIndex <= 2 && fieldIndex < maxFieldIndex)
  {
   otable	  = document.getElementById("table_photo");
   table_row  = otable.insertRow(-1);
   table_col  = table_row.insertCell(0);
   table_col2 = table_row.insertCell(-1);
   table_col.innerHTML  = "<input type='file' name='photo"+fieldIndex+"' id='idinput"+fieldIndex+"' onchange=\"add_photo_admin('photo"+fieldIndex+"','"+comment+"');\">";
   table_col2.innerHTML = "<span class=\"admin-options\">" + comment + ":</span> <input type='text' name='opis"+fieldIndex+"' id='idopis"+fieldIndex+"'>";
   fieldIndex = fieldIndex + 1;
  }
}

//add fields for photos
function add_photo_admin2(fileName, comment, maxIndex)
{
  if (fieldIndex <= maxIndex && fieldIndex < maxFieldIndex)
  {
   otable	  = document.getElementById("table_photo");
   table_row  = otable.insertRow(-1);
   table_col  = table_row.insertCell(0);
   table_col2 = table_row.insertCell(-1);
   table_col.innerHTML  = "<input type='file' name='photo"+fieldIndex+"' id='idinput"+fieldIndex+"' onchange=\"add_photo_admin2('photo"+fieldIndex+"','"+comment+"', '"+maxIndex+"');\">";
   table_col2.innerHTML = "<span class=\"admin-options\">" + comment + ":</span> <input type='text' name='opis"+fieldIndex+"' id='idopis"+fieldIndex+"'>";
   fieldIndex = fieldIndex + 1;
  }
}

function showHideFields(x, val, mas)
{
   for (i = 0; i < mas.length; i ++)
    if (x.value == val) document.getElementById(mas[i]).style.visibility = "hidden";
   else document.getElementById(mas[i]).style.visibility = "visible";
}

function setAnnoun(id, type)
{
  document.userForm.announ.value = id;
  document.userForm.annType.value = type;
}

function setEditObject(val)
{
  document.userForm.objectId.value = val;
}


function bigImg(url, url2, imgName, mess, width, height)
{
 windowOrders = window.open(url + "others/showImg.php?img=" + imgName + "&mess=" + mess + "&url="+ url +"&url2=" + url2,
 "image", "scrollbars=yes, toolbar=no, status=no, resizable=yes, width="+ width +", height=" + height);
}

function setCookie(name, value, days)
 {
  if (days)
   {
     var date = new Date();
     date.setTime(date.getTime()+(days*24*60*60*1000));
     var expires = "; expires="+date.toGMTString();
   }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
 }

function setLang(lang, actUrl)
{
 setCookie('lang', lang, 120);
 document.location.href = actUrl;
}

function js_style(div_id)
{
 hideset = document.getElementById('hidden'+div_id);
 divset = document.getElementById('plus'+div_id);

 if (hideset.value == 'show')
 {
  divset.innerHTML = "&nbsp;&nbsp;+&nbsp;";
  hideset.value = "hide";
 }
 else if (hideset.value == 'hide')
 {
  divset.innerHTML = "&nbsp;&nbsp;-&nbsp;";
  hideset.value = "show";
 }
}

function alertBox(mode, txt, codes)
{
 $('alertBox').style.display = 'block';
 $('alertBoxVal').innerHTML  = txt;
 if (mode == 1) $('abOk').style.display = 'block';
 else if (mode == 2) 
   {
    $('abConfirm').style.display = 'block';
    //code to exists in alertBoxOk
    $('jscode').value = codes;
   }
}
function progressBox(txt)
{
 $('progressBox').style.display = 'block';
 $('progressBoxVal').innerHTML  = txt;
}

function alertBoxOk()
{
  eval($('jscode').value);
  $('alertBox').style.display = 'none';
  $('alertBoxVal').innerHTML  = '';
}

function alertBoxCancel()
{
 $('alertBox').style.display = 'none';
 $('alertBoxVal').innerHTML  = '';
}

function showYaMapsBlock()
{
  $('yaMapsBlock').style.display = "block";
}

function hideYaMapsBlock()
{
  $('yaMapsBlock').style.display = "none";
  document.userForm.ya_coords.value = "";
}

function addToURL(obj, str)
{
 addStr = $('addtourl').value;
 str2   = $('addtourl').value + '&' + str;
 str3   = '&' + str;
     
 if (obj.checked == true)
 {
  if ( (addStr.search(str) == -1) || (url.search(str) == -1) ) $('addtourl').value = str2;
  else 
  {
   $('addtourl').value = addStr.replace(str2, '');
  }
 }
 else if (obj.checked == false)
 {
  url = url.replace(str3, '');
  $('addtourl').value = addStr.replace(str2, '');
 }
}

function templateSet()
{
 $('svt').checked = false;
 $('templname').disabled = true;
 document.userForm.submit();
}

function setTemplDisable()
{
 if ($('svt').checked == true) $('templname').disabled = false;
 else $('templname').disabled = true;
}

function deleteTemplate(txt, txt2)
{
 if (trim($('templset').value) == '') alert(txt);
 else
 {
  if (confirm(txt2))
  {
   $('tmpl_del').value = $('templset').value;
   document.userForm.submit();
  }
 }
}
