//************************************************
// Project:	   System 2.0.0
// Filename:   datefunc.js
// Date&Time:  03.03.2002
//			   04.03.2002	
// Version:	   1.0
//
// Purpose:    datetime and string functions
// Author:     M.Klygin
//************************************************
//**Start Encode**

var defaultFormat = "dd-MM-yyyy HH:mm:ss" ;
var dateParts = new Array("yyyy", "MM", "dd" ,"HH", "mm", "ss") ;



//var WshShell = WScript.CreateObject("Wscript.Shell");
//
//var testDate="23/02/2002 13:02:03" ;
//var testFormat="dd-mm-yyyy HH:MM:SS" ;
//
//var test = parseDate(testDate, testFormat)
//
////WScript.Echo(test);
//
////WScript.Echo(padr("qwe",5,"0"));
////WScript.Echo(left("qwerty",2));
////WScript.Echo(right("qwerty",2));
////WScript.Echo(replace("qwerty",2,3,"1234"));
//
////WScript.Echo(formatDate(test,testFormat)) ;
//
//WScript.Echo(matchpos("12345", "3*5")) ;
//


//************************************************
// Function:	parseDate
// Arguments:
//	as_Date		string with date
//	as_Format	format string
//
// Purpose:	    convert string to date using given format
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function parseDate(as_Date, as_Format)
{
	var fmt = dateParts;
	var val = new Array(fmt.length) ;

	if (arguments.length <= 1)
	{
		as_Format = defaultFormat ;
	}

	for (i = 0; i < fmt.length; i++)
	{
		val[i] = 0 ;
		var pos = as_Format.indexOf(fmt[i]) ;
		if (pos >= 0)
		{
			val[i] = parseInt(as_Date.substr(pos, fmt[i].length), 10);
		} ;
	} ;

	var res = new Date(val[0], val[1] - 1, val[2], 
					   val[3], val[4], val[5]) ;

	return res;
}

//************************************************
// Function:	formatDate
// Arguments:
//	ad_Date		date
//	as_Format	format string
//
// Purpose:	    convert date to string using given format
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************

function formatDate(ad_Date, as_Format)
{
	var fmt = dateParts ;
	var tmp ;
	var res ;

	if (arguments.length <= 1)
	{
		as_Format = defaultFormat ;
	}

	res = as_Format ;
	for (i = 0; i < fmt.length; i++)
	{
		var pos = res.indexOf(fmt[i]) ;
		if (pos >= 0)
		{
			switch (fmt[i])
			{
			case "yyyy":
				tmp = ad_Date.getYear();
				break ;
			case "MM":
				tmp = ad_Date.getMonth() + 1;	// month 0-11 (january-december)
				break ;
			case "dd":
				tmp = ad_Date.getDate();
				break ;
			case "HH":
				tmp = ad_Date.getHours();
				break ;
			case "mm":
				tmp = ad_Date.getMinutes();
				break ;
			case "ss":
				tmp = ad_Date.getSeconds();
				break ;
			};
			
			tmp = padl(tmp, fmt[i].length, "0") ;
			res = replace(res, pos, fmt[i].length, tmp) ;
		} ;
	} ;

	return res;
}

//************************************************
// Function:	padr
// Arguments:
//	arg			string or number
//	ai_len		length
//	as_char		symbol
//
// Purpose:	    analog of FoxPro padr() function
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function padr(arg, ai_len, as_char)
{
	var ls_result, li_len ;

    ls_result = new String(arg) ;
    li_len = ls_result.length ;

    if (li_len < ai_len)
    {
        ls_result = ls_result + fill(as_char, ai_len - li_len)
    }
    return ls_result ;
}

//************************************************
// Function:	padl
// Arguments:
//	arg			string or number
//	ai_len		length
//	as_char		symbol
//
// Purpose:	    analog of FoxPro padl() function
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function padl(arg, ai_len, as_char)
{
	var ls_result, li_len ;

    ls_result = new String(arg) ;
    li_len = ls_result.length ;

    if (li_len < ai_len)
    {
        ls_result = fill(as_char, ai_len - li_len) + ls_result 
    }
    return ls_result ;
}

