﻿var glatlng = null;

var mks=new Array();

var areaDes = null;
var nSaveRate = 0;
var gScrStatus = "";

//Xu ly phan doi mau ben trai
var idxNote = 0;
var idxNoteFrts = 0;
var divPlaceIconFrts = "";
var divPlaceContentFrts = "";
var isNodeNew = false;
//Bien chua HTML cua node vua tao moi
var gHtmlCreate = "";
var isFinish=false;
var isShared = "1";
var goldX_W84 = 0;
var goldY_W84 = 0;
var gProDisWar = null;
function mm_ParamKeeper()
{    
    var nGroupID = $get("hdnGroupId").value;
    if(nGroupID == "")
    {
        nGroupID = "0";
    }
    
    this.goupId = parseInt(nGroupID);
    this.nodeIdx = idxNote;
    this.mode = (gScrStatus == "VIEW") ? 1 : 2;
    return {goupId:this.goupId,nodeIdx:this.nodeIdx,mode:this.mode};
}

//**********************************************
// Dscription: Chuyen qua trang thai tạo 1 group moi hoac edit group co san
// Inout: isClear -> biên nay cho bien co nen clean các control input hay ko nhap hay ko(tuy theo trang thai Add/Edit
// Output: none
//**********************************************
function ShowCreateMode(isClear)
{
    if(isClear)
        ClearScreen();    
    $get("divGuideCreate").style.display = "none";
    $get("divInputGroupPnl").style.display = "";
    $get("divInputGroup").style.display = "";    
    $get("divMyMapPanel").style.display = "none";
    $get("divMyMapList").style.display = "none";    
    $get("divGroupDetailsPnl").style.display = "none";
    $get("divGroupDetailsButton").style.display = "none";
    $get("divGroupDetails").style.display = "none";
    $get("divListNodeModeEdit").style.display = "none";
    $get("divListNodeModeEdit").innerHTML = "";
    //$get("divInputNewNoteInfo").style.display = "none";
    //$get("divInputNewNoteInfo").innerHTML = "";
    $get("divCommentPnl").style.display = "none";
    $get("divComment").style.display = "none";
        
    $get("hdnGroupId").value="";
    $get("hdnNoteId").value="";
    gScrStatus = "NEW";
    
    //Dong mo Marker Bar 
    map.removeControl(null, "LVToolbarMapControl");
    //Xoa node tren ban do
    map.clearOverlays();
    mks=new Array();
}

//**********************************************
// Dscription: Nap danh sach tat cac các group bản đo toi tao
// Inout: none
// Output: none
//**********************************************
function ShowListMyMapGroup()
{
    $get("divGuideCreate").style.display = "none";
    $get("divInputGroupPnl").style.display = "none";
    $get("divInputGroup").style.display = "none";    
    $get("divMyMapPanel").style.display = "";
    $get("divMyMapList").style.display = "";        
    $get("divGroupDetailsPnl").style.display = "none";
    $get("divGroupDetailsButton").style.display = "none";
    $get("divGroupDetails").style.display = "none";
    $get("divListNodeModeEdit").style.display = "none";
    $get("divListNodeModeEdit").innerHTML="";
    //$get("divInputNewNoteInfo").style.display = "none";
    $get("divCommentPnl").style.display = "none";
    $get("divComment").style.display = "none";    
    gScrStatus = "VIEW";
    $get("hdnGroupId").value = "";
    GetGroupListByUser();       
    fireResizeWindow();
}

//**********************************************
// Dscription: Hien thi chi thong tin chi tiet cua group, va thong tin tom tac cua tat cac ca node trong group
// Inout: nGroupID -> Id de lay thong tin tu database
//      : isUpdateView = > bien cho biet có tăng trạng thái view của group len ko
// Output: none
//**********************************************
function ShowDetailGroup(nGroupID, isUpdateView)
{
    $get("divGuideCreate").style.display = "none";
    $get("divInputGroupPnl").style.display = "none";
    $get("divInputGroup").style.display = "none";    
    $get("divMyMapPanel").style.display = "none";
    $get("divMyMapList").style.display = "none";        
    $get("divGroupDetailsPnl").style.display = "";
    $get("divGroupDetailsButton").style.display = "";
    $get("divGroupDetails").style.display = "";
    $get("divListNodeModeEdit").style.display = "";
    /*if(!isUpdateView)
    {
        $get("divInputNewNoteInfo").innerHTML = "";
        $get("divInputNewNoteInfo").style.display = "none";
    }
    else
    {
        $get("divInputNewNoteInfo").style.display = "";
    }*/
    
    $get("divCommentPnl").style.display = "none";
    $get("divComment").style.display = "none";    
    gScrStatus = "VIEW";
    GetGroupDetailsById(nGroupID, false, isUpdateView);        
    fireResizeWindow();
    mm_goupId = nGroupID;
}

