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) { //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 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 setChange(){
    document.forms[0].has_change.value = 'Y';
}
function openDisplay(url){
    wd = screen.width - 10;
    ht = screen.height - 75;
    window.open(url,'EAPPDisplay','width='+wd+',height='+ht+',top=0,left=0,menubar=no,toolbar=yes,scrollbars=yes');
}
// Check browser version
var isNav4 = false, isNav5 = false, isIE4 = false
var strSeperator = "/";
// If you are using any Java validation on the back side you will want to use the / because
// Java date validations do not recognize the dash as a valid date separator.
var vDateType = 3; // Global value for type of date format
//                1 = mm/dd/yyyy
//                2 = yyyy/dd/mm  (Unable to do date check at this time)
//                3 = dd/mm/yyyy
var vYearType = 4; //Set to 2 or 4 for number of digits in the year for Netscape
var vYearLength = 2; // Set to 4 if you want to force the user to enter 4 digits for the year before validating.
var err = 0; // Set the error code to a default of zero
if(navigator.appName == "Netscape") {
if (navigator.appVersion < "5") {
isNav4 = true;
isNav5 = false;
}
else
if (navigator.appVersion > "4") {
isNav4 = false;
isNav5 = true;
   }
}
else {
isIE4 = true;
}
function DateFormat(vDateName, vDateValue, e, dateCheck, dateType) {
    vDateType = dateType;
// vDateName = object name
// vDateValue = value in the field being checked
// e = event
// dateCheck
// True  = Verify that the vDateValue is a valid date
// False = Format values being entered into vDateValue only
// vDateType
// 1 = mm/dd/yyyy
// 2 = yyyy/mm/dd
// 3 = dd/mm/yyyy
//Enter a tilde sign for the first number and you can check the variable information.
    if (vDateValue == "~") {
    alert("AppVersion = "+navigator.appVersion+" \nNav. 4 Version = "+isNav4+" \nNav. 5 Version = "+isNav5+" \nIE Version = "+isIE4+" \nYear Type = "+vYearType+" \nDate Type = "+vDateType+" \nSeparator = "+strSeperator);
    vDateName.value = "";
    vDateName.focus();
    return true;
    }
    var whichCode = (window.Event) ? e.which : e.keyCode;
// Check to see if a seperator is already present.
// bypass the date if a seperator is present and the length greater than 8
    if (vDateValue.length > 8 && isNav4) {
        if ((vDateValue.indexOf("-") >= 1) || (vDateValue.indexOf("/") >= 1))
            return true;
    }
//Eliminate all the ASCII codes that are not valid
    var alphaCheck = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ/-";
    if (alphaCheck.indexOf(vDateValue) >= 1) {
        if (isNav4) {
        vDateName.value = "";
        vDateName.focus();
        vDateName.select();
        return false;
        }
        else {
            vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
            return false;
        }
    }
    if (whichCode == 8) //Ignore the Netscape value for backspace. IE has no value
        return false;
    else {
//Create numeric string values for 0123456789/
//The codes provided include both keyboard and keypad values
        var strCheck = '47,48,49,50,51,52,53,54,55,56,57,58,59,95,96,97,98,99,100,101,102,103,104,105';
        if (strCheck.indexOf(whichCode) != -1) {
            if (isNav4) {
                if (((vDateValue.length < 6 && dateCheck) || (vDateValue.length == 7 && dateCheck)) && (vDateValue.length >=1)) {
                    //alert("Invalid Date\nPlease Re-Enter");
                    alert("Please enter the Date in MM/DD/YYYY");
                    vDateName.value = "";
                    vDateName.focus();
                    vDateName.select();
                    return false;
                }
                if (vDateValue.length == 6 && dateCheck) {
                    var mDay = vDateName.value.substr(2,2);
                    var mMonth = vDateName.value.substr(0,2);
                    var mYear = vDateName.value.substr(4,4)
//Turn a two digit year into a 4 digit year
                    if (mYear.length == 2 && vYearType == 4) {
                        var mToday = new Date();
//If the year is greater than 30 years from now use 19, otherwise use 20
                        var checkYear = mToday.getFullYear() + 30;
                        var mCheckYear = '20' + mYear;
                        if (mCheckYear >= checkYear)
                            mYear = '19' + mYear;
                        else
                            mYear = '20' + mYear;
                    }
                    var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
                    if (!dateValid(vDateValueCheck)) {
                        //alert("Invalid Date\nPlease Re-Enter");
                        alert("Please enter the Date in MM/DD/YYYY");
                        vDateName.value = "";
                        vDateName.focus();
                        vDateName.select();
                        return false;
                    }
                    return true;
                }
                else {
// Reformat the date for validation and set date type to a 1
                    if (vDateValue.length >= 8  && dateCheck) {
                        if (vDateType == 1) // mmddyyyy
                        {
                            var mDay = vDateName.value.substr(2,2);
                            var mMonth = vDateName.value.substr(0,2);
                            var mYear = vDateName.value.substr(4,4)
                            vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
                        }
                        if (vDateType == 2) // yyyymmdd
                        {
                            var mYear = vDateName.value.substr(0,4)
                            var mMonth = vDateName.value.substr(4,2);
                            var mDay = vDateName.value.substr(6,2);
                            vDateName.value = mYear+strSeperator+mMonth+strSeperator+mDay;
                        }
                        if (vDateType == 3) // ddmmyyyy
                        {
                            var mMonth = vDateName.value.substr(2,2);
                            var mDay = vDateName.value.substr(0,2);
                            var mYear = vDateName.value.substr(4,4)
                            vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
                        }
//Create a temporary variable for storing the DateType and change
//the DateType to a 1 for validation.
                        var vDateTypeTemp = vDateType;
                        vDateType = 1;
                        var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
                        if (!dateValid(vDateValueCheck)) {
                            alert("Invalid Date\nPlease Re-Enter");
                            vDateType = vDateTypeTemp;
                            //vDateName.value = "";
                            vDateName.focus();
                            vDateName.select();
                            return false;
                        }
                        vDateType = vDateTypeTemp;
                        return true;
                    }
                    else {
                        if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
                            alert("Invalid Date\nPlease Re-Enter");
                            vDateName.value = "";
                            vDateName.focus();
                            vDateName.select();
                            return false;
                        }
                   }
              }
            }
            else {
// Non isNav Check
                if (((vDateValue.length < 8 && dateCheck) || (vDateValue.length == 9 && dateCheck)) && (vDateValue.length >=1)) {
                    alert("Invalid Date\nPlease Re-Enter");
                    vDateName.value = "";
                    vDateName.focus();
                    return true;
                }
// Reformat date to format that can be validated. mm/dd/yyyy
                if (vDateValue.length >= 8 && dateCheck) {
// Additional date formats can be entered here and parsed out to
// a valid date format that the validation routine will recognize.
                    if (vDateType == 1) // mm/dd/yyyy
                    {
                        var mMonth = vDateName.value.substr(0,2);
                        var mDay = vDateName.value.substr(3,2);
                        var mYear = vDateName.value.substr(6,4)
                    }
                    if (vDateType == 2) // yyyy/mm/dd
                    {
                        var mYear = vDateName.value.substr(0,4)
                        var mMonth = vDateName.value.substr(5,2);
                        var mDay = vDateName.value.substr(8,2);
                    }
                    if (vDateType == 3) // dd/mm/yyyy
                    {
                        var mDay = vDateName.value.substr(0,2);
                        var mMonth = vDateName.value.substr(3,2);
                        var mYear = vDateName.value.substr(6,4)
                    }
                    if (vYearLength == 4) {
                        if (mYear.length < 4) {
                            alert("Invalid Date\nPlease Re-Enter");
                            vDateName.value = "";
                            vDateName.focus();
                            return true;
                        }
                    }
// Create temp. variable for storing the current vDateType
                    var vDateTypeTemp = vDateType;
// Change vDateType to a 1 for standard date format for validation
// Type will be changed back when validation is completed.
                    vDateType = 1;
// Store reformatted date to new variable for validation.
                    //var vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
                    var vDateValueCheck = mDay+strSeperator+mMonth+strSeperator+mYear;
                    if (mYear.length == 2 && vYearType == 4 && dateCheck) {
//Turn a two digit year into a 4 digit year
                        var mToday = new Date();
//If the year is greater than 30 years from now use 19, otherwise use 20
                        var checkYear = mToday.getFullYear() + 30;
                        var mCheckYear = '20' + mYear;
                        if (mCheckYear >= checkYear)
                            mYear = '19' + mYear;
                        else
                            mYear = '20' + mYear;
                        vDateValueCheck = mMonth+strSeperator+mDay+strSeperator+mYear;
// Store the new value back to the field.  This function will
// not work with date type of 2 since the year is entered first.
                        if (vDateTypeTemp == 1) // mm/dd/yyyy
                            vDateName.value = mMonth+strSeperator+mDay+strSeperator+mYear;
                        if (vDateTypeTemp == 3) // dd/mm/yyyy
                            vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
                    }
                    if (!dateValid(vDateValueCheck)) {
                        alert("Invalid Date\nPlease Re-Enter");
                        vDateType = vDateTypeTemp;
                        //vDateName.value = "";
                        vDateName.focus();
                        return true;
                    }
                    vDateType = vDateTypeTemp;
                    return true;
                }
                else {
                    if (vDateType == 1) {
                        if (vDateValue.length == 2) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                        if (vDateValue.length == 5) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                    }
                    if (vDateType == 2) {
                        if (vDateValue.length == 4) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                        if (vDateValue.length == 7) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                    }
                    if (vDateType == 3) {
                        if (vDateValue.length == 2) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                        if (vDateValue.length == 5) {
                            vDateName.value = vDateValue+strSeperator;
                        }
                    }
                    return true;
                }
            }
            vDateName.value = mDay+strSeperator+mMonth+strSeperator+mYear;
            if (vDateValue.length == 10&& dateCheck) {
                if (!isValidDate(vDateName)) {
// Un-comment the next line of code for debugging the isValidDate() function error messages
//alert(err);
                    //alert("Invalid Date\nPlease Re-Enter");
                    alert("Please enter the Date in MM/DD/YYYY");
                    vDateName.focus();
                    vDateName.select();
                }
            }
            return false;
        }
        else {
// If the value is not in the string return the string minus the last
// key entered.
            if (isNav4) {
                vDateName.value = "";
                vDateName.focus();
                vDateName.select();
                return false;
            }
            else
            {
                vDateName.value = vDateName.value.substr(0, (vDateValue.length-1));
                return false;
            }
        }
    }
}

