////////////////////////////////////////////////////////////////////////////////////
// table �ؼ����
function Table_CreateTable(DivID,TableID,Width,Height)
{
    document.getElementById(DivID).innerHTML='<table id="'+TableID+'" width="'+Width+'px" height="'+Height+'px"  border="1" class="am-table am-table-bordered am-table-radius am-table-striped" border="1" align="left" cellpadding="0" cellspacing="0" bordercolor="a9cfa4" style="border-collapse:collapse;empty-cell:show;table-layout: fixed;font-size: 13px;"> <tbody id="rows"></tbody></table>';
}
function Table_InsertHead(TableID,headValues)
{
    // rowValue: [[����,���,�Ű�],[����,���,�Ű�],...,[����,���,�Ű�]]
    var tableObj=document.getElementById(TableID);
    var rowObj= tableObj.insertRow(0);
    for(var nCellIdx in headValues)
    {
        var CellValue=headValues[nCellIdx];
        var CellObj = rowObj.insertCell(nCellIdx);
        CellObj.innerHTML=CellValue[0];
        CellObj.setAttribute("width",CellValue[1]);
        CellObj.setAttribute("align",CellValue[2]);
        CellObj.style.background="#d6cfc5";
        CellObj.style.height=21;
        CellObj.style.fontSize=14;
    }
    return 0;
}
function Table_InsertRow(TableID,rowValue)
{
    // rowValue: [[����,���,�Ű�],[����,���,�Ű�],...,[����,���,�Ű�]]
    var tableObj=document.getElementById(TableID);
    var nRow=tableObj.rows.length;
    var rowObj= tableObj.insertRow(nRow);
    for(var nCellIdx in rowValue)
    {
        var CellValue=rowValue[nCellIdx];
        var CellObj = rowObj.insertCell(nCellIdx);
        CellObj.innerHTML=CellValue[0];
        CellObj.setAttribute("width",CellValue[1]);
        CellObj.setAttribute("align",CellValue[2]);
    }
    return nRow;
}
function Table_DeleteRow(TableID,row)
{
    var tableObj=document.getElementById(TableID);
    tableObj.deleteRow(row);
}
function Table_GetRowCount(TableID)
{
    var tableObj=document.getElementById(TableID);
    return tableObj.rows.length;
}
function Table_GetCellValue(TableID,row,col)
{
    var tableObj=document.getElementById(TableID);
    return tableObj.rows[row].cells[col].innerHTML;
}
function Table_SetCellValue(TableID,row,col,value)
{
    var tableObj=document.getElementById(TableID);
    tableObj.rows[row].cells[col].innerHTML=value;
}

//////////////////////////////////////////////////////////////////
// json����
function CMyJson()
{
    // ����
    this.m_arrKey=new Array();      // key
    this.m_arrValue=new Array();    // value
    this.m_arrIdx=0;
    this.m_arrAnsData=new Array();     // Ӧ������
    this.m_ansIdx=0;
    // ����
    this.InsertElem=function(key,value,bStr) // ����һ��Ԫ��
    {
        this.m_arrKey[this.m_arrIdx]='"'+key+'"';
        if(bStr)
            this.m_arrValue[this.m_arrIdx]='"'+value+'"';
        else
            this.m_arrValue[this.m_arrIdx]=value;
        this.m_arrIdx++;
    }
    this.Clear=function()   // ���
    {
        this.m_arrIdx=0;
    }
    this.GetJsonStr=function() // �õ�json��
    {
        var strJson=new String();
        var strTmp=new String();
        var nIdx=0;
        strTmp='';
        for(nIdx=0;nIdx<this.m_arrIdx;++nIdx)
        {
            strTmp+=(this.m_arrKey[nIdx]+':'+this.m_arrValue[nIdx]);
            if(nIdx<this.m_arrIdx-1)
                strTmp+=',';
        }
        strJson='{'+strTmp+'}';
        return strJson;
    }
    this.GetJsonStr2=function() // �õ�json��
    {
        var strJson=new String();
        var strTmp=new String();
        var nIdx=0;
        strTmp='';
        for(nIdx=0;nIdx<this.m_arrIdx;++nIdx)
        {
            strTmp+=(this.m_arrKey[nIdx]+':'+this.m_arrValue[nIdx]);
            if(nIdx<this.m_arrIdx-1)
                strTmp+=',';
        }
        strJson='[{'+strTmp+'}]';
        var regexp=new RegExp('"',"gm");
        strJson=strJson.replace(regexp,'\\"');
        return strJson;
    }
    this.AttachData=function(data)
    {
        this.m_arrAnsData=data;
    }
    this.Rewind=function()
    {
        this.m_ansIdx=0;
    }
    this.MoveToNext=function()
    {
        this.m_ansIdx++;
    }
    this.MoveToRecord=function(idx)
    {
        if(idx<0 || idx>=this.m_arrAnsData.length)
            this.m_ansIdx=-1;
        else
            this.m_ansIdx=idx;
        return (this.m_ansIdx>=0?true:false);
    }
    this.MoreRecord=function()
    {
        return (this.m_ansIdx>=0 && this.m_ansIdx<this.m_arrAnsData.length?true:false)
    }
    this.GetFieldValue=function(KeyIdx)
    {
        return this.m_arrAnsData[this.m_ansIdx][KeyIdx];
    }
    this.DetachData=function()
    {
        this.m_arrAnsData=new Array();
        this.m_ansIdx=0;
    }
    this.GetFieldNumInRecord=function()
    {
        return this.m_arrAnsData[this.m_ansIdx].length;
    }
    this.KeyToIndex=function(key,arrIdx)
    {
        var len=this.m_arrAnsData[arrIdx].length;
        var idx=0;
        for(idx=0;idx<len;++idx)
        {
            if(this.m_arrAnsData[arrIdx][idx]==key)
                return idx;
        }
        return -1;
    }
}

