/**
* VMS共通JavaScript
*/

/**
* 現在の日付を(yy-mm-dd)形式で得る
* @param void
* @return string 今日の日付(yy-mm-dd)
*/
function getToday()
{
    n = new Date();
    yy = n.getFullYear();
    m = n.getMonth()+1+"";
    mm = ('0'+m).slice((-1+m.length),(1+m.length));
    d = n.getDate()+"";
    dd = ('0'+d).slice((-1+d.length),(1+d.length));
    return yy+"-"+mm+"-"+dd;
}

/**
* 税を入力する
*
* @param void
* @return string 今日の日付(yy-mm-dd)
*/
function setTax( priceObj, taxObj, taxRate )
{
    taxObj.value = Math.floor( priceObj.value * taxRate );
}

/**
* 税額を得る
*
* @param  double 価格
* @param  double 税率
* @return int    税額
*/
function getTax(price, rate, mode)
{
    tax = 0;
    switch (mode) {
        default:
        case 1:
            // 切り捨て
            tax = Math.floor(price * rate);
            break;
        case 2:
            // 四捨五入
            tax = Math.round(price * rate);
            break;
        case 3:
            // 切り上げ
            tax = Math.ceil(price * rate);
            break;
    }
    return tax;
}

/**
* 税込金額を得る
*
* @param  double 価格
* @param  dublle 税率
* @return int    税額
*/
function getPretaxPrice(price, rate, mode)
{
    return getTax(price, rate + 1, mode);
}

/**
* 税抜金額を得る
*
* @param  double 税込価格
* @param  double 税率
* @return int    税額
*/
function getUnPretaxPrice(price, rate, mode)
{
    rate = rate + 1;
    switch (mode) {
        default:
        case 1:
            // 切り捨て
            price = Math.ceil(price / rate);
            break;
        case 2:
            // 四捨五入
            price = Math.round(price / rate);
            break;
        case 3:
            // 切り上げ
            price = Math.floor(price / rate);
            break;
    }
    return price;
}

/**
* フォームエレメントが送信されるか確認する
* checkbox・hidden
* @param string フォームエレメント名
* @return bool 結果
*/
function checkSendObject( objName )
{
    var flag = false;
    for (i=0 ; i<document.getElementsByName(objName).length ; i++)
    {
        switch( document.getElementsByName(objName)[i].type )
        {
            // チェックボックス
            case "checkbox":
            if( document.getElementsByName(objName)[i].checked )
            {
                flag = true;
            }
            break;
            // ヒドゥン
            case "hidden":
            if( document.getElementsByName(objName)[i].value )
            {
                flag = true;
            }
            break;
        }
    }
    return flag;
}

/**
* チェックボックスのチェック数を得る
* @param object フォームオブジェクト
* @return int チェック数
*/
function countChecked( obj )
{
    var c = 0;

    for ( i=0; i<obj.elements.length; i++ )
    {
        if ( obj.elements[i].checked )
        {
            c = c + 1;
        }
    }
    return c;
}

/**
* ポストで新しいウインドウを開く
* @param string オープンさせるURL
* @param int ウインドウ幅
* @param int ウインドウ高さ
* @return void
*/
function postNewWindow(objForm, actionUrl, targetWindowName, fileName, w, h)
{
    // ファイル名縦横サイズ指定でwindow.open
    if (fileName&&w&&h)
    {
        win=window.open(fileName, targetWindowName,'width='+w+',height='+h);
        win.focus();
    }
    // パラメータ保存
    targetBackup = objForm.target;
    actionBackup = objForm.action;
    // パラメータ変更
    objForm.target=targetWindowName;
    objForm.action=actionUrl;
    objForm.submit();

    // 保存しておいたパラメータを戻す
    objForm.target = targetBackup;
    objForm.action = actionBackup;
}

