/***********************************************
* Change the font size.
***********************************************/
function changeFontSize(fontSize) {
    var content = document.getElementById("content");
    content.className = 'tutorialContent' + fontSize;
    var p = content.getElementsByTagName("p");
    for (i = 0; i < p.length; i++) {
        if ((p[i].className != null) && (p[i].className.indexOf('tutorialContentTitle') > -1)) {
            p[i].className = 'tutorialContentTitle' + fontSize;
        }
    }
    var resize = document.getElementById("fontResize");
    var imgs = resize.getElementsByTagName("img");
    for (i = 0; i < imgs.length; i++) {
        var j = i + 1;
        if (j == fontSize) {
            imgs[i].src = "/home/images/icon_font" + j + "_on.gif";
        } else {
            imgs[i].src = "/home/images/icon_font" + j + "_off.gif";
        }
    }
}

/***********************************************
* Show the Disclaimer.
***********************************************/
function js_dbdisclaimer(S_P_Anchor) {
    hw = open(S_P_Anchor, 'dbpower', 'resizable=yes,scrollbars=yes,width=380,height=200');
    if (hw != null) { if (hw.opener == null) hw.opener = self; }
    if (hw.focus) hw.focus();
    hw.moveTo(0, 0);
}


/***********************************************
* Show the specified tab.
***********************************************/
function openTabs(noOfTabs, tabindex, form) {

    if (form) {
        form.currenttab.value = tabindex;
    }
    for (var i = 0; i < noOfTabs; i++) {

        // Show the appropriate page.
        if (i == tabindex) {
            document.getElementById("tabpage" + i).style.display = '';
        } else {
        document.getElementById("tabpage" + i).style.display = 'none';
        }

        // Change the tab button styles
        var tab = document.getElementById("tab" + i);
        var classname = tab.getAttribute("className");
        if (classname == undefined) {
            classname = tab.getAttribute("class");
        }
        if (i == tabindex) {
            classname = classname.replace("_off", "_on");
        } else {
            classname = classname.replace("_on", "_off");
        }
        tab.setAttribute("class", classname);
        tab.setAttribute("className", classname);
    }
}

/***********************************************
* Popup the specified URL.
***********************************************/
function popup(a,h,w){
	if (a != '') {
	        hw = open(a, 'data', "resizable=yes,scrollbars=yes,width="+w+",height="+h);
        	if (hw != null){if (hw.opener == null) hw.opener = self;}
	        if (hw.focus) hw.focus();
	}
}

/*******************************************************************
* Check whether the input email address is well-formated or not
*******************************************************************/
function isValidEmail(str) {
	if ((str.indexOf(".") > 2) && (str.indexOf("@") > 0)) {
		return true;
	} else {
		return false;
	}
}

/*******************************************************************
* Login
*******************************************************************/
var xmlHttp;

