var password1="123456"; //from Hotline
var password2="abcdef"; //from shops 
var password_hotline = "hotline";
var password_frontline = "frontline";
var caseDetailMaxChars=4000; // case detail max allowed chars
var max_no_of_fields=2; // max number of fields for phone, fax and email
var max_no_of_files=3; // max number of upload files
var max_no_of_fax=1; //max number of fields for fax 
var contextRoot = "";
var errmsg="Upload files cannot exceed 2M or due to error reading or saving file, your request cannot be proceed!";


//********************************************************************************************
String.prototype.trim = function () {
	var reExtraSpace = /^\s+(.*?)\s+$/;
	return this.replace(reExtraSpace, "$1");
};

function openCalendar(id,name) {
    window.open(contextRoot + "/eng/misc/complain/jsp/calendar.jsp?field=" + id + "&name=" + name, "calendar", "width=220,height=220,status=yes","false");
}

var whitespace = "\t\n\r";


//JC
	var BrowserDetect = {
		init: function () {
			this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
			this.version = this.searchVersion(navigator.userAgent)
				|| this.searchVersion(navigator.appVersion)
				|| "an unknown version";
			this.OS = this.searchString(this.dataOS) || "an unknown OS";
		},
		searchString: function (data) {
			for (var i=0;i<data.length;i++)	{
				var dataString = data[i].string;
				var dataProp = data[i].prop;
				this.versionSearchString = data[i].versionSearch || data[i].identity;
				if (dataString) {
					if (dataString.indexOf(data[i].subString) != -1)
						return data[i].identity;
				}
				else if (dataProp)
					return data[i].identity;
			}
		},
		searchVersion: function (dataString) {
			var index = dataString.indexOf(this.versionSearchString);
			if (index == -1) return;
			return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
		},
		dataBrowser: [
			{ 	string: navigator.userAgent,
				subString: "OmniWeb",
				versionSearch: "OmniWeb/",
				identity: "OmniWeb"
			},
			{
				string: navigator.vendor,
				subString: "Apple",
				identity: "Safari"
			},
			{
				prop: window.opera,
				identity: "Opera"
			},
			{
				string: navigator.vendor,
				subString: "iCab",
				identity: "iCab"
			},
			{
				string: navigator.vendor,
				subString: "KDE",
				identity: "Konqueror"
			},
			{
				string: navigator.userAgent,
				subString: "Firefox",
				identity: "Firefox"
			},
			{
				string: navigator.vendor,
				subString: "Camino",
				identity: "Camino"
			},
			{		// for newer Netscapes (6+)
				string: navigator.userAgent,
				subString: "Netscape",
				identity: "Netscape"
			},
			{
				string: navigator.userAgent,
				subString: "MSIE",
				identity: "Explorer",
				versionSearch: "MSIE"
			},
			{
				string: navigator.userAgent,
				subString: "Gecko",
				identity: "Mozilla",
				versionSearch: "rv"
			},
			{ 		// for older Netscapes (4-)
				string: navigator.userAgent,
				subString: "Mozilla",
				identity: "Netscape",
				versionSearch: "Mozilla"
			}
		],
		dataOS : [
			{
				string: navigator.platform,
				subString: "Win",
				identity: "Windows"
			},
			{
				string: navigator.platform,
				subString: "Mac",
				identity: "Mac"
			},
			{
				string: navigator.platform,
				subString: "Linux",
				identity: "Linux"
			}
		]
	
	};
	BrowserDetect.init();
	
	//JC>


// Check whether string s is empty.
function isEmpty(s)
{	
	if (s != null && s.length > 0) 
	{
		s = s.trim();
	
	}
	s2='  '+s;
	s2=s2.trim();
	//alert("["+s+"]")
	//alert("["+s.trim()+"]")

	return ((s2 == null) || (s2.length == 0))
}

// Returns true if string s is empty or 
// whitespace characters only.
function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}


// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{	
	//if (isEmpty(s)) 
    //   if (isEmail.arguments.length == 1) return defaultEmptyOK;
    //   else return (isEmail.arguments[1] == true);

    // is s whitespace?
    if (isWhitespace(s)) return false;
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++;
    }
    if ((i >= sLength) || (s.charAt(i) != "@")) {
      alert("Your email addrss must have '@' sign.");
    	return false;
    }
    else i += 2;
    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) {
    	alert("'" + s + "' is not a valid email address.  Please enter again.");
    	return false;
    }
    else return true;
}


// Returns true if character c is a digit 
// (0 .. 9).
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}



// isInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters in string s are numbers.
//
// Accepts non-signed integers only. Does not accept floating 
// point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// By default, returns defaultEmptyOK if s is empty.
// There is an optional second argument called emptyOK.
// emptyOK is used to override for a single function call
//      the default behavior which is specified globally by
//      defaultEmptyOK.
// If emptyOK is false (or any value other than true), 
//      the function will return false if s is empty.
// If emptyOK is true, the function will return true if s is empty.
//
// EXAMPLE FUNCTION CALL:     RESULT:
// isInteger ("5")            true 
// isInteger ("")             defaultEmptyOK
// isInteger ("-5")           false
// isInteger ("", true)       true
// isInteger ("", false)      false
// isInteger ("5", false)     true
function isInteger (s)
{   var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);

    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    // All characters are numbers.
    return true;
}

// isSignedInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if all characters are numbers; 
// first character is allowed to be + or - as well.
//
// Does not accept floating point, exponential notation, etc.
//
// We don't use parseInt because that would accept a string
// with trailing non-numeric characters.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
//
// EXAMPLE FUNCTION CALL:          RESULT:
// isSignedInteger ("5")           true 
// isSignedInteger ("")            defaultEmptyOK
// isSignedInteger ("-5")          true
// isSignedInteger ("+5")          true
// isSignedInteger ("", false)     false
// isSignedInteger ("", true)      true
function isSignedInteger (s)
{   if (isEmpty(s)) 
       if (isSignedInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isSignedInteger.arguments[1] == true);

    else {
        var startPos = 0;
        var secondArg = defaultEmptyOK;

        if (isSignedInteger.arguments.length > 1)
            secondArg = isSignedInteger.arguments[1];

        // skip leading + or -
        if ( (s.charAt(0) == "-") || (s.charAt(0) == "+") )
           startPos = 1;    
        return (isInteger(s.substring(startPos, s.length), secondArg))
    }
}



// isNonnegativeInteger (STRING s [, BOOLEAN emptyOK])
// 
// Returns true if string s is an integer >= 0.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isNonnegativeInteger (s)
{   var secondArg = defaultEmptyOK;

    if (isNonnegativeInteger.arguments.length > 1)
        secondArg = isNonnegativeInteger.arguments[1];

    // The next line is a bit byzantine.  What it means is:
    // a) s must be a signed integer, AND
    // b) one of the following must be true:
    //    i)  s is empty and we are supposed to return true for
    //        empty strings
    //    ii) this is a number >= 0

    return (isSignedInteger(s, secondArg)
         && ( (isEmpty(s) && secondArg)  || (parseInt (s) >= 0) ) );
}


// isYear (STRING s [, BOOLEAN emptyOK])
// 
// isYear returns true if string s is a valid 
// Year number.  Must be 2 or 4 digits only.
// 
// For Year 2000 compliance, you are advised
// to use 4-digit year numbers everywhere.
//
// And yes, this function is not Year 10000 compliant, but 
// because I am giving you 8003 years of advance notice,
// I don't feel very guilty about this ...
//
// For B.C. compliance, write your own function. ;->
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isYear (s)
{   if (isEmpty(s)) 
       if (isYear.arguments.length == 1) return defaultEmptyOK;
       else return (isYear.arguments[1] == true);
    if (!isNonnegativeInteger(s)) return false;
    return ((s.length == 2) || (s.length == 4));
}

