/*
 Ajax Chat for TRPG ver.2.6.2.1
 (c)2007-2009 Cake All Rights Reserved.
 Mail : cake_67@users.sourceforge.jp
 Home : http://trpgtools-onweb.sourceforge.jp/
 */

/*****************
 *  JaveScriptライブラリ：共通
 *****************/
// 変数
var preZIndex = parseInt((new Date()).getTime()/100);
var writescript = './write.php';

// ブラウザ判断
isIE();

// IE判断
function isIE() {
    var userAgent = navigator.userAgent;
    if (userAgent.search('MSIE')!='-1') {
        isIE = true;
    } else {
        isIE = false;
    }
}

// エンコード
function convert(C) {
    C = encodeURIComponent(C);
    return C;
}

// PHPのhtmlspecialchars
function phpHtmlspecialchars(str) {
    if (!str) {
        return str;
    }
    str = str.replace(/&/g,"&amp;");
    str = str.replace(/</g,"&lt;");
    str = str.replace(/>/g,"&gt;");
    str = str.replace(/"/g,"\"");
    str = str.replace(/'/g,"\'");
    return str;
}

// PHPのaddslashes
function phpAddslashes(str) {
    if (!str) {
        return str;
    }
    str = str.replace(/"/g,'\\"');
    str = str.replace(/'/g,"\\'");
//    str = str.replace(/\/g,"\\");
    return str;

}

// 表示ON/OFF
function showTarget(target) {
    var id = '#'+target;
    $(id).show();

    var inputId = '#'+target+' [name="pc_input"]';
    $(inputId).focus();
}
function hideTarget(target) {
    var id = '#'+target;
    $(id).hide();

    var prevPc = $(id).prev().prev();
    var prevId = $(id).prev().prev().attr("id");
    if (prevId) {
        var inputId = '#'+prevId+' [name="pc_input"]';
        $(inputId).focus();
    }
}
function toggleColumn(target) {
    if (!document.getElementById(target)) { 
        return false;
    }
    dispStatus = document.getElementById(target).style.display;
    if (dispStatus == 'none') {
        showTarget(target);
        var newZIndex = parseInt((new Date()).getTime()/100);
        newZIndex = newZIndex - preZIndex;
        $(this).css("z-index", newZIndex);
    } else {
        hideTarget(target);
    }
}

// ウインドウサイズ変更でフォームサイズ変更
// ウィンドウサイズ取得のブラウザ補正
function getWinWidth() {
    if (window.innerWidth) {
        return window.innerWidth;
    } else if (document.documentElement && document.documentElement.clientWidth) {
        return document.documentElement.clientWidth;
    } else if ( document.body && document.body.clientWidth) {
        return document.body.clientWidth;
    }
}
function getWinHeight() {
    if (window.innerHeight) {
        return window.innerHeight;
    } else if (document.documentElement && document.documentElement.clientHeight) {
        return document.documentElement.clientHeight;
    } else if ( document.body && document.body.clientHeight) {
        return document.body.clientHeight;
    }
}

// 画像読み込み
function loadImageObj(fileName) {
    var imgObj = new Image();
    if (fileName!="undefined" && fileName) {
        imgObj.src = fileName;
    } else {
        imgObj.src = '';
    }
    return imgObj;
}

// 指定IDのオブジェクトのz-index変更
function changeZIndex(id, val) {
    id = "#"+id;
    $(id).css("z-index", val)

}

// チャットログ書き込み
function writeComment(params, noUpdate)
{
    $.ajax({
        type: "GET",
        url: writescript+params,
        success: function(data) {
            // 受信完了イベント
            if (!noUpdate) {
                window.mainframe.getLog(true);
            }
        }
    });
}

