/************************** js funcion list *****************************
- int    GetObjPosXY(obj, posXY)
- str    RemoveHTMLTags()
- void  AutoResizeTextAreaHeight(what, minRows)
- int    GetBrowserDimension(what)
- int    GetBrowserScrollXY(what)
- str    ReplaceAll(what, strTarget, strSubString)
- void  SetSelectedDate(date, nulled, id) 	
- void   CallCalendar(fieldId)
- void   ToggleLoadingMask(ShowHide, MaskLayer, Opacity, BgColor)

************************************************************************/




//-----------------------------------------------------------------
//-- Fx name: GetObjPosXY
//-- Description: Get position of an object --
//-- Author : Kevin Lee 
//-- Date: Sep 29, 09
//-- limitation: NA
//-----------------------------------------------------------------
// what = "x" or "y"; "" = y
// e.g.  GetObjPosXY(ABC, "y") = y position of object ABC 
//
function GetObjPosXY(obj, posXY)
{
	var posX = obj.offsetLeft;
	var posY = obj.offsetTop;
	while(obj.offsetParent)
	{
		posX=posX+obj.offsetParent.offsetLeft;
		posY=posY+obj.offsetParent.offsetTop;
		if(obj==document.getElementsByTagName('body')[0])  {break}
		else  {obj=obj.offsetParent;}
	}
	
	if (posXY == "x")
	{  return posX;   }
	else if (posXY == "y")
	{  return posY;   }
}





//-----------------------------------------------------------------
//-- Fx name: RemoveHTMLTags
//-- Description: remove HTML tags from string --
//-- Author : Kevin Lee 
//-- Date: April 1, 08
//-- limitation: state does not hold at post back
//-----------------------------------------------------------------
// e.g. RemoveHTMLTags(document.form1.aTextArea)
function RemoveHTMLTags()
{
    var re = /(<([^>]+)>)/gi;
    for (i=0; i<arguments.length; i++)
    arguments[i].value=arguments[i].value.replace(re, "")
}






//-----------------------------------------------------------------
//-- Fx name: AutoResizeTextAreaHeight
//-- Description: Auto resize the TextArea height at the event of keyup --
//-- Author : Kevin Lee 
//-- Date: April 1, 08
//-- limitation: state does not hold at post back
//-----------------------------------------------------------------
// what = textarea object
// minRows = minimum number of rows preset for textarea
// e.g. <Textarea rows=4  onkeyup = "AutoResizeTextAreaHeight(this, 4)" ></textarea>
//
function AutoResizeTextAreaHeight(what, minRows)
{
	var CRCount =  what.value.split("\n");
	
	what.rows = minRows;
	if (CRCount.length  > what.rows)
	{ what.rows =  CRCount.length;}
}




//-----------------------------------------------------------------
//-- Fx name: GetBrowserDimension
//-- Description: Get browser window current size --
//-- Author : Kevin Lee 
//-- Date: May 28, 08
//-- limitation: it returns integer value; to be compatible to Mozilla and IE, specification of style.height need to add unit "px"
//-----------------------------------------------------------------
// what = "height" or "width"; "" = height
// e.g.  GetBrowserDimension("height") = height of current browser window 
//
function GetBrowserDimension(what) {
	  var myWidth = 0, myHeight = 0;
	  if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
	    myWidth = window.innerWidth;
	    myHeight = window.innerHeight;
	  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
	    myWidth = document.documentElement.clientWidth;
	    myHeight = document.documentElement.clientHeight;
	  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	    //IE 4 compatible
	    myWidth = document.body.clientWidth;
	    myHeight = document.body.clientHeight;
	  }
	  
	  if (what.toLowerCase() == "width")
	  {  return myWidth;   }  
	  else
	  {  return myHeight;   }  
 }


 
 
//-----------------------------------------------------------------
//-- Fx name: GetBrowserScrollXY
//-- Description: Get browser current scroll position --
//-- Author : Kevin Lee 
//-- Date: May 28, 08
//-- limitation: NA
//-----------------------------------------------------------------
// what = "x" or "y"; "" = y
// e.g.  GetBrowserScrollXY("y") = current vertical scroll position 
//
 function GetBrowserScrollXY(what) {
	  var scrOfX = 0, scrOfY = 0;
	  if( typeof( window.pageYOffset ) == 'number' ) {
	    //Netscape compliant
	    scrOfY = window.pageYOffset;
	    scrOfX = window.pageXOffset;
	  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
	    //DOM compliant
	    scrOfY = document.body.scrollTop;
	    scrOfX = document.body.scrollLeft;
	  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
	    //IE6 standards compliant mode
	    scrOfY = document.documentElement.scrollTop;
	    scrOfX = document.documentElement.scrollLeft;
	  }
	  
	  if (what.toLowerCase() == "x")
	  {	  return scrOfX;    }
	  else
	  {	  return scrOfY;    }
}
 
 
 

//-----------------------------------------------------------------
//-- Fx name: ReplaceAll
//-- Description: Replaces all instances of the given substring, case insensitive --
//-- Author : Kevin Lee 
//-- Date: May 13, 08
//-- limitation: NOT TESTED YET!!!
//-----------------------------------------------------------------
//    strTarget, // The substring you want to replace
//    strSubString // The string you want to replace in.
//  e.g. ReplaceAll("this is a sentence", "this", "it");
function ReplaceAll(what, strTarget, strSubString)
{
	var strText = what;
	var intIndexOfMatch = strText.indexOf( strTarget );

	// Keep looping while an instance of the target string
	// still exists in the string.
	while (intIndexOfMatch != -1)
	{
		// Relace out the current instance.
		strText = strText.replace( new RegExp( strTarget, "gi" ), strSubString )
		
		// Get the index of any next matching substring.
		intIndexOfMatch = strText.indexOf( strTarget );
	}
	
	// Return the updated string with ALL the target strings
	// replaced out with the new substring.
	return( strText );
}