/*
function postNewWindow ( win, url )
{
//win = window.open("blank.htm","newWindow","width="+w+",height="+h);
win.document.open('text/html');
win.document.write("<html>");
win.document.write("<head></head>");
win.document.write("<body>");
win.document.write("<form method='post' action='" + url + "'>\n");
for( i=0; i < win.window.opener.document.forms[0].elements.length; i++ )
{
//win.document.write(win.window.opener.document.forms[0].elements[i].type);
switch( win.window.opener.document.forms[0].elements[i].type )
{
// チェックのついているチェックボックスのみ表示
case "checkbox":
if(win.window.opener.document.forms[0].elements[i].checked)
{
name  = win.window.opener.document.forms[0].elements[i].name;
value = win.window.opener.document.forms[0].elements[i].value;
win.document.write("<input type='hidden' name='" + name + "' value='" + value + "'>\n");
}
break;

// サブミットボタンは非表示
case "submit":
break;

// サブミットボタンは非表示
default:
name  = win.window.opener.document.forms[0].elements[i].name;
value = win.window.opener.document.forms[0].elements[i].value;
win.document.write("<input type='hidden' name='" + name + "' value='" + value + "'>\n");
break;
}
}
win.document.write("</form>");
win.document.write("</body></html>");
win.document.close();
win.document.forms[0].submit();
win.focus();
}*/

/**
* フォーム内の全てのチェックボックスをトグルする
* @param object formオブジェクト
* @return bool check状態
*/
var checkFlag = false;
function checkAllToggle( obj )
{
    if ( ! obj ) return;
    checkFlag = !checkFlag;
    for( var i=0; i<obj.elements.length; i++ ) obj.elements[i].checked = checkFlag;
    return checkFlag;
}

/**
* チェックボックスをトグルする
* @param string オブジェクト名
* @return void
*/
function toggle(objName)
{
    document.getElementById(objName).checked = (document.getElementById(objName).checked ? false : true);
}

/**
* チェックボックスの状態で背景色を変更する
* @param string 対象オブジェクト名
* @param string チェックボックスオブジェクト名
* @param string チェック色
* @return void
*/
function changeRowColor(rowID, checkBoxID, color)
{
    //alert(rowName);
    // チェック状態による色の変更
    if (document.getElementById(checkBoxID).checked)
    {
        document.getElementById(rowID).style.backgroundColor = color;
    } else {
        document.getElementById(rowID).style.backgroundColor = "";
    }
}

/**
* フォーム内のチェックボックスの状態で背景色を変更する
* （フォーム内に他のチェックボックスが無い場合に限る）
* @param string 対象フォーム
* @param string 対象オブジェクト接頭名
* @param string チェック色
* @return void
*/
function changeRowColorAll(rowID, formID, color)
{
    formObj = document.getElementById(formID);
    for( var i=0; i<formObj.elements.length; i++ )
    {
        rowObj = document.getElementById(rowID+formObj.elements[i].name);
        if (rowObj)
        {
            if (formObj.elements[i].checked)
            {
                rowObj.style.backgroundColor = color;
            } else {
                rowObj.style.backgroundColor ="";
            }
        }
    }
}

/**
* 動的セレクトボックス作成（旧）
*
* @param object 対象エレメント
* @param string 選択値
* @param string 親の選択値
* @return void
*/
function changeSelect(target, myValue, parentValue)
{
    alert('aaaaaaaaa');
    if (typeof(target)=="undefined") {return;}
    if (typeof(selectOptions)=="undefined") {return;}

    // 選択値の設定
    var targetValue = 0;
    if (target.options.length) targetValue = target.options[target.selectedIndex].value;
    if (!targetValue) targetValue = myValue
    //alert(targetValue);

    // SelectBoxの初期化
    target.length = 1;
    target.options[0].value = "0";
    target.options[0].text  = selectOptions[0][0];

    // SelectBoxを作成
    // selectOptions配列に要素を設定しておく事
    indexCount=1;
    for (i=1; i<selectOptions[parentValue].length; i++) {
        if (selectOptions[parentValue][i]) {
            target.length++;
            target.options[indexCount].value = i;
            target.options[indexCount].text  = selectOptions[parentValue][i];
            // 選択値の設定
            if (parseInt(targetValue)==i) {
                target.selectedIndex = indexCount;
            }
            indexCount++;
        }
    }
}

