/**********************************************************************************************
**
**  File name: functions.js
**  Creation date: 24th May, 2001
**  Last update: 7th January, 2002
**
**
**
**  Functions:
**
**    Image functions -
**      void MM_swapImgRestore()    // from Dreamweaver4
**      void MM_preloadImages(String, String, ......, String)    // from Dreamweaver4
**      void MM_preloadimagenew(String, String, ......, String)    // from Dreamweaver4
**      void MM_findObj()    // from Dreamweaver4
**      void MM_swapImage(String,null,String,)    // from Dreamweaver4
**
**    Layers functions -
**      void MM_showHideLayers()    // from Dreamweaver4
**      void showMenu(String)
**      void hideMenu(String)
**
**    String functions -
**      boolean isWhitespace(String)
**      boolean isAlphaNumeric(String)
**      boolean isAlphabets(String)
**      boolean isInteger(String)
**      boolean isFloat(String)
**      boolean isEmail(String)
**
**    Cookie functions - 
**      function setCookie(String, String, int)
**      function getCookie(String)
**
**    Other functions -
**      void loadTop(String, String)
**      void switchlang()
**      String getParameter()
**      boolean checkBrowser(int,int,int,int,int,int,int)
**      boolean orangeOnLoad()
**
****************************************************************************
** Modified History
** ================
** HKL  05/10/2001 add functions getSectionL1() and getSectionL2(),
**                 modify switchlang()
** Alex 24/10/2001 add function checkBrowser(int,int,int,int,int,int,int)
** Alex 07/01/2002 add function orangeOnLoad()
**********************************************************************************************/

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: MM_swapImgRestore
//  return value: void
//  parameter(s): void
//  description:
//    This function restores the images that was swapped by the function MM_swapImage
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
//        it is always used as counter part with function MM_swapImg()
///////////////////////////////////////////////////////////////////////////////////////////////
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 name: MM_preloadImages
//  return value: void
//  parameter(s): a list of strings of the full path of the image files that are to be preloaded
//  description:
//    This function restores the images that was swapped by the function MM_swapImage
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
///////////////////////////////////////////////////////////////////////////////////////////////
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 name: MM_preloadimagenew
//  return value: void
//  parameter(s): a list of strings of the full path of the image files that are to be preloaded
//  description:
//    This function restores the images that was swapped by the function MM_swapImage
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
///////////////////////////////////////////////////////////////////////////////////////////////
function MM_preloadimagenew() { //v3.0
  var d=document; if(d.imagenew){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadimagenew.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 name: MM_findObj
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
///////////////////////////////////////////////////////////////////////////////////////////////
function MM_findObj(n, d) { //v3.0
  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); return x;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: MM_swapImage
//  return value: void
//  parameter(s): String - image name
//                String - usually empty string ''
//                String - full path name of the image file to swap with
//  description:
//    This function swaps the image named as the 1st parameter with the image specified by the
//    3rd parameter
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
//        it is always used as counter part with function MM_swapImgRestore()
///////////////////////////////////////////////////////////////////////////////////////////////
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 name: MM_showHideLayers
//  return value: void
//  parameter(s): String - layer name
//                String - usually empty string ''
//                String - action, either 'hide' or 'show'
//  description:
//    This function hides or shows the layer named as the 1st parameter
//  Note: this function is copied from Macromedia Dreamweaver 4. P.s. reference DW4 for detail
//////////////////////////////////////////////////////////////////////////////////////////////
function MM_showHideLayers() { //v3.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v='hide')?'hidden':v; }
    obj.visibility=v; }
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: showMenu
//  return value: void
//  parameter(s): String - menu layer name
//  description:
//    This function shows the menu layer named as the 1st parameter
//  Note: this function create and accesses a variable menuOpened in document object
//        it is always used as counter part with function hideMenu(String)
///////////////////////////////////////////////////////////////////////////////////////////////
function showMenu(navName) {
  if( document.menuOpened!=null )
    hideMenu(navName);
  document.menuOpened = navName;
  MM_showHideLayers(navName,'','show');
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: hideMenu
//  return value: void
//  parameter(s): String - layer name
//                String - usually empty string ''
//                String - action, either 'hide' or 'show'
//  description:
//    This function hides the menu layer named as the 1st parameter
//  Note: this function accesses a variable menuOpened in document object
//        it is always used as counter part with function showMenu(String)
///////////////////////////////////////////////////////////////////////////////////////////////
function hideMenu() {
  navName = document.menuOpened;
  document.menuOpened=null;
  setTimeout("if(document.menuOpened==null || document.menuOpened!='"+navName+"') MM_showHideLayers('"+navName+"','','hide');",10);
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isWhitespace
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return false only if it reaches a character inside the string
//    that is not one of the following characters: space, tab, next line
///////////////////////////////////////////////////////////////////////////////////////////////
function isWhitespace(str)
{
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if( c!=' ' && c!='\n' && c!='\t' )
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isAlphaNumeric
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return true only if the string contains only numbers(0-9) and
//    alphabets(a-z, A-Z)
///////////////////////////////////////////////////////////////////////////////////////////////
function isAlhpaNumeric(str)
{
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if( !((c>='A' && c<='Z') || (c>='a' && c<='z') || (c>='0' && c<='9')) )
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isAlphabets
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return true only if the string contains alphabets(a-z, A-Z)
///////////////////////////////////////////////////////////////////////////////////////////////
function isAlphabets(str)
{
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if( !((c>='A' && c<='Z') || (c>='a' && c<='z')) )
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isInteger
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return true only if the string contains digit characters(0-9)
///////////////////////////////////////////////////////////////////////////////////////////////
function isInteger(str)
{
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if( !(c>='0' && c<='9') )
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isFloat
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return true only if the string contains digit characters(0-9) or point(.) once
///////////////////////////////////////////////////////////////////////////////////////////////
function isFloat(str)
{
  var intCount = 0;
  for(var i=0; i<str.length; i++) {
    var c = str.charAt(i);
    if( c=='.' )
      if( intCount > 0 )
        return false;
      else
        intCount++;
    else if( !(c>='0' && c<='9') )
      return false;
  }
  return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: isEmail
//  return value: boolean, i.e. true or flase
//  parameter(s): a String
//  description:
//    This function will return false only the string is not in the pattern
//    "string@string[.string]" where string here is composed of alphanumeric characters or underscore(_) only.
///////////////////////////////////////////////////////////////////////////////////////////////
function isEmail(strEmail)
{
  invalidChars ="/:,?#;";

  if (strEmail==null || isWhitespace(strEmail))
    return false;
  for (ai=0;ai<invalidChars.length;ai++) {
    badChar = invalidChars.charAt(ai);
    if (strEmail.indexOf(badChar,0) > -1)
      return false;
  }
  atPos = strEmail.indexOf("@",1);
  if (atPos ==-1)
    return false;
  if (strEmail.indexOf("@",atPos+1) > -1)
    return false;
  periodPos = strEmail.indexOf(".",atPos);
  if (periodPos == -1)
    return false;
  if (periodPos+3 > strEmail.length)
    return false;

  return true;
}


///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: setCookie
//  return value: none
//  parameter(s):
//    name - the name string of the cookie
//    value - the corresponding value string of the cookie with the name above
//    expire - the number of minutes after the current time for the cookie to expire
//  description:
//    This function will set the cookie of the page
///////////////////////////////////////////////////////////////////////////////////////////////
function setCookie(name, value, expMin)
{
  var expire = new Date();
  expire.setTime(expire.getTime()+expMin*60*1000);
  document.cookie = name + "=" + escape(value)   + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()));
}


///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: getCookie
//  return value: the corresponding value string of the cookie value specified by the name parameter
//  parameter(s):
//    name - the name string of the cookie
//  description:
//    This function will set the cookie of the page
///////////////////////////////////////////////////////////////////////////////////////////////
function getCookie(Name)
{
  var search = Name + "=" ;
  if (document.cookie.length > 0) { // if there are any cookies
    offset = document.cookie.indexOf(search);
    if (offset != -1) { // if cookie exists
      offset += search.length ;         // set index of beginning of value
      end = document.cookie.indexOf(";", offset);          // set index of end of cookie value
       if (end == -1)
         end = document.cookie.length;
       return unescape(document.cookie.substring(offset, end))
    }
  }
  return null;
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: loadTop
//  return value: none
//  parameter(s):
//    frame - the framem object at where top banner page is loaded
//    url - the url to be loaded at the frame
//  description:
//    This function is used to load the top frame with new url.
///////////////////////////////////////////////////////////////////////////////////////////////
function loadTop(frame, url)
{
  if( frame==null )
    frame = parent.frames["orangetop"];
  if( url==null )
    frame.location.reload();
  else
    frame.location.href = url;
}

///////////////////////////////////////////////////////////////////
//  function name: switchlang
//  return value: none
//  parameter(s): none
//  description:
//        This function is used to change the language of a page by 
//        replacing "/eng" to "/chi/" or //    vice versa.
//        HKL - take care of the case if switch need to be done by
//              pass language=?? variables
////////////////////////////////////////////////////////////////////
//function switchlang() {
//   return switchlang("");
//}
//function switchlang(tolan) {
// var path = top.location.href;
// if (path.indexOf("/eng/") >=0 || path.indexOf("/chi/") >=0) {
//     /* case: use chi and eng to switch langauge */
//     if( top.location.href.indexOf("/eng/")==-1 )
//         top.location.href = top.location.href.replace(/\/chi\//gi,"\/eng\/");
//     else
//         top.location.href = top.location.href.replace(/\/eng\//gi,"\/chi\/");
//  } else {
//     var nextLanguage=tolan
//     /* pass language parameter */
//     var pathname=window.location.pathname;
//     var end = pathname.indexOf('.');
//     var temp= pathname.substring(0,end);
//     var begin = temp.lastIndexOf('/')+1;
//     var filename=temp.substring(begin,end)+pathname.substring(end);
//     alert("language="+nextLanguage);
//     document.location.href="MFL040.jsp?language="+
//                            nextLanguage+"&filename="+filename;
//  }
//}
//
///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: popup
//
///////////////////////////////////////////////////////////////////////////////////////////////
// first time set the pop-up window does not exist
var popupWin = null;

function openWindow(url,name,widthpw,heightpw) {
var dimension = 'scrollbars,width=' + widthpw + ',height=' + heightpw + ',top=0,left=0,resizable=yes';
var chkNS = 'n';
  if (checkBrowser(0,1,0,0,0,0,0) && (url.indexOf('.html') < 0) && (url.indexOf('.htm') < 0) && (url.indexOf('.jsp') < 0)) chkNS = "y";

  // if pop-up window exists, kill it
  if (popupWin != null) {
	if (popupWin.closed) {
	  if (chkNS == 'y') {
             window.location.href = url; 
          } else {
             popupWin = window.open(url,name,dimension);
          }
	} else {
	  popupWin.close();
	}
  }

  if (chkNS == 'y') {
     window.location.href = url;
  } else {
     popupWin = window.open(url,name,dimension);
  }

  if (self.focus) {
    popupWin.focus();
  }

}

function clearUp() {
  if (popupWin != null) {
    popupWin.close();
  }
}

/********************************************************************/
/* HKL -- section for usage of getParameter(String)                 */
/********************************************************************/
function getParameter(a) {
    var st = this.location.search;
    if (st != null && st != "") {
        st = st.substring(1,st.length);
        var pp = st.split("&");
        for (ii=0; ii<pp.length; ii++) {
            if (pp[ii].indexOf(a) >= 0) {
               var bb = pp[ii].split("=");
               return bb[1];
            }
        }
    }
    return "";
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: checkBrowser
//  return value: boolean
//  parameter(s):
//            NSvers - the version of the Netscape(NS) browser to be checked against.
//            NSpass - a flag to indicate that NS pass the check
//            NSnoPass - a flag to indicate that NS version is ignored for the check
//            IEvers - the version of the Internet Explorer(IE) browser to be checked against.
//            IEpass - a flag to indicate that IE pass the check
//            IEnoPass - a flag to indicate that IE version is ignored for the check
//            OBpass - a flag indicate that browsers other than IE & Netscape pass the check
//  description:
//    This function check for the version of the browser which load the page
//  Note: This function is modified from Macromedia Dreamweaver 4. P.s. reference DW4 for detail;
//        For all the arguments that is a flag, 1 means 'on', otherwise, 'off'
///////////////////////////////////////////////////////////////////////////////////////////////
function checkBrowser(NSvers,NSpass,NSnoPass,IEvers,IEpass,IEnoPass,OBpass) { //v4.0
  var result=false, verStr=navigator.appVersion, app=navigator.appName, version = parseFloat(verStr);
  if (app.indexOf('Netscape') != -1) {
    if (version >= NSvers) {if (NSpass>0) result=(NSpass==1)?true:false;}
    else {if (NSnoPass>0) result=(NSnoPass==1)?true:false;}
  } else if (app.indexOf('Microsoft') != -1) {
    if (version >= IEvers || verStr.indexOf(IEvers) != -1)
     {if (IEpass>0) result=(IEpass==1)?true:false;}
    else {if (IEnoPass>0) result=(IEnoPass==1)?true:false;}
  } else if (OBpass>0) result=(OBpass==1)?true:false;

  return result;
}

// HKL - 22112001
function checkLogin() {
    var ii = document.switchform.logintype.value;
    if (ii == "Orange" || ii == "SVA") {
       return confirm("By Confirm, you will be logged out from your " + 
                      ii + " session"); 
    } else
       return true;
}

function popupSMSCharge(lang) {
  window.open('http://www.orangehk.com/common/jsp/popupCharge.jsp?lang='+lang, 'popupCharge', 'width=300,height=150,toolbar=no,menubar=no,scrollbars=no,resizable=yes,location=no,status=no');
}

///////////////////////////////////////////////////////////////////////////////////////////////
//  function name: orangeExecuteWorklist
//  return value: boolean
//  parameter(s):
//          aryStatments - an string array that contains Javascript statement to be executed.
//  description:
//    This function retrieve statements from a predefined array and exectue them when called
//  Note: This function is initailly created for the purpose about onLoad event.
///////////////////////////////////////////////////////////////////////////////////////////////
function orangeExecuteWorklist(worklist) {
  if( worklist==null || worklist[0]==null )
    return false;

  for(var i=0; i<worklist.length; i++)
    eval( worklist[i] );

  return true;
}

var onLoadWorklist = new Array();


///////////////////////////////////////////////////////////////////////////////
// function name:checksearchForm
///////////////////////////////////////////////////////////////////////////////
function checksearchForm(form) {
  form.qu.value=form.query.value;
  form.submit();
}

