﻿// JScript File



    window.onload = OnLoadEvent;
    
    function OnLoadEvent()
    {

    }
    

/*------------------ Pager Functions --------------------------
---------------------------------------------------------------
---------------------------------------------------------------
---------------------------------------------------------------*/

    var arrFakeTrs = new Array();
    
    //pager parameters:
    var intRowsPerPage;
    var strLabelPagerId;
    var strLabelPagerDescriptionId;
    var strMessagePagerDescription; 
    var strMessagePagerDescriptionSingle; 
    var strVacancyTRName;
    var strRelativeUrl;
    

    
    function InitPagerParameters(_intRowsPerPage,_strLabelPagerId,_strLabelPagerDescriptionId, _strMessagePagerDescription,_strMessagePagerDescriptionSingle, _strVacancyTRName,_strRelativeUrl)
    {
        intRowsPerPage = _intRowsPerPage;
        strLabelPagerId = _strLabelPagerId;
        strLabelPagerDescriptionId = _strLabelPagerDescriptionId;
        strMessagePagerDescription = _strMessagePagerDescription; 
        strMessagePagerDescriptionSingle = _strMessagePagerDescriptionSingle; 
        strVacancyTRName = _strVacancyTRName;
        strRelativeUrl = _strRelativeUrl;    
    }
    
    function GoToPage(intPage)
    {
        
        
        if (intPage==0 || intPage==null)
            intPage=1;
                
        var intFirstPosition = ((intPage-1) * intRowsPerPage);
        var intLastPosition = intFirstPosition + intRowsPerPage -1;            
        
        var arrAllRows = document.getElementsByTagName("tr");
        var arrVacancyRows = new Array();
        for(var i=0;i<arrAllRows.length;i++)
            if (arrAllRows[i].name!=null)
                if (arrAllRows[i].name==strVacancyTRName)
                    arrVacancyRows.push(arrAllRows[i]);
                
        var blnNeedPager = arrVacancyRows.length>intRowsPerPage;
        
        //displaying this page vacancies
        var intCountDisplay = 0;
        for (var i=0;i<arrVacancyRows.length;i++)
        {
            var blnDisplay = (i>=intFirstPosition && i<=intLastPosition)
            
            arrVacancyRows[i].style.display = blnDisplay ? "" : "none";
            GetNextSibling(arrVacancyRows[i]).style.display = "none";
            
            if (blnDisplay)
                intCountDisplay++;                            
        }
        
        if (blnNeedPager)
        {
            var strMessage = "";
            if (intCountDisplay>1)
            {
                strMessage = strMessagePagerDescription.replace("{0}",intCountDisplay);
                strMessage = strMessage.replace("{1}",arrVacancyRows.length);
            }
            else
            {
                strMessage = strMessagePagerDescriptionSingle.replace("{1}",arrVacancyRows.length);
            }
            document.getElementById(strLabelPagerDescriptionId).innerHTML = strMessage;
            
            var intCountPages = Math.ceil(arrVacancyRows.length / intRowsPerPage);
            

            //fill missing rows of page with temp rows:            
            if (intCountDisplay<intRowsPerPage)
            {                            
                //check if not already created
                if (arrFakeTrs.length==0)
                {
                    var objLastRealTR = arrVacancyRows[arrVacancyRows.length-1];
                    var objTable = objLastRealTR.parentNode;            
                    
                    for (var i=intCountDisplay;i<intRowsPerPage;i++)
                    {
                        //creating fake tr:
                        var objFakeTR = document.createElement("tr");                        
                        objFakeTR.className = objLastRealTR.className;
                        objFakeTR.style.visibility = "hidden";
                        arrFakeTrs.push(objFakeTR);
                        objTable.insertBefore(objFakeTR,GetNextSibling(GetNextSibling(objLastRealTR)));                        
                        
                        var objFakeTD = document.createElement("td");
                        objFakeTD.className = objLastRealTR.childNodes[1].className;
                        objFakeTD.innerHTML = "Temp";
                        objFakeTR.appendChild(objFakeTD);
                    }
                }
                else
                {
                    //showing fake trs if display is none:
                    for(var i=0;i<arrFakeTrs.length;i++)
                        arrFakeTrs[i].style.display = "";
                }
            }
            else
            {
                //hiding fake trs
                for(var i=0;i<arrFakeTrs.length;i++)
                    arrFakeTrs[i].style.display = "none";                
            }
            
            BuildPager(intCountPages,intPage);
        }
    
    }
    

    
    function BuildPager(intCountPages,intCurrentPage)
    {

      var strHtml = "<table cellspacing=\"5\" dir=\"ltr\"><tr>";
      if (intCurrentPage>1)
        strHtml+= "<td title='עמוד קודם' style=\"width:12px;cursor:pointer\" onclick=\"javascript:GoToPage(" + (intCurrentPage-1) + ")\"><img src=\"" + strRelativeUrl + "/images/prev_page.gif\" ></td>";
      else
        strHtml+= "<td title='עמוד קודם' ><img src=\"" + strRelativeUrl + "/images/prev_page_disabled.gif\" ></td>";
        //strHtml+= "<td style=\"width:12px;\"/>"
        
      for(var i=1;i<=intCountPages;i++)
      {            
        if (intCurrentPage!=i)
          strHtml+= "<td class='PageNumber' onclick=\"javascript:GoToPage(" + i + ")\"><u>" + i + "</u></td>";
        else
          strHtml+= "<td class='PageNumberSelected'>" + i + "</td>";
      }
        
      if (intCurrentPage<intCountPages)
        strHtml+= "<td title='עמוד הבא' style=\"width:12px;cursor:pointer\" onclick=\"javascript:GoToPage(" + (intCurrentPage+1) + ")\"><img src=\"" + strRelativeUrl + "/images/next_page.gif\" ></td>";
      else
        strHtml+= "<td title='עמוד קודם' ><img src=\"" + strRelativeUrl + "/images/next_page_disabled.gif\" ></td>";
        //strHtml+= "<td style=\"width:12px;\"/>"        
                            
      strHtml += "</tr></table>";
      
      document.getElementById(strLabelPagerId).innerHTML = strHtml;
      
    }    
    
    function GetParam(strParam)
    {
      try
        {
          return String(String(window.location).match(new RegExp("[?&]" + strParam + "=[^&$]*","gi"))).split('=')[1];
        } catch (err)
        {
          return "";
        }
    }      
        
 
