﻿NhaDat_Class = function() { };
////////////////////////////////////
NhaDat_Class.prototype = {

    //List Functions you have to aready
    //1) InsertTemplate
    //2) SearchTemplate
    //3) ManageTemplate
    //4) ViewByMaSo
    //5) ReloadLeftMenuFilter
    //6) UpdatePicByMaSo

    ReloadLeftMenuFilter: function(subItemId) {
        $("input:radio[@name='SearchType']").filter("[value=0]").attr("checked", true);
        $("#SubItem").val(subItemId);
        $("#Province").val("0");
        $("#District").val("0");
        $("#Cost").val("0");
        $("#MainKeyword").val("");
    },

    //////////////////////////////////////////////////////////////////////////
    InsertTemplate: function() {
        $("html, body").animate({ scrollTop: 0 }, 0);

        if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
        $("#checkzone-content").load("./Template/NhaDat/InsertTemplate.htm", function() {
            Boxy.get("#WaitZone").hide();
            NhaDat.GenerateNewCapcha();
            SubItem.ListAllByMainItemId($("#SubItemId"), GlobalVariable.MainItem);

            //Load GMap
            Other.GMapInit("0", "", "");

            //Bind change-event for "#LoaiHinh"
            jQuery("#LoaiHinh").ready(function() {
                jQuery("#LoaiHinh").change(function() {
                    if ($(this).val() == 'canthue') { $('.option').css('display', 'none') } else { $('.option').css('display', 'inline') };
                })
            });
        });
    },

    PrintTemplate: function() {
        var myWin = window.open("", "Caigichothue", "menubar=yes,scrollbars=yes,width=800,height=550,left=100,top=100");
        myWin.document.write("<html><head><style type='text/css'>td {font-size: 12px;}</style></head><body style='font-size: 12px; font-family: Arial; margin:5;padding:5;'>" + "<div style='margin:0 auto;text-align:center'><img src='../Images/logo.jpg'/></div><br>" + $("#PrintZone").html() + "</body></html>");
        myWin.document.close();
        myWin = null;
    },
    //////////////////////////////////////////////////////////////////////////
    SearchTemplate: function(pageSize, pageIndex) {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("1", GlobalVariable.MainItem, GlobalVariable.SubItem, pageIndex, GlobalVariable.Tmp, "0", "0");

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatSearchTemplate";
        var userId = GlobalVariable.Tmp;

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&UserId=" + userId + "&SearchType=" + GlobalVariable.SearchType + "&MainKeyword=" + $("#MainKeyword").val() + "&SubItem=" + $("#SubItem").val() + "&Province=" + $("#Province").val() + "&District=" + $("#District").val() + "&Cost=" + $("#Cost").val() + "&SortColumn=" + GlobalVariable.SortColumn + "&SortDirection=" + GlobalVariable.SortDirection + "&PageSize=" + pageSize + "&PageIndex=" + pageIndex,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                if (result.Objects != null) {
                    $("#checkzone-content").setTemplateURL("./Template/NhaDat/SearchTemplate.htm");
                    //Add Params to JTemplate
                    $("#checkzone-content").setParam("numberPage", result.NumberPage);
                    $("#checkzone-content").setParam("rowCount", result.RowCount);
                    $("#checkzone-content").setParam("pageIndex", pageIndex);
                    //
                    $("#checkzone-content").processTemplate(result);
                }
                else {
                    if (pageIndex - 1 > 0)
                        NhaDat.SearchTemplate(GlobalVariable.PageSize, pageIndex - 1);
                    else
                        $("#checkzone-content").html(GlobalVariable.MsgNoRecord);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
                //Start Tooltip
                Other.ImagePreview();
            }
        });
    },

    ManageTemplate: function(pageSize, pageIndex) {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("2", GlobalVariable.MainItem, GlobalVariable.SubItem, pageIndex, GlobalVariable.Tmp, "0", "0");

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatManageTemplate";
        var postValue = "&SortColumn=" + GlobalVariable.SortColumn + "&SortDirection=" + GlobalVariable.SortDirection + "&PageSize=" + pageSize + "&PageIndex=" + pageIndex;

        switch (GlobalVariable.Tmp) {
            case "0": postValue = postValue + "&ThoiHan=1&Lock=1"; break;
            case "1": postValue = postValue + "&ThoiHan=1&Lock=0"; break;
            case "2": postValue = postValue + "&ThoiHan=0&Lock=1"; break;
        }

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + postValue,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                if (result.Objects != null) {
                    $("#checkzone-content").setTemplateURL("./Template/NhaDat/ManageTemplate.htm");
                    //Add Params to JTemplate
                    $("#checkzone-content").setParam("numberPage", result.NumberPage);
                    $("#checkzone-content").setParam("rowCount", result.RowCount);
                    $("#checkzone-content").setParam("pageIndex", pageIndex);
                    //
                    $("#checkzone-content").processTemplate(result);
                }
                else {
                    if (pageIndex - 1 > 0)
                        NhaDat.ManageTemplate(GlobalVariable.PageSize, pageIndex - 1);
                    else
                        $("#checkzone-content").html(GlobalVariable.MsgNoRecord);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
                //Start Tooltip
                Other.ImagePreview();
                //Start Upload Image
                Gui.UploadPictureAll();
            }
        });
    },

    SearchTemplateExcept: function(maSo, mainItemId, subItemId, pageSize, pageIndex) {
        //$("html, body").animate({ scrollTop: 0 }, 0);

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatSearchTemplateExcept";

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo + "&MainItemId=" + mainItemId + "&SubItemId=" + subItemId + "&SortColumn=" + GlobalVariable.SortColumn + "&SortDirection=" + GlobalVariable.SortDirection + "&PageSize=" + pageSize + "&PageIndex=" + pageIndex,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                if (result.Objects != null) {
                    $("#checkzone-content-2").setTemplateURL("./Template/NhaDat/SearchTemplateExcept.htm");
                    //Add Params to JTemplate
                    $("#checkzone-content-2").setParam("maSo", maSo);
                    $("#checkzone-content-2").setParam("mainItemId", mainItemId);
                    $("#checkzone-content-2").setParam("subItemId", subItemId);

                    $("#checkzone-content-2").setParam("numberPage", result.NumberPage);
                    $("#checkzone-content-2").setParam("rowCount", result.RowCount);
                    $("#checkzone-content-2").setParam("pageIndex", pageIndex);
                    //
                    $("#checkzone-content-2").processTemplate(result);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
            }
        });
    },

    TopTemplate: function() {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatTopTemplate";

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&SortColumn=Visit&SortDirection=DESC&PageSize=10",
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                if (result.Objects != null) {
                    $("#LeftMenuTop").setTemplateURL("./Template/NhaDat/TopTemplate.htm");
                    $("#LeftMenuTop").processTemplate(result);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
            }
        });
    },

    TodayTemplate: function() {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatTodayTemplate";

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&SortColumn=Visit&SortDirection=DESC&PageSize=10",
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                if (result.Objects != null) {
                    $("#LeftMenuToday").setTemplateURL("./Template/NhaDat/TodayTemplate.htm");
                    $("#LeftMenuToday").processTemplate(result);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
            }
        });
    },

    ViewByMaSo: function(maSo, tieuDe) {
        $("html, body").animate({ scrollTop: 0 }, 0);
        Ajax.SetQueryString("0", GlobalVariable.MainItem, GlobalVariable.SubItem, "0", "0", maSo, Other.DecodeUnicode(tieuDe));

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatSelectByMaSo";
        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                $("#checkzone-content").setTemplateURL('./Template/NhaDat/ViewByMaSo.htm');
                $("#checkzone-content").processTemplate(result);
                document.title = result.Objects[0].TieuDe;
                document.getElementsByName('description')[0].setAttribute('content', result.Objects[0].TieuDe);
                document.getElementsByName('keywords')[0].setAttribute('content', result.Objects[0].TieuDe);

                //Load GMap
                if (result.Objects[0].LienHeMap != "") {
                    Other.GMapInit("2", result.Objects[0].LienHeMap, "<b>Vị trí tài sản:</b><br/>" + result.Objects[0].TieuDe);
                }
                else {
                    Other.GMapInit("2", "107.8912353515625|9.61699822032795", "<b>Vị trí tài sản chưa xác định</b>");
                }

            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
                //Start Tooltip
                //Other.ImagePreview();
                $("#gallery a").lightBox();
            }
        });
    },

    SelectByMaSo: function(maSo, tieuDe) {
        $("html, body").animate({ scrollTop: 0 }, 0);

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatSelectByMaSo";
        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
            },
            success: function(result) {
                $("#checkzone-content").setTemplateURL('./Template/NhaDat/SelectByMaSo.htm');
                $("#checkzone-content").processTemplate(result);

                //Load GMap
                if (result.Objects[0].LienHeMap != "") {
                    Other.GMapInit("1", result.Objects[0].LienHeMap, "<b>Bạn đã chọn vị trí này:</b><br />Bạn có thể chọn lại 1 vị trí khác.");
                }
                else {
                    Other.GMapInit("0", "", "");
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
                //Bind change-event for "#LoaiHinh" and execute change-event       
                jQuery("#LoaiHinh").ready(function() {
                    jQuery("#LoaiHinh").change(function() {
                        if ($(this).val() == 'canthue') { $('.option').css('display', 'none') } else { $('.option').css('display', 'inline') };
                    }).change();
                });
            }
        });
    },

    //////////////////////////////////////////////////////////////////////////

    Insert: function() {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatInsert";

        if ($("#IC").val().toLowerCase() != GlobalVariable.CapCha.toLowerCase()) {
            alert("Mã xác nhận không đúng!");
            NhaDat.GenerateNewCapcha();
            return false;
        }

        var postValue = NhaDat.CheckValidAndPost();
        switch (postValue) {
            case "0": alert("Tiêu đề tin không được để trống (số ký tự < 400)."); $("#TieuDe").focus(); $("#TieuDe").select(); return false; break;
            case "1": alert("Bạn phải chọn đúng Tỉnh/thành và Quận/huyện."); $("#TinhThanh").focus(); $("#TinhThanh").select(); return false; break;
            case "2": alert("Phường/xã không được để trống."); $("#PhuongXa").focus(); $("#PhuongXa").select(); return false; break;
            case "3": alert("Số nhà không được để trống."); $("#SoNha").focus(); $("#SoNha").select(); return false; break;
            case "4": alert("Tên đường không được để trống."); $("#Duong").focus(); $("#Duong").select(); return false; break;
            case "5": alert("Diện tích không được để trống."); $("#DienTich").focus(); $("#DienTich").select(); return false; break;
            case "6": alert("Giá không được để trống."); $("#Price").focus(); $("#Price").select(); return false; break;
            case "7": alert("Phường/xã có số ký tự < 100."); $("#PhuongXa").focus(); $("#PhuongXa").select(); return false; break;
            case "8": alert("Số nhà có số ký tự < 50."); $("#SoNha").focus(); $("#SoNha").select(); return false; break;
            case "9": alert("Tên đường có số ký tự < 500."); $("#Duong").focus(); $("#Duong").select(); return false; break;
            case "10": alert("Diện tích phải nhập ký tự số (số ký tự <50)."); $("#DienTich").focus(); $("#DienTich").select(); return false; break;
            case "11": alert("Giá phải nhập ký tự số (số ký tự <50)."); $("#Price").focus(); $("#Price").select(); return false; break;
            case "12": alert("Mô tả không được để trống (số ký tự < 4000)."); $("#MoTa").focus(); $("#MoTa").select(); return false; break;
            case "13": alert("Các mục trong [Cấu trúc tiện ích] và [Thông tin tài sản] có số ký tự < 20."); return false; break;
            case "14": alert("Các mục trong [Cấu trúc tiện ích] và [Thông tin tài sản] phải nhập ký tự số."); return false; break;
            case "15": alert("Họ Tên không được để trống (số ký tự < 200)."); $("#LienHeHoTen").focus(); $("#LienHeHoTen").select(); return false; break;
            case "16": alert("Số điện thoại không được để trống và chỉ chấp nhận ký tự số (số ký tự < 50)."); $("#LienHeDienThoai").focus(); $("#LienHeDienThoai").select(); return false; break;
            case "17": alert("Địa chỉ liên hệ không được để trống (số ký tự < 500)."); $("#LienHeDiaChi").focus(); $("#LienHeDiaChi").select(); return false; break;
            case "18": alert("Email liên hệ < 200 ký tự."); $("#LienHeEmail").focus(); $("#LienHeEmail").select(); return false; break;
            case "19": alert("Email liên hệ không đúng chuẩn."); $("#LienHeEmail").focus(); $("#LienHeEmail").select(); return false; break;
            case "20": alert("Nick yahoo < 100 ký tự."); $("#LienHeYahoo").focus(); $("#LienHeYahoo").select(); return false; break;
            case "21": alert("Nick skype < 100 ký tự."); $("#LienHeSkype").focus(); $("#LienHeSkype").select(); return false; break;
        }

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + postValue,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
                $("#InsertButton").attr('disabled', 'disabled');
            },
            success: function(result) {
                if (result.Success == "True") {
                    alert("Tin của bạn thêm mới thành công [Mã số của tin bạn vừa đăng là: ND" + result.MaSo + "]");
                    //Reload list user item (NhaDat.ManageTemplate)
                    NhaDat.ManageTemplate(GlobalVariable.PageSize, '1');
                    //Reload Right Menu (SubItem of MainItem)
                    MainItem.RightMenuCountByUserId();
                    SubItem.RightMenuByMainItemId($("#RightMenuSubItem"), GlobalVariable.MainItem);
                    //Reload LeftMenuToday
                    NhaDat.TodayTemplate();
                }
                else {
                    switch (result.MaSo) {
                        case "1": alert("Tài khoản của bạn đã bị khóa!"); break;
                        case "2": alert("Thời gian đăng tin của bạn đã hết!"); break;
                        case "3": alert("Bạn đã sử dụng hết số tin đăng cho phép!"); break;
                        default: alert(GlobalVariable.MsgErrorInsertData); break;
                    }
                    $("#InsertButton").attr('disabled', '');
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
                $("#InsertButton").attr('disabled', '');
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
            }
        });
    },

    Update: function() {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatUpdate";

        var postValue = NhaDat.CheckValidAndPost();
        switch (postValue) {
            case "0": alert("Tiêu đề tin không được để trống (số ký tự < 400)."); $("#TieuDe").focus(); $("#TieuDe").select(); return false; break;
            case "1": alert("Bạn phải chọn đúng Tỉnh/thành và Quận/huyện."); $("#TinhThanh").focus(); $("#TinhThanh").select(); return false; break;
            case "2": alert("Phường/xã không được để trống."); $("#PhuongXa").focus(); $("#PhuongXa").select(); return false; break;
            case "3": alert("Số nhà không được để trống."); $("#SoNha").focus(); $("#SoNha").select(); return false; break;
            case "4": alert("Tên đường không được để trống."); $("#Duong").focus(); $("#Duong").select(); return false; break;
            case "5": alert("Diện tích không được để trống."); $("#DienTich").focus(); $("#DienTich").select(); return false; break;
            case "6": alert("Giá không được để trống."); $("#Price").focus(); $("#Price").select(); return false; break;
            case "7": alert("Phường/xã có số ký tự < 100."); $("#PhuongXa").focus(); $("#PhuongXa").select(); return false; break;
            case "8": alert("Số nhà có số ký tự < 50."); $("#SoNha").focus(); $("#SoNha").select(); return false; break;
            case "9": alert("Tên đường có số ký tự < 500."); $("#Duong").focus(); $("#Duong").select(); return false; break;
            case "10": alert("Diện tích phải nhập ký tự số (số ký tự <50)."); $("#DienTich").focus(); $("#DienTich").select(); return false; break;
            case "11": alert("Giá phải nhập ký tự số (số ký tự <50)."); $("#Price").focus(); $("#Price").select(); return false; break;
            case "12": alert("Mô tả không được để trống (số ký tự < 4000)."); $("#MoTa").focus(); $("#MoTa").select(); return false; break;
            case "13": alert("Các mục trong [Cấu trúc tiện ích] và [Thông tin tài sản] có số ký tự < 20."); return false; break;
            case "14": alert("Các mục trong [Cấu trúc tiện ích] và [Thông tin tài sản] phải nhập ký tự số."); return false; break;
            case "15": alert("Họ Tên không được để trống (số ký tự < 200)."); $("#LienHeHoTen").focus(); $("#LienHeHoTen").select(); return false; break;
            case "16": alert("Số điện thoại không được để trống và chỉ chấp nhận ký tự số (số ký tự < 50)."); $("#LienHeDienThoai").focus(); $("#LienHeDienThoai").select(); return false; break;
            case "17": alert("Địa chỉ liên hệ không được để trống (số ký tự < 500)."); $("#LienHeDiaChi").focus(); $("#LienHeDiaChi").select(); return false; break;
            case "18": alert("Email liên hệ < 200 ký tự."); $("#LienHeEmail").focus(); $("#LienHeEmail").select(); return false; break;
            case "19": alert("Email liên hệ không đúng chuẩn."); $("#LienHeEmail").focus(); $("#LienHeEmail").select(); return false; break;
            case "20": alert("Nick yahoo < 100 ký tự."); $("#LienHeYahoo").focus(); $("#LienHeYahoo").select(); return false; break;
            case "21": alert("Nick skype < 100 ký tự."); $("#LienHeSkype").focus(); $("#LienHeSkype").select(); return false; break;
        }

        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + postValue,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
                if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
                $("#UpdateButton").attr('disabled', 'disabled');
            },
            success: function(result) {
                if (result.Success == "True") {
                    alert(GlobalVariable.MsgSuccessUpdateData);
                    //Reload list user item (NhaDat.ManageTemplate)
                    NhaDat.ManageTemplate(GlobalVariable.PageSize, result.PageIndex);
                    //Reload Right Menu (SubItem of MainItem)
                    MainItem.RightMenuCountByUserId();
                    SubItem.RightMenuByMainItemId($("#RightMenuSubItem"), GlobalVariable.MainItem);
                }
                else {
                    alert(GlobalVariable.MsgErrorUpdateData);
                    $("#UpdateButton").attr('disabled', '');
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
                $("#UpdateButton").attr('disabled', '');
            },
            complete: function() {
                Boxy.get("#WaitZone").hide();
            }
        });
    },

    UpdateCreateDateByMaSo: function(maSo) {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatUpdateCreateDateByMaSo";
        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
            },
            success: function(result) {
                alert(GlobalVariable.MsgSuccessUpdateData);
                //Reload list user item (NhaDat.ManageTemplate)
                NhaDat.ManageTemplate(GlobalVariable.PageSize, result.PageIndex);
                //Reload Right Menu (SubItem of MainItem)
                MainItem.RightMenuCountByUserId();
                SubItem.RightMenuByMainItemId($("#RightMenuSubItem"), GlobalVariable.MainItem);
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
            }
        });
    },

    UpdatePicByMaSo: function(maSo, pic) {
        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatUpdatePicByMaSo";
        $.ajax({
            url: "Handler.ashx",
            dataType: "json",
            data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo + "&Pic=" + pic,
            type: "POST",
            timeout: GlobalVariable.AjaxTimeOut,
            beforeSend: function() {
            },
            success: function(result) {
                if (result.Success == "False") {
                    alert(GlobalVariable.MsgErrorUpdateData);
                }
            },
            error: function(xhr, message, ex) {
                alert(GlobalVariable.MsgErrorLoadData);
            },
            complete: function() {
            }
        });
    },

    DeleteByMaSo: function(maSo) {

        var classPath = "Main.BL.NhaDatBL";
        var methodName = "NhaDatDeleteByMaSo";

        if (confirm("Bạn có đồng ý xóa tin này?")) {
            $.ajax({
                url: "Handler.ashx",
                dataType: "json",
                data: "ClassPath=" + classPath + "&MethodName=" + methodName + "&MaSo=" + maSo,
                type: "POST",
                timeout: GlobalVariable.AjaxTimeOut,
                beforeSend: function() {
                    if (Boxy.get("#WaitZone") == null) new Boxy("#WaitZone", { title: "Đang tải dữ liệu...", modal: false, closeable: false, fixed: false }); else Boxy.get("#WaitZone").show();
                },
                success: function(result) {
                    if (result.Success == "True") {
                        alert(GlobalVariable.MsgSuccessDeleteData);
                        //Reload list user item (NhaDat.ManageTemplate)
                        NhaDat.ManageTemplate(GlobalVariable.PageSize, result.PageIndex);
                        //Reload Right Menu (SubItem of MainItem)
                        MainItem.RightMenuCountByUserId();
                        SubItem.RightMenuByMainItemId($("#RightMenuSubItem"), GlobalVariable.MainItem);
                    }
                    else {
                        alert(GlobalVariable.MsgErrorDeleteData);
                    }
                },
                error: function(xhr, message, ex) {
                    alert(GlobalVariable.MsgErrorLoadData);
                },
                complete: function() {
                    Boxy.get("#WaitZone").hide();
                }
            });
        }
    },

    //////////////////////////////////////////////////////////////////////////
    CheckValidAndPost: function() {
        dienTich = jQuery.trim($("#DienTich").val());
        duong = encodeURIComponent(jQuery.trim($("#Duong").val()));
        duongLink = encodeURIComponent(jQuery.trim($("#DuongLink").val()));
        duongTruocNha = jQuery.trim($("#DuongTruocNha").val());
        ghiChu = encodeURIComponent(jQuery.trim($("#GhiChu").val()));
        huongTaiSan = $("#HuongTaiSan").val();
        khuonVienDai = jQuery.trim($("#KhuonVienDai").val());
        khuonVienNgang = jQuery.trim($("#KhuonVienNgang").val());
        khuonVienTong = jQuery.trim($("#KhuonVienTong").val());
        lienHeYahoo = jQuery.trim($("#LienHeYahoo").val());
        lienHeSkype = jQuery.trim($("#LienHeSkype").val());
        lienHeChinhChu = $("#LienHeChinhChu:checked").val();
        lienHeDiaChi = encodeURIComponent(jQuery.trim($("#LienHeDiaChi").val()));
        lienHeMap = jQuery.trim($("#LienHeMap").val());
        lienHeDienThoai = jQuery.trim($("#LienHeDienThoai").val());
        lienHeEmail = jQuery.trim($("#LienHeEmail").val());
        lienHeHoTen = encodeURIComponent(jQuery.trim($("#LienHeHoTen").val()));
        loaiHinh = $("#LoaiHinh").val();
        mainItemId = GlobalVariable.MainItem;
        moTa = encodeURIComponent(jQuery.trim($("#MoTa").val()));
        phapLy = $("#PhapLy").val();
        phuongXa = jQuery.trim($("#PhuongXa").val());
        quanHuyen = $("#QuanHuyen").val();
        soLau = jQuery.trim($("#SoLau").val());
        soNha = jQuery.trim($("#SoNha").val());
        soPhongKhac = jQuery.trim($("#SoPhongKhac").val());
        soPhongKhach = jQuery.trim($("#SoPhongKhach").val());
        soPhongNgu = jQuery.trim($("#SoPhongNgu").val());
        soPhongTam = jQuery.trim($("#SoPhongTam").val());
        subItemId = $("#SubItemId").val();
        price = jQuery.trim($("#Price").val());
        thoiHan = $("#ThoiHan").val();
        tieuDe = encodeURIComponent(jQuery.trim($("#TieuDe").val()));
        tinhThanh = $("#TinhThanh").val();
        xayDungDai = jQuery.trim($("#XayDungDai").val());
        xayDungNgang = jQuery.trim($("#XayDungNgang").val());
        xayDungTong = jQuery.trim($("#XayDungTong").val());
        metVuong = $("#MetVuong:checked").val();
        choDauXeHoi = $("#ChoDauXeHoi:checked").val();
        dayDuTienNghi = $("#DayDuTienNghi:checked").val();
        hoBoi = $("#HoBoi:checked").val();
        sanVuon = $("#SanVuon:checked").val();
        tienDeO = $("#TienDeO:checked").val();
        tienIchKhac = $("#TienIchKhac:checked").val();
        tienKinhDoanh = $("#TienKinhDoanh:checked").val();
        tienSanXuat = $("#TienSanXuat:checked").val();
        tienSinhVienThue = $("#TienSinhVienThue:checked").val();
        tienVanPhong = $("#TienVanPhong:checked").val();

        if (tieuDe == "" || tieuDe.length > 400)
            return "0";
        if (quanHuyen == 0)
            return "1";
        if (loaiHinh == "chothue") {
            if (phuongXa == "")
                return "2";
            if (soNha == "")
                return "3";
            if (duong == "")
                return "4";
            if (dienTich == "")
                return "5";
            if (price == "")
                return "6";
        }
        if (loaiHinh == "canthue") {
            if (dienTich == "")
                dienTich = "0";
            if (price == "")
                price = "0";
        }
        if (phuongXa.length > 100)
            return "7";
        if (soNha.length > 50)
            return "8";
        if (duong.length > 500)
            return "9";
        if (dienTich.length > 50 || isNaN(dienTich.replace(/,/g, "")))
            return "10";
        if (price.length > 50 || isNaN(price.replace(/,/g, "")))
            return "11";
        if (moTa == "" || moTa.length > 4000)
            return "12";
        if ((duongTruocNha.length > 20) || (khuonVienDai.length > 20) || (khuonVienNgang.length > 20) || (soLau.length > 20) || (soPhongKhac.length > 20) || (soPhongKhach.length > 20) || (soPhongNgu.length > 20) || (soPhongTam.length > 20) || (xayDungDai.length > 20) || (xayDungNgang.length > 20) || (xayDungTong.length > 20) || (khuonVienTong.length > 20))
            return "13";
        if ((isNaN(duongTruocNha.replace(/,/g, ''))) || (isNaN(khuonVienDai.replace(/,/g, ''))) || (isNaN(khuonVienNgang.replace(/,/g, ''))) || (isNaN(soLau)) || (isNaN(soPhongKhac)) || (isNaN(soPhongKhach)) || (isNaN(soPhongNgu)) || (isNaN(soPhongTam)) || (isNaN(xayDungDai.replace(/,/g, ''))) || (isNaN(xayDungNgang.replace(/,/g, ''))) || (isNaN(xayDungTong.replace(/,/g, ''))) || (isNaN(khuonVienTong.replace(/,/g, ''))))
            return "14";
        if (lienHeHoTen == "" || lienHeHoTen.length > 200)
            return "15";
        if ((lienHeDienThoai == "") || (lienHeDienThoai.length > 50) || (isNaN(lienHeDienThoai.replace(/,/g, ''))))
            return "16";
        if (lienHeDiaChi == "" || lienHeDiaChi.length > 500)
            return "17";
        if (lienHeEmail.length > 200)
            return "18";
        if ((Other.CheckValidEmail(lienHeEmail) == false) && (lienHeEmail != ""))
            return "19";
        if (lienHeYahoo.length > 100)
            return "20";
        if (lienHeSkype.length > 100)
            return "21";

        return ("&DienTich=" + dienTich + "&Duong=" + duong + "&DuongLink=" + duongLink + "&DuongTruocNha=" + duongTruocNha + "&GhiChu=" + ghiChu + "&Price=" + price + "&HuongTaiSan=" + huongTaiSan + "&KhuonVienDai=" + khuonVienDai + "&KhuonVienNgang=" + khuonVienNgang + "&KhuonVienTong=" + khuonVienTong + "&LienHeYahoo=" + lienHeYahoo + "&LienHeSkype=" + lienHeSkype + "&LienHeChinhChu=" + lienHeChinhChu + "&LienHeDiaChi=" + lienHeDiaChi + "&LienHeMap=" + lienHeMap + "&LienHeDienThoai=" + lienHeDienThoai + "&LienHeEmail=" + lienHeEmail + "&LienHeHoTen=" + lienHeHoTen + "&LoaiHinh=" + loaiHinh + "&MainItemId=" + mainItemId + "&MetVuong=" + metVuong + "&MoTa=" + moTa + "&PhapLy=" + phapLy + "&PhuongXa=" + phuongXa + "&QuanHuyen=" + quanHuyen + "&SoLau=" + soLau + "&SoNha=" + soNha + "&SoPhongKhac=" + soPhongKhac + "&SoPhongKhach=" + soPhongKhach + "&SoPhongNgu=" + soPhongNgu + "&SoPhongTam=" + soPhongTam + "&SubItemId=" + subItemId + "&ThoiHan=" + thoiHan + "&TieuDe=" + tieuDe + "&TinhThanh=" + tinhThanh + "&XayDungDai=" + xayDungDai + "&XayDungNgang=" + xayDungNgang + "&XayDungTong=" + xayDungTong + "&ChoDauXeHoi=" + choDauXeHoi + "&DayDuTienNghi=" + dayDuTienNghi + "&HoBoi=" + hoBoi + "&SanVuon=" + sanVuon + "&TienDeO=" + tienDeO + "&TienIchKhac=" + tienIchKhac + "&TienKinhDoanh=" + tienKinhDoanh + "&TienSanXuat=" + tienSanXuat + "&TienSinhVienThue=" + tienSinhVienThue + "&TienVanPhong=" + tienVanPhong);
    },

    //////////////////////////////////////////////////////////////////////////
    GenerateNewCapcha: function() {
        GlobalVariable.CapCha = CyloCaptcha.genRan(4); //get a new 4 random number
        $("#DC").html(CyloCaptcha.display(GlobalVariable.CapCha));
    }

};

var NhaDat = new NhaDat_Class();

