/*
'--------------------------------------------------------------------
'$Revision: 6589 $
'  $Author: CAng $
'    $Date: 2009-11-23 16:28:57 +1100 (Mon, 23 Nov 2009) $
'     $URL: svn://lonsec-utilserv/ITDev/trunk/Website%202005/aspx/js/utility.js $
'$NoKeywords: $
'--------------------------------------------------------------------
*/


function showHideContent(id, show) {
	var elem = document.getElementById(id);
	if (elem) {
		if (show) {
			elem.style.display = 'block';
			elem.style.visibility = 'visible';
		} else {
			elem.style.display = 'none';
			elem.style.visibility = 'hidden';
		}
	}
}        


/*
from ISBN: 0-596-00467-2
6.4 Creating a New Window
*/
//var myWindow;
function openCenteredWindow(url,scroll, windowFeatures) {

  if (windowFeatures==null)
  {
    var width = 825;
    var height = parseInt(screen.availHeight-70);
    var left = parseInt(screen.availWidth - width)/2;
    var top = 0;
    //TVB: hardcode to enable scroll bar for CFC.aspx 
    if( !scroll )
    {
		if (url.toLowerCase().indexOf('cfc.aspx') !=-1)
		{
			scroll = true;
		}
    }
    //NOTE:  This should be later remove when OpenWindow configuration allow to have scrollbar enable.
	if (scroll || scroll==null)
	   windowFeatures = "scrollbars=yes,width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;
	else
	   windowFeatures = "scrollbars=no,width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;
  }
    //myWindow = window.open(url, "subWind", windowFeatures);
    
    random = Math.floor(Math.random() * 5000).toString();
    var Mywindow = window.open(url,'subWin' + random , windowFeatures);
    Mywindow.focus();
}

//by Tony Nguyen 24 June 2005;

function printPreview() {
	
    var width = 900;
    var height = 600;
    var left = parseInt((screen.availWidth/2) - (width/2));
    var top = parseInt((screen.availHeight/2) - (height/2));
    var windowFeatures = "scrollbars=yes,width=" + width + ",height=" + height + 
        ",status,resizable,left=" + left + ",top=" + top + 
        ",screenX=" + left + ",screenY=" + top;
    //myWindow = window.open(url, "subWind", windowFeatures);

    var Mywindow = window.open("/aspx/Public/General/PrintPreview.aspx", "PrintPreview", windowFeatures);
    Mywindow.focus()
}



//SHOW AND HIDE A LAYER, compatible Netscape, IE, FireFox
//ShowHideLayer(name,'show') : Show a layer
//ShowHideLayer(name,'hide') : Hide a layer

function ShowHideLayer(name,what)
{

 var obj = document.getElementById(name);

  if(obj.style.display!=null)
  {
		what=(what=="hide")?"none":"block";
		if(obj) obj.style.display=what;
  }
  
  if(obj.visibility!=null)
  {
		if(obj) obj.visibility=what;
  }

}

// Show and Hide a Layer with heading, see /asp/portfolio.asp how to use

	function ShowHide(name)
	{
	
		s = document.getElementById(name);	 
		if(document.all)
		{
			if (s.style.display == "block")
				s.style.display = "none";
			else
				s.style.display = "block"; 
		}
        else
		{
			if (a.style.visibility = 'visible')
				s.style.visibility = 'hidden';
			else
				a.style.visibility = 'visible';
		}
	}

function FormatPercent(dblNum)
{
    
    dblNum.value = dblNum.value.replace("%","")
    
	if (isNaN(dblNum.value) || (dblNum.value.length==0))
	{
      return;
    }
    else
    {
	  dblNum.value = FormatNumber(dblNum.value,false,2,true) + '%'
	}
//	alert(dblNum)
}

function FormatPercentage(dblNum, nDecimal)
{
   var dblValue = dblNum.replace("%","");
   if ( isNaN(dblValue) || (dblValue.length == 0) )
   {
		dblValue = dblNum;
		
   }
   else
   {
        if( nDecimal > 0 )
			dblValue = FormatNumber(dblValue,false,nDecimal,true) + '%';
		else
			dblValue = FormatNumber(dblValue,false,nDecimal,false) + '%';
   }
   
   return dblValue;
}

function FormatNumber(dblNum, bShowCommas, nPrecision, bTrailingZeros) {

	//if (isNaN(dblNum))
      //return "0";
      
    var minus='';
	var i, j;

	// if negative, save the fact
    if (dblNum < 0) { minus='-'; }
    
    // remove any characters except for digits and a decimal point
    dblNum = FormatClean(dblNum);
    
	// separate integer ...
    var intNum = parseInt(dblNum);
    var sintNum = '' + intNum;

    // ... and decimal parts
    var decNum = dblNum - intNum;

	// then round number to nPrecision decimal places, defaults to 2
	//nPrecision = (!nPrecision) ? 2 : nPrecision;
	decNum= Math.round(decNum*Math.pow(10,nPrecision))/Math.pow(10,nPrecision);
    var sdecNum = '' + decNum;

	if (bTrailingZeros == true) {
	// append the number of zeros necessary to make up the precision requested
		j = sdecNum.length;
		i = sdecNum.indexOf('.') + 1;
		if (i == 0) {
			sdecNum += ".";
			i = j;
		}	
		for (i=j-i; i<nPrecision; i++) sdecNum += "0";
	}

	if (bShowCommas == true) {
	// place commas every three digits in the integer portion
		var sVal='';
		var Delim=',';
		var len;

		for (i = 0; i < Math.floor((sintNum.length-(1+i))/3); i++)
		{
			len = sintNum.length-(4*i+3)
			sintNum = sintNum.substring(0,len) + Delim + sintNum.substring(len);
		}
	}

	// return the reassembled whole
	return minus + sintNum + sdecNum.substring(1,sdecNum.length);
}

