﻿/*
Check HTTPS
*/
if (
location.href.indexOf("www.uaf.com.hk")>=0 && (
location.href.indexOf("/loan_services/i_money_internet_loan/apply/new_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/i_money_internet_loan/apply/existing_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/i_money_internet_loan/loan_confirmation/index.html")>=0 ||
location.href.indexOf("/loan_services/i_money_internet_loan/i_express_money_confirmation/index.html")>=0 ||
location.href.indexOf("/loan_services/macau_i_money_internet_loan/apply/new_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/macau_i_money_internet_loan/apply/existing_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/macau_i_money_internet_loan/loan_confirmation/index.html")>=0 ||
location.href.indexOf("/loan_services/e_cash_revolving_loan/apply/new_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/e_cash_revolving_loan/apply/existing_customer/index.html")>=0 ||
location.href.indexOf("/loan_services/e_cash_revolving_loan/account_operation/index.html")>=0 ||
location.href.indexOf("/loan_services/e_cash_revolving_loan/loan_confirmation/index.html")>=0 ||
location.href.indexOf("/loan_services/debts_consolidation_loan/apply_now/index.html")>=0 ||
location.href.indexOf("/loan_services/property_loan/apply/index.html")>=0 ||
location.href.indexOf("/loan_services/sme_loan/apply_now/index.html")>=0 ||
location.href.indexOf("/loan_services/loan_for_higher_education_students_in_hong_kong/apply_now/index.html")>=0 ||
location.href.indexOf("/loan_services/loan_for_higher_education_students_in_hong_kong/loan_confirmation/index.html")>=0 ||
location.href.indexOf("/loan_services/tax_loan/apply_now/index.html")>=0 ||
location.href.indexOf("/loan_services/phone_express_money_confirmation/loan_confirmation/index.html")>=0 ||
location.href.indexOf("/bonus_points_program/bonus_points_redemption/index.html")>=0 ||
location.href.indexOf("/members_get_members_program/application_online/index.html")>=0 ||
location.href.indexOf("/members_get_members_program/download_application_form/index.html")>=0
)) {
	if (location.href.indexOf("https://")<0) {
		window.location = location.href.replace("http://","https://");
	}
} else if (location.href.indexOf("https://")>=0) {
	window.location = location.href.replace("https://","http://");
}

jQuery.preloadImages = function() {
	$('.btn_over').each(function() {
		jQuery("<img>").attr("src", replaceFilename($(this).attr('src'), ".", "_over."));
		jQuery('<input>').attr("src", replaceFilename($(this).attr('src'), ".", "_over."));
	});
	$('.btn_o').each(function() {
		jQuery("<img>").attr("src", replaceFilename($(this).attr('src'), ".", "_o."));
	});
}

function updatelink() {
	var langFolder = "en";
	var filename = "index.html";

	if (1 == zmsCharsetID){
		langFolder = "en";
	}else if (2 == zmsCharsetID){
		langFolder = "tc";
	}else if (3 == zmsCharsetID){
		langFolder = "sc";
	}

	if (document.getElementById('home_logo')) {
		document.getElementById('home_logo').href = "/"+langFolder+"/home/" + filename;
	}
	if (document.getElementById('home')) {
		document.getElementById('home').href = "/"+langFolder+"/home/" + filename;
	}
	if (document.getElementById('aboutus')) {
		document.getElementById('aboutus').href = "/"+langFolder+"/home/about_us/" + filename;
	}
	if (document.getElementById('branch')) {
		document.getElementById('branch').href = "/"+langFolder+"/home/branch_network/" + filename;
	}
	if (document.getElementById('sitemap')) {
		document.getElementById('sitemap').href = "/"+langFolder+"/home/site_map/" + filename;
	}
	if (document.getElementById('contactus')) {
		document.getElementById('contactus').href = "/"+langFolder+"/home/contact_us/" + filename;
	}
	if (document.getElementById('career')) {
		document.getElementById('career').href = "/"+langFolder+"/home/careers/" + filename;
	}
	if (document.getElementById('bonuspoint')) {
		document.getElementById('bonuspoint').href = "/"+langFolder+"/bonus_points_program/" + filename;
	}
	if (document.getElementById('mgm')) {
		document.getElementById('mgm').href = "/"+langFolder+"/members_get_members_program/" + filename;
	}
	if (document.getElementById('login')) {
		document.getElementById('login').href = "/"+langFolder+"/loan_services/e_cash_revolving_loan/account_operation/" + filename;
	}
	if (document.getElementById('logout')) {
		document.getElementById('logout').href = "/logout.aspx";
	}
	if (document.getElementById('disclaimer')) {
		document.getElementById('disclaimer').href = "/"+langFolder+"/disclaimer/" + filename;
	}
	if (document.getElementById('privacypolicy')) {
		document.getElementById('privacypolicy').href = "/"+langFolder+"/privacy_policy/" + filename;
	}
}