//**********************************************
// Dscription: Chuyen qua trang thái edit nhóm bản đồ, eidt node
// Inout: none
// Output: none
//**********************************************
function ShowEditGroup()
{
    $get("divGuideCreate").style.display = "none";
    $get("divInputGroupPnl").style.display = "";
    $get("divInputGroup").style.display = "";
    $get("divMyMapPanel").style.display = "none";
    $get("divMyMapList").style.display = "none";        
    $get("divGroupDetailsPnl").style.display = "";
    $get("divGroupDetailsButton").style.display = "none";
    $get("divGroupDetails").style.display = "none";    
    $get("divListNodeModeEdit").style.display = "none";
    //$get("divInputNewNoteInfo").style.display = "";
    $get("divCommentPnl").style.display = "none";
    $get("divComment").style.display = "none";    
    gScrStatus = "EDIT";
    showMMPoint();
    fireResizeWindow();
}

//**********************************************
// Dscription: Show bar hình ban tay, bong den để tạo node tren Map
// Inout: none
// Output: none
//**********************************************
function showMMPoint(){
    var toolbarMM = new LVToolbarMapControl(new LVToolbarMapControlOptions(2, 2 | 4));
    map.addControl(toolbarMM);
    toolbarMM.action4 = function(latlng) {
        glatlng = latlng;
        isNodeNew = true;
        map.addOverlay(createMarkerEx(glatlng,mks.length, 0));
        mks[mks.length -1].click();
        btnSaveNote_Click(strMsgNotitle, '&nbsp;', 0);
    }

}

function ShowEditComment()
{
    $get("divGuideCreate").style.display = "none";
    $get("divInputGroupPnl").style.display = "none";
    $get("divInputGroup").style.display = "none";
    $get("divMyMapPanel").style.display = "none";
    $get("divMyMapList").style.display = "none";        
    $get("divGroupDetailsPnl").style.display = "";
    $get("divGroupDetailsButton").style.display = "";
    $get("divGroupDetails").style.display = "";    
    $get("divListNodeModeEdit").style.display = "none";
    //$get("divInputNewNoteInfo").style.display = "none";
    $get("divCommentPnl").style.display = "";
    $get("divComment").style.display = "";
    nSaveRate = 0;
    GetCommentByID();
    fireResizeWindow();
}

//---------------Access tblMyMapGroup Start------------------------------
function btnSaveGroup_Click()
{
    var strGTitle = $get("txtGTitle").value;
    var strGDes = $get("txtGDes").value;
    if(strGTitle=="")
    {
        alert(strMsgNoGroupName);
        return;
    }
    
    if(strGDes.length > 250)
    {
        alert(strDesLenError);
        return;
    }        
    
    if($get("rdPublic").checked==true)
    {
        isShared = "1";
    }
    else if($get("rdHide").checked==true)
    {
        isShared = "0";
    }
    
    LockUnLockScreeb(false);
    if(gScrStatus == "NEW")
        MyMapAjax.InsertGroup(strGTitle, strGDes, isShared, SaveGroup_Cbk);
    else
        MyMapAjax.UpdateGroup(parseInt($get("hdnGroupId").value), strGTitle, strGDes, isShared, SaveGroup_Cbk);
}

function SaveGroup_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return;
    }
    finishMode = 1;
    //ClearScreen();   
    if(gScrStatus == "NEW")
    {    
        if(!isFinish)
        {
            $get("hdnGroupId").value = res.value;
            gScrStatus = "EDIT";
            ShowEditGroup();
        }
        else
        {
            ShowListMyMapGroup();
            isFinish=false;
        }
    }
    LockUnLockScreeb(true);
}

function btnDel_Click(nGroupId, isShared)
{
    if(!confirm(strMsgDeleteGroup))
        return;
        
    LockUnLockScreeb(false);
    MyMapAjax.DeleteGroup(nGroupId, isShared, DeleteGroup_Cbk)
}

function DeleteGroup_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return;
    }    
    GetGroupListByUser(); 
}

function btnFinishGroup_Click()
{    
    if(gScrStatus != "DEL")
    {
        map.closeInfoWindow();   
    }
    
    if($get("txtGTitle").value =="" && $get("txtGDes").value =="")    
    {
        if(gScrStatus == "NEW")
            ShowListMyMapGroup();
        else
        {   
            GetGroupDetailNoNode();
            //ShowDetailGroup($get("hdnGroupId").value, false, true);            
        }
    }
    else
    {   
        if($get("hdnGroupId").value != "")
        {   
            GetGroupDetailNoNode();            
        }
        else
        {
            if(confirm(strMsgSave))
            {
                isFinish=true;
                btnSaveGroup_Click();
            }            
        }
    }
}

//**********************************************
// Dscription: Lấy danh sách các nhóm bản do
// Inout: none
// Output: none
//**********************************************
function GetGroupListByUser()
{
    //Dong mo Marker Bar 
    map.removeControl(null, "LVToolbarMapControl");
    //Xoa node tren ban do
    map.clearOverlays();
    mks=new Array();
    LockUnLockScreeb(false);
    MyMapAjax.GetGroupListByUser(GetGroupListByUser_Cbk);
}

function GetGroupListByUser_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return ;
    }
    $get("divMyMapList").innerHTML = res.value;    
    LockUnLockScreeb(true);
}

//**********************************************
// Dscription: Lay lai thong tin của nhóm khi edit nhóm, màh ko lay lay node
// Inout: none
// Output: none
//**********************************************
function GetGroupDetailNoNode()
{    
    //khong phep Move Maker
    for(var i=0; i<mks.length; i++)
    {
        if(mks[i] != null)
        {
            mks[i].options.draggable =false;
        }
    }
    
    map.removeControl(null, "LVToolbarMapControl");
    gScrStatus = "VIEW";
    fireResizeWindow();
    LockUnLockScreeb(false);
    $get("divScroll").className = "mmLstOver";    
    MyMapAjax.GetGroupDetailNoNode(parseInt($get("hdnGroupId").value), GetGroupDetailNoNode_Cbk);
}

