var TAG_Option = "<option value=\"{value}\">{label}</option>";

function MM_displayStatusMsg(msgStr) { //v1.0
  status=msgStr;
  document.MM_returnValue = true;
}

function foldername(){
	 path = location.href;
	 len_path = path.length;
	 path2 = path.substring (7,len_path);
	 path2_id = path2.lastIndexOf("/");
	 path2 = path2.substring(0,path2_id);
	 path2_id = path2.lastIndexOf("/");
	 len_path = path2.length;
	 path2 = path2.substring (path2_id+1,len_path);
	 return path2;
}

function filename(){
	path = location.href;
	file_id = path.lastIndexOf("/");
	file_id2 = path.lastIndexOf(".");
	fname = path.substring(file_id+1,file_id2);
	return fname;
}

function get_prefix(){
	f_name = filename();
	output = f_name.substring(0,3);
	return output;
}

// modified on 29Sep2010
function JobPosition(_id,_label,_ref)
{
	this.id = _id;
	this.label = _label;
	this.refNo = _ref;
	this.refFrom = "HKHS";
}

function Option(_value,_label)
{
	this.value = _value;
	this.label = _label;
}

function getCurrentYear()
{
	var d = new Date();
	var yr = d.getFullYear();
	return yr;
}

function createOPTGrp(beg,end,defaultMesx)
{
	var insLen = end - beg +1;
	var insCount = beg;
	var optGrp = new Array();
		optGrp[0] = new Option("",defaultMesx)
	for (var i=1; i<=insLen; i++)
	{
		optGrp[i] = new Option(insCount,insCount);
		insCount++;
	}
	return optGrp;
}

function getOPTGrpHTML(yrGrp)
{
	var grpHTML = "";
	var len = yrGrp.length;
	for (var i=0; i<len; i++)
	{
		var tmpTAG = TAG_Option;
			tmpTAG = tmpTAG.replace(/{value}/g,yrGrp[i].value);
			tmpTAG = tmpTAG.replace(/{label}/g,yrGrp[i].label);
			grpHTML += tmpTAG;
	}
	return grpHTML;
}

function getYearGrpOPTS()
{
	var yrNum = getCurrentYear();
	var yrStart = yrNum - 80;
	var yrEnd = yrNum - 15;
	var yrGrp = createOPTGrp(yrStart,yrEnd,"Year...");
	return getOPTGrpHTML(yrGrp);
}
function getMonthGrpOPTS()
{
	var mthStart = 1;
	var mthEnd = 12;
	var mthGrp = createOPTGrp(mthStart,mthEnd,"Month...");
	return getOPTGrpHTML(mthGrp);
}

function getDayGrpOPTS()
{
	var mthStart = 1;
	var mthEnd = 31;
	var mthGrp = createOPTGrp(mthStart,mthEnd,"Day...");
	return getOPTGrpHTML(mthGrp);
}

function revealHideElement(_idElement,toShow)
{
	if (document.getElementById(_idElement))
	var element = document.getElementById(_idElement);
	else return;
	if (toShow)
		element.className = "visible";
	else
		element.className = "hidden";
}

function revealHideInput(_idInput,toShow)
{
	if (document.getElementById(_idInput))
	var element = document.getElementById(_idInput);
	else return;
	if (toShow)
		element.className = "expand";
	else
		element.className = "collapse";
}

function initUploadProcess(_startProcess)
{
	var Uploader = 'cvUploadPanel';
	var PathHolder = 'cvPathHolder';
	var formSubmitter = 'formSubmitter';
	var formNUploadSubmitter = 'formNUploadSubmitter';
	var fileName = 'applt_cv';
	var fileNameNew = 'applt_cvNewName';
	if (_startProcess) {
	revealHideElement(Uploader,true);
	revealHideElement(PathHolder,false);
	revealHideElement(formSubmitter,false);
	revealHideElement(formNUploadSubmitter,true);
	revealHideInput(fileName,false);
	} else {
	revealHideElement(Uploader,false);
	revealHideElement(PathHolder,true);
	//revealHideElement(formSubmitter,true);
	//revealHideElement(formNUploadSubmitter,false);
	revealHideElement(formSubmitter,false);
	revealHideElement(formNUploadSubmitter,true);
	revealHideInput(fileName,true);
	}
}