// isMonth (STRING s [, BOOLEAN emptyOK])
// 
// isMonth returns true if string s is a valid 
// month number between 1 and 12.
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isMonth (s)
{   if (isEmpty(s)) 
       if (isMonth.arguments.length == 1) return defaultEmptyOK;
       else return (isMonth.arguments[1] == true);
    return isIntegerInRange (s, 1, 12);
}

// isIntegerInRange (STRING s, INTEGER a, INTEGER b [, BOOLEAN emptyOK])
// 
// isIntegerInRange returns true if string s is an integer 
// within the range of integer arguments a and b, inclusive.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.


function isIntegerInRange (s, a, b)
{   if (isEmpty(s)) 
       if (isIntegerInRange.arguments.length == 1) return defaultEmptyOK;
       else return (isIntegerInRange.arguments[1] == true);

    // Catch non-integer strings to avoid creating a NaN below,
    // which isn't available on JavaScript 1.0 for Windows.
    if (!isInteger(s, false)) return false;

    // Now, explicitly change the type to integer via parseInt
    // so that the comparison code below will work both on 
    // JavaScript 1.2 (which typechecks in equality comparisons)
    // and JavaScript 1.1 and before (which doesn't).
    var num = parseInt (s);
    return ((num >= a) && (num <= b));
}



// isDay (STRING s [, BOOLEAN emptyOK])
// 
// isDay returns true if string s is a valid 
// day number between 1 and 31.
// 
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isDay (s)
{   if (isEmpty(s)) 
       if (isDay.arguments.length == 1) return defaultEmptyOK;
       else return (isDay.arguments[1] == true);   
    return isIntegerInRange (s, 1, 31);
}

function makeArray(n) {
//*** BUG: If I put this line in, I get two error messages:
//(1) Window.length can't be set by assignment
//(2) daysInMonth has no property indexed by 4
//If I leave it out, the code works fine.
//   this.length = n;
   for (var i = 1; i <= n; i++) {
      this[i] = 0
   } 
   return this
}


var daysInMonth = makeArray(12);
daysInMonth[1] = 31;
daysInMonth[2] = 29;   // must programmatically check this
daysInMonth[3] = 31;
daysInMonth[4] = 30;
daysInMonth[5] = 31;
daysInMonth[6] = 30;
daysInMonth[7] = 31;
daysInMonth[8] = 31;
daysInMonth[9] = 30;
daysInMonth[10] = 31;
daysInMonth[11] = 30;
daysInMonth[12] = 31;


// Notify user that contents of field theField are invalid.
// String s describes expected contents of theField.value.
// Put select theField, pu focus in it, and return false.

// Date Validation
// *******************************************************************************************
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isIntegerS(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31;
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30};
		if (i==2) {this[i] = 29};
   } 
   return this
}

function isDate(dtStr)
{
	//var dtStr = datecontrol.value;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	var strDay=dtStr.substring(0,pos1);
	strYr=strYear;
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1);
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1);
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1);
	}
	month=parseInt(strMonth);
	day=parseInt(strDay);
	year=parseInt(strYr);
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : DD/MM/YYYY");
		return false;
	}
	else if (strMonth.length<1 || month<1 || month>12){
	  alert(month);
		alert("Please enter a valid month");
		return false;
	}
	else if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day");
		return false;
	}
	else if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear);
		return false;
	}
	else if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isIntegerS(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date");
		return false;
	}
return true;
}

//********************************************************************************************
//JD Change the values
	//var rowno1=1;
	//var rowno2=1;
	//var rowno3=1;
	//var rowno4=1;
	
	var rowno1=1; // phone number 
	var rowno2=1; //Email
	var rowno3=1;//fax
	var rowno4=4;//attachmenyt
//JD Change the values

