﻿
function IsFitInHeight(posY,height)
{
    return (GetScrollHeight()>=(posY+height))
}

function IsFitInWidth(posX,width)
{
    return (GetScrollWidth()>=(posX+width))
}
       
function GetScrollWidth()
{
   var w = window.pageXOffset ||
           document.body.scrollLeft ||
           document.documentElement.scrollLeft;
           
   return w ? w : 0;
}

function GetScrollHeight()
{
   var h = window.pageYOffset ||
           document.body.scrollTop ||
           document.documentElement.scrollTop;
           
   return h ? h : 0;
}
             
function CurrentX(event)
{
    return event.clientX+(document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);
}

function CurrentY(event)
{
    return event.clientY+(document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
}

function attachToEvent(eventOwner,eventName,delegate)
 {    
      if(window.addEventListener)
      { // Mozilla, Opera, Safari
        if(eventName.substring(0,2) == "on")
        {
            eventName = eventName.substring(2,eventName.length-1);
        }
        
        eventOwner.addEventListener(eventName,delegate,false);
      }
      else//IE
      {
        if(eventName.substring(0,2) != "on")
        {
            eventName = "on"+ eventName;
        }
        eventOwner.attachEvent(eventName,delegate);
      }

      
 }
 function detachFromEvent(eventOwner,eventName,delegate)
 {
      //IE only for now Ж-(
      eventOwner.detachEvent(eventName,delegate);
 }

function IsInArea(xpoint,ypoint,srcElement)
{
    var res = false;
    var elLeft = srcElement.style.pixelLeft;
    var elWidth = srcElement.style.pixelWidth;
    var elTop = srcElement.style.pixelTop;
    var elHeight = srcElement.style.pixelHeight;
   
    if(srcElement!=null)
    {
        res = ((elLeft+elWidth)>=xpoint && xpoint>=elLeft && (elTop+elHeight)>=ypoint && ypoint>=elTop)
    }
    
    return res; 
}
 function getWindowHeight() {
			var windowHeight = 0;
			if (typeof(window.innerHeight) == 'number') {
				windowHeight = window.innerHeight;
			}
			else {
				if (document.documentElement && document.documentElement.clientHeight) {
					windowHeight = document.documentElement.clientHeight;
				}
				else {
					if (document.body && document.body.clientHeight) {
						windowHeight = document.body.clientHeight;
					}
				}
			}
			return windowHeight;
		}
function getWindowWidth() {
			var windowWidth = 0;
			if (typeof(window.innerWidth) == 'number') {
				windowWidth = window.innerWidth;
			}
			else {
				if (document.documentElement && document.documentElement.clientWidth) {
					windowWidth = document.documentElement.clientWidth;
				}
				else {
					if (document.body && document.body.clientWidth) {
						windowWidth = document.body.clientWidth;
					}
				}
			}
			return windowWidth;
		}

 var net = new Object();
  //constructor for prototype of progressbar
    net.PerisProgressBar = function(settings)
    {
         this._settings = settings._Settings;
         this._invokers = new Array();
         this.divProgressBar = null;
         this._allowToClear = true;
         this._AddedNewInvoker = false;
         this._unloadAttempts = 0;
         this._ForceClosing = true;
         var ppb = this;
         this._StopTracing = false;
         
         attachToEvent(window,"load",function(event)
            {
                ppb.blockUp = new net.ActivityBlocker("ProgressBackground");
                attachToEvent(document,"click",function(event){ppb.traceInvokers(event)});    
                attachToEvent(document,"mousedown",function(event){ppb.traceInvokers(event)});    
                attachToEvent(window,ppb._settings._eventname,function(event){ppb.ShowProgressBar(event)});
                attachToEvent(window,"unload",function(event){ppb._ForceClosing = false});
                for(var i = 0;i<document.forms.length;i++)
                {
                    attachToEvent(document.forms[i],"submit",function(event){ppb._AddedNewInvoker=true;})
                }
            }
            
         )

         
    }
    // beginning of the prototype implementation
    net.PerisProgressBar.prototype ={
        //shows progress bar
        ShowProgressBar:function(event){
                this._StopTracing = true;
                this._allowToClear = false;
                this._ForceClosing = true;
                var pb = this;
                pb._unloadAttempts = pb._unloadAttempts+1;
                
                if(this.divProgressBar==null)
                {
                    this.divProgressBar = document.getElementById(this._settings._divname);
                    pb.divProgressBar.style.position = "absolute";                 
                        
                    attachToEvent(window,"resizeend",function(event){pb.progressAlignCenter(event);})
                    attachToEvent(window,"scroll",function(event){pb.progressAlignCenter(event);})
                        
                    //attachToEvent(imgBody,"dblclick",function(){pb.CloseProgressBar();});
                    attachToEvent(pb.divProgressBar,"dblclick",function(event){pb.CloseProgressBar();});
                    attachToEvent(pb.divProgressBar.getElementsByTagName("IMG")[0],"dblclick",function(event){pb.CloseProgressBar();});
                }
                
                pb.CloseProgressBar(); 
 
                if(this._AddedNewInvoker)
                {
                    if(this.AllowInvocation())
                    {   pb.setCaption();
                        pb.progressAlignCenter.call(pb);       
                        setTimeout(function()
                            {pb.divProgressBar.style.zIndex = 10001;
                            pb.divProgressBar.style.visibility = "visible";
                            pb.blockUp.Block();
                        },1);
                        
                        setTimeout(function(){pb.CheckValidators();},500);
                        
                        
                    }
                }

                    
                this._AddedNewInvoker = false;
                this._allowToClear = true;
                this._StopTracing = false;;
        },
        //Hide progress bar
        CloseProgressBar:function(){
            this.divProgressBar.style.visibility = "hidden";
            this.blockUp.UnBlock();
        },
        CheckValidators:function()
        {
		    if(typeof(Page_ValidationSummaries)!='undefined')
		    {
			    for(var i = 0;i<Page_ValidationSummaries.length;i++)
			    {
				    if(Page_ValidationSummaries[i].style.display!="none" && Page_ValidationSummaries[i].innerText.length!=-1)
				    {
				        this.CloseProgressBar();
				        break;
				    }
    			}
	    	 }
		    
			if(typeof(Page_Validators)!='undefined')
            {
			    for(var i = 0;i<Page_Validators.length;i++)
                {
			        if(Page_Validators[i].style.display!="none" && Page_Validators[i].style.visibility!="hidden")
					{
					        this.CloseProgressBar();
					        break;
					}
                }
            }
    		 
        },
        AllowInvocation:function()
        {
            var i;
            var j;
            var allow = true;
            var array = this._settings._suspends;
            
            if(this._invokers==null){this._invokers = new Array();}
            for(i =0;i<array.length;i++)
            {
                for(j = 0;j<this._invokers.length;j++){
                    if(this.Match(array[i].ClientId,this._invokers[j]))
                    {
                        allow = false;
                        break;
                    }
                }
            }
            
            return allow;
        },
        Match:function(idwhat,idwith)
        {
            var _match;
            _match = (idwhat==idwith);
            if(this._settings._comparemode=="False" && !_match)
            {
                _match = (idwith.indexOf(idwhat)>0);
            }
            
            return _match
            
        }
        ,
        //Aligns progress bar by center 
        progressAlignCenter:function(){
            if (document.getElementById) {
				var windowHeight = getWindowHeight();
				if (windowHeight > 0) {
				    if(this.divProgressBar){
					    var contentElement = this.divProgressBar;
					    var contentHeight = contentElement.offsetHeight;
					    if (windowHeight - contentHeight > 0) {
						    //contentElement.style.position = 'relative';
						    contentElement.style.top = ((windowHeight / 2) - (contentHeight / 2));
						    contentElement.style.left = document.body.clientWidth/2;
						    contentElement.children[0].style.top = contentElement.style.top;
						    contentElement.children[0].style.left = contentElement.style.left;
					    }
					    else {
						    contentElement.style.position = 'static';
					    }
			        }
				}
			}
		}
		,
		//Looks after users clicking on elements. Stores these elements in temporary list (for several secs)
		traceInvokers:function(event)
		{
		    if(this._StopTracing)
		        return;
		        
		   
		    this._allowToClear = false;
		    var _array = this._invokers;
		    var ppb = this;
		    ppb._AddedNewInvoker = false;
		    if(event.srcElement!=null)
		    {
		          _array[_array.length] = event.srcElement.id;
		           setTimeout(function(){ppb._allowToClear=true;},700);
		           setTimeout(function(){ppb.clearInvokers(ppb)},1200);
		           ppb._AddedNewInvoker = true;
		    }
		},
		//Removes unnecessary elems from invokers list
		clearInvokers:function(_ppb)
		{
		    _ppb._AddedNewInvoker = false;
		    if(_ppb._allowToClear)
		    {
		        _ppb._invokers = null;
		        _ppb._invokers = new Array();
		    }
		},
		//Set custom captions
		setCaption:function()
		{
		    var array = this._settings._captions;
		    var captionIndex = -1;
		    for(var i = 0;i<this._invokers.length;i++)
		    {
		        for(var j=0;j<array.length;j++)
		        {
		            if(this.Match(array[j].ClientId,this._invokers[i]))
		            {
		                captionIndex = j;
		            }
		        }
		    }
		    if(captionIndex!=-1)
		    {
		        this.divProgressBar.getElementsByTagName("SPAN")[0].innerText = array[captionIndex].Caption;
		    }
		    else
		    {
		        this.divProgressBar.getElementsByTagName("SPAN")[0].innerText = "";
		    }
		}
        
     //the end of progress bar prototype
     }
 //user activity blockirator
    net.ActivityBlocker=function(css,timeOut)
    {
        this.timeOut = timeOut;
        this.blockerDiv = document.getElementById("__PERIS_BLOCKER");
        if(this.blockerDiv==null){
         this.blockerDiv = document.createElement("DIV");
         this.blockerDiv.id = "__PERIS_BLOCKER";
         
        this.blockerDiv.className = css;
        this.blockerDiv.style.zIndex = 10001;
        this.blockerDiv.style.width = getWindowWidth();
        this.blockerDiv.style.height = getWindowHeight();
        this.blockerDiv.style.top = 0;
        this.blockerDiv.style.left = 0;
        this.blockerDiv.style.position = "absolute";
        this.blockerDiv.style.visibility = "hidden";
        document.body.appendChild(this.blockerDiv);
        }
    }
    //user activity blockirator prototype
    net.ActivityBlocker.prototype={
        Block:function(bc){
            this.blockerDiv.style.width = getWindowWidth();
            this.blockerDiv.style.height = getWindowHeight();
            this.blockerDiv.style.visibility = "visible";
          
        },
        UnBlock:function(){
            this.blockerDiv.style.visibility = "hidden";
        }
    }
    //Snap View browser
    net.Snap=function(snaparams){

        this._params = snaparams; 
                
        this.Value = document.getElementById(this._params.id).value;
        
        this._mainDiv = document.createElement("DIV");
        this._mainDiv.style.position = "absolute";
        this._mainDiv.style.width = this._params.framewidth!="0" ? this._params.framewidth : "100";
        this._mainDiv.style.height = this._params.frameheight!="0" ? this._params.frameheight : "100";
        this._mainDiv.style.visibility = "hidden";
        this._mainDiv.style.zIndex = 1000;
        this._mainDiv.className = this._params.css;
        this.SetCountDown = false;
        this._IsHandlingMovement = false;
        
        this._iframe = document.createElement("IFRAME");
        this._iframe.border = 0;
        this._iframe.frameBorder = 0;
        this._iframe.width = this._params.framewidth!="0" ? this._params.framewidth : "100";;
        this._iframe.height = this._params.frameheight!="0" ? this._params.frameheight : "100";
        this._iframe.style.overflowX = "visible";
        this._iframe.style.overflowY = "visible";
        this._iframe.scrolling = "no";      
        this._controlelem = document.getElementById(this._params.controlid);        
        
        this._img = document.createElement("IMG");
        this._img.src = this._params.loadimgpath;
        this._img.src = this._params.loadimgpath;
        
        this._imgHolderDiv = document.createElement("DIV");
        this._imgHolderDiv.style.position = "absolute";
        this._imgHolderDiv.style.visibility = "hidden";
        this._imgHolderDiv.appendChild(this._img);
        this._imgHolderDiv.style.zIndex = 10001;
        
       
          
        var snap = this;
        attachToEvent(window,"load",function(){
            document.body.appendChild(snap._imgHolderDiv);
            snap._mainDiv.appendChild(snap._iframe);
            document.body.appendChild(snap._mainDiv);
            if(snap._controlelem!=null)
            {
                attachToEvent(snap._controlelem,"mouseover",function(event){snap.Show(event);});
            }
            
        });
               
    }
    net.Snap.prototype={
        IsLoading:function()
        {
            return (this._iframe.src!="" && this._iframe.readyState != "complete");
        },
        ShowProgressBar:function()
        {
            var snap = this;
            var downloadandclose=function()
            {
                if(typeof(snap._iframe.readyState)!="undefined")
                { 
                    
                    if(snap._iframe.readyState!="complete")
                    {
                         setTimeout(downloadandclose,100); 
                    }
                    else
                    {
                        snap._imgHolderDiv.style.visibility = "hidden";
                    }
                }
                else
                {
                    //I removed this part of code because mostly all known browsers have security policy that denies access to document obj in child windows.
//                     if(snap._params.autosize!="false")
//                     {
//                         snap._iframe.width = window.document.body.scrollWidth+15;
//                         snap._iframe.height = window.document.body.scrollHeight+15;            
//                         snap._iframe.contentWindow.document.body.style.overflow = "visible";
//                         snap._iframe.contentWindow.document.body.scroll="no"
//                         snap._mainDiv.style.width = window.document.body.scrollWidth+20;
//                         snap._mainDiv.style.height = window.document.body.scrollHeight+20;
//                     }
                     snap._imgHolderDiv.style.visibility = "hidden";
                }
            }
            var px = (typeof snap._mainDiv.style.left == "string") ? "px" : 0;    
            snap._imgHolderDiv.style.left =  snap._mainDiv.offsetLeft + ( snap._mainDiv.offsetWidth - snap._imgHolderDiv.offsetWidth)/2 + px;
            snap._imgHolderDiv.style.top =  snap._mainDiv.offsetTop +  (snap._mainDiv.offsetHeight - snap._imgHolderDiv.offsetHeight)/2 + px;
            snap._imgHolderDiv.style.visibility = "visible";
            setTimeout(downloadandclose,500);    
            
        },
        Show:function(event)
        {
            
            var snap = this;
            snap.SetCountDown = false;
            var px = (typeof snap._mainDiv.style.left == "string") ? "px" : 0; 
            var curX = CurrentX(event);
            var curY = CurrentY(event);
            
            //curY = IsFitInHeight(curY,snap._mainDiv.style.pixelHeight) ? curY - 10: curY - snap._mainDiv.style.pixelHeight + 10;
            var divWidth = snap._mainDiv.offsetWidth || snap._mainDiv.style.pixelWidth;
            ///curX = IsFitInWidth(curX,divWidth) ? curX - 10: curX - divWidth + 10;
            snap._mainDiv.style.left = curX + 10 + px;
            snap._mainDiv.style.top = curY - 20 + px;
                
            if(snap._iframe.src == "")
            {
               var urlstr = snap._params.url;
               
               if(snap.Value!="")
                    urlstr = urlstr + "?id="+snap.Value;
               if(snap._params.urlparams!="")
                    urlstr = urlstr + "&"+snap._params.urlparams;
                
               
               snap._iframe.src = urlstr;   
               snap.ShowProgressBar();
               
            }
            
            if(snap.IsLoading())
            {
                this._imgHolderDiv.style.left =  this._mainDiv.offsetLeft + ( this._mainDiv.offsetWidth - this._imgHolderDiv.offsetWidth)/2 + px;
                this._imgHolderDiv.style.top =  this._mainDiv.offsetTop +(this._mainDiv.offsetHeight - this._imgHolderDiv.offsetHeight)/2 + px;
            }
            
            snap._mainDiv.style.visibility = "visible"; 
           // snap._iframe.style.visibility = "visible";            
            
            
            snap.StartCountDown=function(){snap.Hide();};
            
            snap.HandleMovement=function(event){
                if(!IsInArea(CurrentX(event),CurrentY(event),snap._mainDiv))
                {
                    //snap.SetCountDown = false;
                   // snap.StopCountDown();
//                }
//                else
//                {
                    if(!snap.SetCountDown)
                    {
                        snap.SetCountDown = true;
                        setTimeout(snap.StartCountDown,parseInt(snap._params.timeout));
                    }
                }
            };
            if(!snap._IsHandlingMovement)
            {
                attachToEvent(document,"mousemove",snap.HandleMovement); 
                snap._IsHandlingMovement = true;
            }
            
        },
        Hide:function()
        {
        
            this._mainDiv.style.visibility = "hidden";
            //this._iframe.style.visibility = "hidden";
            this._imgHolderDiv.style.visibility  = "hidden";
            this._mainDiv.style.left = -10000;
            this._mainDiv.style.top = -10000;
            //detachFromEvent(document,"mousemove",this.HandleMovement);
            
        },
        StopCountDown:function()
        {
           clearTimeout(this.StartCountDown);
        }
        
    }
    
                
    