//check valid date
function isValidDate(dateStr) {

// Checks for the following valid date formats:
// MM/DD/YY MM/DD/YYYY MM-DD-YY MM-DD-YYYY
//Also separates date into month, day, and year variables

	var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

	var matchArray = dateStr.match(datePat); // is the format ok?
	if (matchArray == null) {
		alert("Invalid Date\nPlease Re-Enter");
		
		return false;
	}
	month = matchArray[3]; // parse date into variables
	day = matchArray[1];
	year = matchArray[4];
	if (month < 1 || month > 12) { // check month range
		alert("Invalid Date\nPlease Re-Enter");
		return false;
	}
	if (day < 1 || day > 31) {
		alert("Invalid Date\nPlease Re-Enter");
		return false;
	}
	if ((month==4 || month==6 || month==9 || month==11) && day==31) {
		alert("Invalid Date\nPlease Re-Enter");
		return false
	}
	if (month == 2) { // check for february 29th
		var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		if (day>29 || (day==29 && !isleap)) {
			alert("Invalid Date\nPlease Re-Enter");
			return false;
		}
	}
	if (year < 1900) {
		alert("Invalid Date\nPlease Re-Enter");
		return false
	}
	return true; // date is valid
}

//check HK ID
function validateHKID(_id)
{
      var sum = 0,digit = 0;
		var temp='0';
		var temp2 = 0;
		var is_str = "123456789A0";
		temp = _id.charAt(0);
		if(_id.length==9&&temp == 'X'){
			return true;
		}else{
			if(_id.length != 8){
				return false;
			}
			if(temp=='A'){sum = 1 * 8;}
			else if (temp=='B'){sum = 2 * 8;}
			else if (temp=='C'){sum = 3 * 8;}
			else if (temp=='D'){sum = 4 * 8;}
			else if (temp=='E'){sum = 5 * 8;}
			else if (temp=='F'){sum = 6 * 8;}
			else if (temp=='G'){sum = 7 * 8;}
			else if (temp=='H'){sum = 8 * 8;}
			else if (temp=='I'){sum = 9 * 8;}
			else if (temp=='J'){sum = 10 * 8;}
			else if (temp=='K'){sum = 11 * 8;}
			else if (temp=='L'){sum = 12 * 8;}
			else if (temp=='M'){sum = 13 * 8;}
			else if (temp=='N'){sum = 14 * 8;}
			else if (temp=='O'){sum = 15 * 8;}
			else if (temp=='P'){sum = 16 * 8;}
			else if (temp=='Q'){sum = 17 * 8;}
			else if (temp=='R'){sum = 18 * 8;}
			else if (temp=='S'){sum = 19 * 8;}
			else if (temp=='T'){sum = 20 * 8;}
			else if (temp=='U'){sum = 21 * 8;}
			else if (temp=='V'){sum = 22 * 8;}
			else if (temp=='W'){sum = 23 * 8;}
			else if (temp=='X'){sum = 24 * 8;}
			else if (temp=='Y'){sum = 25 * 8;}
			else if (temp=='Z'){sum = 26 * 8;}
			else{return false;}
			var k = 8;
			var i;
			for(i=1;i < _id.length;i++){
				temp = _id.charAt(i);
				if(isNaN(temp) && i < 7){
					return false;
				}else{
					if(i < 7){
						sum = sum + (temp - '0') * (k - i);
					}else if(i == 7){
						if((11-(sum % 11)) != (is_str.indexOf(temp)+1)){
							return false;
						}
					}
				}
			}
			return true;
		}
}

