var g_CurrentlyPullingID = 0;
var m_ImgArray = new Array(); 

var g_CurrentlyPullingFolderID = 0;
var g_InitialFolderIDArray = new Array();
var m_IE  = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var m_Opera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;


// =======================================================
   function ChooseCity(par_Path) {

       l_Obj = $qpl('city_list');
       if (l_Obj.style.display == 'block')
       {
         l_Obj.style.display = 'none';
         return false;  
       }

       if (g_CurrentlyPullingID > 0) {
          alert(QPL_STR_JS_PROCESS_REQ);
          setTimeout('ClearPullID()', 5000);
          return false;
       }

       g_CurrentlyPullingID = -1;

       l_NewURL = g_CurPage.replace(/q/, '%71');
       l_NewURL = l_NewURL.replace(/\//g, '*');
       l_URL = par_Path + '/_index.php?qact=citylist&red=' + encodeURI(l_NewURL);
           
       $qpl('please_wait').style.display= 'block';
       PullInfo(6, l_URL + '&rand=' + Math.random());  

       return false;
   }
// =======================================================
   function bookmark(t, title, url) {
          if (title == undefined)    
              title = document.title;  
         if (url == undefined)    
            url = top.location.href;  
         if (window.sidebar) {    
            window.sidebar.addPanel(title, url, '');  
         } else if (window.opera && window.print) {    
            t.setAttribute('rel', 'sidebar');
            t.setAttribute('href', url);
            t.setAttribute('title', title);
            return true;
         } else {    
             window.external.AddFavorite(url, title);  
         }
       return false;
  }

// =======================================================
function makesubmitadv(parActName) {
  var f = document.forms.adv;
  f.qact.value = parActName;
  f.submit();
}
// =======================================================

function ChangePic(par_PicIndex, par_AdvID)
{
var obj = $qpl("Big" + par_AdvID);
    obj.src = m_ImgArray[par_AdvID][par_PicIndex];
}
// =======================================================
function checkSubmitSendForm() 
{
  var f = document.forms.email; 

  if ((f.FIO.value == '') ||
      (f.Email.value == '') ||
      (f.Text.value == '')) {
      alert(QPL_STR_JS_FILL_ALL); 
      return false;
  }

  return true;
}

// ==========================================================

function DoCommonRequest(par_Request, par_ID) {
    
  if (g_CurrentlyPullingID > 0) {
     alert(QPL_STR_JS_PROCESS_REQ);
     setTimeout('ClearPullID()', 5000);
     return false;
  }

  g_CurrentlyPullingID = par_ID;
  l_URL = par_Request;
      
  $qpl('please_wait').style.display= 'block';
  PullInfo(4, l_URL + '&rand=' + Math.random());  

  return false;
}

// ==========================================================

function RemindTheAd(par_Request, par_ID) {
    
  if (g_CurrentlyPullingID > 0) {
     alert(QPL_STR_JS_PROCESS_REQ);
     setTimeout('ClearPullID()', 5000);
     return false;
  }

  g_CurrentlyPullingID = par_ID;
  l_URL = par_Request;
      
  $qpl('please_wait').style.display= 'block';
  PullInfo(5, l_URL + '&rand=' + Math.random());  

  return false;
}

// ==========================================================

function LoadSendEmailForm(par_ID, par_Path) {
  if ((m_IE) || (m_Opera)) {
     if (g_CurrentlyPullingID > 0) {
        alert(QPL_STR_JS_PROCESS_REQ);
        setTimeout('ClearPullID()', 5000);
        return false;
     }

     g_CurrentlyPullingID = par_ID;
     l_URL = par_Path + '/_index.php?qact=show_email_form&id=' + par_ID + '&ajax=1';
     $qpl('please_wait').style.display= 'block';

     var obj = document.forms["email"];
     if (obj)
     {
       obj.parentNode.removeChild(obj);
     }
         
     PullInfo(2, l_URL + '&rand=' + Math.random());  

     return false;
  } else {
     window.location.href = par_Path + '/_index.php?qact=show_email_form&id=' + par_ID + '&ajax=0';
  }
}

  // ===================================================

function sort(par_Criteria, par_Direction) {
  
  var f = document.forms.advForm;  
  var l_SortedBy = f.SortedBy.value;
  var l_Direction = f.Dir.value;

  if (par_Direction == '')
  {
    if (l_SortedBy == par_Criteria)
    {
      if (l_Direction == 'a')
      {
        par_Direction = 'd';
      }
      else
      {
        par_Direction = 'a';
      }
    }
    else
    {
      par_Direction = 'a';
    }
  }
  
  f.Dir.value = par_Direction;
  f.SortBy.value = par_Criteria;
  f.qact.value = 'search_adv';
  f.submit();

  return false;
}

  // ===================================================

function FindSimiliar(par_ID, pat_Path) {
	var f = document.forms.advForm;
	if ((f != null) && (f.OwnerAdvID != null)) 
    {
        f.OwnerAdvID.value = par_ID;
        f.Folder.value = '-1';
    	f.qact.value = 'similiar';
    	f.submit();
    } else {
        window.location.href= pat_Path + '/_index.php?qact=similiar&Folder=-1&OwnerAdvID=' + par_ID;
    }
	
	return false;
}

  // ===================================================

function makesubmitadvForm(parActName) {
  var f = document.forms.advForm;
  f.qact.value = parActName;
  f.submit();
  return false;
}

  // ===================================================

function ChangeSort() {
  var f = document.forms.advForm;
  var value = f.sort_select.value;

  sort(value.substr(0, value.length-2), value.substr(value.length-1, value.length));
  return false;
}

  // ===================================================

function ChangePage(parPageID) {

  var f = document.forms.advForm;
  f.SortBy.value = f.SortedBy.value;
  f.SortedBy.value = '';
  f.qact.value = 'search_adv';
  f.pg.value = parPageID;
  f.submit();  
  return false;
}

  // ===================================================

function ClearPullID() {
  g_CurrentlyPullingID = 0;
}

  // ===================================================
function ShowShortContent(par_Obj, par_ID, par_State) {
 l_Obj = $qpl(par_ID);

 if (par_State)
   tooltip(par_Obj, l_Obj.innerHTML);
     else
   hide_info(par_Obj);
}

  // ===================================================

function ObtainAdFullContent(par_ID, par_URL) {
  l_Obj = $qpl('full' + par_ID);
  if (l_Obj.style.display == 'block') {
      l_Obj.style.display = 'none';

      return false;
  }

  l_Obj = $qpl('short' + par_ID);
  if (l_Obj != null) {
      l_Obj.style.display = 'none';
  }

  if (g_CurrentlyPullingID > 0) {
     alert(QPL_STR_JS_PROCESS_REQ);
     setTimeout('ClearPullID()', 5000);
     return false;
  }

  g_CurrentlyPullingID = par_ID;
  $qpl('please_wait').style.display= 'block';
  PullInfo(1, par_URL + '&ajax=1&rand=' + Math.random());  

  return false;
}

  // ===================================================

  function ProcessData(type, reply) {
     $qpl('please_wait').style.display = 'none';

     // show ad
     if (type == 1) {
       l_Obj = $qpl('full' + g_CurrentlyPullingID);

       l_Obj.innerHTML = reply;

       l_ListObj = $qpl("imglist_" + g_CurrentlyPullingID).innerHTML;

       l_ImgList = l_ListObj.split(",");
       m_ImgArray[g_CurrentlyPullingID] = new Array();

       for (i = 0; i < l_ImgList.length; i++) {
          m_ImgArray[g_CurrentlyPullingID][i] = l_ImgList[i];
       }

       l_Obj.style.display = 'block';
       window.location.href='#reflink' + g_CurrentlyPullingID;
     }

     // show send email form
     if (type == 2) {
       l_Obj = $qpl('email_sender' + g_CurrentlyPullingID);
       l_Obj.innerHTML = reply;
     }

     // show compliant result
     if (type == 3) {
       l_Obj = $qpl('compliant' + g_CurrentlyPullingID);
       l_Obj.innerHTML = reply;
     }

     // show request result
     if (type == 4) {
        alert(QPL_STR_JS_REQUEST_DONE);
     }

     // show request result
     if (type == 5) {
        alert(reply);
     }

     // show request result
     if (type == 6) {
        l_Obj = $qpl('city_list');
        l_Obj.innerHTML = reply;
        l_Obj.style.display = 'block'; 
     }

     // process folder change
     if (type == 7) {
       CreateFolderSelect(reply, g_Path);
    g_CurrentlyPullingFolderID = 0;
    //alert(reply);
    
  }

     g_CurrentlyPullingID = 0;
  }

  // ===================================================

  function ProcessError(type, reply) {
     $qpl('please_wait').style.display = 'none';
     alert(QPL_STR_JS_ADV_ERROR + reply + QPL_STR_JS_ADV_ERROR_REPEAT);

     g_CurrentlyPullingID = 0;
  }

  // ===================================================
  
  if(!window.ActiveXObject) {
      HTMLElement.prototype.removeNode = function(removeChildren) {
          if(Boolean(removeChildren)) {return this.parentNode.removeChild(this);}
          else {
              var r = document.createRange();
              r.selectNodeContents(this);
              return this.parentNode.replaceChild(r.extractContents(),this);
          }
      }
  }
  
  function CreateFolderSelect(par_FolderString, par_Path) {
    
    var l_FoldersDiv = $qpl('folders');
    l_FoldersDiv.innerHTML = '';

    var l_SelectArray = par_FolderString.split("|||");

    for(var i = 0; i < l_SelectArray.length; i++) {
      var l_FoldersArray = l_SelectArray[i].split("||");

      var l_DivElement = document.createElement("div");
      l_DivElement.id = "div"+i;
      l_DivElement.style.cssText = "padding-bottom: 10px;";

      var l_SelectElement = document.createElement("select");
      l_SelectElement.id = "select_folder"+i;      

      for(var j = 0; j < l_FoldersArray.length; j++) {
        var l_ParamsArray = l_FoldersArray[j].split("|");
        if (j == 0) {
          // Create onchange attribute only once
          l_SelectElement.onchange =   function () {
                OnFolderChange();
              };                            
        }
                          
        var l_OptionElement = document.createElement("option")
        l_OptionElementText=document.createTextNode(l_ParamsArray[1]);
        l_OptionElement.appendChild(l_OptionElementText);
                  
        var str = l_ParamsArray[0];
        if (str.charAt(str.length-1) == 's') {
          l_OptionElement.value = str.slice(0, str.length-1);  
          l_OptionElement.setAttribute("selected", "selected");
          g_InitialFolderIDArray[i] = l_OptionElement.value;
        } else {
          l_OptionElement.value = str;
        }
            
        l_SelectElement.appendChild(l_OptionElement);
      }
      
      l_DivElement.appendChild(l_SelectElement);
      l_FoldersDiv.appendChild(l_DivElement);
    }
    
    
    //alert(l_FoldersDiv.innerHTML);
  }

  function OnFolderChange() {
    
    var l_Index = 0;
    while ($qpl("select_folder" + l_Index)) {
      var value = $qpl("select_folder" + l_Index).value;
      
      if (g_InitialFolderIDArray[l_Index] != value) {
        var l_FoldersIDArray = value.split("_");
        if (l_FoldersIDArray.length == 2) {
          l_FolderID = l_FoldersIDArray[0];
          l_ParentFolderID = l_FoldersIDArray[1];
        
          g_InitialFolderIDArray[l_Index] = value;
          
          ChangeFolder(l_FolderID, l_ParentFolderID, g_Path);
          
          var f = document.forms.searchAdvFormShort;
          f.Folder.value = l_FolderID;
          f.ParentFolder.value = l_ParentFolderID;
          
          break;
        }
          
      }

      l_Index++;            
    }    
  }

  function ChangeFolder(par_FolderID, par_ParentFolderID, par_Path) {
    if (g_CurrentlyPullingFolderID > 0) 
          {
       alert(QPL_STR_JS_PROCESS_REQ);
       setTimeout('ClearPullID()', 5000);
       return false;
    }
  
    g_CurrentlyPullingFolderID = 1;
    l_URL = par_Path + '/_index.php?qact=change_folder_short_search&Folder='+par_FolderID+'&ParentFolder='+par_ParentFolderID+'&rand=' + Math.random();
    
    $qpl('please_wait').style.display= 'block';
    
    PullInfo(7, l_URL);  
  
    return false;
  }