function GetGroupDetailNoNode_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return ;
    }
    
    if(res.value[1] == "false")    
        $get("dvBntEdit").style.display="none";
    else
        $get("dvBntEdit").style.display="";
        
    $get("divGroupDetails").innerHTML = res.value[0];
    
    $get("divInputGroupPnl").style.display = "none";
    $get("divInputGroup").style.display = "none"; 
	$get("divGroupDetailsPnl").style.display = "";
    $get("divGroupDetailsButton").style.display = "";
    $get("divGroupDetails").style.display = "";
    if($get("divCommentPnl").style.display == "")
    {
        $get("divCommentPnl").style.display = "none";
        $get("divComment").style.display = "none"; 
    }
    
    if($get("divListNodeModeEdit").style.display = "none")
    {
        $get("divListNodeModeEdit").style.display = "";
    }
    
    LockUnLockScreeb(true);
}

function GetGroupDetailsById(nGroupID, isEdit, isUpdateView)
{    
    LockUnLockScreeb(false);
    
    if(isUpdateView == false)
        $get("divScroll").className="mmLstOverEdit";
    else
        $get("divScroll").className = "mmLstOver";
        
    $get("hdnGroupId").value = nGroupID;    
    MyMapAjax.GetGroupDetailsById(parseInt(nGroupID), isUpdateView, GetGroupDetailsById_Cbk);
}

function GetGroupDetailsById_Cbk(res)
{    
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return ;
    }
    if(res.value[4] == "false")    
        $get("dvBntEdit").style.display="none";
    else
        $get("dvBntEdit").style.display="";
    
    $get("divGroupDetails").innerHTML = res.value[0];
    $get("divListNodeModeEdit").innerHTML = res.value[1];
     map.clearOverlays();     
     mks = new Array();
    if(res.value[2] != null && res.value[2] != "")
    {
        var arrParPos = res.value[2].split(';');
        var arrNoteId = res.value[3].split(';');
        var arrPosToLoadMap = arrParPos[0].split(',');
        //'841276,2432728;841276,2432728;841276,2432728'
       
        if(arrParPos != null)
        {
            for(var i=0;i<arrParPos.length;i++)
            {
                var latlng=arrParPos[i].split(',');
                createMarkerEx(new LVLatLng(latlng[0],latlng[1]),i, arrNoteId[i]);
                map.addOverlay(mks[i],"");                
            }
            var latlng0=arrParPos[0].split(',');
            map.setCenter(new LVLatLng(latlng0[0],latlng0[1]),10);
        }        
    }    
    LockUnLockScreeb(true);
}

function createMarkerEx(point, index, nNodeId) 
{
    var icon = new LVIcon();
    //var letter = String.fromCharCode("A".charCodeAt(0)  + index);
    icon.image = gIcPath + "ic_pos.png";
    icon.iconSize = new LVSize(19, 27);
    var markerOptions = new LVMarkerOptions(icon); //{ icon: baseIcon };
    if(gScrStatus == "VIEW")
    {
        markerOptions.draggable = false;
    }
    else
    {
        markerOptions.draggable = true;
    }
    var marker = new LVMarker(point, markerOptions);        
    LVEvent.addListener(marker, "dragend", function(latlng) 
    {
        UpdateLocationInMap(nNodeId, latlng.lng(), latlng.lat());
        //alert("dragend lat: " + latlng.lat() + " lng: " + latlng.lng());
    });
    
    LVEvent.addListener(marker, "click", function(mk, ev) 
    {
        idxNote = index + 1;        
        $get("hdnNoteId").value = nNodeId;
        var opts = new LVInfoWindowOptions();
        mk.options.icon.iconSize = new LVSize(19, 26);
        opts.pixelOffset = new LVSize(0, -(mk.options.icon.iconSize.height * 5 / 6));        
        //Resert XY        
        glatlng = mk.latlng;        
        var strHtml = strMsgWaitting;
        if(gHtmlCreate !="")
        {
            strHtml = gHtmlCreate;
        }
        if(gScrStatus == "VIEW")
        {
            GetNodeDetailFromMap(nNodeId);
        }
        else
        {             
            if(!isNodeNew)
            {
                GetEditNodeDetailFromMap(nNodeId);
            }
        }
                
        opts.onCloseFn = function() 
        {                                             
            var icon = new LVIcon();                
            icon.image = gIcPath + "ic_pos.png";
            icon.iconSize = new LVSize(19, 26);
            mk.setIcon(icon);
            ClearNodeFrts();
        }
        opts.onOpenFn = function(iw) 
        {             
            SetActiveNode_LPnl();
        }
        marker.openInfoWindowHtml(strHtml, opts);      
        gHtmlCreate = "";  
        //Add tool editor vao Mô tả tren dialog        
        if(isNodeNew)
        {
            CreateToolEditor();                
        }
    });
    marker.group=GROUP_OVERLAY_MYMAP;
    mks[mks.length]= marker;
    return marker;
}

