
//====================================================================================================
//	File Name		:	functions.js
//----------------------------------------------------------------------------------------------------
//	Purpose			:	Javascript Utility functions
//	Author			:	Nirmal Patel
//	Creation Date	:	05-May-2003
//	Copyright		:	Copyrights © 2003 Dot Infosys
//	Email			:	info@dotinfosys.com
//	History			:
//						Date						Author						Remark
//						05-May-2003					Nirmal Patel				Initial Release
//
//====================================================================================================

//====================================================================================================
//	Function Name	:	popupWindowURL
//	Purpose			:	Whenever you wanna open a link into a new window just call this function
//								you need to pass some arguemnts as described below.
//	Parameters		:
//								url  = url to be open in the new window
//								winname = winname is the window name for the reference of that window
//								w is the width
//								h is the height
//								menu is the parameter, if you want menubar to be enabled on the window
//								resize if you wanna resize the window
//								scroll i fyou needed
//	Return			:	true or false
//	Author			:	Nirmal Patel
//	Creation Date	:	05-May-2003
//----------------------------------------------------------------------------------------------------

function popupWindowURL(url, winname,  w, h, menu, resize, scroll) {

    var x = (screen.width-w)/2;
    var y = (screen.height-h-100)/2;

	if (winname == null) winname = "newWindow";
	if (w == null) w = 800;
	if (h == null) h = 600;
	if (resize == null) resize = 1;

	menutype   = "nomenubar";
	resizetype = "noresizable";
	scrolltype = "noscrollbars";
	if (menu) menutype = "menubar";
	if (resize) resizetype = "resizable";
	if (scroll) scrolltype = "scrollbars";

    cwin=window.open(url,winname,"top=" + y + ",left=" + x + ",screenX=" + x + ",screenY=" + y + "," + "status," + menutype + "," + scrolltype + "," + resizetype + ",width=" + w + ",height=" + h);

	if (!cwin.opener) cwin.opener=self;
	cwin.focus();

	return true;
}

//====================================================================================================
//	Function Name	:	sprintf
//	Purpose			:	Provide the functionality of sprintf
//	Parameters		:		
//	Return			:	true or false
//	Author			:	Dinesh Sailor
//	Creation Date	:	01-July-2003
//----------------------------------------------------------------------------------------------------
function sprintf()
{
    if (!arguments || arguments.length < 1 || !RegExp)
    {
        return;
    }
    var str = arguments[0];
    var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
    var a = b = [], numSubstitutions = 0, numMatches = 0;
    while (a = re.exec(str))
    {
        var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
        var pPrecision = a[5], pType = a[6], rightPart = a[7];

        //alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

        numMatches++;
        if (pType == '%')
        {
            subst = '%';
        }
        else
        {
            numSubstitutions++;
            if (numSubstitutions >= arguments.length)
            {
                alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
            }
            var param = arguments[numSubstitutions];
            var pad = '';
                   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
              else if (pPad) pad = pPad;
            var justifyRight = true;
                   if (pJustify && pJustify === "-") justifyRight = false;
            var minLength = -1;
                   if (pMinLength) minLength = parseInt(pMinLength);
            var precision = -1;
                   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
            var subst = param;
                   if (pType == 'b') subst = parseInt(param).toString(2);
              else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
              else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
              else if (pType == 'u') subst = Math.abs(param);
              else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
              else if (pType == 'o') subst = parseInt(param).toString(8);
              else if (pType == 's') subst = param;
              else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
              else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
        }
        str = leftpart + subst + rightPart;
    }
    return str;
}

//====================================================================================================
//	Function Name	:	Record_Focus
//	Purpose			:	Provide the functionality for record selection
//	Parameters		:	mouseState		-	State of the mouse	
//						rowLostFocus	-	Class name to be use when row lost focus
//						rowGetFocus		-	Class name to be use when row get focus
//						rowSelected		-	Class name to be use when row get selectd
//						curRow			-	Id of current row
//						lastSelection	-	Id of last selection
//	Return			:	None
//	Author			:	Dinesh Sailor
//	Creation Date	:	18-July-2003
//----------------------------------------------------------------------------------------------------
function Record_Focus(mouseState, rowLostFocus, rowGetFocus, rowSelected, curRow, lastSelection)
{
	switch(mouseState)
    {
    	case 0:		// Row Get Focus
        	if(curRow.className != rowSelected)
				curRow.className = rowGetFocus;
        	break;
        case 1:		// Row Lost Focus
        	if(curRow.className != rowSelected)
				curRow.className = rowLostFocus;
        	break;
        case 2:		// Row Get clicked
        	if(curRow.className != rowSelected)
            {
				curRow.className = rowSelected;
                if(lastSelection.value)
		            document.getElementById(lastSelection.value).className = rowLostFocus;

	            lastSelection.value = curRow.id;
            }
            else
			{
	            curRow.className = rowGetFocus;
                if(lastSelection.value)
		            lastSelection.value = '';
			}				

        	break;
    }
}