function chkNewApp(pol_no,section_comp){
    if(pol_no == ''){
        alert('Please save the Policy first before goto next section');
        return false;
    }
    else
        return chkSectionComp(section_comp);
    //return true;
}
function chkSectionComp(x){
    if(!x){ //section not complete
        alert('Please complete the sections before this section first!');
        return false;
    }
    return true;
}
function goNextField(datepart,field,field_index,field_index2,form){
//datepart : D=day,M=month,Y=year
//field : field_name
// field_index : current field index
//field_index2 : use for range type
//form : document.form
    flag = false;
    with(form){
        if(datepart == 'D' && flag){
            if(field_index2 != ''){
                curr_field = eval(field+field_index+'_dd'+field_index2);
                next_field = eval(field+field_index+'_mm'+field_index2);
            }
            else{
                curr_field = eval(field+field_index+'_dd');
                next_field = eval(field+field_index+'_mm');
            }
            if(curr_field.value.length == 2){
                next_field.value = '';
                next_field.focus();
            }
        }
        else if(datepart == 'M' && flag){
            if(field_index2 != ''){
                curr_field = eval(field+field_index+'_mm'+field_index2);
                next_field = eval(field+field_index+'_yy'+field_index2);
            }
            else{
                curr_field = eval(field+field_index+'_mm');
                next_field = eval(field+field_index+'_yy');
            }
            if(curr_field.value.length == 2){
                next_field.value = '';
                next_field.focus();
            }
        }
    }
}