function goBack() {
	history.go(-1);
}

function goHome() {
	var langFolder = "en";
	var filename = "index.html";

	if (1 == zmsCharsetID){
		langFolder = "en";
	}else if (2 == zmsCharsetID){
		langFolder = "tc";
	}else if (3 == zmsCharsetID){
		langFolder = "sc";
	}

	document.location = "/"+langFolder+"/home/" + filename;
}

function switchLang(charsetID){
	var url = window.location.toString();
	var isStaticURL = false;
	var langPaths = new Array("en", "tc", "sc");
	var oldLangPath = "";

	// Check url type (is static page or dynamic page)
	for (i=0; i < langPaths.length; i++){
		if (url.indexOf("/"+langPaths[i]+"/") != -1){
			oldLangPath = "/"+langPaths[i]+"/";
			isStaticURL = true;
			break;
		}
	}

	if (isStaticURL){
		var path;
		path = window.location.href;
		window.location = path.replace(oldLangPath, "/"+langPaths[charsetID - 1]+"/");
	}else{
		if (url.indexOf("lang=") != -1){
			window.location = 
				url.replace(/lang=\d/, "lang="+charsetID);
		}
	}
}

function switchPda() {
	if (screen.width<800 || screen.height<600) {
		if (zmsCharsetID==1) {
			location.href = "/pda/default_en.htm";
		} else if (zmsCharsetID==2) {
			location.href = "/pda/default_tc.htm";
		} else if (zmsCharsetID==3) {
			location.href = "/pda/default_sc.htm";
		}
	} else {
		if (zmsCharsetID==1) {
			alert('Please view in a PDA device');
		} else if (zmsCharsetID==2) {
			alert('請用PDA儀器瀏覽。');
		} else if (zmsCharsetID==3) {
			alert('请用PDA仪器浏览。');
		}
	}
}

function Menu() {
	this.contentId = 0;
	this.text = "";
	this.alt = "";
	this.href = "";
	this.target = "";
	this.childs = new Array();
}

function showHide(theOne, isShow){
	// theOne = JQuery Selector
	// isShow = true for show
	if(isShow){
		theOne.show();
	}else{
		theOne.hide();
	}
}
function hideAll(){
	$(".sub").each(function(){
		$(this).css('display', 'none');
	});
}

/*
function configureLeftMenu(){
	$(".menu .sub").each(function(){
		var currSub = $(this);
		$(this).parent().hover(function(){
			hideAll();
			var mainMenu = $(this).parent(".menu");
			mainMenu.attr("active", 1)
			var iWidth = mainMenu.width();
			currSub.css({display: "block", top: getY(this), left: getX(this) + iWidth - 10});
			currSub.show();

			var subMenuWidth = currSub.width();
			var subMenuHeight = currSub.height();
			currSub.children("li:eq(0)").children("iframe").css("width", subMenuWidth+"px");
			currSub.children("li:eq(0)").children("iframe").css("height", subMenuHeight+"px");
		}, function(){
			currSub.hide();
		});
	});
}
*/
function getY(arg){
	var y = 0;
	if (arg.offsetParent){
		y = getY(arg.offsetParent);
	}
	return arg.offsetTop + y;
}

function getX(arg){
	var x = 0;
	if (arg.offsetParent){
		x = getX(arg.offsetParent);
	}
	return arg.offsetLeft + x;
}

function configureRevisionDate(){
	if (typeof(reviewDateExists) != "undefined"
			&& reviewDateExists){
		var placeHolder = document.getElementById("phReviewDate");
		var divDate = document.getElementById("div_reviewdate");
		if (placeHolder != null && divDate != null){
			divDate.innerHTML = loadDateString(reviewDate_Year, reviewDate_Month, reviewDate_Day);
			placeHolder.style.display = "inline";
		}
	}else if (typeof(revisionDateExists) != "undefined"
			&& revisionDateExists){
		var placeHolder = document.getElementById("phRevisionDate");
		var divDate = document.getElementById("div_revisiondate");
		if (placeHolder != null && divDate != null){
			divDate.innerHTML = loadDateString(revisionDate_Year, revisionDate_Month, revisionDate_Day);
			placeHolder.style.display = "inline";
		}
	}
}

