﻿function SF_Util()
{

	var self = this;

	///////////////////////////////////////////////////////////////////////////
	//	Constants

	var c_nKeyCodeF1 = 112;
	var c_nKeyCodeF12 = 123;
	var c_nKeyCode0 = 48;
	var c_nKeyCode9 = 57;
	var c_nKeyCodeA = 65;
	var c_nKeyCodeZ = 90;
	
	var c_nKeyCodeTab   = 9;
	var c_nKeyCodeEnter = 13;
	var c_nKeyCodeEsc   = 27;
		
	var c_nKeyCodeSpace   = 32;
		
	var c_nKeyPageUp		= 33;
	var c_nKeyPageDown		= 34;
	var c_nKeyEnd			= 35;
	var c_nKeyHome			= 36;	
	var c_nKeyLeftArrow		= 37;
	var c_nKeyUpArrow		= 38;
	var c_nKeyRightArrow	= 39;
	var c_nKeyDownArrow		= 40;

    var c_sBase64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	
	///////////////////////////////////////////////////////////////////////////
	//	String utils
	
	this.IsTrue = function( sVal )
	{
		var bRet = false;

		if ("boolean" == typeof( sVal ))
		{
			bRet = sVal;
		}
		else if (null != sVal && sVal.length > 0)
		{
			var ch = sVal.toLowerCase().charAt(0);
			if( ch == "t" || ch == "y" || ch == "1" )
			{
				bRet = true;
			}
		}

		return bRet;
	}

	this.Trim = function( sVal )
	{
		var sRes = "";
		if (sVal)
		{
			// Use a regular expression to replace leading and trailing spaces with the empty string
		 	sRes = sVal.replace( /(^\s*)|(\s*$)/g, "" );
	 	}
	 	return sRes;
	}

	///////////////////////////////////////////////////////////////////////////
	//	Base64 Encode/Decode

    this.Base64Encode = function( sIn )
    {
        var sOut = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        sIn = Base64Utf8Encode(sIn);
        while (i < sIn.length)
        {
            chr1 = sIn.charCodeAt(i++);
            chr2 = sIn.charCodeAt(i++);
            chr3 = sIn.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2))
            {
                enc3 = enc4 = 64;
            }
            else if (isNaN(chr3))
            {
                enc4 = 64;
            }

            sOut = sOut +
				c_sBase64Chars.charAt(enc1) + c_sBase64Chars.charAt(enc2) +
				c_sBase64Chars.charAt(enc3) + c_sBase64Chars.charAt(enc4);
        }

        return sOut;
    }

    // public method for decoding
	this.Base64Decode = function(sIn)
	{
		var sOut = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		sIn = sIn.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		while (i < sIn.length)
		{
			enc1 = c_sBase64Chars.indexOf(sIn.charAt(i++));
			enc2 = c_sBase64Chars.indexOf(sIn.charAt(i++));
			enc3 = c_sBase64Chars.indexOf(sIn.charAt(i++));
			enc4 = c_sBase64Chars.indexOf(sIn.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			sOut = sOut + String.fromCharCode(chr1);

			if (enc3 != 64)
			{
				sOut = sOut + String.fromCharCode(chr2);
			}
			if (enc4 != 64)
			{
				sOut = sOut + String.fromCharCode(chr3);
			}
		}

		sOut = Base64Utf8Decode(sOut);

		return sOut;
	}
	
	///////////////////////////////////////////////////////////////////////////
	//	Methods
	
	this.HTMLEncode = function( s )
	{
		var sRes = "";
		
		if( s )
		{
			var bAll = true;
            
            sRes = ReplaceStr( s, " ", "&nbsp;",bAll);
            sRes = ReplaceStr( sRes, "\n", "<br>",bAll);
            sRes = self.XMLEncode( sRes );
		}
		
		return sRes;
	}
	
	this.HTMLDecode = function( s )
	{
		var sRes = "";
		
		if( s )
		{
			var bAll = true;
            
            sRes = ReplaceStr( s, "&nbsp;", " ",bAll);
            sRes = ReplaceStr( sRes, "<br>", "\n",bAll);
			sRes = self.XMLDecode( sRes );
		}
		
		return sRes;
	}
	
	this.XMLEncode = function(s)
    {
        var sRes = "";
        
        if( s )
        {
            var bAll = true;
            sRes = ReplaceStr(s, "&", "&amp;",bAll);
            sRes = ReplaceStr(sRes, "<", "&lt;",bAll);
            sRes = ReplaceStr(sRes, ">", "&gt;",bAll);
            sRes = ReplaceStr(sRes, '"', "&quot;",bAll);
 //           sRes = ReplaceStr(sRes, "'", "&apos;",bAll);
        }
        
        return sRes;
    }
    
    this.XMLDecode = function(s)
    {
        var sRes = "";
        
        if( s )
        {
            var bAll = true;
            sRes = ReplaceStr(s, "&lt;", "<",bAll);
            sRes = ReplaceStr(sRes, "&gt;", ">",bAll);
            sRes = ReplaceStr(sRes, "&quot;", '"',bAll);
//            sRes = ReplaceStr(sRes, "&apos;", "'",bAll);
            sRes = ReplaceStr(sRes, "&amp;", "&", bAll);
        }
        
        return sRes;
    }
	
	///////////////////////////////////////////////////////////////////////////
	//	General
	
	this.PostExec = function( fn, oData )
	{
		function fnPostHandler()
		{	
			fn( oData );
		}

		return window.setTimeout( fnPostHandler, 0 );
	}
	
	/////////////////////////////////////////////////////////////////////////
	//	Keyboard utils
	
	this.IsDigitOrLetterKey = function( code )
	{
		return (( c_nKeyCode0 <= code )&&( c_nKeyCode9 >= code )) ||
				(( c_nKeyCodeA <= code )&&( c_nKeyCodeZ >= code ));
	}
	
	this.IsFunctionKey = function( code )
	{
		return ( c_nKeyCodeF1 <= code )&&( c_nKeyCodeF12 >= code );
	}
	
	this.IsEnterKey = function( code )
	{
		return ( c_nKeyCodeEnter == code )
	}
	
	this.IsSpaceBarKey = function( code )
	{
		return ( c_nKeyCodeSpace == code )
	}
	
	this.IsEscKey = function( code )
	{
		return ( c_nKeyCodeEsc == code )
	}
	
	this.IsTabKey = function( code )
	{
	    return ( c_nKeyCodeTab == code )
	}
	
	this.GetFnKeyStr = function( code )
	{
		var s = "F" + (code - c_nKeyCodeF1 + 1 );
	}	
	
	this.IsNavigationKey = function( code )
	{
		return ( c_nKeyPageUp <= code )&&( c_nKeyDownArrow >= code );
	}
	
	this.IsUpArrowKey = function( code )
	{
		return ( c_nKeyUpArrow == code );
	}
	
	this.IsLeftArrowKey = function( code )
	{
		return ( c_nKeyLeftArrow == code );
	}
	
	this.IsRightArrowKey = function( code )
	{
		return ( c_nKeyRightArrow == code );
	}
	
	this.IsDownArrowKey = function( code )
	{
		return ( c_nKeyDownArrow == code );
	}
	
	this.IsPageUpKey = function( code )
	{
		return ( c_nKeyPageUp == code );
	}
	
	this.IsPageDownKey = function( code )
	{
		return ( c_nKeyPageDown == code );
	}
	
	this.IsHomeKey = function( code )
	{
		return ( c_nKeyHome == code );
	}
	
	this.IsEndKey = function( code )
	{
		return ( c_nKeyEnd == code );
	}

    this.ArrayIndexOf = function( arr, obj )
    {
        var nRes = -1;
        if( null != arr )
        {
            var nCount = arr.length;
            for( var i = 0; i < nCount; ++i )
            {
                if( obj == arr[ i ] )
                {
                    nRes = i;
                    break;
                }
            }
        }
        return nRes;
    }
    
	/////////////////////////////////////////////////////////////////////////
	//	Implementation

    function ReplaceStr( sWhere, sWhat, sWith, bAll )
    {
        var oRegExp = new RegExp( sWhat, bAll ? "g" : "" );
        return sWhere.replace( oRegExp, sWith );
    }

    // private method for UTF-8 encoding
	function Base64Utf8Encode( str )
	{
		str = str.replace(/\r\n/g,"\n");
		var sUtfStr = "";
		for (var n = 0; n < str.length; n++)
		{
			var c = str.charCodeAt(n);
			if (c < 128)
			{
				sUtfStr += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048))
			{
				sUtfStr += String.fromCharCode((c >> 6) | 192);
				sUtfStr += String.fromCharCode((c & 63) | 128);
			}
			else
			{
				sUtfStr += String.fromCharCode((c >> 12) | 224);
				sUtfStr += String.fromCharCode(((c >> 6) & 63) | 128);
				sUtfStr += String.fromCharCode((c & 63) | 128);
			}
		}

		return sUtfStr;
	}

	// private method for UTF-8 decoding
	function Base64Utf8Decode( sUtfStr )
	{
		var str = "";
		var i = 0;
		var c = c1 = c2 = 0;
		while ( i < sUtfStr.length )
		{
			c = sUtfStr.charCodeAt(i);

			if (c < 128)
			{
				str += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224))
			{
				c2 = sUtfStr.charCodeAt(i+1);
				str += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else
			{
				c2 = sUtfStr.charCodeAt(i+1);
				c3 = sUtfStr.charCodeAt(i+2);
				str += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}

		return string;
	}
}

//These scripts should be removed in release mode.
function ASSERT(bExpr, sMsg) {
    if (!bExpr) DbgMsgX(sMsg + "\n\nInvoke debugger?");
}
function DbgMsgX(sMsg) {
    if (confirm(sMsg)) {
        try {
            eval('debugger')
        }
        catch (e) { }
    }
}



Common={};
if(typeof(Sys)!=='undefined')Sys.Application.notifyScriptLoaded();