// Get cookie by name
function getCookie(name) {
	return $.cookie(name);
}
		
// Set cookie
function setCookie(name, value, expires, path, domain, secure) {
	var options = {};
	if (expires != null) options.expires = expires;
	if (path != null) options.path = path;
	if (domain != null) options.domain = domain;
	if (secure != null) options.secure = secure;
	$.cookie(name, value, options);
}

// An adaptation of Dorcht's function for deleting a cookie.
function delCookie(name, path, domain) {
	var options = {};
	if (path != null) options.path = path;
	if (domain != null) options.domain = domain;
	$.cookie(name, null, options);
}

function changeLanguage(newLanguage)
{
	var l1 = window.location.href;

	var re = /(\?|&)langid=.\&?/gi;		
	l1 = l1.replace(re, "");
	
	if ((l1 == "http://baby.boom.com.hk/index.asp") || ((l1 == "http://home.boom.com.hk/index.html") && (newLanguage == 'C'))){
		return "/index_chi.asp";
	} else if ((l1 == "http://baby.boom.com.hk/index_chi.asp") || ((l1 == "http://home.boom.com.hk/index.html") && (newLanguage == 'E'))) {
		return "/index.asp";
	}	
	
	re = /\/aboutus\//;
	if (l1.search(re) != -1) {
		if (newLanguage == 'C') {
			re = /\.(asp|html)/;
			l1 = l1.replace(re, '_chi.$1');
		}
		if (newLanguage == 'E') {
			re = /_chi\./;
			l1 = l1.replace(re, '.');
		}		
	}

	re = /\/portfolio\/anywhere\/wap\//;
	if (l1.search(re) != -1) {
		if (newLanguage == 'C') {
			re = /\.(asp|html)/;
			l1 = l1.replace(re, 'c.$1');
		}
		if (newLanguage == 'E') {
			re = /c\./;
			l1 = l1.replace(re, '.');
		}		
	}

	re = /\/portfolio\/margin\//;
	if (l1.search(re) != -1) {
		re = /_/;			
		if (l1.search(re) != -1) {
			if (newLanguage == 'C') {
				re = /_/;
				l1 = l1.replace(re, '_c');
				
			}
			if (newLanguage == 'E') {
				re = /_c/;
				l1 = l1.replace(re, '_');
			}
		} else {
			if (newLanguage == 'C') {
				re = /\.htm/;
				l1 = l1.replace(re, 'c.htm');
				
			}
			if (newLanguage == 'E') {
				re = /c\.htm/;
				l1 = l1.replace(re, '.htm');
			}
		}
	}
	
//	re = /\?.+=.+/gi;
//	if (l1.search(re) != -1) {
//		l1 = l1 + '&langid=' + newLanguage;
//	} else {
//		l1 = l1 + '?langid=' + newLanguage;
//	}
	
	re = /(#.*)\?/;
	found = l1.match(re);
	l1 = l1.replace(re, '?');
			
	if ((found != null) && (found.length >= 2)) {			
		l1 = l1 + found[1];
	}
	return l1;
}

/* top menu */
function updateMenuTableWidth() {
	var minWidth = 1008, mw = window.mainMenuWidth ? window.mainMenuWidth: 679, w = $(document).width(), m = $("#menuwrapper");
	if (screen.width !== undefined && w > screen.width) w = screen.width;
	var pos = m.position();
	m.width(mw);
	if (w > minWidth) minWidth = w;
	$("#menuTable").width(minWidth);	
	$("#menuContainer").width(mw);
	$("div.footer").width(minWidth);
}

function switchLanguage(lang) {
	var cookieLanguage = null, oldurl = window.location.href;
	switch (lang) {
		case "en":
			cookieLanguage = "E";
			break;
		case "zh":
			cookieLanguage = "C";
			break;
		case "gb":
			cookieLanguage = "GB";
			break;
		default:
			lang = "zh";
			cookieLanguage = "C";
	}
	var newurl = changeLanguage(cookieLanguage);
	setCookie("locale", lang, "Fri, 01-Jan-2038 00:00:01 GMT", "/", "boom.com.hk", null);
	setCookie("language", cookieLanguage, "Fri, 01-Jan-2038 00:00:01 GMT", "/", "boom.com.hk", null);
	
	if (oldurl == newurl) {
		window.location.reload();
	} else {
		window.location = newurl;
	}
}

/* swap images */
function MM_swapImgRestore() {
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_swapImage() {
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=document.getElementById(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src;x.src=a[i+2];}
}
/* end swap images */

function gotoLoginPage() {
	window.location.href="https://www.boom.com.hk/client2/login_form.asp?checkCookie=Y&GoTo=default&main_menu=default";
	return false;
	
	//return popupPage("BOOMTRADE","https://trade.boom.com.hk/auth/");
}

function popupPage(target, url, width, height, option) {
	window.location.href=url;
	
	/*
	var opt = option ? option:"statusbar=no,menubar=yes,scrollbars=yes,resizable=yes";
	var w = width && /\d+/.test(width) && width > 0 ? width:1125; 
	var h = height && /\d+/.test(height) && height > 0 ? height:768;

	if (target == "BOOMTRADE") {
		opt = "statusbar=no,menubar=no,scrollbars=yes,resizable=yes";
	}

	opt += ",width="+w+",height="+h+",top=0,left=0";
	var newWindow=window.open(url,target,opt);
	if (newWindow && newWindow.focus) newWindow.focus();
	*/
	return false;
}