function validatingPath(str)
{
	if (str=="") return false;
	var Filename
	var FullPath
		FullPath = str;
		Filename = /([^\\]+)$/.exec(FullPath)[1];	
		//Extracts the extension to determine whether or not the uploaded file is in the correct format
	var Ext
		Ext = (/[.]/.exec(Filename)) ? /[^.]+$/.exec(Filename) : undefined;
		Ext = String(Ext);
		Ext = Ext.toLowerCase();
	if (Ext != "doc" && Ext != "pdf"){
		alert("We only accept file in max 2MB and in MS Word or PDF format!");
		return false;
	} else return Filename;
}

function tempFileNameInput(str)
{
	document.getElementById("applt_cv").value = str;
}

function applicationSubmitUpload()
{
	var validate1 = true;
	var uploadForm = document.getElementById('cvUploadFrame').contentWindow.document.forms[0];
	var uploadFrame = document.getElementById('cvUploadFrame');
	var strInput = null;
	switch (uploaderMode)
	{
		case "SWF":	strInput = uploadForm.txtFileName;	break;
		case "ASP":	strInput = uploadForm.txtFileAtth;	break;
	}
	var fileName = "";
	if (strInput != null)
	fileName = validatingPath(strInput.value);
	//Places filename in the original form text box before submitting.
	if (fileName != false)
	tempFileNameInput(fileName);
		
	var fldOutStanding = formValidation();
	if (fldOutStanding != 0)
	return;

	if (fileName != false) {
		//Places filename in the original form text box before submitting.
		tempFileNameInput(fileName);
		switch (uploaderMode)
		{
			case "SWF":	uploadFrame.contentWindow.doSubmit();	break;
			case "ASP":	uploadForm.submit();					break;
		}
		
    } else {
        var msg = "";
			msg+='Please select your CV file on the upload field';
			alert(msg);
        return;
    }  
}

function applicationUploadTransfer(newName) //Being used by the ASP version of Upload
{
	/*parent.document.forms[0].applt_cvNewName.value = newName;
	parent.initUploadProcess(false);
	parent.submitform();*/
	document.forms[0].applt_cvNewName.value = newName;
	initUploadProcess(false);
	submitform();
}

function redirectTO(_url)
{
	top.document.location.href=_url;
}

function selfSubmitChk()
{
	document.forms[0].action = "";
	document.forms[0].submit();
}

function unlockSubmitBtn(unlock)
{
	if(unlock){
	document.forms[0].Submit.disabled = false;
	//if(document.forms[0].applt_cvNewName.value != "" || uploaderMode == "ASP")
	document.forms[0].SubmitUpload.disabled = false;
	}else{
	document.forms[0].Submit.disabled = true;
	document.forms[0].SubmitUpload.disabled = true;
	}
}

function submitform()
{
	var fldOutStanding = formValidation();
	if (fldOutStanding == 0)if (fldOutStanding == 0) {
	//document.forms[0].action = "application_submit.asp";
	document.forms[0].submit();
	}
}
function submitForward()
{
	document.forms[0].submit();
}

// Modified on 20 Oct 2010
function applicationProcedureForward()
{
	document.forms[0].action = "ref_apply_online.asp";
	document.forms[0].submit();
}

function applicationForwardSubmit()
{
	var formObj = document.forms[0];
	if (formObj.pos_label.value == "" || formObj.pos_name.value == "" ||
		formObj.pos_ref.value == "" || formObj.pos_div.value == "")
		document.location.href = "recruit_online.asp";
	else
		document.forms[0].submit();
}

function elementSwitch(on,eleId,mesx,inline)
{
	if (document.getElementById(eleId))
	var eleObj = document.getElementById(eleId);
	else return;
	
	if (on) {
		eleObj.className = "visible";
		if (inline) eleObj.style.display = "inline";
	} else {
		eleObj.className = "hidden";
	}
	
	if (!on) elementsValueEraser(eleId);
	else if (on == true && mesx != "") elementsValueAssigner(eleId,mesx);
}

