// JavaScript Document
function SwitchLang(lang) {

var gopath;
	if (top.location.href.indexOf("#") != -1) {
		locationx = top.location.href.substring(0, top.location.href.indexOf("#"));
	} else {
		locationx = top.location.href;
	}
	if (lang == 'en') {
		if (locationx.indexOf("/en/") == -1) {
			if (locationx.indexOf("/chs/") != -1) {
				gopath = top.location.replace(locationx.replace("/chs/","/en/"));
			} else if (locationx.indexOf("/tc/") != -1) {
				gopath = top.location.replace(locationx.replace("/tc/","/en/"));
			} 
		}
	} else if (lang == 'chs') {
		if (locationx.indexOf("/chs/") == -1) {
			if (locationx.indexOf("/en/") != -1) {
				gopath = top.location.replace(locationx.replace("/en/","/chs/"));
			} else if (locationx.indexOf("/tc/") != -1) {
				gopath = top.location.replace(locationx.replace("/tc/","/chs/"));
			} 
		}
	} else if (lang == 'tc') {
		if (locationx.indexOf("/tc/") == -1) {
			if (locationx.indexOf("/en/") != -1) {
		 		gopath = top.location.replace(locationx.replace("/en/","/tc/"));
			} else if (locationx.indexOf("/chs/") != -1) {
		 		gopath = top.location.replace(locationx.replace("/chs/","/tc/"));
			} 
		}
	} 
}

function scaleSize(htmlText, attrName, scaleFactor)
{
    var pattern = "(<.*?" + attrName + "=([\"']?))(\\d+)((.*?)\\2.*?>)"
    var searchPattern  = new RegExp(pattern, "gi")
    var replacePattern = new RegExp(pattern, "i")
    var copyHtmlText = htmlText

    while ( ( result = searchPattern.exec(htmlText)) != null )
    {
       if ( ! ( result[5] == "%" ) )
       {
         scaleValue = Math.round(parseInt(result[3]) * scaleFactor)
         replaceStr = result[0].replace(replacePattern, "$1" +scaleValue + "$4");
         copyHtmlText = copyHtmlText.replace(result[0], replaceStr)
       }
    }   
    return copyHtmlText; 
}

function printPage(tempContent) {

        var mmScript=
'<script type="text/javascript">' + '\n' +
'<!--' + '\n' +
'function MM_swapImgRestore() { ' + '\n' +
'  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;'+ '\n' +
'}' + '\n' +
'function MM_preloadImages() { ' + '\n' +
'  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();' + '\n' +
'    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)' + '\n' +
'    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}' + '\n' +
'}' + '\n' +
'function MM_findObj(n, d) { ' + '\n' +
'  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {' + '\n' +
'    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}' + '\n' +
'  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];' + '\n' +
'  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);' + '\n' +
'  if(!x && d.getElementById) x=d.getElementById(n); return x;' +
'}' + '\n' +
'function MM_swapImage() { ' + '\n' +
'  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)' + '\n' +
'   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}' + '\n' +
'}' + '\n' +
'//-->' +  '\n' +
'</script>' +  '\n'

        var tc_but_pattern  = /<a\s[^<]*SwitchLang\('tc'\).+?>.*?<\/a>/i
        var chs_but_pattern = /<a\s[^<]*SwitchLang\('chs'\).+?>.*?<\/a>/i
        var en_but_pattern  = /<a\s[^<]*SwitchLang\('en'\).+?>.*?<\/a>/i

        tempContent = tempContent.replace(tc_but_pattern, "")
        tempContent = tempContent.replace(chs_but_pattern, "")
        tempContent = tempContent.replace(en_but_pattern, "")

        tempContent = tempContent.replace("printPage(document.getElementById('printTarget').innerHTML);", "window.print()");
  
        tempContent = scaleSize(tempContent, "width", 0.85)    
        tempContent = scaleSize(tempContent, "height", 0.85)     

	var newWin=window.open("", "", "width=800,height=600,left=10,top=10,scrollbars=yes");
	var txt="<html><head><link rel=\"stylesheet\" type=\"text/css\" href=\"css/print_style.css\" />" + mmScript + "</head><body onload='window.print();'>" + tempContent + "</body></html>";
	newWin.document.open();
	newWin.document.write(txt);
 	newWin.document.close(); 

}

