String.prototype.trim = function(){return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))}
String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}
String.prototype.endsWith = function(str) {return (this.match(str+"$")==str)}


function saveLoginBlockInfoToCookie() {
    var unameIdBox = document.getElementById('login_name_block_id');
    var passIdBox = document.getElementById('login_pass_block_id');
    var unameId = "";
    var passId = "";
    if(unameIdBox) {
        unameId = unameIdBox.value;
    }
    if(passIdBox) {
        passId = passIdBox.value;
    }
    var unameBox = document.getElementById(unameId);
    var passBox = document.getElementById(passId);
    
    var checkBox = document.getElementById('kioku_check_block_id');
    var temp = "";

    if(checkBox) {
        if(checkBox.checked) {
            var dt = new Date(); 
            dt.setDate(dt.getDate() + 60); 
            if(unameBox) {
                temp = encryptText(unameBox.value);
                setCookie('uname', temp, dt);
            }
            if(passBox) {
                temp = encryptText(passBox.value);
                setCookie('pass', temp, dt);
            }
        }
        else {
            if(unameBox) {
                //clearCookie('uname');
            }
            if(passBox) {
                //clearCookie('pass');
            }
        }
    }
    return true;
}

function loadLoginBlockInfoFromCookie() {

    var unameIdBox = document.getElementById('login_name_block_id');
    var passIdBox = document.getElementById('login_pass_block_id');
    var unameId = "";
    var passId = "";
    if(unameIdBox) {
        unameId = unameIdBox.value;
    }
    if(passIdBox) {
        passId = passIdBox.value;
    }
    var unameBox = document.getElementById(unameId);
    var passBox = document.getElementById(passId);
    var temp = "";
    if(unameBox) {
        temp = decryptText(getCookie('uname'));
        if(temp.length > 0) {
            unameBox.value = temp
        }
    }
    if(passBox) {
        temp = decryptText(getCookie('pass'));
        if(temp.length > 0) {
            passBox.value = temp;
        }
    }
}


function saveLoginDefaultInfoToCookie() {
    var unameId = 'login_uname_default_id';
    var passId = 'login_pass_default_id';
    var unameBox = document.getElementById(unameId);
    var passBox = document.getElementById(passId);
    var checkBox = document.getElementById('kioku_check_default_id');
    var temp = "";

    if(checkBox) {
        if(checkBox.checked) {
            var dt = new Date(); 
            dt.setDate(dt.getDate() + 60); 
            if(unameBox) {
                temp = encryptText(unameBox.value);
                setCookie('uname', temp, dt);
            }
            if(passBox) {
                temp = encryptText(passBox.value);
                setCookie('pass', temp, dt);
            }
        }
        else {
            if(unameBox) {
                //clearCookie('uname');
            }
            if(passBox) {
                //clearCookie('pass');
            }
        }
    }
    return true;
}

function loadLoginDefaultInfoFromCookie() {


    var unameId = 'login_uname_default_id';
    var passId = 'login_pass_default_id';
    var unameBox = document.getElementById(unameId);
    var passBox = document.getElementById(passId);
    var temp = "";
    if(unameBox) {
        temp = decryptText(getCookie('uname'));
        if(temp.length > 0) {
            unameBox.value = temp
        }
    }
    if(passBox) {
        temp = decryptText(getCookie('pass'));
        if(temp.length > 0) {
            passBox.value = temp;
        }
    }
}



function encryptText(val)
{
    var data = escape(val);
    var res = "";
    var i;
    for(i = 0;i < data.length;i++) {
        var temp = data.substring(i, i + 1);
        temp = temp.charCodeAt(0) * (Math.floor("W".charCodeAt(0) * 4 / 49));
        rand = Math.floor(Math.random() * 9000 + 1000);
        res += temp + "," + rand + ",";
    }
    return res;
}

function decryptText(val)
{
    var data = val.split(",");
    var res = "";
    var i;
    for(i = 0;i < data.length;i+=2) {
        var temp = data[i];
        temp = parseInt(temp) / (Math.floor("H".charCodeAt(0) / 11 + 1));
        if(temp > 0) {
            res += String.fromCharCode(temp);
        }
    }
    res = unescape(res);
    if(res.length > 0 && res.charCodeAt(0) == 0) {
        res = "";
    }
    return res;
}


/**
 *  クッキーから情報を取得する
 */
function getCookie(key,  tmp1, tmp2, xx1, xx2, xx3) 
{
    tmp1 = " " + document.cookie + ";";
    xx1 = xx2 = 0;
    len = tmp1.length;
    while (xx1 < len) {
        xx2 = tmp1.indexOf(";", xx1);
        tmp2 = tmp1.substring(xx1 + 1, xx2);
        xx3 = tmp2.indexOf("=");
        if (tmp2.substring(0, xx3) == key) {
            return(unescape(tmp2.substring(xx3 + 1, xx2 - xx1 - 1)));
        }
        xx1 = xx2 + 1;
    }
    return("");
}

/**
 *  クッキーに情報を保存する
 */
function setCookie(key, val, dt, tmp) 
{
    tmp = key + "=" + escape(val) + "; ";
    // tmp += "path=" + location.pathname + "; ";
    if(dt == null) {
        tmp += "expires=Tue, 31-Dec-2030 23:59:59; ";
    }
    else {
        tmp += "expires=" + dt.toGMTString() + "; ";
    }
    document.cookie = tmp;
    
}

/**
 *  クッキーをクリアする
 */
function clearCookie(key) 
{
    document.cookie = key + "=" + " ; expires=Tue, 1-Jan-1980 00:00:00; ";
}

function onLoadProcLoginForm()
{
    setTimeout('loadLoginBlockInfoFromCookie(); loadLoginDefaultInfoFromCookie(); ', 100);
}


window.onload = onLoadProcLoginForm;


