﻿/*
Javascript library containing utility functions
*/

var UtilLib = Class.create();

UtilLib.prototype =
{
    // Constructor
    initialize : function()
    {
    },
    
    IsEmptyXmlNode : function(piXmlNode)
    {
        return ((piXmlNode == null) || (piXmlNode.text == null) || (piXmlNode.text.blank()));
    },
    
    GetElementOffset : function(piElem)
    {
        var offset = {x:0, y:0};
        
        while( piElem != null ) 
        {
            offset.y += piElem.offsetTop;
            offset.x += piElem.offsetLeft;
            piElem = piElem.offsetParent;
        }
        
        return offset;
    },
    
    // Check if the event occured within the actual target elem.
    // This will be checked by comparing mouse and elem positions
    EventOccuredWithinTarget : function(piEvent, piTarget)
    {
        // Check if parameters are filled
        if((piEvent == null) || (piTarget == null))
        {
            throw 'Null Reference Exception! One of the function parameters is not filled.';
        }
        
        // Declare return value valiable
        var retVal = false;
        
        // Get the elements ofset positition
        var elemOffset = Helpers.GetElementOffset(piTarget);
        var elemWidth = $(piTarget).getWidth();
        var elemHeight = $(piTarget).getHeight();
        
        // Get the cursor posititon
        var cursor = {x:0, y:0};
        cursor.x = Event.pointerX(piEvent)
        cursor.y = Event.pointerY(piEvent)
        
        if( ((cursor.x > elemOffset.x) && (cursor.y > elemOffset.y)) && ((cursor.x < (elemOffset.x + elemWidth)) && (cursor.y < (elemOffset.y + elemHeight))) )
        {
            retVal = true;
        }
        
        return retVal;
    },
    
    SplitString : function(piString, piChar)
    {
        // Check if input parameters are present and throw exception if not
        if((piString == null) || (piChar == null))
        {
            throw 'Error: The function SplitString requires 2 parameters.';
        }
        
        // Get all the characters of the string an push them into an Array
        var charCollection = piString.toArray();
        
        // Create string Array which will be returned back
        var returnArray = new Array();
        
        // Create variable holding the current word
        var currentWord = '';
        
        // Iterate through the char Array
        for(var i = 0; i < charCollection.length; i++)
        {
            if(charCollection[i] != piChar)
            {
                currentWord += charCollection[i];
            }
            else
            {
                returnArray.push(currentWord.strip());
                currentWord = '';
            }
        }
        
        returnArray.push(currentWord.strip());
        
        return returnArray;
    },
    
    ParseBoolean : function(piBoolString)
    {
        if(piBoolString == null)
        {
            throw 'Error: The function \'UtilLib.ParseBoolean\' requires 1 inout parameter.';
        }
        
        var retVal = false;
        if(piBoolString.toLowerCase() == 'true')
        {
            retVal = true;
        }
        
        return retVal;
    },
    
    GetWindowSize : function() 
    {
        var win = {width:0, height:0};
        
        if( typeof( window.innerWidth ) == 'number' ) 
        {
            //Non-IE
            win.width = window.innerWidth;
            win.height = window.innerHeight;
        } 
        else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) 
        {
            //IE 6+ in 'standards compliant mode'
            win.width = document.documentElement.clientWidth;
            win.height = document.documentElement.clientHeight;
        } 
        else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) 
        {
            //IE 4 compatible
            win.width = document.body.clientWidth;
            win.height = document.body.clientHeight;
        }
        
        return win;
    },
    
    GetWindowScrollOffset : function()
    {
        var offset = {top:0, left:0};
        
	    if (self.pageYOffset) // all except Explorer
	    {
		    offset.left = self.pageXOffset;
		    offset.top = self.pageYOffset;
	    }
	    else if (document.documentElement && document.documentElement.scrollTop)
		    // Explorer 6 Strict
	    {
		    offset.left = document.documentElement.scrollLeft;
		    offset.top = document.documentElement.scrollTop;
	    }
	    else if (document.body) // all other Explorers
	    {
		    offset.left = document.body.scrollLeft;
		    offset.top = document.body.scrollTop;
	    }
	    
	    return offset;
    }
}
    