//************************************************
// Function:	fill
// Arguments:
//	ac_char		symbol
//	ai_len		length
//
// Purpose:	    fill string with given char
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function fill(ac_char, ai_len)
{
	var ls_res;

	ls_res = new String() ;

	for (i = 0; i < ai_len; i++)
	{
		ls_res+=ac_char ;
	};

	return ls_res;

}

//************************************************
// Function:	left
// Arguments:
//	as_string	string
//	ai_len		length
//
// Purpose:	    analog of left() function
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function left(as_string, ai_len)
{
	return as_string.substr(0, ai_len) ;
}

//************************************************
// Function:	right
// Arguments:
//	as_string	string
//	ai_len		length
//
// Purpose:	    analog of right() function
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function right(as_string, ai_len)
{
	return as_string.substr(as_string.length - ai_len, ai_len) ;
}

//************************************************
// Function:	replace
// Arguments:
//	as_string	source string
//	ai_pos		started position
//	ai_len		length
//	as_replace	replace string
//
// Purpose:	    analog of replace() function
// Version:		1.0	   
// Author:		M.Klygin
// History:		03.03.2002	initial version
//************************************************
function replace(as_string, ai_pos, ai_len, as_replace)
{
	return left(as_string, ai_pos) + as_replace + as_string.substr(ai_pos+ai_len) ;
}

//************************************************
// Function:	ltrim
// Arguments:
//	inString	string
//
// Purpose:	    analog of ltrim() function
// Version:		1.0	   
// Author:		Sybase (look to dwstr.js)
// History:		
//************************************************
function ltrim(inString)
{
    var index, tempChar, outString = "";
    var strLength = inString.length;
    // skip leading blanks
    for (index=0; index < strLength; index++)
        {
        tempChar= inString.charAt (index);
        if (tempChar != " ")
            break;
        }
    if (index < strLength)
        outString = inString.substring(index, strLength);

    return outString;
}

//************************************************
// Function:	rtrim
// Arguments:
//	inString	string
//
// Purpose:	    analog of rtrim() function
// Version:		1.0	   
// Author:		Sybase (look to dwstr.js)
// History:		
//************************************************
function rtrim(inString)
{
    var index, tempChar, outString = "";
    // skip trailing blanks
    for (index=inString.length-1; index >= 0; index--)
        {
        tempChar= inString.charAt (index);
        if (tempChar != " ")
            break;
        }
    if (index >= 0)    
        outString = inString.substring(0, index+1);

    return outString;
}

//************************************************
// Function:	convertlike
// Arguments:
//	inString	string
//
// Purpose:	
// Version:		1.0	   
// Author:		Sybase (look to dwstr.js)
// History:		
//************************************************
function convertlike(inString, escapeChar)
{
    var index;
    var outString = "";
    var tempChar;
    var strLength = inString.length;

    if (arguments.length < 2)
        escapeChar = "";
        
    // force to string type or charAt will fail!
	inString = inString + "";
    for ( index=0; index < strLength; index++ )
        {
        tempChar = inString.charAt( index );
        if (tempChar == escapeChar) 
            {
            index++;
            outString += inString.charAt( index );
            }
        else if (tempChar == "%" || tempChar == "*")
            outString += ".*";
        else if (tempChar == "_" || tempChar == "?")
            outString += ".";
        else
            outString += tempChar;
        }
    return new RegExp(outString);
}

//************************************************
// Function:	match
// Arguments:
//	inString	string
//
// Purpose:	    analog of LIKE statement
// Version:		1.0	   
// Author:		M.Klygin
// History:		04.03.2002	initial version
//************************************************
function match(inString, pattern)
{
	var re = convertlike(pattern) ;
	//var rc = re.test(inString) ;
	//return rc ;
	var rc = inString.search(re) ;
	return rc >= 0;
	//return rc ;
}

//************************************************
// Function:	matchpos
// Arguments:
//	inString	string
//	pattern		search pattern
//
// Purpose:	    seek position by pattern
// Version:		1.0	   
// Author:		M.Klygin
// History:		04.03.2002	initial version
//************************************************
function matchpos(inString, pattern)
{
	var re = convertlike(pattern) ;
	var rc = inString.search(re) ;
	return rc ;
}