// function to add more fields for input. Fields include
// 1.phone number; 2.email; 3.fax number 4.attachment
function add_field(d) {
	var disp;
	if (d==1)
	{
		disp = document.getElementById("phone_number");
		if (rowno1 < max_no_of_fields){
			rowno1 += 1;
			var row = disp.insertRow();
			var cell = row.insertCell();
			cell.innerHTML = " ";
			var cell = row.insertCell();
			cell.innerHTML = "(&nbsp;<input type=\"text\" name=\"phone_countrycode_" + rowno1 + "\" size=\"4\"/>&nbsp;) - <input type=\"text\" name=\"phone_number_"  + rowno1 + "\" size=\"15\" value=\"\"/>";
		}
	}
	else if(d==2)
	{
		disp = document.getElementById("email");
		if (rowno2 < max_no_of_fields){
			rowno2 += 1;
			var row = disp.insertRow();
			var cell = row.insertCell();
			cell.innerHTML = " ";
			var cell = row.insertCell();
			cell.innerHTML = "<input type=\"text\" id=\"email_" + rowno2 + "\" name=\"email_" + rowno2 + "\" size=\"20\" style=\"width=206px\"/>";
		}
	}
	else if(d==3)
	{
		disp = document.getElementById("fax_number");
		if (rowno3 < max_no_of_fax){
			rowno3 += 1;
			var row = disp.insertRow();
			var cell = row.insertCell();
			cell.innerHTML = " ";
			var cell = row.insertCell();
			cell.innerHTML = "(&nbsp;<input type=\"text\" name=\"fax_countrycode_" + rowno3  + "\" size=\"4\"/>&nbsp;) - <input type=\"text\" name=\"fax_number_"  + rowno3 + "\" size=\"15\"/>";
		}
	}
	else if(d==4)
	{
		disp = document.getElementById("attachment");
		if (rowno4 < max_no_of_fields){
			rowno4 += 1;
			var row = disp.insertRow();
			var cell = row.insertCell();
			cell.innerHTML = " ";
			var cell = row.insertCell();
			cell.innerHTML = '<input size="30" type="file" id="attachment_file_' + rowno4  + '" name="attachment_file_' + rowno4 + '">';
		}
	}
}

function AfterConfirmPwdJspSubmitForFirefox()
{
//JC4-	alert('Start AfterConfirmPwdJspSubmitForFirefox')
//JC4-	alert('document.getElementById("secondpwd").value' + document.getElementById("secondpwd").value)
//JC4-	alert('document.getElementById("pwd_hotline1").value' + document.getElementById("pwd_hotline1").value)
//JC4-	alert('pwd_frontline2' + document.getElementById("pwd_frontline2").value)
//JC4-	alert('2')
//	var pass = f.value; 
	var disp1 = document.getElementById("office_use");
	var disp2 = document.getElementById("sourceOfInfo");
	var disp3 = document.getElementById("officeUseDisplay");
	var disp4 = document.getElementById("hotline");
	var disp5 = document.getElementById("hktbCounter");
	var disp6 = document.getElementById("hktb");
	var disp7 = document.getElementById("hotlineRefNo");
	var lineflag = document.getElementById("lineFlag");
	
	if(document.getElementById("secondpwd").value==document.getElementById("pwd_hotline1").value)
	{
		document.getElementById("lineFlag").value="hot"
		disp1.style.display = "block";
		disp2.value="HOT";
		if(IdExist("hotline")) //JD-
			disp4.style.display = "inline";
		disp3.value="true";
	//JC4-	if(IdExist("hktbcounter")) //JD-
	//JC4-		disp5.style.display = "none";
		if(IdExist("hktb")) //JD-
			disp6.style.display = "none";
		if(IdExist("hotline_ref_num")) //JD-
			disp7.style.display = "block";
	}
	
	if (document.getElementById("secondpwd").value== document.getElementById("pwd_frontline2").value){
		lineflag.value = "front";
	//	lineFlag.value = "front";
		if(IdExist("office_use")) //JD-
		disp1.style.display = "block";
//		alert('JDRemoveLater: 3.3' )
		disp2.value="CSC";
		disp4.style.display = "none";
		sourceOfInfo = "CSC";
//		alert('JDRemoveLater: 3.3' )
		disp3.value="true";
//JC4-		if(IdExist("hktb_counter")) //JD-
//JC4-		disp5.style.display = "block";
		disp6.style.display = "block";
//		alert('JDRemoveLater: 3.4' )
		if(IdExist("hotline_ref_num")) //JD-
		disp7.style.display = "none";
		
//			alert('JDRemoveLater: 3.2' )
		
		}
//JC4-		alert('End AfterConfirmPwdJspSubmitForFirefox')
	
}


