﻿/// <summary>
/// Search and returns an element on the document
/// </summary>
/// <param name="elementId">Element ID</param>
function $( elementId )
{
    return document.getElementById( elementId );
} // function $

/// <summary>
/// Returns the number of days for the given month of the given year
/// </summary>
function $DaysInMonth(month, year)
{
	return 32 - new Date(year, month, 32).getDate();
} // $DaysInMonth

/// <summary>
/// Returns the max length of all the string within
/// the given array of strings
/// </summary>
function $MaxStringLength( arrayOfStrings )
{
	var maxLength = 0;
	
	for (var i = 0; i < arrayOfStrings.length; i++)
		if ( arrayOfStrings[i].length > maxLength )
			maxLength = arrayOfStrings[i].length;
	
	return maxLength;
} // $MaxStringLength

/// <summary>
/// Returns the top and left position of the given
/// element
/// </summary>
/// <param name="elementId">Element ID</param>
function $Pos ( elementId ) 
{
    var top = 0;
    var left = 0;
    var object = null;
    
    object = $( elementId );
	if ( object.offsetParent ) 
	{
		do 
		{
			left += object.offsetLeft;
			top += object.offsetTop;
		} 
		while ( object = object.offsetParent );
	}
	
	return [left,top];
} // function $Pos

/// <summary>
/// Returns the top and left position of the given
/// element. This functions scans the given element parent,
/// and the parent of the parent and so on, to get the left
/// and top position of the given element. This function
/// stop the scan when any parent has an absolute positioning.
/// </summary>
/// <param name="elementId">Element ID</param>
function $PosTillFirstAbsoluteParentFound ( elementId )
{
    var top = 0;
    var left = 0;
    var object = null;
    
    object = $( elementId );
	if ( object.offsetParent ) 
	{
		do 
		{
		    if ( object != null && object.style.position == 'absolute' )
		        break;
		
			left += object.offsetLeft;
			top += object.offsetTop;
			
			//alert( object.id + ', left: ' + object.offsetLeft + ', top: ' + object.offsetTop );
		} 
		while ( object = object.offsetParent );
	}
	
	return [left,top];
} // function $PosTillFirstAbsoluteParentFound

/// <summary>
/// Returns true if the given value is a numeric value
/// </summary>
function IsNumeric( value )
{
    var validChars = "0123456789";
    var isNumber = true;
    var character = null;

    for (i = 0; i < value.length && isNumber == true; i++) 
    { 
        character = value.charAt( i ); 
        if ( validChars.indexOf( character ) == -1 ) 
        {
            isNumber = false;
        }
    }
    
    return isNumber;  
} // IsNumeric

/// <summary>
/// Returns the height of the given element
/// </summary>
function GetHeight( element )
{
    var height = 0;
    
    height = element.clientHeight;
    if ( ! height || height == 0 )
        height = element.offsetHeight;
        
    return height;
} // GetHeight

/// <summary>
/// Returns the left of the given element
/// </summary>
function GetLeft( element )
{
    var left = 0;
    
    left = element.clientLeft;
    if ( ! left || left == 0 )
        left = element.offsetLeft;

    return left;
} // GetLeft

/// <summary>
/// Returns the top of the given element
/// </summary>
function GetTop( element )
{
    var top = 0;
    
    top = element.clientTop;
    if ( ! top || top == 0 )
        top = element.offsetTop;
        
    return top;
} // GetTop

/// <summary>
/// Returns the width of the given element
/// </summary>
function GetWidth( element )
{
    var width = 0;
    
    width = element.clientWidth;
    if ( ! width || width == 0 )
        width = element.offsetWidth;
        
    return width;
} // GetWidth

/// <summary>
/// Attach a new event handler to the given element and event
/// </summary>
function AttachEventHandler( element, eventName, functionObject )
{
    var ok = false;
    
    if ( element.attachEvent )
    {
        ok = element.attachEvent( 'on' + eventName, functionObject );
    }
    else if ( element.addEventListener )
    {
        element.addEventListener( eventName, functionObject, false );
        ok = true;
    }
        
    return ok;
} // AttachEventHandler

/// <summary>
/// Fix the given string adding the given character 
/// an specific cant of times
/// </summary>
function $FixString( originalString, length, fixChar, appendAtTheBegining )
{
	var newString = '';
	
	newString = originalString + '';
	while ( newString.length < length )
	{
		if ( appendAtTheBegining )
			newString = fixChar + '' + newString;
		else
			newString += '' + fixChar;
	}
		
	return newString;
} // $FixString

/// <summary>
/// Create and return a new selection range on 
/// the given text box control
/// </summary>
function $CreateSelectionRange( textControl, startIndex, length )
{
	var newSelectionRange = null;
	
	// IE	
	if ( textControl.createTextRange ) 
	{
		newSelectionRange = textControl.createTextRange();				
		newSelectionRange.moveStart( 'character', startIndex );
		newSelectionRange.moveEnd( 'character',  ( startIndex + length ) - textControl.value.length );
	}
	
	return newSelectionRange;
} // $CreateSelectionRange

/// <summary>
/// Select and focus the given selection range
/// on the given text box control
/// </summary>
function $SetSelectionRange( textControl, startIndex, length )
{
	var selectionRange = null;
	
	// IE	
	if ( textControl.createTextRange ) 
	{
		selectionRange = $CreateSelectionRange( textControl, startIndex, length );
		selectionRange.select();
	}
	// Firefox	
	else if ( textControl.selectionStart || textControl.selectionStart == '0' )
	{
		textControl.setSelectionRange(startIndex, startIndex + length);
	}
} // $SetSelectionRange

/// <summary>
/// Returns the caret position on the given 
/// text box control
/// </summary>
function $GetCaretPosition( textControl ) 
{	
	var pos = 0;
	var selection = null;
	
	// IE	
	if ( document.selection ) 
	{		
		textControl.focus();		
		selection = document.selection.createRange();		
		selection.moveStart ('character', -textControl.value.length);		
		pos = selection.text.length;
	}	
	// Firefox	
	else if ( textControl.selectionStart || textControl.selectionStart == '0' )		
		pos = textControl.selectionStart;
			
	return pos;
} // $GetCaretPosition

/// <summary>
/// Broadcast Message
/// </summary>
function $BroadcastMessage( targetObject, message )
{
    var array = null;
    
    //Prepare parameters for the message
    array = new Array();
    //Add the first parameter as the target object
    array.push( targetObject );
    //(exclude first 2 since those are: targetObject and message parameters)
    for(var i = 2; i < arguments.length; i++)
        array.push( arguments[i] );
        
    //Send the message if exists on the target object
    if ( targetObject[message] )
        targetObject[message].apply( this, array );
} // $BroadcastMessage

document.Screen =
{
	/// <summary>
	/// Returns the document Width
	/// </summmary>
	Width : function()
	{
		return window.innerWidth != null ? window.innerWidth : 
			   document.documentElement && document.documentElement.clientWidth ? 
			   document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null;
	}, // Width
	
	/// <summary>
	/// Returns the document Height
	/// </summmary>
	Height : function()
	{
		return window.innerHeight != null ? window.innerHeight :
        	   document.documentElement && document.documentElement.clientHeight ? 
        	   document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null;
	}, // Height
	
	/// <summary>
	/// Returns the scroll offset left
	/// </summmary>
	ScrollOffsetLeft : function()
	{
		return window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft;
	}, // ScrollOffsetLeft
	
	/// <summary>
	/// Returns the scroll offset top
	/// </summmary>
	ScrollOffsetTop : function()
	{
		return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
	} // ScrollOffsetTop
};