﻿function awi_GetKeyCode()
{
	var keycode;
	var e = window.event;
	if (e.keyCode) keycode = e.keyCode;
	else if (e.which) keycode = e.which;
	return keycode;
}

function awi_GetRootPath(rootPath)
{
    if(rootPath != '')
    {
        if(rootPath != "/")
        {
            rootPath = rootPath + "/";
        }
    }
    return rootPath;
}

function awi_switchImg(rootPath,oImg,path)
{
    if(rootPath != '')
    {
        if(rootPath != "/")
        {
            rootPath = rootPath + "/";
        }
        path = rootPath + path;
    }
    oImg.src = path;
}

function awi_TextBoxLengthFocus(objOrig,objDest,len)
{
    if(awi_GetKeyCode() != 9 && awi_GetKeyCode() != 16)
    {
        if(awi_GetType(objOrig) == 'domelement' && awi_GetType(objDest) == 'domelement')
        {
            if(objOrig.tagName.toLowerCase() == 'input')
            {
                if(objOrig.value.length == len)
                {
                    objDest.focus();
                }
            }
        }
    }
}

function awi_$(sObjId)
{
    if(awi_GetType(sObjId) == 'string')
    {
       return awi_$(document.getElementById(sObjId));
    }
    else if(awi_GetType(sObjId) == 'domelement')
    {
        return sObjId;
    }   
    else
    {
        return false;
    }
}