function CreateToolEditor() {
    //return;
    if($get('txtNoteDes') != null && typeof $get('txtNoteDes') != "undefined")
    {        
        $get('txtNoteDes').style.height="127px";
        areaDes = new nicEditor({fullPanel : true}).panelInstance('txtNoteDes');        
        areaDes.nicInstances[0].editorContain.style.height="130px";
        areaDes.nicInstances[0].editorContain.style.margin = "";
        areaDes.nicInstances[0].editorContain.style.backgroundColor = "white";
        //areaDes.nicInstances[0].editorContain.style.border = "1px solid teal";
        
        areaDes.nicInstances[0].elm.style.height="127px";
        //areaDes.nicInstances[0].elm.style.width="260px";
        areaDes.nicInstances[0].elm.style.overflowX="hidden";              
        areaDes.nicInstances[0].elm.style.overflowY="auto";
        areaDes.nicInstances[0].elm.style.margin = "";
        areaDes.nicInstances[0].elm.style.padding = "";
        //areaDes.nicInstances[0].elm.style.border = "1px solid blue";
        
        areaDes.nicInstances[0].editorContain.onkeyup=function(){SetDescriptNode(areaDes.nicInstances[0].editorContain, 'divNodeDes_' + idxNote);}
        
        /*areaDes.nicInstances[0].elm.style.overflowX="hidden";
        areaDes.nicInstances[0].elm.style.overflowY="auto";
        areaDes.nicInstances[0].elm.style.height="120px";
        areaDes.nicInstances[0].elm.style.margin = "";
        areaDes.nicInstances[0].elm.onkeyup=function(){SetDescriptNode(areaDes.nicInstances[0].elm, 'divNodeDes_' + idxNote);}
        */
        /*if($get("hdnNoteId").value != "")
            areaDes.nicInstances[0].elm.onkeyup=function(){SetDescriptNode(areaDes.nicInstances[0].elm, 'divNodeDes_' + idxNote);}
        else
            areaDes.nicInstances[0].elm.onkeyup=function(){SetDescriptNode(areaDes.nicInstances[0].elm, null);}
        */
            
        if($get('hdnNoteId').value == "")
        {
            nWidthEditMode = 12;
            nHeigEditMode = 2;
        }
    }    
}            
function GetGroupInforByIdForEdit()
{
    //Cho phep Move Maker
    for(var i=0; i<mks.length; i++)
    {
        mks[i].options.draggable =true;
    }
    
    finishMode = 1;
    gScrStatus="EDIT";
    if(idxNote >0)
    {
        mks[(idxNote-1)].click();
    }
    GetGroupInfoById(parseInt($get("hdnGroupId").value));    
}
function GetGroupInfoById(nGroupId)
{
    LockUnLockScreeb(false);
    MyMapAjax.GetGroupInfoById(nGroupId, GetGroupInfoById_Cbk);
}

function GetGroupInfoById_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return ;
    }
    
    $get("hdnGroupId").value = res.value[0];
    $get("txtGTitle").value = res.value[1];
    $get("txtGDes").value = res.value[2];
      
    if(res.value[3]=="1")
    {
        $get("rdPublic").checked=true;
    }
    else if(res.value[3]=="0")
    {
        $get("rdHide").checked=true;    
    }
    
    isShared=res.value[3];
    
    LockUnLockScreeb(true);
    ShowEditGroup();   
    GetNoteListByGroupID($get("hdnGroupId").value);
}

function ClearScreen()
{    
    $get("txtGTitle").value = "";
    $get("txtGDes").value = "";
    $get("rdPublic").checked=true;
}
//---------------Access tblMyMapGroup End------------------------------

