﻿

//Tabs object
//**********************************************
var res = {
    version: '1.0.0',
    cookieName: 'resCollection',

    resCollection: null,

    addResource: function(id)
    {
        if (!this.resCollection)
        {
            this.resCollection = id;
        }
        else
        {
            if (!this.isExistsID(id))
                this.resCollection = this.resCollection + "," + id;
        }

        this.saveToCookie();
    },

    removeResourceId: function(id)
    {
        var newList = null;
        if (this.resCollection)
        {
            var list = this.resCollection.split(',');
            for (var i = 0; i < list.length; i++)
            {
                if (list[i] != id)
                {
                    if (newList != null)
                        newList += ',' + list[i];
                    else
                        newList = list[i];
                }
            }
        }
        this.resCollection = newList;

        this.saveToCookie();
    },

    saveToCookie: function()
    {
        $Cookie(this.cookieName, this.resCollection, 1);
    },


    GetList: function()
    {
        return $Cookie(this.cookieName);
    },

    isExistsID: function(id)
    {
        this.resCollection = this.GetList();
        if (this.resCollection)
        {
            var list = this.resCollection.split(',');
            for (var i = 0; i < list.length; i++)
            {
                if (list[i] == id)
                    return true;
            }
            return false;
        }
        else
            return false;
    },

    ClearCollection: function()
    {
        this.resCollection = null;
        $Cookie(this.cookieName, " ", -1);
    }
}


//**********************************************

//TableWork
//**********************************************

var tWork = {
    version: '1.0.0',        

    GetListByStatus:function (obj,tblStatus,typeStatusPage,UpId,nameCookie)
    {
        var linksHolder = $(tblStatus);
        if (linksHolder)
        {
            var elTd = linksHolder.getElementsByTagName("td");
            for (var i=0; i<elTd.length; i++)
                elTd[i].className = "notselect";
        }
        
        obj.className = "select";
       
       var status=obj.id;
       status=status.replace("status_","");
       
       var typeStatus=$(typeStatusPage);
       if(typeStatus)
       {
           if(status!="-1")
           {           
             typeStatus.value=status;
             this.readCookie(nameCookie,status); 
           }     
           else
           {                    
             typeStatus.value="";
             this.readCookie(nameCookie,-1); 
           }         
       }
//       alert(typeStatus.value);     
      __doPostBack(UpId);
        
     
    },
    
    readCookie:function (name,status)
    {
	    var nameEQ = name + "=statusPage=";
	    var cookie=document.cookie;
	    var ca = cookie.split(';');
	    for(var i=0;i < ca.length;i++)
	    {
		    var c = ca[i];
//		    while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ)!= -1)
            {            
                var t = c.split('&');
                cookie=cookie.replace(/nameEQ+status/,t[0]);              
            }
	    }
	    return null;
    }      
}

//**********************************************

//SiteMaster
//**********************************************

var eMaster = 
{
    version: '1.0.0',
        
    DeleteLastOf:function (obj)
    {
        var rptHref=$(obj);       
        if(rptHref)
        {
           var elSpan=rptHref.getElementsByTagName("span")           
           if(elSpan.length>0)
                elSpan[elSpan.length-1].style.display="none";           
        }
     
    }   
}


//**********************************************

//Page Print
//**********************************************
   
function DoPageBreake()
{

   var printPageHeight =780; // for FF
//       if($isIE())
//            printPageHeight =780; // for IE

   //var divBlockText=$(pageUpdatePanel);
   var divBlockText = $("printHolder");
   var iNewline=null;
   var elLastHeight=0;
   var elLastPlus=0;
   var tempHeight=0;
   var typelong=false;      
   
   if(divBlockText)
   {
       var divArr = divBlockText.getElementsByTagName("div");
        if(divArr && divArr.lenght>0)
        {
            //anlayze elemnts to determinate which element is longer then his neighbor
            DoChooseRightOrLeft(divArr);
        
            for(var i=0;i<divArr.length;i++)
            {
                var el=divArr[i];
                var elClassName=GetClassName(el);           
                var elHeight=el.offsetHeight;
                
                if(elClassName=="n")
                    iNewline=i;
                
                if(elClassName!="x")
                {  
                   typelong=GetTypelong(el);
                   
                   if(elClassName=="c"||typelong)
                   {
                       elOffsetTop=GetOffsetTop(el);
                       
                       if(i!=0)                             
                            elLastPlus=elOffsetTop - tempHeight;        
                       else                
                            elLastPlus=elOffsetTop;                                                      
                
//                           tempHeight=elOffsetTop+elHeight;                       

//                         alert("elClassName :"+elClassName+"  "+"elLastHeight :"+elLastHeight+"  "+"elHeight :"+elHeight); 
//                         alert("elClassName :"+elClassName+"  "+"elLastHeight+elLastPlus+elHeight :"+(elLastHeight+elLastPlus+elHeight));                     
                        if((elLastHeight+elLastPlus+elHeight)>printPageHeight)
                        {
                            if(elHeight<printPageHeight)
                            {
                                if((elClassName=="r"||elClassName=="l") && iNewline!=null)
                                {
                                    divArr[iNewline].style.pageBreakBefore="always";
                                    iNewline=null;
                                }    
                                else
                                    el.style.pageBreakBefore="always";
                                elLastHeight=elHeight;                                   
                            }
                            else
                            {                                    
                                var delta=elHeight-(printPageHeight-(elLastHeight+elLastPlus));                                  
                                elLastHeight=GetMyHeight(delta,printPageHeight);                                   
                            }     
                                                     
                        }
                        else                          
                            elLastHeight=elLastHeight+elLastPlus+elHeight;
                    
                        tempHeight=elOffsetTop+elHeight; 
                   }                 
                }
            }                
        }
   }
}