/* This function shows the confirm box *************************************/
var winConfirm_main = null;
function showConfirm_main(context_path, confirm_msg, button_value, button_action, page_loc)
{
	try
	{
	  var newwin = window.open( "", "winConfirm_main", "top=100000");
	  newwin.close();
	} catch (e111) { }

  var windowWidth = screen.width / 2;
  var windowHeight = screen.height / 4;
  var locX = ( screen.width - windowWidth ) / 2;
  var locY = ( screen.height - windowHeight ) / 2;

//  if(screen.Width > 1024)
//    locX = screen.width/2 + (screen.width/2-windowWidth)/2;

  var windowFeatures = "width=" + windowWidth
                     + ",height=" + windowHeight
                     + ",screenX=" + locX
                     + ",screenY=" + locY
                     + ",left=" + locX
                     + ",top=" + locY
                     + ",resizable=1"
                     + ",scrollbars=1";

  //alert("button_value.length=>"+button_value.length+"<");
  //var k = 0;
  //for (k=0; k < button_value.length; k++)
  //  alert("Choice "+button_choice[k]+" is "+button_value[k]+".");

  /* Close window first to make sure that our window has */
  /* the desired features */
  //alert("winConfirm=>"+winConfirm+"<");
  //if ( ( winConfirm != null ) && !winConfirm.closed )
  //{
  //  winConfirm.close();
  //}

  /* Open the new confirm */
  winConfirm_main = open( "", "winConfirm_main", windowFeatures );

  /* Write to the confirm box */
  var theHTML = '<HEAD><TITLE>Warning...</TITLE>'
    + '<meta http-equiv="Content-Type" content="text/html; charset=big5">'
    + '<link rel="stylesheet" href="'+context_path+'/web/css/style_global.css">'
    + '</HEAD>'
    + '<script language="javascript" src="'+context_path+'/web/jscript/js.js"></script>'
    + '<BODY background="'+context_path+'/web/image/common/content_back.gif">'
    + '<table align=center><tr><td align="center" class="note"><b>'
    + confirm_msg
    + '</b></td></tr>'
    + '<tr><td align="center"><br><FORM NAME="buttonForm">';

  var i=0;
  for (i=0; i < button_value.length; i++)
  {
    theHTML = theHTML
    + '<INPUT TYPE="button" VALUE="'+button_value[i]+'"'
    //+ ' ONCLICK="buttonClicked('+i+','+button_action+');self.close();">'
    + ' ONCLICK="';
    if (button_action[i]!=null && button_action[i]!='')
    {
        theHTML = theHTML + button_action[i];
    }
    //theHTML = theHTML + 'self.close();';
    theHTML = theHTML + '">'
    + '&nbsp;';
  }
  theHTML = theHTML + '</font></FORM></td></tr></table></BODY>';

  //alert(theHTML);
  winConfirm_main.document.clear();
  winConfirm_main.document.writeln( theHTML );
  winConfirm_main.focus();
  //20041221-REF001-PAT-S
  return winConfirm_main;
  //20041221-REF001-PAt-E
}
/* This function is called by the buttons on the confirm box ***************/