function loadDateString(yyyy, mm, dd){
	switch (zmsCharsetID)
	{
		case 1:
		case 4:
			var monthName = new Array("", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
			return dd+" "+monthName[mm]+" "+yyyy;
			break;
		case 2:
		case 5:
			return yyyy+"年"+mm+"月"+dd+"日";
			break;
		case 3:
		case 6:
			return yyyy+"年"+mm+"月"+dd+"日";
			break;
	}
}

function replaceFilename(url, fromstr, tostr){
	var newUrl;
	if (url) {
		var filestart = url.lastIndexOf("/");
		newUrl = 
			url.substring(0, filestart)
			+ url.substring(filestart)
			.replace(fromstr, tostr);
	}
	return newUrl;
}

Menu.prototype = {
	addChild : function(m) {
		this.childs[this.childs.length] = m;
	}
}

function generateMenu(rootMenu) {
	// Must override
}

function printPage() {
	var URL;
	URL = location.href;
	if (URL.indexOf("?")>0) {
		URL += "&print=1";
	} else {
		URL += "?print=1";
	}
	window.open(URL);
}

function ShowPrintHeader(){
	document.body.style.background = "none";

	var langFolder = "en";
	if (1 == zmsCharsetID){
		langFolder = "en";
		altText = "UA Finance";
	}else if (2 == zmsCharsetID){
		langFolder = "tc";
		altText = "UA 亞洲聯合財務";
	} else if (3 == zmsCharsetID){
		langFolder = "sc";
		altText = "UA 亚洲联合财务";
	}
	
	var imageFolder = "/filemanager/template/"+langFolder+"/images/";
	var html = '';
	html += '<table border="0" cellpadding="0" cellspacing="0" width="600" class="print_table">';
	html += '<tbody>';
	html += '<tr align="left" valign="top">';
	html += '<td><img src="'+imageFolder+'uaf_print_banner.jpg" alt="'+altText+'" title="'+altText+'" border="0" /></td>';
	html += '</tr>';
	html += '<tr align="left" valign="top">';
	html += '<td>';
	html += '<br />';
	html += '<div id="div_pagename" class="hdtitle"></div>';

	document.writeln(html);
}

function ShowPrintFooter(){
}

function openPopup(path,width,height,targetname) {
	window.open(path,targetname,'height='+height+', width='+width+', toolbar=no, menubar=no, location=no, status=no, scrollbars=yes, resizable=yes');
}

function openPopupWithPosition(path,width,height,targetname,left,top) {
	window.open(path,targetname,'height='+height+', width='+width+', toolbar=no, menubar=no, location=no, status=no, scrollbars=yes, resizable=yes, left='+left+', top='+top+', screenX='+left+', screenY='+top+'');
}

function jump(target,width,height,link) {
	if (link != '' && link!= 'undefined') {
		if (target=='' || target=='_self') {
			window.location = link;
		} else {
			window.open(link);
		}
	}
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function HTMLEncode(strHTML)
{
	var div = document.createElement('div');
	div.innerText = strHTML;
	div.textContent = strHTML;
	return div.innerHTML;
}

function HTMLDecode(strEncodeHTML)
{
	var div = document.createElement('div');
	div.innerHTML = strEncodeHTML;
	return div.innerText;
}

function getX(arg)
{
	var x = 0;
	if (arg.offsetParent){
		x = getX(arg.offsetParent);
	}
	return arg.offsetLeft + x;
}

function previewModeLinkHandler() {
	alert('Links in preview mode is not allowed.');
	return false;
}

function isInteger(text) {
	var re = new RegExp(/^[0-9]*$/);
	return text.match(re)!=null;
}

function isAlpha(text) {
	var re = new RegExp(/^[a-zA-Z ]*$/);
	return text.match(re)!=null;
}

function isAlphaNumeric(text) {
	var re = new RegExp(/^[0-9a-zA-Z ]*$/);
	return text.match(re)!=null;
}

function isChinese(text) {
	var re = new RegExp(/^([\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi);
	return text.match(re)!=null;
}

function isAlphaChinese(text) {
	var re = new RegExp(/^([a-zA-Z ]|[\u4E00-\u9FA5]|[\uFE30-\uFFA0])*$/gi);
	return text.match(re)!=null;
}

function isValidEmail(email) {
	var re = new RegExp(/^[a-zA-Z0-9]+([_.-][a-zA-Z0-9]+)*@([a-zA-Z0-9]+([\.-][a-zA-Z0-9]+)*)+\.[a-zA-Z]{2,}$/);
	return email.match(re)!=null;
}

if (location.href.indexOf("/home/careers/")>0) {
	document.writeln('<link href="/filemanager/template/common/css/common_font.css" rel="stylesheet" type="text/css" />');
}

function configureTextAreaMaxLength(){
    $("textarea[jQueryMaxLength]").each(function() {
        var maxLength = $(this).attr("jQueryMaxLength");
        $(this)[0].onkeypress = function (){
            return this.value.length < maxLength;
        };
    });
}