//this url is describe the problem of printing and page break.
//http://css-discuss.incutio.com/?page=PrintStylesheets


//scan an elements to determinate which element should be moved to a next page in print
function DoPageBreake2()
{

    var printPageHeight = 1122;  //780; // for FF
    //       if($isIE())
    //            printPageHeight =780; // for IE

    //var divBlockText=$(pageUpdatePanel);
    var divBlockText = $("printHolder");
    var iNewline = null;
    var typelong = false;   
       
    var divArr = divBlockText.getElementsByTagName("div");
    //this is a top div.
    //all height calculations and top positions should be performed according to this position
    var topOffset = getParentObjTop(divBlockText);
    if (divArr && divArr.length > 0)
    {
        //anlayze elemnts to determinate which element is longer then his neighbor
        DoChooseRightOrLeft(divArr);

        //start from element 1, the first elemnt should not be moved to next page
        for (var i = 1; i < divArr.length; i++)
        {
            var el = divArr[i];
            //remember new line element, for future use.
            //the page break applies on newLine div, to insure that next divs with a float left or 
            //right will start on new page
            if(GetClassName(el)=="n")
                iNewline=i;
                
            //Check if current element     
            typelong=GetTypelong(el);

            if (GetClassName(el) == "c" || typelong)
            {
                var elHight = el.clientHeight;
                var elTopPos = getParentObjTop(el) - topOffset; //relative to parent div holder;
                if (printPageHeight > elHight )
                {
                    //the elemnt is too long, thus this element should start from new page
                    if (elHight + elTopPos > printPageHeight)
                    {
                        if (typelong)
                        {
                            
                            //divArr[iNewline].innerHTML = "insert pageBreakBefore:always"; //for bebug
                            divArr[iNewline].style.pageBreakBefore = "always";
                            //topOffset = getParentObjTop(divArr[iNewline]) - topOffset;
                            topOffset = getParentObjTop(divArr[iNewline]);
                        }
                        else //assume that elemnt is a center div
                        {
                            //el.innerHTML = el.innerHTML + "<br/>insert pageBreakBefore:always"; //for bebug
                            el.style.pageBreakBefore = "always";
                            //topOffset = getParentObjTop(el) - topOffset;
                            topOffset = getParentObjTop(el);
                        }
                    }
                    else
                        continue;
                }

            }
            
        }
        
    }
}


//if element has attribute long return true
function GetTypelong(el)
{
    var OK=false
    if(el)
    {
        var elClassName=GetClassName(el);  
        if(elClassName=="r"||elClassName=="l")
        {
            var typelong=el.getAttribute("type");
            if(typelong=="long")
                OK=true;
        }
    }
    return OK;
}


//anlyze element, to difine which elemnt is longer
function DoChooseRightOrLeft(divArr)
{
    var newline = false;
    var newlineEnd = false;

    var elRight = null;
    var elLeft = null;

    if (divArr)
    {
        for (var i = 0; i < divArr.length; i++)
        {
            var el = divArr[i];
            var elClassName = GetClassName(el);

            if (elClassName == "n")
                newline = true;

            if (newline)
            {
                switch (elClassName)
                {
                    case "r":
                        elRight = el;
                        break;
                    case "l":
                        elLeft = el;
                        break;
                    case "n":
                        break;
                    default:
                        newlineEnd = true;
                        break;
                }
            }

            if (newline && newlineEnd)
            {
                if (newlineEnd)
                {
                    if (elRight != null && elLeft != null)
                    {
                        //compare left and right elemnt height, who is bigger
                        if ((elRight.offsetHeight - elLeft.offsetHeight) >= 0)
                            elRight.setAttribute("type", "long");
                        else
                            elLeft.setAttribute("type", "long");
                    }
                    else
                    {
                        //if (elRight != null && elLeft == null)
                        if (elRight)
                            elRight.setAttribute("type", "long");
                        else
                            if (elLeft)
                                elLeft.setAttribute("type", "long");

//                            if (elRight == null && elLeft != null)
//                                elLeft.setAttribute("type", "long");
                    }
                    newline = false;
                    newlineEnd = false;
                }
            }
        }
    }

}


function GetClassName(el)
{
    var className="x";
    if(el)
    {
        var _indexOf=el.className;
        if(_indexOf.indexOf("newline")!=-1)
            _indexOf="newline";
        else
            _indexOf=_indexOf.replace("box_",""); 
        
        switch(_indexOf)
        {
            case "center":className="c";
            break;
            case "right":className="r";
            break;
            case "left":className="l";
            break;
            case "newline":className="n";
            break;                                
            default:
            break;
        }          
    }
    return className;
}    

function GetMyHeight(blockHeight,printPageHeight)
{ 
    while(blockHeight-printPageHeight>=0)
    {
      blockHeight=blockHeight-printPageHeight;
    }
    return blockHeight;
}
function GetOffsetTop(el)
{
    var off=el.offsetTop;
    var pObj=el.offsetParent;
    if(pObj) off+=GetOffsetTop(pObj);  
    return off;
}    


//**********************************************