//---------------Access tblMyMapNote Start------------------------------
function AddChildNode(nNodeId)
{
    var newIdx = mks.length;
    if($get('divNodeName_'+newIdx) != null)
        return ;
        
    var child1 = document.createElement('div');
    child1.id="divPlaceContent_" + newIdx;
    child1.className="mm_pdtb2";    
    var strNewNode = new Array();    
    strNewNode[strNewNode.length] = "<div id='divPlaceIcon_" + newIdx + "' class='ic_pos'>";
    strNewNode[strNewNode.length] = "   <div>";
    strNewNode[strNewNode.length] = "       <div class='mm_fbld mm_ClB' id='divNodeName_" + newIdx + "'><a href='javascript:void(0)' class='mm_nodname' onclick='ShowNoteDetail(" + newIdx + ")'>" + strMsgNotitle + " " + newIdx + "</a></div>";
    strNewNode[strNewNode.length] = "       <div id='divNodeDes_" + newIdx + "' class='mm_noddes'>&nbsp;</div>";
    strNewNode[strNewNode.length] = "   </div>";
    strNewNode[strNewNode.length] = "</div>";        
    child1.innerHTML = strNewNode.join('');     
    $get("divListNodeModeEdit").style.display="";
    $get("divListNodeModeEdit").appendChild(child1);
    
}
var gstrNodName="";
var gstrNodeDes="";
var gNodeId = 0;
function btnSaveNote_Click(strName, strDes, nNodeId)
{       
    if(strName ==""  && strDes == "")
    {        
        if($get("txtNoteName").value=="")
        {
            alert(strMsgNoNoteName);
            return;
        }
        
        strName = $get("txtNoteName").value;
        
        if(areaDes != null)
        {
            if(areaDes.nicInstances[0].editorContain.innerText != "")
            {            
                strDes = areaDes.nicInstances[0].editorContain.lastChild.innerHTML;                
                var strDesText = areaDes.nicInstances[0].elm.lastChild.nodeValue;  
                if(strDesText==null || typeof strDesText == "undefined")
                {
                    strDesText = areaDes.nicInstances[0].editorContain.lastChild.innerText;                    
                }                
                if(strDesText==null || typeof strDesText == "undefined")
                {                
                    strDesText = strDes;                    
                    if(strDesText.length > 300)
                    {
                        alert(strDesLenError);
                        return;
                    }
                }
                else
                {                
                    if(strDesText.length > 250)
                    {
                        alert(strDesLenError);
                        return;
                    }
                }                
            }        
        }        
    }    
    gstrNodName = strName;
    gstrNodeDes = strDes;
    gNodeId = nNodeId;    
    
    LockUnLockScreeb(false);
    
    if(goldX_W84 == glatlng.lng() && goldY_W84 == glatlng.lat() || isNodeNew==false)
    {
        if(gProDisWar != null)
        {
            MyMapAjax.UpdateMyMapNote(parseInt(($get("hdnGroupId").value)), gNodeId, gstrNodName, gstrNodeDes, glatlng.lng(), glatlng.lat(), gProDisWar.provid, gProDisWar.provname, gProDisWar.provfullname, gProDisWar.disid, gProDisWar.disname, gProDisWar.wardid, gProDisWar.wardname, isShared, UpdateMyMapNote_Cbk);
        }
        else
        {
            MyMapAjax.UpdateMyMapNote(parseInt(($get("hdnGroupId").value)), gNodeId, gstrNodName, gstrNodeDes, glatlng.lng(), glatlng.lat(), "", "", "", "", "", "", "", isShared, UpdateMyMapNote_Cbk);
        }
    }   
    else
    {
        MyMapAjax.GetProDisWardByCoord(glatlng.lng(), glatlng.lat(), GetProDisWarByCoord_Cbk);            
        goldX_W84 = glatlng.lng();
        goldY_W84 = glatlng.lat();
    }     
    
    /*var nProID = "-1";
    var strProName = "";        
    var nDisID = "-1";
    var strDisName = "";        
    var nWardID = "-1";
    var strWardName = "";
    
    
    if(curProDisWard.proId > 0)
    {
        nProID = curProDisWard.proId;
        strProName = curProDisWard.proNameSum;
    }
    if(curProDisWard.disId > 0)
    {
        nDisID = curProDisWard.disId;
        strDisName = curProDisWard.disName;
    }
    if(curProDisWard.wardId >0)
    {
        nWardID = curProDisWard.warId;
        strWardName = curProDisWard.warName;
    }
        
    if(isNodeNew)
    {   
        MyMapAjax.InsertMyMapNote(parseInt(($get("hdnGroupId").value)), strName + " " + (mks.length), strDes, glatlng.lng(), glatlng.lat(), nProID, strProName, nDisID, strDisName, nWardID, strWardName, InsertMyMapNote_Cbk);        
    }
    else
    {   
        var strAddress = strProName + " "+ strDisName + " " + strWardName;
        MyMapAjax.UpdateMyMapNote(parseInt(($get("hdnGroupId").value)), nNodeId, strName, strDes,  strAddress, glatlng.lng(), glatlng.lat(), UpdateMyMapNote_Cbk);
    }
    */
    
}
function GetProDisWarByCoord_Cbk(res)
{
    if(res.error!=null || res.value==null)
    {
        return;
    }
        
    gProDisWar = eval(res.value);
    //var objRnt = eval("({provid:'46',provname:'Hồ Chí Minh',provfullname:'Thành phố Hồ Chí Minh',disid:'6', disname:'Quận 10', wardid:'5', wardname:'Phường 14'})");
     
    if(isNodeNew)
    {   
        MyMapAjax.InsertMyMapNote(parseInt(($get("hdnGroupId").value)), gstrNodName + " " + (mks.length), gstrNodeDes, glatlng.lng(), glatlng.lat(), gProDisWar.provid, gProDisWar.provname, gProDisWar.provfullname, gProDisWar.disid, gProDisWar.disname, gProDisWar.wardid, gProDisWar.wardname, isShared, InsertMyMapNote_Cbk);        
    }
    else
    {           
        MyMapAjax.UpdateMyMapNote(parseInt(($get("hdnGroupId").value)), gNodeId, gstrNodName, gstrNodeDes, glatlng.lng(), glatlng.lat(), gProDisWar.provid, gProDisWar.provname, gProDisWar.provfullname, gProDisWar.disid, gProDisWar.disname, gProDisWar.wardid, gProDisWar.wardname, isShared, UpdateMyMapNote_Cbk);
    }
}

function InsertMyMapNote_Cbk(res)
{       
    if(res.error != null)
    {        
        LockUnLockScreeb(true);
        return;
    }     
       
    if(res.value>0)
    {           
        gHtmlCreate = CreateNewNodeInMap(res.value);
        map.removeOverlay(mks[mks.length - 1]);
        mks[mks.length - 1] = null;
        mks.splice(mks.length - 1, 1);        
        map.addOverlay(createMarkerEx(glatlng,mks.length, res.value));
        mks[mks.length -1].click();        
    }    
    isNodeNew = false;
    LockUnLockScreeb(true);
}

