/// <reference name="MicrosoftAjax.js"/>

var $U =
{
    ctl : function(e)
    {
        if(typeof e != 'object')
        {
            e = document.getElementById(e);
        }
        
        return e;
    },
    
    defined: function(e)
    {
        return (typeof e != 'undefined'); 
    },
    
    build : function()
    {
        var ctl = $U.ctl(arguments[0]);
        
        if(!ctl || !ctl.id)
        {
            var tag = 'div';
            
            if(arguments.length > 1) tag = arguments[1];
            
            ctl = document.createElement(tag);
            ctl.id = arguments[0];
            
            var container = document.body;
            
            if(arguments.length > 2) container = $U.ctl(arguments[2]);
            
            container.appendChild(ctl);
        }
        
        return ctl;
    },
    
    show : function(e)
    {
        var ctl = $U.ctl(e);
        ctl.style.display = '';
    },
    
    hide : function(e)
    {
        var ctl = $U.ctl(e);
        ctl.style.display = 'none';
    },
    
    visible : function(e, status)
    {
        var ctl = $U.ctl(e);
        
        if(status)
        {
            ctl.style.visibility = 'visible';
        }
        else
        {
            ctl.style.visibility = 'hidden';
        }
    },
    
    focus : function(e)
    {
        var ctl = $U.ctl(e);
        if(ctl.select)
        {
            ctl.select();
        }
        if(ctl.focus)
        {
            ctl.focus();
        }
    },
    
    html : function(e, html)
    {
        var ctl = $U.ctl(e);
        ctl.innerHTML = html;
    },
    
	checked: function(e)
	{
		var ctl = $U.ctl(arguments[0]);
		
		if(arguments.length > 1)
		{
			ctl.checked = arguments[1];
			
			return;
		}
		
		return ctl.checked;
	},
	
	disabled: function(e)
	{
		var ctl = $U.ctl(arguments[0]);
		
		if(arguments.length > 1)
		{
			ctl.disabled = arguments[1];
			return;
		}
		
		return ctl.disabled;
	},
	
    text : function()
    {
        var total = arguments.length;
        var ctl = $U.ctl(arguments[0]);
        
        if(ctl.type == 'text' || ctl.type == 'hidden' || ctl.type == 'password' || ctl.type == 'file' || ctl.type == 'select-one' || ctl.type == 'textarea')
        {
            if(total == 1) return ctl.value;
            
            ctl.value = arguments[1];
        }
        else if(ctl.type == 'checkbox' || ctl.type == 'radio')
        {
            if(total == 1)
            {
                if(ctl.checked)
                {
                    return ctl.value;
                }
                
                return null;
            }
            else
            {
                ctl.checked = (ctl.value == arguments[1]);
            }
        }
    },
	
	listValues : function()
	{
		var total = arguments.length;
		var ctl = $U.ctl(arguments[0]);
		var totalOptions = ctl.options.length;
		
		if(total == 1)
		{
			var result = new Array();
			for(var i = 0; i < totalOptions; i++)
			{
				var opt = ctl.options[i];
				
				if(opt.selected)
				{
					result[result.length] = opt.value;
				}
			}
			
			return result;
		}
		
		var data = arguments[1];
		var totalData = data.length;
		for(var i = 0; i < totalOptions; i++)
		{
			var opt = ctl.options[i];
			opt.selected = false;
			for(var j = 0; j < totalData; j++)
			{
				if(opt.value == data[j])
				{
					opt.selected = true;
				}
			}
		}
	},
    
    clearOptions : function(e)
    {
        var ctl = $U.ctl(e);
        
        var total = ctl.options.length;
        
        for(var i = 0; i < total; i++)
        {
            ctl.options[0] = null;
        }
    },
    
    addOption : function(e, text, value)
    {
        var ctl = $U.ctl(e);
        
        ctl.options[ctl.options.length] = new Option(text, value);
    },
    
    removeOption: function(e, value)
    {
        var ctl = $U.ctl(e);
        
        var total = ctl.options.length;
        
        for(var i = 0; i < total; i++)
        {
            if(ctl.options[i].value == value)
            {
                ctl.options[i] = null;
                
                return;
            }
        }
    },
    
    setDropDownDisplay : function(e)
    {
        var elements = document.getElementsByTagName('select');
        var total = elements.length;
        for(var i = 0; i < total; i++)
        {
            $U.visible(elements[i], e);
        }
    },
    
    viewPortInfo: function()
    {
        var vpWidth = 0;
        var vpHeight = 0;
        var cntHeight = 0;
        var scrollX = 0;
        var scrollY = 0;
        
        if(document.documentElement && document.documentElement.clientWidth)
        {
            vpWidth = document.documentElement.clientWidth;
            vpHeight = document.documentElement.clientHeight;
        }
        else if(document.body && document.body.clientWidth)
        {
            vpWidth = document.body.clientWidth;
            vpHeight = document.body.clientHeight;
        }
        else if(window.innerWidth)
        {
            vpWidth = window.innerWidth;
            vpHeight = window.innerHeight - 18;
        }
        
        if(document.body && document.body.offsetHeight)
        {
            cntHeight = document.body.offsetHeight;
        }
        
        if(document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop))
        {
            scrollX = document.documentElement.scrollLeft;
            scrollY = document.documentElement.scrollTop;
        }
        else if(document.body && (document.body.scrollLeft || document.body.scrollTop))
        {
            scrollX = document.body.scrollLeft;
            scrollY = document.body.scrollTop;
        }
        else if(window.pageXOffset || window.pageYOffset)
        {
            scrollX = window.pageXOffset;
            scrollY = window.pageYOffset;
        }
        else if(window.scrollX || window.scrollY)
        {
            scrollX = window.scrollX;
            scrollY = window.scrollY;
        }
        
        return {'width' : vpWidth, 'height' : vpHeight, 'contentHeight' : cntHeight, 'scrollX' : scrollX, 'scrollY' : scrollY};
    },
    
    
    position: function()
    {
        var ctl = $U.ctl(arguments[0]);
        
        if(arguments.length == 1)
        {
		    var width = ctl.offsetWidth;
		    var height = ctl.offsetHeight;
    		
		    var curleft = curtop = 0;
		    if (ctl.offsetParent) {
			    do {
				    curleft += ctl.offsetLeft;
				    curtop += ctl.offsetTop;
			    } while (ctl = ctl.offsetParent);
		    }
		    return {'left' : curleft, 'top' : curtop, 'width' : width, 'height': height};
		}
		else
		{
		    var pos = arguments[1];
		    
		    ctl.style.position = 'absolute';
		    if(!$U.defined(pos.noDisplay)) $U.show(ctl);
		    		    
		    if ($U.defined(pos.width)) ctl.style.width = pos.width + 'px';
	        if ($U.defined(pos.height)) ctl.style.height = pos.height + 'px';		    
	        
		    if(pos.align)
		    {
		        var info = $U.viewPortInfo();
		        
		        var x = ((info.width - ctl.offsetWidth)/2);
		        var y = ((info.height - ctl.offsetHeight)/2) + info.scrollY;
		        
		        if(pos.align == 't')
		        {
		            y = 0;
		        }
		        else if(pos.align == 'b')
		        {
		            y = (info.height - ctl.offsetHeight);
		        }
		        else if(pos.align == 'l')
		        {
		            x = 0;
		        }
		        else if(pos.align == 'r')
		        {
		            x = (info.width - ctl.offsetWidth);
		        }
		        else if(pos.align == 'tl' || pos.align == 'lt')
		        {
		            x = 0;
		            y = info.scrollY;
		        }
		        else if(pos.align == 'tr' || pos.align == 'rt')
		        {
		            x = (info.width - ctl.offsetWidth);
		            y = info.scrollY;
		        }
		        else if(pos.align == 'br' || pos.align == 'rb')
		        {
		            x = (info.width - ctl.offsetWidth);
		            y = (info.height - ctl.offsetHeight);
		        }
		        else if(pos.align == 'bl' || pos.align == 'lb')
		        {
		            x = 0;
		            y = (info.height - ctl.offsetHeight);
		        }
		        
				if(y < 0) y = 0;
				if(x < 0) x = 0;
				
		        ctl.style.top = y + 'px';
		        ctl.style.left = x + 'px';
		    }
		    else
		    {
		        if ($U.defined(pos.top)) ctl.style.top = pos.top + 'px';
		        if ($U.defined(pos.left)) ctl.style.left = pos.left + 'px';
		    }
		}
    },
    
    showProgress: function()
    {
        var ctl = $U.build('__ajax_status');
                
        $U.html(ctl, 'Working. Please wait...');
        ctl.className = 'progress';
        
        var args = arguments[0];
        var align = 'tr'; //top right align
        
        if( $U.defined(args))
        {
            if( $U.defined(args.message)) $U.html(ctl, args.message);
            if( $U.defined(args.css)) ctl.className = args.css;
            if( $U.defined(args.align)) align = args.align;
        }
        
        $U.position(ctl, {'align': align});
    },
    
    hideProgress: function()
    {
        var ctl = $U.ctl('__ajax_status');
        ctl.style.display = 'none';
    },
    
    showModal: function()
    {
        var ctl = $U.build('__ajax_block_ui');
        
        $U.setDropDownDisplay(false);
        //$U.hide('logo');
		
        var info = $U.viewPortInfo();
        
        ctl.className = 'blockUI';
        
        $U.position(ctl, {'top': 0, 'left': 0, 'width': info.width, 'height': Math.max(info.height, info.contentHeight)});
        
        
        var modal = $U.ctl(arguments[0]);
        modal.className = 'modal';
        
        var args = {'align' : 'c'};
        
        if(arguments.length > 1)
        {
            args = arguments[1];
            
            if(!$U.defined(args.align)) args.align = 'c';
        }
        
        $U.position(modal, args);
    },
    
    hideModal: function(e)
    {
        var modal = $U.ctl(e);
        //$U.hide(modal);
        
		$('#'+e).fadeOut('medium', 
		function()
		{
			var ctl = $U.ctl('__ajax_block_ui');
        	$U.hide(ctl);
			$U.setDropDownDisplay(true); 
			//$U.show('logo');
		});
    },
	
	showResponse : function(e, response)
	{
		if(response.succeed)
		{
			$U.message(e, response.message, 'message');
		}
		else
		{
			$U.message(e, response.message, 'error');
		}
	},
	
	message: function()
	{
		var ctl = $('#'+arguments[0]);
		
		var css = 'message';
		
		if(arguments.length > 2)
		{
			css = arguments[2];
		}
		
		$U.hideProgress();
		
		ctl.html(arguments[1]).addClass(css).fadeIn('fast');
	}
}