function FormatClean(num)
// remove formatting from number passed
{
	var sNum = num + '';
	var sVal='';
	var nNumLen = sNum.length;
	var sChar='';
	    
	for(var i=0;i<nNumLen;i++)
	{
		sChar = sNum.charAt(i);
		nChar = sChar.charCodeAt(0);
		if ((nChar >=48) && (nChar <=57) || (sChar=="."))  { sVal += sNum.charAt(i);   }
	}

	return sVal;
}


/* 
Macromedia functions
Image Rollovers
*/

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_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_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];}
}




/* Display control name tip */

function displayControlName()
{
   var el = document.all;
   
   for (var i=0; i< el.length; i++)
   {
      if (el[i].tagName=="INPUT" || el[i].tagName=="SELECT" || el[i].tagName=="TEXTAREA")
      {
          el[i].onmouseover= function() 
          {
            displayTip(this.name,true);
          }
          
          el[i].onmouseout= function() 
          {
			displayTip(this.name,false);
          }
      }
   
   } 
}

function displayTip(tip,display)
{
  if (display)
  {
       var _debug = document.getElementById("_debug");
       if (_debug != null)
      {

		_debug.innerHTML= tip;
		_debug.style.left= event.clientX + document.body.scrollLeft;
		_debug.style.top=  event.clientY + document.body.scrollTop;
		_debug.style.display='block';
	   }
   }	   	
   else
   {
        var _debug = document.getElementById("_debug");
        if (_debug != null)
			_debug.style.display='none';
   	}
}


function HTTPRequest(pageUrl)
{
	// Initialize the XmlHttp object
    try {
        //Mozilla Browsers
        xmlRequest = new XMLHttpRequest();
    } 
    catch (e) {
        try {
            //IE
            xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
        } 
        catch (e) {
            //Something else that won't work with this code...
            xmlRequest=false;
        }
    } 
    // Post our XmlRequest and get our desired string
    xmlRequest.open("GET", pageUrl, false);
    xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlRequest.send(null);
    
    return xmlRequest.responseText;
}    


function getElementByIdLike(elementId)
{
            var sel = document.all;
            for (var i = 0 ; i < sel.length; i++)
               if (sel[i].id.indexOf(elementId) !=-1)
                  return sel[i];
}

function PostCodeChangeEvent(postcode,suburb)
  {

	var pageUrl = "/aspx/public/general/GetSuburb.aspx?postcode=" + postcode.value;

	var text = HTTPRequest(pageUrl);

    // Return the XmlHttp object
	//alert(text);
    while (suburb.options.length>0)
		suburb.options[0] = null; 
	  
	if (text=="Invalid" )
	{
	   postcode.value="";
	   alert("Invalid post code!");
	   return;
	}
	
	var suburb_array=text.split(";");
	
	for (var i=0; i < suburb_array.length; i++)
	 {
	    var suburb_array2 = suburb_array[i].split(",");
		
		var newOption =	 new Option(suburb_array2[1] + ", " + suburb_array2[2],false,false)
		newOption.value = suburb_array2[0];
		suburb.options.add(newOption);
 	 }
	 suburb.disabled=false;	   
	if (suburb.options.length>1)
	{
		suburb.focus();
	}
	//else
 	    //suburb.disabled=true;
  }



/*
from ISBN: 0-596-00467-2
Submitting a Form by an Enter Key Press in Any Text Box
*/
function submitViaEnter(evt, submitButton) {
    evt = (evt) ? evt : event;
    var target = (evt.target) ? evt.target : evt.srcElement;
    var form = target.form;
    var charCode = (evt.charCode) ? evt.charCode :
        ((evt.which) ? evt.which : evt.keyCode);
    if (charCode == 13 || charCode == 3) {
		if (document.getElementById(submitButton.split(":").join("_")) != null)
        {
			var eventTarget = submitButton.split(":").join("$");
            __doPostBack(eventTarget,'');
            return false;
        }
		if (document.getElementById(submitButton.split("$").join("_")) != null)
        {
            __doPostBack(submitButton,'');
            return false;
        }
    }
    return true;
}

//STATISTICS WINDOW
function displayStats(url) {
				
				showStats(url,'','')		
}

function showStats(url, ModuleID,InvestmentID) {
						
				var windowFeatures = "scrollbars=yes,width=500px,height=400px ,status=no,resizable=yes, screenX=200, screenY=200, top=200, left=200";
				if (ModuleID != '' || InvestmentID != '')
				{
				    var MyDialog = window.open('/aspx/admin/Stats.aspx?ModuleID='+ModuleID+'&InvestmentID='+InvestmentID, "CustomDialog", windowFeatures);
				}
				else
				{
    				var MyDialog = window.open('/aspx/admin/Stats.aspx?url='+url, "CustomDialog", windowFeatures);
				}
				MyDialog.focus();
            }