//-----------------------------------------------------------------
//-- Fx name: SetSelectedDate
//-- Description: works with eWorld date picker, set selected date to a dedicated textbox --
//-- Author : Kevin Lee 
//-- Date: ??? 2007
//-- limitation: target date textbox has to have the save prefix as the date picker control
//-----------------------------------------------------------------
// id = ID of the date picker control
// e.g. APPROVAL_PICKER  &  APPROVAL_DATE
//
function SetSelectedDate(date, nulled, id) 
{
    var DateFieldName = id.replace(/PICKER/gi, 'DATE');
    document.getElementById(DateFieldName).value = date;
}




//-----------------------------------------------------------------
//-- Fx name: CallCalendar
//-- Description: works with eWorld date picker, trigger eWorld Calendar at and event trigger of a control (mainly for TextBox)--
//-- Author : Kevin Lee 
//-- Date: ??? 2007
//-- limitation: target date textbox has to have the save prefix as the date picker control
//-----------------------------------------------------------------
// fieldID = ID of the control that will trigger the eWorld Calendar
// e.g. a textbox called ABC_DATE: onclick="CallCalendar(this)" will fire the calendar control ABC_PICKER
//
function CallCalendar(fieldId)
{
    if (fieldId.readonly!="readonly")
    {
    	fieldId.blur();  // remove the focus from the textbox
    	var pickerId = fieldId.id.substring(0, fieldId.id.length-4) + "PICKER"; //replace the name 
    	try
    	{
        	CalendarPopup_FindCalendar(pickerId).Show();
    	}
    	catch(error){}
    }
}






//-----------------------------------------------------------------
//-- Fx name: ToggleLoadingMask
//-- Description: display Div layer as mask layer that hide background layers with semi-transparent bgcolor; Support IE6/7, Legacy Mozilla, Firfox, Safari, Konquer --
//-- Author : Kevin Lee 
//-- Date: June 2 08
//-- limitation: only support div layer with style attribute "display:none" set as default / on PostBack Page_Load need to load ToogleLoadingMask("hide", "xxxx", '','') to clear mask layer;
//-----------------------------------------------------------------
// ShowHide = "show" or "hide"
// MaskLayer = Layer Div ID that represents the mask
// Opacity = between 0 to 1; 0 = transparent; 1 = solid color; default 0.5
// BgColor = Mask layer background color in Hex code (e.g. #ffffff)
// e.g. OnClick="ToggleLoadingMask('show', 'LayerLoading', 0.8, '#000000')"
//
function ToggleLoadingMask(ShowHide, MaskLayer, Opacity, BgColor) 
{
    /*
    opacity: 0.5;
    This one is the official CSS3 method, at the moment it works in newer Mozilla (Gecko) browers, Safari and Opera.
    -moz-opacity: 0.5;
    This one works in older versions of Mozilla browsers.
    -khtml-opacity: 0.5;
    This is used by older versions of browsers that use the KHTML and WebKit rendering engine, namely Konquerer on Linux and Safari on Mac OS X.
    filter: alpha(opacity=50);
    This one works only in MSIE.
    
     object.opacity = (opacity / 100); 
    object.MozOpacity = (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter = "alpha(opacity=" + opacity + ")"; 
    */
    
    var MaskOpacity = 0.5;
    var MaskBgColor = "#FFFFFF";
    if (!isNaN(Opacity) && Opacity < 1)
    {
        MaskOpacity = Opacity;
    }
    if (BgColor != "")
    {    MaskBgColor = BgColor;  }
    

    if (document.getElementById(MaskLayer) != null)
	{
        if (ShowHide.toLowerCase() == "show")
        {
	        document.getElementById(MaskLayer).style.zIndex = "100";
	        document.getElementById(MaskLayer).style.display = "";
	        document.getElementById(MaskLayer).style.left = "0";
	        document.getElementById(MaskLayer).style.top = "0";
	        document.getElementById(MaskLayer).style.position = "absolute";
	        document.getElementById(MaskLayer).style.backgroundColor = MaskBgColor;
	        document.getElementById(MaskLayer).style.border = "none";
	        document.getElementById(MaskLayer).style.margin = "0"
	        document.getElementById(MaskLayer).style.width = "100%";
	        document.getElementById(MaskLayer).style.height = "100%";
	        document.getElementById(MaskLayer).style.filter = "alpha(opacity="+(MaskOpacity*100)+")";
	        document.getElementById(MaskLayer).style.MozOpacity = MaskOpacity;
	        document.getElementById(MaskLayer).style.opacity = MaskOpacity;
	        document.getElementById(MaskLayer).style.KhtmlOpacity = MaskOpacity;
	     }
	     else
	     {
	        document.getElementById(MaskLayer).style.zIndex = "-1";
	        document.getElementById(MaskLayer).style.display = "none";
	     }
	 }
	 
	 
	 setTimeout("ToggleLoadingMask('hide','"+MaskLayer+"','','')", 30000);
}