function show_officeuser(f,password_hotline,password_frontline){
//JC4-	alert('SO:1' + f.value)
	//	alert('show_officeuser(' + f.value + "," + password_hotline + "," + password_frontline + ")")
	var pass = f.value;
	var disp1 = document.getElementById("office_use");
	var disp2 = document.getElementById("sourceOfInfo");
	var disp3 = document.getElementById("officeUseDisplay");
	var disp4 = document.getElementById("hotline");
	var disp5 = document.getElementById("hktbCounter");
	var disp6 = document.getElementById("hktb");
	var disp7 = document.getElementById("hotlineRefNo");
	var lineflag = document.getElementById("lineFlag");
//JC4-	alert('SO:2')
	var lineFlag = document.getElementById("lineFlag");
	
	var systemStatus = document.getElementById("fm_ty_web");
	if (pass.substring(0,6)==password1){
//		alert('JDRemoveLater: 1.1' + password1 )
//JC4-		alert('SO:3')
		if (true){//systemStatus.value == "WEB"
			if (!popupNewPasswordWindow(password_hotline)) {
//JC4-				alert('SO:End')
				return false;
			}else{
//JC4-				alert('SO:4 not End')
				lineflag.value = "hot";
			}
//JC4-			alert('SO:5 not End')
//		alert('JDRemoveLater: 1.2' )
		lineFlag.value = "hot";
//		alert('JDRemoveLater: 1.2.1' )
		//JDRemoveLater
		if(disp1==null)
		{
			alert('JD:Yeah catch the bug');
		}
		else
		{
		//	alert(''+ disp1.value)
		}
		//JDRemoveLater>
		disp1.style.display = "block";
//		alert('JDRemoveLater: 1.2.2' )
		disp2.value="HOT";
//		alert('JDRemoveLater: 1.3' )
		if(IdExist("hotline")) //JD-
		disp4.style.display = "inline";
//		alert('JDRemoveLater: 1.3.1' )
//		if(IdExist("officeUseDisplay")) //JD-
		disp3.value="true";
//		alert('JDRemoveLater: 1.3.2' )
	//JC4-	if(IdExist("hktbcounter")) //JD-
	//JC4-	disp5.style.display = "none";
//		alert('JDRemoveLater: 1.4' )
		if(IdExist("hktb")) //JD-
		disp6.style.display = "none";
		if(IdExist("hotline_ref_num")) //JD-
		disp7.style.display = "block";
		}
	} else disp2.value="WEB";
//	alert('JDRemoveLater: 2.1' )
	if (pass.substring(0,6)==password2){
//JC4-		alert('SO:10')
//		alert('JDRemoveLater: 3.1' + password2)
		if (true) {systemStatus.value == "WEB"
			if (!popupNewPasswordWindow(password_frontline)) {
//JC4-				alert('SO:End')
				return false;
			}else{
				lineflag.value = "front";
			}
//			alert('JDRemoveLater: 3.2' )
		lineFlag.value = "front";
		if(IdExist("office_use")) //JD-
		disp1.style.display = "block";
//		alert('JDRemoveLater: 3.3' )
		disp2.value="CSC";
		disp4.style.display = "none";
		sourceOfInfo = "CSC";
//		alert('JDRemoveLater: 3.3' )
		disp3.value="true";
//JC4-		if(IdExist("hktb_counter")) //JD-
//JC4-		disp5.style.display = "block";
		disp6.style.display = "block";
//		alert('JDRemoveLater: 3.4' )
		if(IdExist("hotline_ref_num")) //JD-
		disp7.style.display = "none";
		}
	} else disp2.value="WEB";
//JC4-	alert('SO:End')
}