function UpdateMyMapNote_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return;
    }
    ClearNodeFrts();
    map.closeInfoWindow();    
    LockUnLockScreeb(true);    
    //GetGroupDetailsById(parseInt($get("hdnGroupId").value), true, false);    
}

function UpdateLocationInMap(nodeId, strX, strY)
{
    MyMapAjax.UpdateLocationInMap(nodeId, strX, strY, UpdateLoc_Cbk);
}
function UpdateLoc_Cbk(res){;}

function GetNoteListByGroupID(nGroupId)
{
    MyMapAjax.GetMyMapNoteLstByGroupID(parseInt(nGroupId), GetNoteListByGroupID_Cbk);
}

function GetNoteListByGroupID_Cbk(res)
{
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return;
    }    
    $get("divListNodeModeEdit").innerHTML = res.value;    
    $get("divListNodeModeEdit").style.display="";
    $get("divScroll").className = "mmLstOverEdit";
    if(idxNote>0){
        SetActiveNode_LPnl();
    }
    LockUnLockScreeb(true);
}
function GetNodeDetailFromMap(nNoteId)
{
    MyMapAjax.ShowNoteDetailByID(nNoteId, ShowNoteDetailByID_Cbk)
}
function ShowNoteDetailByID_Cbk(res)
{    
    var strHtml = res.value;    
    var tabs = new LVInfoWindowTab("", strHtml); //param: 1 title, 2 Noi dung moi;    
    mks[idxNote-1].updateInfoWindow(tabs, function(iw) { ; });        
}

function GetEditNodeDetailFromMap(nNoteId)
{
    MyMapAjax.GetNoteDetailByID(nNoteId, GetNoteDetailByID_Cbk);    
}
function GetNoteDetailByID_Cbk(res)
{
    strValue = res.value;
    var strHtml = "";
    strHtml = CreateContentDialog(idxNote,strValue[1], strValue[2], strValue[0]);
    var tabs = new LVInfoWindowTab("", strHtml); //param: 1 title, 2 Noi dung moi;
    mks[idxNote-1].updateInfoWindow(tabs, function(iw) { ; });
    CreateToolEditor();
    $get("divNodeName_" + idxNote).innerHTML="<A class='mm_lnk mm_ClB mm_pt' onclick='ShowNoteDetail("+idxNote+")' href='javascript:void(0)'>" + strValue[1] + "</a>";
    $get("divNodeDes_" + idxNote).innerHTML=strValue[2];
}
function ShowNoteDetail(id)
{    
    idxNote = id;    
    mks[(idxNote-1)].click();    
}

function ClearDlgScreen()
{
    $get("txtNoteName").value = "";
    $get("txtNoteDes").value = "";    
}
//---------------Access tblMyMapNote End------------------------------

//---------------Access tblNodeEdit Start------------------------------
function btnSaveComment_Click()
{
    if($get("txtComment").value == "")
        return;
        
    MyMapAjax.InsertComment($get("hdnGroupId").value, $get("txtComment").value, btnSaveComment_Cbk);
}

function btnSaveComment_Cbk(res)
{
    if(res.error != null)
        return;
        
    $get("txtComment").value="";    
    GetCommentByID();
}

function btnCloseComment_Click()
{
    GetGroupDetailNoNode();
    //ShowDetailGroup($get("hdnGroupId").value, false);
}

function DeleteComment(nGroupID,nCommentID)
{
    MyMapAjax.DeleteCommentByID(nGroupID, nCommentID, DeleteComment_Cbk);
}

function DeleteComment_Cbk(res)
{
    if(res.error != null)
        return;
    
    GetCommentByID();
}

function GetCommentByID()
{
    MyMapAjax.GetCommentByID($get("hdnGroupId").value, GetCommentByID_Cbk);
}

function GetCommentByID_Cbk(res)
{
    if(res.error != null)
    {
        return;
    }
    
    $get("divComment").innerHTML = res.value;
}
//---------------Access tblNodeEdit End------------------------------


//---------------Common Start------------------------------
function LockUnLockScreeb(isLock)
{   
    if(isLock)
    {
        $get("dvIndicator").className = "dvIndicator fa s10 b colRed pdr10 pdt47 posRel";
        $get("dvIndicator").style.display = "none";        
    }
    else
    {
        $get("dvIndicator").style.display = "";        
        $get("dvIndicator").style.height="660px";
        $get("dvIndicator").className="dvIndicator fa s10 b colRed pdr10 posRel lckScr";
    }
}
function $get(eleName)
{
    return document.getElementById(eleName);
}


function ShowDlgPosInfo(strHtml)
{   
    //Đổi màu ben panel left
    SetActiveNode_LPnl();        
    mks[(idxNote-1)].click();   
}

//---------------Common End------------------------------

