﻿// #Region "Pager Utility Class"
// callbackPage, async, method
function PagerUtility()
{
    // Public Properties
	var args = PagerUtility.arguments;
	var callback = null; var async = true; var method = 0;
	if(args.length > 0)callback = args[0];
	if(args.length > 1)async = args[1];
	if(args.length > 2)method = args[2];	
    
	this.callBackPage	    = callback;
	this.request            = null;
	this.xml                = null;
	this.httpMethod         = method;
	this.async              = async;
	this.parameters		    = null;			
	this.contentContainer   = null;
	this.pagerContainer     = null;
	this.showLoader         = true;
	this.pattern            = null;
	this.state				= null;
	this.extractMethod		= default_extractMethod;	
	this.bw                 = new BrowserLib;
	
	// Events					
	this.onLoad             = null;	
	this.onBeforeRequest    = null;
	this.onRequestComplete  = null;
	this.onEmpty            = null;
}
// #End Region

// #Region "Public Enums"
PagerUtility.prototype.httpMethods = {get:0, post:1}
PagerUtility.prototype.requestState = {idle:0, loading:1}
// #End Region

// #Region "Public Methods"
PagerUtility.prototype.setLoader   = _setLoader;
PagerUtility.prototype.loadPage    = _loadPage;
PagerUtility.prototype.isLoading   = function() {return(this.state == 1)}
// #Region

// #Region "Private Methods"
function _loadPage()
{
	// Change Status
	this.state = this.requestState.loading;

    // IE < 7
    if(window.ActiveXObject)
		this.request = new ActiveXObject('MSXML2.XmlHttp');		
	else
	    this.request = new XMLHttpRequest();	
	
	// Create url		
	var url = 'http://' + location.hostname + '/' + 'XmlCallback/' + this.callBackPage;	
	
	// Set Loader
	if(this.showLoader)
	    this.setLoader();		
	    
    // Set Http Method
    var httpMethod = "POST";
    if(this.httpMethod == this.httpMethods.get)
        httpMethod = "GET";
    
    // Create Request
    //httpMethod = "POST";
    if(httpMethod == 'GET')
    {
		url += '?{0}'.Format(this.parameters);
		this.parameters = null;
	}
	
	this.request.open(httpMethod, url, this.async);	
	if(!this.onLoad)
	   this.onLoad = setXml;	
	
	// Assign Event Handler	
	this.request.onreadystatechange = this.onLoad;
			
	if(httpMethod == 'POST')
		this.request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	    		    
	// Execute before request handler
	if(this.onBeforeRequest && typeof(this.onBeforeRequest) == 'function')
	    this.onBeforeRequest();
	  
	// Set Parameters
	this.request.send(this.parameters);	
			
	// Cache request for internal handler
	var rq = this;
		
	// Internal handler
	function setXml()
	{
	    if(rq)
	    {	        
	        if(rq.request.readyState == 4)	        	        
	        {	            
	            /////////////////////////////////////////////////	                
			    var xml = rq.request.responseXML;

			    var content = findObj(rq.contentContainer);			    	    
			    var pager = findObj(rq.pagerContainer);
				
				if(rq.bw.ie)
                    rq.request.responseXML.setProperty("SelectionLanguage", "XPath");
							
			    if(rq.bw.opera)
			    {					
				    var tags = xml.getElementsByTagName('*');				
				    if(typeof(tags[1].innerHTML) != 'undefined')
				    {
						if(content)content.innerHTML = __clear(tags[1].innerHTML);
						if(pager)pager.innerHTML = __clear(tags[2].innerHTML);
					}
					else // Newer version of opera
					{
						var data = '';
						if(rq.extractMethod)
							data = rq.extractMethod(tags[1]);
						else
							data = __read(tags[1]);
													
						if(content)content.innerHTML = data;
						if(pager)pager.innerHTML = __read(tags[2]);
					}
			    }
			    else
			    {				
					var data = '';					
					if(rq.extractMethod)
						data = rq.extractMethod(xml.selectSingleNode('//data'));											
					else
						data = __read(xml.selectSingleNode('//data'));
											
				    if(content)content.innerHTML = data;				    					
				    if(pager)pager.innerHTML = __read(xml.selectSingleNode('//pager'));				
			    }						
																									
			    if(content && 
			            (content.childNodes.length == 0 || content.innerHTML.trim().length == 0))
			    {					
				    if(rq.onEmpty && typeof(rq.onEmpty) == 'function')
				        rq.onEmpty();				   
			    }
    			
			    if(pager)
			        pager.disabled = false;
	             ////////////////////////////////////////////////
	             // Execute Event Handler
	             
	             if(rq.onRequestComplete && typeof(rq.onRequestComplete) == 'function')
	                rq.onRequestComplete()
	               
				rq.state = rq.requestState.idle;
	        }
	    }
	}
}

// Show Loader
function _setLoader()
{		
	var src = findObj(this.contentContainer);
	if(src)
	{
		var height = src.offsetHeight;
		var width = src.offsetWidth;
		
	    src.innerHTML = '<table bgcolor="#D1D1D1" width="' + width + '" height="' + 
				      (height) + '"><tr><td align="center">' +
				      '<img src="/images/loading.gif" /></td></tr></table>';
		
//	    if(this.bw.ie)
//		    src.childNodes[0].style.filter = "alpha(opacity=30);"
//	    else
//		    src.childNodes[0].style.MozOpacity = 0.3;
		$(src.childNodes[0]).css('opacity', 0.3);
    }
}

function default_extractMethod(node)
{	
	if(!node)
		return '';
	else
	{
		var html = __read(node);
				
		// Create a dummy container
		var span = document.createElement('span');				
		span.innerHTML = html;
		
		var div = span.getElementsByTagName('div')[2];		
		return div.innerHTML;
	}
}

function __read(node)
{
	if(!node)
		return '';
	else
	{
		if(window.ActiveXObject)
			return node.text;
		else
			return node.textContent;
	}
}

function __clear(str)
{
	str = str.replace('<![CDATA[', '');
	str = str.replace(']]>', '');
	return str;
}

// #End Region