function createXMLHttpRequest() {
    if (window.ActiveXObject) {
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if (window.XMLHttpRequest) {
        xmlHttp = new XMLHttpRequest();
    }
}

function createQueryString() {
    var loginid = document.getElementById("loginid").value;
    var passwd = document.getElementById("passwd").value;

    var queryString = "loginid=" + loginid + "&passwd=" + passwd
        + "&formaction=login";

    return queryString;
}

function handleStateChange() {
    if (xmlHttp.readyState == 4) {
        if (xmlHttp.status == 200) {
            updatePage();
        }
    }
}

function updatePage() {
    eval(xmlHttp.responseText);
}

function loginXmlhttpPost(strURL) {
    createXMLHttpRequest();

    var queryString = createQueryString();

    xmlHttp.open("POST", strURL, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(queryString);
}

function logoutXmlhttpPost(strURL) {
    
    createXMLHttpRequest();

    xmlHttp.open("POST", strURL, true);
    xmlHttp.onreadystatechange = handleStateChange;
    xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlHttp.send(null);
}
/*******************************************************************
* Cookies
*******************************************************************/
function getCookie(NameOfCookie) {
    if (document.cookie.length > 0) {
        begin = document.cookie.indexOf(NameOfCookie + '=');
        if (begin != -1) {
            begin += NameOfCookie.length + 1;
            end = document.cookie.indexOf(';', begin);
            if (end == -1) end = document.cookie.length;
            return unescape(document.cookie.substring(begin, end));
        }
    }
    return null;
}

function setCookie(NameOfCookie, value, expirehours) {
    var ExpireDate = new Date();
    ExpireDate.setTime(ExpireDate.getTime() + (expirehours * 3600 * 1000));
    document.cookie = NameOfCookie + '=' + escape(value) + ';path=/;' + ((expirehours == null) ? '' : '; expires=' + ExpireDate.toGMTString());
}

function checkcookies() {
/*
    skey = getCookie('skey');
    if (skey != null) {
        document.getElementById('loginarea').innerHTML = "您好！<br /><br /><a href=\"/ch/member/member_changepwd_c.cgi\" class=\"top_smalltext\">更改密碼</a> | <a href=\"#\" onclick=\"javascript: logoutXmlhttpPost(\'/ch/member/logout_c.cgi\');\">登出</a>";
    } else {
        var text = "";
        text = text + "                       <form name=\"loginform\" onsubmit=\"javascript: loginXmlhttpPost('/ch/member/login_c.cgi');return false;\">";
        text = text + "                       <lable class=\"text12pt\">會員名稱:</label>  <input type=\"text\" name=\"loginid\" id=\"loginid\" style=\"width:100px\" value=\"\" /><br /> ";
        text = text + "                       <lable class=\"text12pt\">登入密碼:</label>  <input type=\"password\" name=\"passwd\" id=\"passwd\" style=\"width:100px\" value=\"\" /><br /> ";
        text = text + "                       <a href=\"/ch/member/member_register_c.cgi\" class=\"top_smalltext\"><font color=\"#CC3300\">新登記</font></a> | <a href=\"/ch/member/member_forgetpwd_c.cgi\" class=\"top_smalltext\">忘記密碼?</a> ";
        text = text + "                       <input name=\"login\" type=\"submit\" class=\"top_loginbutton\" id=\"login\" value=\"登入\" onclick=\"javascript: loginXmlhttpPost('/ch/member/login_c.cgi');document.loginform.loginid.focus();return false;\"> ";
        text = text + "                       <input name=\"reset\" type=\"reset\" class=\"top_loginbutton\" id=\"reset\" value=\"重設\"> ";
        text = text + "                       </form>";
        document.getElementById('loginarea').innerHTML = text;
    }
*/
}

function checkcookies1() {
    skey = getCookie('skey');
    if (skey != null) {
    document.getElementById('loginarea').innerHTML = "您好！<br /><br /><a href=\"/ch/member/member_changepwd_c.cgi\" class=\"top_smalltext\">更改密碼</a> | <a href=\"#\" onclick=\"javascript: logoutXmlhttpPost(\'/ch/member/logout_c.cgi\');\">登出</a>";
    } else {
    var text = "";
    text = text + "                       <form name=\"loginform\" onsubmit=\"javascript: loginXmlhttpPost('/ch/member/login_c.cgi');return false;\">";
    text = text + "                       <lable class=\"text12pt\">會員名稱:</label>  <input type=\"text\" name=\"loginid\" id=\"loginid\" style=\"width:100px\" value=\"\" /><br /> ";
    text = text + "                       <lable class=\"text12pt\">登入密碼:</label>  <input type=\"password\" name=\"passwd\" id=\"passwd\" style=\"width:100px\" value=\"\" /><br /> ";
    text = text + "                       <a href=\"/ch/member/member_register_c.cgi\" class=\"top_smalltext\"><font color=\"#CC3300\">新登記</font></a> | <a href=\"/ch/member/member_forgetpwd_c.cgi\" class=\"top_smalltext\">忘記密碼?</a> ";
    text = text + "                       <input name=\"login\" type=\"submit\" class=\"top_loginbutton\" id=\"login\" value=\"登入\" onclick=\"javascript: loginXmlhttpPost('/ch/member/login_c.cgi');document.loginform.loginid.focus();return false;\"> ";
    text = text + "                       <input name=\"reset\" type=\"reset\" class=\"top_loginbutton\" id=\"reset\" value=\"重設\"> ";
    text = text + "                       </form>";
    document.getElementById('loginarea').innerHTML = text;
    }
}


/*******************************************************************
* About Selector
*******************************************************************/
var AboutCloseTimer = 0;

function showAboutSelector(show) {
    selector = document.getElementById('aboutSelector');
    if (show == 'hide') {
        selector.style.display = 'none';
    } else {
        selector.style.display = 'block';
        if (AboutCloseTimer){
        	window.clearTimeout(AboutCloseTimer);
        	AboutCloseTimer = null;
        }
    }
}

function setAboutMenuClose(){
	AboutCloseTimer  = window.setTimeout(AboutMenuClose,500);	
}

function AboutMenuClose(){
	selector = document.getElementById('aboutSelector');
	selector.style.display = 'none';
}

/*******************************************************************
* Country Selector
*******************************************************************/
var CountryCloseTimer = 0;

function showCountrySelector(show) {
    selector = document.getElementById('countrySelector');
    if (show == 'hide') {
        selector.style.display = 'none';
    } else {
        selector.style.display = 'block';
        if (CountryCloseTimer){
        	window.clearTimeout(CountryCloseTimer);
        	CountryCloseTimer = null;
        }
    }
}

function setCountryMenuClose(){
    CountryCloseTimer  = window.setTimeout(CountryMenuClose,500);	
}

function CountryMenuClose(){
	selector = document.getElementById('countrySelector');
	selector.style.display = 'none';
}