function popupNewPasswordWindow(pwd){
//JC4-	alert('popup win')
	var secondpwd;
    //To popup new window to confirm new password.
   // 	if( BrowserDetect.browser=="Explorer" )
   	if( BrowserDetect.browser=="Explorer" )
	{
		window.showModalDialog("/eng/misc/complain/jsp/confirmPassword.jsp", window, "dialogHeight:200px;  dialogWidth:400px; center: Yes;edge:sunken; status:0")
	}
	else
	{
		window.open("/eng/misc/complain/jsp/confirmPassword.jsp",window,'height=200,width=400,toolbar=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no ,modal=yes');

	}

	secondpwd = document.getElementById("secondpwd");
	if (pwd != secondpwd.value)
	{
		return false;
	}else
		return true;
}

function showDiv (d) {
	var disp1 = document.getElementById("D1");
	var disp2 = document.getElementById("D2");
	var disp3 = document.getElementById("D3");
	var disp4 = document.getElementById("D4");
	if (d==1)
	{
		disp1.style.display = "block";
		disp2.style.display = "none";
		disp3.style.display = "none";
		disp4.style.display = "none";
	}
	else if(d==2)
	{
		disp2.style.display = "block";
		disp1.style.display = "none";
		disp3.style.display = "none";
		disp4.style.display = "none";
	}
	else if(d==3)
	{
		disp3.style.display = "block";
		disp1.style.display = "none";
		disp2.style.display = "none";
		disp4.style.display = "none";
	}
	else if(d==4)
	{
		disp3.style.display = "none";
		disp1.style.display = "none";
		disp2.style.display = "none";
		disp4.style.display = "block";
	}
}

function setActButton (DivName) 
{
	switch (DivName) {
	case "D1":
		document.getElementById("btnOriginal").className = "ActiveTab";
	    document.getElementById("btnConsult").className = "DeactiveTab";
	    document.getElementById("btnHistory").className = "DeactiveTab";
	    document.getElementById("btnContactHist").className = "DeactiveTab";
	    isConsult = false;
	    break;
	case "D2":
	    document.getElementById("btnOriginal").className = "DeactiveTab";
	    document.getElementById("btnConsult").className = "ActiveTab";
	    document.getElementById("btnHistory").className = "DeactiveTab";
	    document.getElementById("btnContactHist").className = "DeactiveTab";
	    isConsult = true;
	    break;
	case "D3":
	    document.getElementById("btnHistory").className = "ActiveTab";
	    document.getElementById("btnConsult").className = "DeactiveTab";
	    document.getElementById("btnOriginal").className = "DeactiveTab";
	    document.getElementById("btnContactHist").className = "DeactiveTab";
	    isConsult = true;
	    break;
	case "D4":
	    document.getElementById("btnHistory").className = "DeactiveTab";
	    document.getElementById("btnConsult").className = "DeactiveTab";
	    document.getElementById("btnOriginal").className = "DeactiveTab";
	    document.getElementById("btnContactHist").className = "ActiveTab";
	    isConsult = true;
	    break;
	}
}  

	function IdExist(id)
	{
		try
		{
			if(document.getElementById(id)==NaN)
			{
			return false;
			}
			else
			{
				var abc=document.getElementById(id).value
				return true
			}
		}
		catch(e)
		{
			return false
		}
	}
