
//====================================================================================================
//	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)/3;

	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;
    }
}

function formatNumber(pnumber,decimals) 
{  
	if (isNaN(pnumber)) { return 0};  
	if (pnumber=='') { return 0};  
	
	var IsNegative=(parseInt(pnumber)<0); 
	if(IsNegative)pnumber=-pnumber; 
	
	var snum = new String(pnumber);  
	var sec = snum.split('.');  
	var whole = parseInt(sec[0]);  
	var result = '';  
	if(sec.length > 1){  
		var dec = new String(sec[1]);  
		dec = parseInt(dec)/Math.pow(10,parseInt(dec.length-decimals-1)); 
		Math.round(dec); 
		dec = parseInt(dec)/10; 

		if(IsNegative) 
		{ 
		  var x = 0-dec; 
		      x = Math.round(x); 
		  dec = - x; 
		} 
		else 
		{ 
		      dec = Math.round(dec); 
		} 

		/* 
		 * If the number was rounded up from 9 to 10, and it was for 1 'decimal' 
		 * then we need to add 1 to the 'whole' and set the dec to 0. 
		 */ 
		if(dec==Math.pow(10, parseInt(decimals)))
		{ 
		  whole+=1; 
		  dec="0"; 
		} 

	    dec = String(whole) + "." + String(dec);  
	    var dot = dec.indexOf('.');  
	    if(dot == -1){  
	      dec += '.';  
	      dot = dec.indexOf('.');  
	    } 
		var l=parseInt(dot)+parseInt(decimals); 
	    while(dec.length <= l) { dec += '0'; }  
    	result = dec;
  } else{  
    var dot;  
    var dec = new String(whole);  
    dec += '.';  
    dot = dec.indexOf('.');  
	var l=parseInt(dot)+parseInt(decimals); 
    while(dec.length <= l) { dec += '0'; }  
    result = dec;  
  }  
  if(IsNegative)result="-"+result; 
  return result;  
}


function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function ShowHide_Click(fld1, fld2)
{

	if(fld1)
	{
		if(fld1.style.visibility == 'hidden')
		{
			fld1.style.visibility	= 'visible';
			fld1.style.display		= 'block';
			fld2.style.visibility 	= 'hidden';
			fld2.style.display		= 'none';
		}
		else
		{
			fld1.style.visibility 	= 'hidden';
			fld1.style.display		= 'none';
			fld2.style.visibility	= 'visible';
			fld2.style.display		= 'block';
		}
	}
}