//*********************************************************
function CreateNewNodeInMap(nNodeId)
{      
    AddChildNode(nNodeId);     
    var strHtml = CreateContentDialog(mks.length, strMsgNotitle  + " " + (mks.length), '', nNodeId);
    return strHtml;
} 
  function SetNameNode(objThis, divNodeName, nNodeId)
  {
    var divNameTemp = "";
    
    if(divNodeName == null)    
        var divNameTemp = 'divNodeName_'+idxNote;
    else
        divNameTemp = divNodeName;
        
    if($get(divNameTemp) != null)
    {
        $get(divNameTemp).innerHTML = "<a href='javascript:void(0)' class='mm_nodname' onclick='ShowNoteDetail(" + idxNote + ")'>" + objThis.value + "</a>";
        
    }
  }
  
  function SetDescriptNode(objThis, divNodeDes)
  {  
    var divDesTemp = "";
    if(divNodeDes == null || typeof $get('txtNoteDes') == "undefined")
        divDesTemp = 'divNodeDes_'+idxNote;
    else
        divDesTemp = divNodeDes;
        
    if($get(divDesTemp) != null)
    {
        if(objThis.innerText!="")
        {
            var strDes = "<div class='mm_noddes'>" + objThis.innerHTML + "</div>";
            if(areaDes != null)
            {
                if(areaDes.nicInstances[0].editorContain.innerText != "")
                {            
                    strDes = areaDes.nicInstances[0].editorContain.lastChild.innerHTML;
                    //areaDes.selectedInstance.editorContain.lastChild.innerHTML;          
                }        
            }
            $get(divDesTemp).innerHTML = strDes;
        }
        else
        {
            $get(divDesTemp).innerHTML = "&nbsp;";
        }        
    }
  }
  
  function DeleteNoteInMap(nNodeId)
  {
    gScrStatus = "DEL";
    if(nNodeId != 0)    
    {
        if(!confirm(strMsgDel))
            return;
            
        LockUnLockScreeb(false);        
        MyMapAjax.DeleteMyMapNote(parseInt($get("hdnGroupId").value), nNodeId, isShared, DeleteMyMapNote_Cbk);
    }
  }
  
  function DeleteMyMapNote_Cbk(res)
  {
    if(res.error != null)
    {
        LockUnLockScreeb(true);
        return;
    }        
    
    //DongDialog tren bando
    //map.closeInfoWindow();

    //Xoa array chua marker    
    //mks[idxNote - 1] = null;
    //mks.splice(idxNote - 1, 1);
    
    //Remove childNode In Left Panel  
    var objNode = $get("divPlaceContent_" + idxNote);
    if(objNode != null)
    {
        $get("divListNodeModeEdit").removeChild(objNode);
    }
    //GetGroupDetailsById(parseInt($get("hdnGroupId").value), true, false);    
    
    //Xoa Marker tren ban do
    if(idxNote>0)
    {
        var idxTemp = idxNote;
        map.removeOverlay(mks[idxNote - 1])
        mks[idxTemp - 1] = null;
    }
    LockUnLockScreeb(true);
  }  

function SaveRate(indx)
{   
    MyMapAjax.InsertRate($get("hdnGroupId").value, indx);
    nSaveRate = indx;    
}
//Rate Process
function SelectRate(indx)
{   
    var strRate = "";
    for(var i=1; i<=5; i++)
    {
        if(i<=indx)
            strRate += "<img src='Images/ic_st01.gif' onmousedown='SaveRate("+i+");' onmouseover='SelectRate("+i+");'>";
        else
            strRate += "<img src='Images/ic_st03.gif' onmousedown='SaveRate("+i+");' onmouseover='SelectRate("+i+");'>";
    }
    $get("divRateImg").innerHTML = strRate;    
    
    if(nSaveRate != 0)
        $get("divRateName").className = "mm_ClRd mm_fbld";
    else
        $get("divRateName").className = "mm_fbld";
        
    switch(indx)
    {
        case 1:
            $get("divRateName").innerHTML = strRate01;            
            break;
        case 2:
            $get("divRateName").innerHTML = strRate02;
            break;
        case 3:
            $get("divRateName").innerHTML = strRate03;
            break;
        case 4:
            $get("divRateName").innerHTML = strRate04;
            break;
        case 5:
            $get("divRateName").innerHTML = strRate05;
            break;
    }
}

function ClearRate()
{
    var strRateClr = "";
    if(nSaveRate==0)
    {   
        for(var i=1; i<=5; i++)
        {
            strRateClr += "<img id='rate0"+i+"' src='Images/ic_st03.gif' onmouseover='SelectRate("+1+");'>";
        }
        $get("divRateImg").innerHTML = strRateClr;
    }
    else
        SelectRate(nSaveRate);
    
}

function ShowMyMapFromSearch(nGroupID)
{    
    if(MyMapAjax.CheckUserLogin().value==false)
    {
        alert(strMsgNoLogin);
        return
    }    
    choTabs($get("aTab3"));    
    ShowDetailGroup(nGroupID, true);    
}

function CheckLogin()
{
    
    if(!confirm(strMsgNoLogin))
    {
        return;
    }
    MyMapAjax.CreateUrlLogin(document.location.href, CheckLogin_Cbk).value;
    
}
function CheckLogin_Cbk(res)
{
    CreateUrlLoginMapView(res.value[0], res.value[1]);
}