function showHideField(field){
	if(IdExist("localAddressLabel1"))//JC
	var locallabel = document.getElementById("localAddressLabel1");
	if(IdExist("local_address_1"))//JC
	var localadd1 = document.getElementById("local_address_1");

	if(IdExist("home_address_1"))//JC
	var homeadd1 = document.getElementById("home_address_1");
	if(IdExist("homeAddressLabel1"))//JC
	var homelabel1 = document.getElementById("homeAddressLabel1");
	if(IdExist("localAddrTextTd"))//JC
	var localAddrTextTd = document.getElementById("localAddrTextTd");
	if(IdExist("HK"))//JC
	var hk = document.getElementById("HK");
	
	if (field.value=="Hong Kong Resident"){
		//JC4
		if(IdExist("HKA_Display"))
			document.getElementById("HKA_Display").style.display = "none";
		if(IdExist("HA_Display"))
			document.getElementById("HA_Display").style.display = "block";
		//JC4>
		/*JC4
		if(IdExist("local_address_1")) //JC
		document.getElementById("local_address_1").style.display = "none";
		if(IdExist("localAddressLabel1"))//JC
		document.getElementById("localAddressLabel1").style.display = "none";
		if(IdExist("localAddrTextTd"))//JC
		document.getElementById("localAddrTextTd").style.display = "none";
		*/
		
		/*
		if(IdExist("homeAddressLabel1"))//JC
		document.getElementById("home_address_1").style.display = "block";
		if(IdExist("homelabel1"))//JC
		document.getElementById("homeAddressLabel1").style.display = "block";
		*/
		
		if(IdExist("depart_date"))//JC
		document.getElementById("depart_date").value="" //JC
		if(IdExist("depart_date_TR"))//JC
		document.getElementById("depart_date_TR").style.display="none"//JC
		
		if(IdExist("HK"))//JC
		hk.selected="selected";
	}
	else {
		if(IdExist("depart_date_TR"))
		document.getElementById("depart_date_TR").style.display="block"//JC
		//JC4
		if(IdExist("HA_Display"))
			document.getElementById("HA_Display").style.display = "block";
		if(IdExist("HKA_Display"))
			document.getElementById("HKA_Display").style.display = "block";
		
		//JC4>
		
		/*JC4
		if(IdExist("local_address_1")) //JC
		localadd1.style.display = "block";
		if(IdExist("localAddressLabel1"))//JC
		locallabel.style.display = "block";
		if(IdExist("localAddrTextTd"))//JC
		localAddrTextTd.style.display = "block";
		if(IdExist("home_address_1"))//JC
		homeadd1.style.display = "block";
		if(IdExist("homeAddressLabel1"))//JC
		homelabel1.style.display = "block";
		*/
		if(IdExist("selectone"))//JC
		document.getElementById("selectone").selected="selected";
	}
}

function selectField(d){
	var i;
	var n;
	var disp1 = document.getElementById("specialTopic");
	var disp2 = document.getElementById("specialTopicSelected");
	if (d==1){ //add fields
		var fields = document.getElementById(disp1.value);
		var value = document.getElementById(disp1.value).value;
//		alert(disp1.value);
//		alert(fields);
		//n = disp2.innerHTML.indexOf("&nbsp;");
		n=1;
		if (n>0) {
			disp2.innerHTML = disp2.innerHTML + '<OPTION value="' + value + '">' + fields.name + '</option>';
//		alert(disp2.innerHTML);
		}
		else{
			disp2.innerHTML = disp2.innerHTML + '<OPTION value="' + value + '">' + fields + '</option>';
//			alert(disp2.innerHTML);
		}
	}
	else if(d==2){ //deselect fields
	}
}