/**
* containsメソッド定義
* 特定の文字列が含まれているか
*
* @param 対象オブジェクトID
*/
Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i]==element) {
            return true;
        }
    }
    return false
};

/**
* 分類1-2連動プルダウン
* classOptions配列にオブジェクトを設定しておく
*
* @param 対象オブジェクトID
* @param 親（呼び出し元）ID
*
*/
function changeClassOption(targetId, parentId, value, dummy2) {
    //alert(parent);
    var parent = document.getElementById(parentId);
    var target = document.getElementById(targetId);

    var selects = new Array();

    // ターゲットが選択している値をバックアップする
    targetValue = null;
    for (var i = 0; i < target.options.length; i++) {
        if (target.options[i].selected) {
            targetValue = target.options[i].value;
        }
    }

    // 親オブジェクトが選択しているすべての値を配列に取り出す
    hitcount = 0;
    for (var i = 0; i < parent.options.length; i++) {
        if (parent.options[i].selected) {
            selects[hitcount] = parent.options[i].value;
            hitcount++;
        }
    }

    // ターゲットoptions初期化
    for (var i = 0; i < target.options.length; i++) target.removeChild(target.options[i]);
    target.length = 0;

    // 選択値配列を総当りして該当をOptionオブジェクトにする
    // 親が2つ以上の値を持っていても期待通りに動作する
    optionCount = 0;
    target.options[optionCount] = new Option('', '0');
    optionCount++;
    for (var i = 0; i < classOptions.length-1; i++) {
        if (selects.contains(classOptions[i].parent_id)) {
            target.options[optionCount] = new Option(classOptions[i].text, classOptions[i].id);
            // 元選択していた値なら選択済に
            if (classOptions[i].id == targetValue) {
                target.options[optionCount].selected = true;
            }
            optionCount++;
        }
    }
}

/**
* 拡張表記　連動プルダウン
* fieldOptions配列にオブジェクトを設定しておく
*
* @param 対象オブジェクトID
* @param フィールド番号
* @param 親（呼び出し元）ID
* @param 見出し（1項目目）保護フラグ
*
*/
fieldOptions = new Array();
function changeFieldOptions(targetId, field, parentId, titleprotect, defaultValue) {
    //alert(parent);
    var parent = document.getElementById(parentId);
    var target = document.getElementById(targetId);

    var selects = new Array();

    offset = 0;
    if (titleprotect) {
        offset = 1;
    }

    // 親オブジェクトが選択しているすべての値を配列に取り出す
    hitcount = 0;
    for (var i = 0; i < parent.options.length; i++) {
        if (parent.options[i].selected) {
            selects[hitcount] = parent.options[i].value;
            hitcount++;
        }
    }

    // ターゲットoptions初期化
    for (var i = 0 + offset; i < target.options.length; i++) {
        target.options[i].selected = 0;
        target.options[i].value = 0;
        target.removeChild(target.options[i]);
    }
    target.length = offset;


    // 選択値配列を総当りして該当をOptionオブジェクトにする
    // 親が2つ以上の値を持っていても期待通りに動作する
    optionCount = offset;
    //alert(defaultValue);
    for (var i = 0 ; i < fieldOptions.length-1; i++) {
        if (fieldOptions[i].field == field && (selects.contains("0") || selects.contains(fieldOptions[i].parent_id))) {
            target.options[optionCount] = new Option(fieldOptions[i].text, fieldOptions[i].id);
            flag = false;
            if (fieldOptions[i].id == defaultValue) {
                flag = true;
            }
            target.options[optionCount].selected = flag;
            optionCount++;
        }
    }
    // optionsが空ならダミーoption作成
    // optionがブランクだと以前の値が送信され続ける？ためブランクにできない
    if (0 == target.options.length) {
        target.options[optionCount] = new Option('', '');
        target.options[optionCount].selected = true;
    }
    //alert(target.value);
}