//function buttonClicked( buttonChoice, button_action )
//{
    /* Put in this function the result of the user's choice   */
    /* (in this case, write the user's choice in a textfield) */
//    var i=0;
//    for (i=0; i < button_action.length; i++)
//    {
//        if (buttonChoice == i)
//        {
//          if (button_action[0]!=null && button_action[0]!='')
//            button_action[0];
//        }
//    }
//}

// Function to auto-tab next field
// Arguments:
//   obj :  The input object (this)
//   event: Either 'up' or 'down' depending on the keypress event
//   len  : Max length of field - tab when input reaches this length
//   next_field: input object to get focus after this one
var nextfield_length=0;
function TabNext(obj,event,len,next_field) {
    if (event == "down")
    {
        nextfield_length=obj.value.length;
    }
    else if (event == "up")
    {
        if (obj.value.length != nextfield_length)
        {
            nextfield_length=obj.value.length;
            if (nextfield_length == len)
            {
                next_field.focus();
            }
        }
    }
}

function popupDisplay(url,height,width){
    if(screen.Width > 1024)
    {
        var wd=screen.Width/2+20;
        if(width > wd) {width = screen.Width - wd - 10;}
    } else {
        var wd=10;
        if(width > screen.Width) {width = screen.Width - 10;}
    }

    //alert("wd=>"+wd+"<....ht=>"+ht+"<");
    //alert("new: top=>"+top+"<....new: left=>"+left+"<");

            //window.opener='M';
    //var a =
    window.open(url,'','width='+width+',height='+height+',status=1,resizable=1,scrollbars=1,top=50,left='+wd);
    //a.location = url;
    //a.moveTo(screen.Width-wd-10,screen.Height-ht-75);
    //window.close();
}

function validateNum(value) {
    var temp;
    for (i=0; i<value.length; i++) {
        temp = value.substring(i,i+1);
        if ('0123456789'.indexOf(temp) < 0) {
            return false;
        }
    }
    return true;
}

String.prototype.Trim = function()
{
return this.replace(/(^\s*)|(\s*$)/g, "");
}

function replaceComma(value)
{
	//value = trim(value);
	if(value.indexOf(',')>=0)
	    value = replace(value,',','');
	//alert("|"+value+"|");
	return value;
}


function fillStockInput(stockInput, marketCode) {
  if (stockInput.match(/^\d+$/)) {
    var market = "." + marketCode;
    if (stockInput.length == 1) {
      stockInput = "0000" + stockInput;
      if (stockInput.indexOf(market) < 0) stockInput = stockInput + market;
    } else if (stockInput.length == 2) {
      stockInput = "000" + stockInput;
      if (stockInput.indexOf(market) < 0) stockInput = stockInput + market;
    } else if (stockInput.length == 3) {
      stockInput = "00" + stockInput;
      if (stockInput.indexOf(market) < 0) stockInput = stockInput + market;
    } else if (stockInput.length == 4) {
      stockInput = "0" + stockInput;
      if (stockInput.indexOf(market) < 0) stockInput = stockInput + market;
    }
  }

  return stockInput;
}

function replaceStr(value, oldChar, newChar)
{
    if(oldChar == '') return value;
	while (value.indexOf('-')>=0) {
	    value = value.replace(oldChar,newChar);
	}
	return value;
}
