﻿var browser = new Object();
$(function () {
    browser = getBrowser();
});
function PopUpFrame(dialogUrl, windowName, dialogFeatures) {
    if (dialogFeatures == null || dialogFeatures.length <= 0) {
        dialogFeatures = 'height=1024,width=768,location=0,menubar=0,status=0,toolbar=0,resizable=0,modal=1,dialog=1,scrollbars=1';
        var newWindow = window.open(dialogUrl, windowName, dialogFeatures, true);
        if (window.focus) newWindow.focus();
    }
    else {
        var newWindow = window.open(dialogUrl, windowName, dialogFeatures, true);
        if (window.focus) newWindow.focus();
    }
}
function PopUpFrame(dialogUrl, windowName, dialogFeatures, resizeable) {
    if (dialogFeatures == null || dialogFeatures.length <= 0) {
        dialogFeatures = 'height=1024,width=768,location=0,menubar=0,status=0,toolbar=0,modal=1,dialog=1,scrollbars=1';
    }
    dialogFeatures += ',resizable=' + resizeable;
    var newWindow = window.open(dialogUrl, windowName, dialogFeatures, true);
    if (window.focus) newWindow.focus();
}

function CreateGrid(objRow) {
    var gridBody = '';
    gridBody += '<tr>' +
                '<td value="' + objRow.rowId + '"><input type="checkbox" rowId="' + objRow.rowId + '" /></td>' +
                '<td value="' + objRow.rowDesc + '"><span>' + objRow.rowDesc + '</span></td>' +
                '</tr>';
    return gridBody;
}

function RemoveSelectedItem(gridId, hdnId) {
    var hdnVal = $('#' + hdnId).val();
    $('#' + gridId + ' tbody tr').each(function () {
        if ($(this).find('input:checkbox').attr('checked') == true) {
            hdnVal = hdnVal.replace($(this).find('input:checkbox:checked').attr('rowId') + ',', '');
            $(this).remove();
        }
    });

    /*
    if ($('#ChkSelectAll').length > 0) {
        $('#ChkSelectAll').attr('checked', false);
    }
    */
    alert(hdnVal);
    $('#' + hdnId).val(hdnVal);
}

function ModalConfirm(itemSelector, parentGridTBodySelector, hdnSelector) {
    var rowHtml = '';
    var targetWin = window.parent.frames['Frame'] == null ? window : window.parent.frames['Frame'];
    /* 父視窗Grid中的資料建立成$.data Object */
    window.parent.opener.$(parentGridTBodySelector).find('tr').each(function () {
        var oldRow = new Row($(this).find('input:checkbox').attr('rowId'), $(this).find('span').text());
        $.removeData(document.body, 'old_' + oldRow.rowId)
        $.data(document.body, 'old_' + oldRow.rowId, oldRow);
    });
    /* 子視窗Grid中的資料建立成$.data Object */
    targetWin.$(itemSelector).each(function () {
        var newRow = new Row($(this).attr('rowId'), $(this).attr('rowDesc'));
        $.removeData(document.body, 'new_' + newRow.rowId);
        $.data(document.body, 'new_' + newRow.rowId, newRow);
    });
    /* 篩選新舊$.data */
    for (key in $.data(document.body)) {
        if (key.indexOf('new_') > -1) {
            for (oldKey in $.data(document.body)) {
                if (oldKey.indexOf('old_') > -1 && $.data(document.body)[key].rowId == $.data(document.body)[oldKey].rowId) {
                    //alert('key:' + $.data(document.body)[key].rowId + '\noldKey:' + $.data(document.body)[oldKey].rowId);
                    $.removeData(document.body, [oldKey]);
                }
            }
        }
    }
    /*將篩選結果建立rowHtml*/
    var parentHdnVal = '';
    for (key in $.data(document.body)) {
        rowHtml += CreateGrid($.data(document.body)[key]);
        parentHdnVal += $.data(document.body)[key].rowId + ',';
    }
    window.parent.opener.$(hdnSelector).val(parentHdnVal);
    window.parent.opener.$(parentGridTBodySelector).find('tr').remove().end().append(rowHtml);
    var win = window.open('', '_parent');
    win.close();
    return false;
}

function ModalCancel() {
    var win = window.open('', '_parent');
    win.close();
    return false;
}

function getBrowser() {
    var userAgent = navigator.userAgent.toLowerCase();
    $.browser.chrome = /chrome/.test(userAgent);
    if ($.browser.chrome) {
        $.browser.safari = false;
    }
    return $.browser;
}
function ParseJSONString(obj) {
    var str = '';
    if (typeof (obj) == 'object') {
        str += '{';
        for (var key in obj) {
            str += '"' + key + '":' + obj[key] + ',';
        }
        str = str.substring(0, str.length - 1);
        str += '}';
    } else {
        str += '"' + obj.toString() + '"';
    }

    return str;

}

function ParseJSONStringRecursive(obj) {
    var str = '';
    if (typeof (obj) == 'object') {
        str += '{';
        for (var key in obj) {
            str += '"' + key + '":' + ParseJSONStringRecursive(obj[key]) + ',';
        }
        str = str.substring(0, str.length - 1);
        str += '}';
    } else {
        str += '"' + obj.toString() + '"';
    }

    return str;

}

function DiffArrays(arr1, arr2, returnUnion) {
    var ret = [];
    var test = {};
    var bigArray, smallArray, key;
    if (arr1.length >= arr2.length) {
        bigArray = arr1;
        smallArray = arr2;
    } else {
        bigArray = arr2;
        smallArray = arr1;
    }
    for (var i = 0; i < bigArray.length; i++) {
        key = bigArray[i];
        test[key] = true;
    }
    if (!returnUnion) {
        //diffing
        for (var i = 0; i < smallArray.length; i++) {
            key = smallArray[i];
            if (!test[key]) {
                test[key] = null;
            }
        }
    } else {
        //union
        for (var i = 0; i < smallArray.length; i++) {
            key = smallArray[i];
            if (!test[key]) {
                test[key] = true;
            }
        }
    }
    for (var i in test) {
        ret.push(i);
    }
    return ret;
}

function GPMnetGetModifiedRecordsData(dataRows) {

    var modifiedData = new Array();
    while (dataRows.length > 0) {
        var record = dataRows.pop();
        modifiedData.push(record.data);
    }
    return Ext.util.JSON.encode(modifiedData);
}