function MoveOption(objSourceElement, objTargetElement)
    {
        var aryTempSourceOptions = new Array();
        var x = 0;
        var selected = false;
        for (var i = 0; i < objSourceElement.length; i++) {
        	if (objSourceElement.options[i].selected) {
        		selected = true;
            }
        }
        if (!selected)
        	alert("Please select at lease one facility.");

        //looping through source element to find selected options
        for (var i = 0; i < objSourceElement.length; i++) {
            if (objSourceElement.options[i].selected) {
                //need to move this option to target element
                var intTargetLen = objTargetElement.length++;
                objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
                objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
                objTargetElement.options[intTargetLen].selected = "selected";
            }
            else {
                //storing options that stay to recreate select element
                var objTempValues = new Object();
                objTempValues.text = objSourceElement.options[i].text;
                objTempValues.value = objSourceElement.options[i].value;
                aryTempSourceOptions[x] = objTempValues;
                x++;
            }
        }

        //resetting length of source
        objSourceElement.length = aryTempSourceOptions.length;
        //looping through temp array to recreate source select element
        for (var i = 0; i < aryTempSourceOptions.length; i++) {
            objSourceElement.options[i].text = aryTempSourceOptions[i].text;
            objSourceElement.options[i].value = aryTempSourceOptions[i].value;
            objSourceElement.options[i].selected = false;
        }

        sortSelect(objSourceElement);
        sortSelect(objTargetElement);
}
//JP
function LBSelectAll(objTargetElement)
{
	 for (var i = 0; i < objTargetElement.length; i++) {
			objTargetElement.options[i].selected = true;
        }
}

function SelectAllRightLB()
{
	 LBSelectAll(document.getElementById('spec_topic_cd_str'))
	 LBSelectAll(document.getElementById('request_action_cd_str'))
	 LBSelectAll(document.getElementById('special_remarks_se'))
}
//JP>    
    
function sortSelect(obj) {
	var o = new Array();
	if (!hasOptions(obj)) { return; }
	for (var i=0; i<obj.options.length; i++) {
		o[o.length] = 
		new Option( obj.options[i].text, 
					obj.options[i].value, 
					obj.options[i].defaultSelected, 
					obj.options[i].selected) ;
		}
	if (o.length==0) { return; }
	o = o.sort(
		function(a,b) {
			if ((a.text+"") < (b.text+"")) { return -1; }
			if ((a.text+"") > (b.text+"")) { return 1; }
			return 0;
			}
		);

	for (var i=0; i<o.length; i++) {
		obj.options[i] = new Option(o[i].text, o[i].value, o[i].defaultSelected, o[i].selected);
	}
}

function hasOptions(obj){
	if (obj.options.length > 0)
		return true;
	else
		return false;
}


function display_others(field,d){
	if (field.value=="others"){
		if (d==1)
		{
			var disp = document.getElementById("special_topic");
			if (disp.innerHTML.indexOf("<INPUT id=other_special_topic") <0)
				disp.innerHTML = disp.innerHTML + '<input type="text" id="other_special_topic" name="other_special_topic">';
		}
		else if(d==2)
		{
			var disp = document.getElementById("expectation");
			if (disp.innerHTML.indexOf("<INPUT id=other_expectation") <0)
				disp.innerHTML = disp.innerHTML + '<input type="text" id="other_expectation" name="other_expectation">';
		}
	}
	else {
		if (d==1)
		{
			var disp = document.getElementById("special_topic");
			var n = disp.innerHTML.indexOf("<INPUT id=other_special_topic");
			if (n>0)
			{
				disp.innerHTML = disp.innerHTML.substring(0,n);
			}
		}
		else if(d==2)
		{
			var disp = document.getElementById("expectation");
			var n = disp.innerHTML.indexOf("<INPUT id=other_expectation");
			if (n>0)
			{
				disp.innerHTML = disp.innerHTML.substring(0,n);
			}
		}
	}
}

function automark(tb,d){
	var str = document.getElementById("spec_rmk_other_cb");
	if (d==1)// onfocus
		str.checked=true;
	else if (d==2){//onblur
	//alert(tb.value);
		if (isEmpty(tb.value)) 
			str.checked=false;
		else
			str.checked=true;
	}
}

function init(){
	//var country = GetCookie('HKTA_country');
	var country = "tc";
    if (country == "sc"){
    }
        
 
    if (country == "tc"){
    	//document.getElementById("nameLabel").innerHTML = "????????? : ";
    	//document.getElementById("localAddressLabel1").innerHTML = "??????????????? : ";
    }
 
    if (country == "jpn"){
    }
 
    else{
    }
} 