function awi_findPos(obj) {
    var curleft = 0;
    var curtop = 0;
    if(obj == null || obj == false)
    {
        return null;
    }    
    if (obj.offsetParent) {
    
	    do {
		    curleft += obj.offsetLeft;
		    curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
    }
    curtop += 25;
    return [curleft,curtop];
}

function awi_AddListener(obj, evType, fn) {
	if (obj.addEventListener) {obj.addEventListener(evType, fn, false);}
	else if (obj.attachEvent) {obj.attachEvent('on' + evType, fn);}
}

function awi_RemListener(obj, evType, fn) {
	if (obj.removeEventListener) {obj.removeEventListener(evType, fn, false);}
	else if (obj.detachEvent) {obj.detachEvent('on' + evType, fn);}
}

function awi_fixE(ev) {
	var e = ev ? ev : window.event;
	return e;
}

function awi_valPx(pixels) {
	return pixels + "px";
}
function awi_winW() {
	if (document.documentElement && (document.documentElement.clientWidth > 0)) 
	{
	    return document.documentElement.clientWidth;
	}
	else if (window.innerWidth) 
	{
	    return window.innerWidth;
	}
	else 
	{
	    return document.body.clientWidth;
	}
}

function awi_winH() {
	if (window.innerHeight) 
	{
	    return window.innerHeight;
	}
	else if (document.documentElement && (document.documentElement.clientHeight > 0)) 
	{
	    return document.documentElement.clientHeight;
	}
	else 
	{
	    return document.body.clientHeight;
	}
}

var awi_BodyZ = {
	toTop: function(elm) {
		if (!awi_BodyZ.que) 
		{
			awi_BodyZ.que = document.createElement("div");
			document.body.appendChild(awi_BodyZ.que);
		}
		if (!elm.awi_BodyZZ) 
		{
			elm.awi_BodyZZ = awi_BodyZ.nid++;
			awi_AddListener(elm, "mousedown", function() {
				awi_BodyZ.toTop(elm);
				if (awi_BodyZ.trace && (elm != awi_Trace.tDIV)) {awi_TraceObj.show(elm);}
				});
			awi_BodyZ.que.appendChild(elm);
			awi_BodyZ.set(elm);
		}
		else if (elm.awi_BodyZZ != awi_BodyZ.last.awi_BodyZZ) awi_BodyZ.set(elm);
	},

	set: function(elm) {
		elm.style.zIndex = awi_BodyZ.nextZ++;
		awi_BodyZ.last = elm;
	},
	nid:1,
	nextZ:1
}

var awi_Veil = { 
	show: function(display) {
		awi_Veil.init();
		if(display)
		{
		    awi_Veil.fix();
		    awi_Veil.veil.className = "veil_overlay_off";
		}
		else
	    {
	        awi_Veil.veil.className = "veil_overlay_on";
	    }		
	},

	fix: function() {
		awi_Veil.veil.style.width = awi_valPx(Math.max(document.body.scrollWidth, awi_winW()));
		awi_Veil.veil.style.height = awi_valPx(Math.max(document.body.scrollHeight, awi_winH()));
	},
	init: function() {
		if (typeof(awi_Veil.veil) == "undefined") 
		{
			awi_Veil.reqs = [];
			awi_Veil.veil = document.createElement('div');
			awi_Veil.veil.className = "veil_overlay_on";
			awi_Veil.veil.innerHTML = "&nbsp;";
			awi_BodyZ.toTop(awi_Veil.veil);
			awi_AddListener(window, "resize", awi_Veil.fix);
		}		
	}
}

var awi_PrintMgr = {
    timeoutId: '',
    
    clearContents: function(){
        this.init();
        this.printContainer.innerHTML = '';
    },
    
    print: function(){
        awi_PrintMgr.init();
        
        //window.onbeforeprint = awi_PrintMgr.hideForms;
        //window.onafterprint = awi_PrintMgr.showForms;
        
        if(awi_PrintMgr.printContainer.childNodes.length > 0)
        {
            if(this.timeoutId != '')
            {
                clearTimeout (this.timeoutId);
            }
            awi_PrintMgr.hideForms();
            window.print();
            this.timeoutId = setTimeout("awi_PrintMgr.showForms()", 4500 );
        }
    },
    
    addElement: function(obj){
        var sType = awi_GetType(obj);
        awi_PrintMgr.init();
        
        switch(sType)
        {
            case 'array':
            {
                for(var i=0;i<obj.length;i++)
                {
                    if(awi_GetType(obj[i]) == 'domelement')
                    {
                        this.printContainer.appendChild(obj[i]);
                    }
                    else
                    {
                        this.addElement(obj[i]);
                    }
                }
                break;
            }
            case 'domelement':
            {
                this.printContainer.appendChild(obj);
                break;
            }
            case 'string':
            {
                try
                {
                    this.printContainer.appendChild(document.getElementById(obj));
                }
                catch(e)
                {}
                break;
            }
            default:
            {
                break;
            }
        }
        
    },
        
    init: function(){
        if(typeof awi_PrintMgr.printContainer == 'undefined')
        {
            awi_PrintMgr.printContainer = document.createElement('div');
            awi_PrintMgr.printContainer.className = 'print';
            document.body.appendChild(awi_PrintMgr.printContainer);            
        }
    },
    
    hideForms: function(){
        for(var i=0;i<document.forms.length;i++)
        {
            document.forms[i].className = 'noprint';
        }
    },
    
    showForms: function(){
        for(var i=0;i<document.forms.length;i++)
        {
            document.forms[i].className = '';
        }
        awi_PrintMgr.printContainer.innerHTML = '';
        
        
    }
}

function awi_GetType( vExpression )
{	
	var sTypeOf = typeof vExpression;
	if( sTypeOf == "function" )
	{
		var sFunction = vExpression.toString();
		if( ( /^\/.*\/$/ ).test( sFunction ) )
		{
			return "regexp";
		}
		else if( ( /^\[object.*\]$/i ).test( sFunction ) )
		{
			sTypeOf = "object"
        }
	}
	if( sTypeOf != "object" )
	{
		return sTypeOf;
	}
	
	switch( vExpression )
	{
		case null:
			return "null";
		case window:
			return "window";
		case window.event:	
			return "event";
	}
	
	if( window.event && ( event.type == vExpression.type ) )
	{
		return "event";
	}
	
	var fConstructor = vExpression.constructor;
    if( fConstructor != null )
	{
		switch( fConstructor )
		{																	
			case Array:
				sTypeOf = "array";
				break;
			case Date:
				return "date";
			case RegExp:
				return "regexp";
			case Object:
				sTypeOf = "jsobject";
				break;
			case ReferenceError:
				return "error";
			default:
				var sConstructor = fConstructor.toString();
				var aMatch = sConstructor.match( /\s*function (.*)\(/ );
				if( aMatch != null )
				{
					return aMatch[ 1 ];
				}
			
		}
	}

	var nNodeType = vExpression.nodeType;
	if( nNodeType != null )
	{	
		switch( nNodeType )
		{
			case 1:
				if( vExpression.item == null )
				{
					return "domelement";
				}
				break;
			case 3:
				return "textnode";
		}
	}
	
	if( vExpression.toString != null )
	{
		var sExpression = vExpression.toString();
		var aMatch = sExpression.match( /^\[object (.*)\]$/i );
		if( aMatch != null )	
		{
			var sMatch = aMatch[ 1 ];
			switch( sMatch.toLowerCase() )
			{
				case "event":
					return "event";
				case "math":
					return "math";
				case "error":	
					return "error";
				case "mimetypearray":
					return "mimetypecollection";
				case "pluginarray":
					return "plugincollection";
				case "windowcollection":
					return "window";
				case "nodelist":
				case "htmlcollection":
				case "elementarray":
					return "domcollection";
			}
		}
	}
	
	if( vExpression.moveToBookmark && vExpression.moveToElementText )
	{
		return "textrange";
	}
	else if( vExpression.callee != null )
	{
		return "arguments";
	}
	else if( vExpression.item != null )	
	{
		return "domcollection";
	}
	
	return sTypeOf;
}