function SetActiveNode_LPnl()
{
    //Doi mau cho cac node o panel ben phai
    if(idxNoteFrts >0)
    {
        var icon = new LVIcon();
        icon.image = gIcPath + "ic_pos.png";
        icon.iconSize = new LVSize(19, 26);
        mks[(idxNoteFrts-1)].setIcon(icon);
    }
    
    //Doi mau cho cac node o panel ben trai
    if(divPlaceIconFrts != "" && divPlaceContentFrts != "")
    {
        if($get(divPlaceIconFrts) != null)
        {
            $get(divPlaceIconFrts).className="ic_pos";
            $get(divPlaceContentFrts).className="mm_pdtb4";
        }
    }
    
    var icon = new LVIcon();
    icon.image = gIcPath + "ic_posYel.png";
    icon.iconSize = new LVSize(19, 26);
    mks[(idxNote-1)].setIcon(icon);    
    
    var divPlaceIcon = "divPlaceIcon_" + idxNote;
    var divPlaceContent = "divPlaceContent_" + idxNote;
    if($get(divPlaceIcon) != null)
    {
        $get(divPlaceIcon).className="ic_posYel";
    }
    
    if($get(divPlaceContent))
    {
        $get(divPlaceContent).className="mm_bgSlct mm_pdtb4";
    }
    
    //giu lai node dang active de xu ly khi chuyen node
    divPlaceIconFrts = divPlaceIcon;
    divPlaceContentFrts = divPlaceContent;
    idxNoteFrts = idxNote;
}

function ClearNodeFrts()
{    
    if(idxNote <= 0) return;
        
    var divPlaceIcon = "divPlaceIcon_" + idxNote;
    var divPlaceContent = "divPlaceContent_" + idxNote;
    if($get(divPlaceIcon) != null)
    {
        $get(divPlaceIcon).className="ic_pos";
    }
    if($get(divPlaceContent) != null)
    {
        $get(divPlaceContent).className="mm_pdtb4";
    }
    
    divPlaceIconFrts = "";
    divPlaceContentFrts = "";
    idxNoteFrts = 0;
    idxNote = 0;
}

function CreateContentDialog(nCurNodeIdx, strTiltle, strDes, nNodeId)
{
    var strHtml = new Array();
    strHtml[strHtml.length] = "<div class='clickcontrol' style='background-color:#f7f7f7; cursor:default' ><div id='divInputNote' class='mm_f_def'>";
    strHtml[strHtml.length] = "    <div align='center' style='background-color:#f7f7f7; text-align:left'>";
    strHtml[strHtml.length] = "        <div class='mm_flt mm_h18lh mm_pdr5' style='background-color:#f7f7f7;'>" + strName + "</div>";
    strHtml[strHtml.length] = "        <div class='mm_flt' style='background-color:#f7f7f7'><input type='text' id='txtNoteName' maxlength='250' onkeyup=\"SetNameNode(this, 'divNodeName_" + nCurNodeIdx + "', " + nNodeId + ")\" class='mm_txt_dlg'  value='" + strTiltle + "' /></div>";
    strHtml[strHtml.length] = "        <div class='mm_clr'></div>";
    strHtml[strHtml.length] = "    </div>";
    strHtml[strHtml.length] = "    <div align='center' style='background-color:#f7f7f7;text-align:left; width:265px;'>";
    strHtml[strHtml.length] = "        <div class='mm_h18lh' style='background-color:#f7f7f7'>" + strDescription + "</div>";
    strHtml[strHtml.length] = "        <div class='mm_csrtxt' style='background-color:#f7f7f7;text-align:left; padding:0px;'><textarea id='txtNoteDes' class='mm_areapl'  style='overflow-x:hidden; overflow-y:auto;' rows='5' cols='1'>" + strDes + "</textarea></div>";    
    strHtml[strHtml.length] = "    </div>";
    strHtml[strHtml.length] = "    <div class='mm_pdtb5' align='center' style='background-color:#f7f7f7'>";
    strHtml[strHtml.length] = "        <div style='background-color:#f7f7f7;width:110px;'>";
    strHtml[strHtml.length] = "            <div class='mm_flt mm_w50 bg_btn mm_pt' onclick=\"btnSaveNote_Click('', '', "+nNodeId+");\">" + strSave + "</div>";
    strHtml[strHtml.length] = "            <div class='mm_frght mm_w50 bg_btn mm_pt' onclick='DeleteNoteInMap("+nNodeId+")'>" + strDelete + "</div>";
    strHtml[strHtml.length] = "        </div>";
    strHtml[strHtml.length] = "    </div> ";    
    strHtml[strHtml.length] = "</div></div>"; 
    return strHtml.join('');
}

function mm_ComeBackMyMap(groupId, nodeIdx, strShowType)
{
    if ((groupId == -1 || groupId == 0) && (nodeIdx == -1 || nodeIdx == 0))
    {
        ShowListMyMapGroup();        
    }
    else if(groupId!=-1 && groupId!=0)
    {
        if(strShowType==1)
        {
            ShowDetailGroup(groupId, false);            
        }
        else (strShowType==2)
        {
            ShowDetailGroup(groupId, false);
            GetGroupInforByIdForEdit();
        }

        if (nodeIdx != -1 && nodeIdx != 0)
        {            
            ShowNoteDetail(nodeIdx);            
        }
    }
    //alert('groupId : ' + groupId + 'nodeId: ' + nodeIdx + 'strMode: ' + strMode);
}

function CheckRadioByText(objName)
{
    $get(objName).checked=true;
}