///////////////////////////////////////////////////////////////////////////
// ȫ�ֹ������ܺ���
function myfn_GetSelectionText() // ��ȡѡ�е��ı�����
{
    var text="";
    if (window.getSelection)
        text = window.getSelection().toString();
    else if (document.selection && document.selection.type != "Control")
        text = document.selection.createRange().text;
    return text;
}
function myfn_InsertAtCursor(ctrl,txtValue,pos)  // �ڹ�괦�����ı�����
{
    myfn_SetCursorPosInCtrl(ctrl,pos);
    if (document.selection) //IE �����
    {
        ctrl.focus();
        sel = document.selection.createRange();
        sel.text = txtValue;
        sel.select();
    }
    else if (ctrl.selectionStart || ctrl.selectionStart == '0') //FireFox��Chrome��
    {
        var startPos = ctrl.selectionStart;
        var endPos = ctrl.selectionEnd;

        // ���������
        var restoreTop = ctrl.scrollTop;
        ctrl.value = ctrl.value.substring(0, startPos) + txtValue + ctrl.value.substring(endPos, ctrl.value.length);

        if (restoreTop > 0)
            ctrl.scrollTop = restoreTop;

        ctrl.focus();
        ctrl.selectionStart = startPos + txtValue.length;
        ctrl.selectionEnd = startPos + txtValue.length;
    }
    else
    {
        ctrl.value += txtValue;
        ctrl.focus();
    }
}
function myfn_GetCursorPosInCtrl(elem) // ��ȡ���λ������
{
    var index = 0;
    if (document.selection) {// IE Support
        elem.focus();
        var Sel = document.selection.createRange();
        if (elem.nodeName === 'TEXTAREA') {//textarea
            var Sel2 = Sel.duplicate();
            Sel2.moveToElementText(elem);
            var index = -1;
            while (Sel2.inRange(Sel)) {
                Sel2.moveStart('character');
                index++;
            };
        }
        else if (elem.nodeName === 'INPUT') {// input
            Sel.moveStart('character', -elem.value.length);
            index = Sel.text.length;
        }
    }
    else if (elem.selectionStart || elem.selectionStart == '0') { // Firefox support
        index = elem.selectionStart;
    }
    return (index);
}
function myfn_SetCursorPosInCtrl(tobj, spos){    // ���ù��λ��
    if(spos<0)
        spos = tobj.value.length;
    if(tobj.setSelectionRange){ //���ݻ��,�ȸ�
        setTimeout(function(){
                tobj.setSelectionRange(spos, spos);
                tobj.focus();}
            ,0);
    }else if(tobj.createTextRange){ //����IE
        var rng = tobj.createTextRange();
        rng.move('character', spos);
        rng.select();
    }
}
function myfn_SetTextToClipbrd(text) // �������ݵ�������(��������IE)
{
    window.clipboardData.setData("Text",text);
}
function myfn_GetTextFromClipbrd() // �Ӽ������ȡ�ı�����(��������IE)
{
    var text=window.clipboardData.getData("Text");
    return text;
}
function myfn_JsonStringToJsonObj(jsString) // ��Json�ַ���ת��ΪJson����
{
    return eval('('+jsString+')');
}