/*

   FILENAME     : common.js

   DESCRIPTION  : Javascript library for BPI Portal.
	 
   AUTHOR       : Lee Osborne
	
	FUNCTIONS    : confirmDialog( message )
	               centerPopupWindow(url, windowName, inWidth, inHeight, features)
                  maxPopupWindow(url, windowName, features)
                  setLayerText(objName,x,newText)					 
	

   LAST UPDATED: 	18/06/2001

   COMPATABILITY : Internet Explorer 4 and Above.
	
   COPYRIGHT     : This script is copyright Integrated System Technologies 2001 
	                and beyond.

*/


//------------------------------------------------------------------------------




function confirmDialog(message) {
/*
   Function : Display a dialog box and make user select 'OK' or 'Cancel'.
	  
   Returns  : True on 'OK'.
              False on 'Cancel'.
	  
   Author   : Lee Osborne, 29/03/2001.
		  
*/
	  
   if ( confirm(message) ) {

      return true;
   }   
   else {
	
      return false;
   }
   
}


//------------------------------------------------------------------------------


function centerPopupWindow(url, windowName, inWidth, inHeight, features) {
/*  
   Description: Center a popup window within the current user's screen size. ie. 800 x 600.

    Parameters: url        -> URL of the document to be popped up.
                windowName -> The internal name used by the DOM to reference the window.
                inWidth    -> The width of the new window.
                inHeight   -> The height of the new window.
                features   -> The window features. eg. 'scrollbars=no, resizeable', etc.

         Notes:
			       The windows 'Start Menu' obscures the window size.

			       Add 26 pixels on to the width to account for the right-hand
					  scrollbar.
			 
        Author: Lee Osborne, 29/03/2001.
*/


   var winLeft = (screen.width  - inWidth ) / 2;
   var winTop  = (screen.height - inHeight) / 2;
   var properties = 'height=' + inHeight +',width=' + inWidth + ',top=' + winTop +',left=' + winLeft + ',' + features;
   var windowObject = window.open( url, windowName, properties )
   if (parseInt(navigator.appVersion) >= 4) { windowObject.window.focus(); }
}


//------------------------------------------------------------------------------


function maxPopupWindow(url, windowName, features) {
/*
   Description: Maximise a popup window to the current user's screen size. ie. 800 x 600.

   Parameters : url        -> URL of the document to be popped up.
                windowName -> The internal name used by the DOM to reference the window.
                features   -> The window features. eg. 'scrollbars=no, resizeable', etc.

   Author     : Lee Osborne, 29/03/2001.
*/

   properties  = 'height='+ (screen.availheight - 30) +',width='+ (screen.availwidth - 11) +',top=0,left=0,scrollbars=no,resizable';
   var windowObject = window.open( url, windowName, properties )
   if (parseInt(navigator.appVersion) >= 4) { windowObject.window.focus(); }
}


//------------------------------------------------------------------------------


function setLayerText(objName,x,newText) {
/*
   Description: Set the text of the specified layer name.                                

   Parameters : objName    -> The name of the layer.                   
                x          ->                                                           
                newText    -> The text to display in the layer.                        

   Author     : Lee Osborne, 29/03/2001.
*/

   function MM_findObj(n, d) {											
	
      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 && document.getElementById) x=document.getElementById(n); return x;

   } // MM_findObj

   // Find layer object
	
   if ((obj=MM_findObj(objName))!=null) with (obj) {
      
      // Netscape is different
		
		if (navigator.appName=='Netscape') {

		   document.write(unescape(newText)); document.close();
		}
      else {

		   innerHTML = unescape(newText);
		}
   }

} // setLayerText



//------------------------------------------------------------------------------


function generateUsername( fullname_in ) {
/*

   Generate the new user's username, derived from the 
    user's full name.

*/

   if( fullname_in.length!=0 ) {
	
      while( fullname_in.length>0 && fullname_in.charAt( 0 )==' ' ) fullname_in=fullname_in.substring( 1 );
	   while( fullname_in.length>0 && fullname_in.charAt( fullname_in.length-1 )==' ' ) fullname_in=fullname_in.substring( 0, fullname_in.length-1 );

      // find user name
      //
   	var commaIndex=fullname_in.lastIndexOf( ',' );
	   var spaceIndex=fullname_in.lastIndexOf( ' ' );
      var new_username = "";

 	   if( commaIndex<0 && spaceIndex<0 ) {
		
         new_username=fullname_in;
      }
  	   else if( commaIndex<0 && spaceIndex>=0 ) {
		
         var first=fullname_in.charAt( 0 );
  	      if( !( ( first>='a' && first<='z' ) || ( first>='A' && first<='Z' ) ) ) first='';
         new_username=first+fullname_in.substring( spaceIndex+1 );
      }
       else if( commaIndex>=0 && spaceIndex<0 ) {

   	   var first=fullname_in.charAt( commaIndex+1 );
   	   if( !( ( first>='a' && first<='z' ) || ( first>='A' && first<='Z' ) ) ) first='';
	      new_username=first+fullname_in.substring( 0, commaIndex );
     	}
  	   else {
			
   	   var minIndex=( commaIndex<spaceIndex )?commaIndex:spaceIndex;
	      var maxIndex=( commaIndex>spaceIndex )?commaIndex:spaceIndex;
	      var first=fullname_in.charAt( maxIndex+1 );
         if( !( ( first>='a' && first<='z' ) || ( first>='A' && first<='Z' ) ) ) first='';
         new_username=first+fullname_in.substring( 0, minIndex );
      }			
		
	var u      = new_username.toLowerCase();
   var result = u.substring( 0, 12 );

   return result;

   }
	
   else {
	
      // No username entered.
      return "";
   }

} // function generateUsername


//------------------------------------------------------------------------------


function clearFormFields() {      

/*----------------------------------------------------------------------------*/
/* Function    : clearFormFields                                              */
/* Language    : JavaScript                                                   */
/* Author      : Lee Osborne                                                  */
/* Description : Clears all form fields.in a form have a non-blank value      */
/*----------------------------------------------------------------------------*/

   var i = 0;
   var numFields = document.forms[0].elements.length;

   while (i < numFields) {

      document.forms[0].elements[i].value = '';
     i++;
   }

} // function clearFormFields.






<!--
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 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];}}
}

//-->