function radioElementSwitch(radioId,eleIdSufix,mesx,inline)
{
	if (document.getElementById(radioId))
	var radioObj = document.getElementById(radioId);
	else return;
	
	var eleWrapId = radioId + eleIdSufix;
	var on = radioObj.checked;
	elementSwitch(on,eleWrapId,mesx,inline);
}

function elementValueAssigner(input,inputType,mesx)
{
	switch(inputType)
	{
		default:							break;
		case "text":	if (input.value == "" && input.value != mesx)
					input.value = mesx;
											break;
	}
}

function elementsValueAssigner(wrapId,mesx)
{
	if (document.getElementById(wrapId))
	var wrapObj = document.getElementById(wrapId);
	else return;
	
	var inputGrp = wrapObj.getElementsByTagName('input');
	var inputLen = inputGrp.length;
	for (var i=0; i<inputLen; i++)
	{
		var inputType = inputGrp[i]['type'];
			elementValueAssigner(inputGrp[i],inputType,mesx);
	}
}

function elementValueEraser(input,inputType)
{
	switch(inputType)
	{
		default:							break;
		case "radio":	input.checked = false;	break;
		case "text":	input.value = "";		break;
	}
}

function elementsValueEraser(wrapId)
{
	if (document.getElementById(wrapId))
	var wrapObj = document.getElementById(wrapId);
	else return;
	
	var inputGrp = wrapObj.getElementsByTagName('input');
	var inputLen = inputGrp.length;
	for (var i=0; i<inputLen; i++)
	{
		var inputType = inputGrp[i]['type'];
			elementValueEraser(inputGrp[i],inputType);
	}
}

function textInputValueErase(focus,obj,mesx)
{
	if (focus && obj.value == mesx)
	obj.value = "";
	else if (!focus && obj.value == "")
	obj.value = mesx;
}

// modified on 30jan2002
//	top.location.href="/"; 
function home(){
	top.location.href="../../index.asp?lang=eng"; 
}

function sitemap(){
	top.location.href="../sitemap/index.asp";
}

function relatedlinks(){
	top.location.href="../related/index.asp";
}

function recruit(){
	top.location.href="../gm/recruit.asp";
}

function csr(){
	top.location.href="../csr/index.asp";
}

function gourl(i){
	links = Array ("#","../about/index.asp","../wnew/index.asp","../business/index.asp","../info/index.asp","../download/index.asp","../contact/index.asp");
	top.location.href= links[i];	
}

// modified on 30jan2002
//	top.location.href="/eng/gm/copyright.asp#disclaimer";
function disclaimer(){
	top.location.href="../../eng/copyright/index.asp#disclaimer";
}


// modified on 30jan2002
//	top.location.href="/eng/gm/copyright.asp";
function copyright(){
	top.location.href="../../eng/copyright/index.asp";
}

// modified on 08Sep2010
function DirectToTnC(section)
{
	top.location.href="../../eng/copyright/index.asp?section="+section;
}

// language parallel switching

function lang(version){
	link = location.href;
	english = "/eng/";
	chinese = "/chi/";
	
	if (version == "nchi"){
		alert=("This page is currently available only in English.")
	}

	if (version == "neng"){
		alert=("此 頁 暫 時 只 提 供 中 文 版")
	}

	if (version == "en"){
		link = link.replace (chinese, english);
		parent.location.href = link;
	}else if (version == "ch"){
		link = link.replace (english, chinese);
		parent.location.href = link;
	}
}


// modified on 30jan2002
//	ar_image = Array ("/images/cover_photo1.jpg","/images/cover_photo2.jpg","/images/cover_photo3.jpg");
var banners= 1;
function rotateimg(arg,time){

	ar_image = Array ("images/cover_photo1.jpg","images/cover_photo2.jpg","images/cover_photo3.jpg");
	
	if (++banners > arg){
		banners = 1;
	}
	document.rotateimage.src = ar_image[banners-1];
	window.setTimeout('rotateimg(3,7000);',time);
}
